[C language] 1. Initial C language

1. C language is a computer language (a language used by humans and computers to communicate), and C, C++, JAVA, Python, etc. are all computer languages.
2. The development of language was originally a binary language, namely 0 and 1, because the basis of computer hardware is electricity, and the level is only divided into positive and negative electricity, namely 0 and 1. At this time, the person who can program is a scientist.
3. For example, 10100001 is used to represent ADD, symbols like ADD are called mnemonics, and the language expressed by mnemonics is called assembly language, and later development is B language, C language, C++, etc. It's like a camera. At first, people had to go to a photo studio to find a professional photographer to take pictures. With the development of society, with digital cameras and mobile phones, everyone can take pictures. The threshold is getting lower and lower, and more and more people use it. The more.
4. The C language is not very mature early, but gradually becomes mature and popular. Because each company optimizes the C language, the C language of each organization is not uniform, so there is an international standard C89/C90, and even the latter C99, C11, but because C99 and C11 have not become popular, many compilations are caused The device does not support C99 and C11. This unification leads to the C language can be compiled on many compilers, and it is also convenient for communication and popularity.
5. To write C code, a good habit is to create a project. Generally there will be .c files (source files) and .h files (header files)
6. The first C file

#include <stdio.h>//stdio.h是C语言本身提供给我们的库函数,想要调用库函数需要用#include <xxx.h>这样的方式
//stdio的意思是,std-标准  standard input output
int main() //主函数-程序的入口-有且只有一个
//main前面的int表示main函数调用返回一个整数值
//以前的教材也有void maiin()这样的写法,但这样的写法已经过时了,不要再用了
{
    
    
  printf("hello world\n");//在屏幕上输出hello world并换行
  //\n是换行的意思
  return 0;//返回0
}

7. Data type
char //character data type
short //short integer
int //integer
long //long integer
long //longer integer
float //single-precision floating-point number
double //double-precision floating-point number

//%c 打印字符格式的数据 
//%d 打印整型十进制的数据 
//%f 打印浮点型的数据,小数
//%p 以地址的形式打印
//%x 打印16进制的数字
//%x 打印8进制的数字
#include <stdio.h>
int main() 
{
    
    
	char ch ='A' ;// 向内存申请一块空间来存放字符A,而ch是一种字符类型,同时也是这块空间的名字
	printf("%c\n",ch);//%c 打印字符格式的数据 
  	return 0;//返回0
}

Analyze the size of memory occupied by each data type

#include <stdio.h>
int main() 
{
    
    
	printf("char所占的内存大小为 %d\n",sizeof(char));//1个字节,8个位
	printf("short所占的内存大小为%d\n",sizeof(short));//2个字节,16个位
	printf("int所占的内存大小为%d\n",sizeof(int));//4个字节,32个位
	printf("long所占的内存大小为%d\n",sizeof(long));//4个字节或者8个字节
	printf("long long所占的内存大小为%d\n",sizeof(long long));//8个字节,64个位
	printf("float所占的内存大小为%d\n",sizeof(float));//4个字节,32个位
	printf("double所占的内存大小为%d\n",sizeof(double));//8个字节,64个位
  	return 0;//返回0
}

Why long may be 4 bytes or 8 bytes. Because the C language standard stipulates that only sizeof(long)>=sizeof(int) is required, and int occupies 4 bytes, so long occupies 4 or 8 bytes are in line with the standard, so it may appear under different compilers Different results, but all meet the standards.

From the above, we can know
that the size of the number that short can represent is 0-2 2^16-1, that is, 0-65535; the
size of the number that can be represented by int is 0-2
2^32-1, that is, 0-65535;

Global variables and local variables

#include <stdio.h>
int num1=10;//全局变量
int main() 
{
    
    
int num2=20;//局部变量
  	return 0;
}

The variable num1 in the above code belongs to a global variable, which is defined outside the code block {}; and num2 is a local variable, which is a variable defined inside the code block {}.
It is recommended that the names of local variables and global variables should not be the same. It is easy to misunderstand and cause bugs. When the names of local variables and global variables are the same, local variables take precedence.

#include <stdio.h>
int a=10;//全局变量
int main() 
{
    
    
	int a=20;//局部变量
	printf("%d\n",a);
	return 0;
}
//输出为20,说明局部变量优先级更高

Use of variables, input and output

#include <stdio.h>
int main() 
{
    
    
#include <stdio.h>
int main() 
{
    
    
	//C语言语法规定,变量要定义在当前代码的最前面 
	//计算两个数的和 
	int num1=0;
	int num2=0;
	int sum=0;
	//输入函数
	scanf("%d%d",&num1,&num2); //取地址符号&
	sum=num1+num2;
	printf("sum=%d\n",sum); 
	return 0;
}
}

The scope and life cycle of
variables 1. The scope of local variables is the local scope of the variable
2. The scope of global variables is the entire project

correct
error

The extern keyword, used to declare external symbols, can call variables in one source file from another source file.

//sum.c
int num=100
//main.c
int main()
{
    
    
extern int  num;
printf("num=%d\n",num);//打印num=100
return 0;
}

Life cycle: The life cycle of a variable refers to a period of time between the creation of the variable and the destruction of the variable.
1. The life cycle of a local variable is: the life cycle of entering the scope begins, and the life cycle of out of the scope ends
2. The life cycle of a global variable is: the life cycle of the entire program

Figure one
Figure II

Constant:
1. Literal constant
2. Constant variable modified by const , the value of the variable cannot be changed

Figure one
Figure II

3.#define defined identifier constant

4. Enumeration constants
Enumeration keywords:enum

#include <stdio.h>
enum Sex
{
    
    
	MALE,
	FEMALE,
	SECRET	
};
//MALE,FEMALE,SECRET-枚举常量 
int main() 
{
    
    
	printf("%d\n",MALE);//0 
	printf("%d\n",FEMALE);//1 
	printf("%d\n",SECRET);//2 
	return 0;
}

5. String + escape character + comment
A string of characters surrounded by double quotation marks is called a string literal, or string for short.
Note: The end mark of a string is an '\0'escape character, it '\0'is the end mark when calculating the length of the string, and it is not counted as the content of the string.

"abcdefg";
"hello bit";
"";//空字符串 

Question: If you want to "abc"store the string , where should it be placed?
The string is followed by default '\0', which is the end of the string.

#include <stdio.h>
int main() 
{
    
    
	char a1[]={
    
    "abc"};//数组 
	//"abc"---'a''b''c''\0'--'\0'字符串的结束标志 
	char a2[]={
    
    'a','b','c','\0'};
	printf("%s\n",a1);
	printf("%s\n",a2);
	return 0;
}
#include <stdio.h>
#include <cstring>
int main() 
{
    
    
	char a1[]={
    
    "abc"};//数组 
	//"abc"---'a''b''c''\0'--'\0'字符串的结束标志 
	char a2[]={
    
    'a','b','c','\0'};
	char a3[]={
    
    'a','b','c'};
	printf("%d\n",strlen(a1));//strlen--计算字符串长度的 
	printf("%d\n",strlen(a2));
	printf("%d\n",strlen(a3));
	return 0;
}

'\n'Demonstration of escape characters

#include <stdio.h>
int main() 
{
    
    
	printf("abcn");//打印出来是abcn 
	printf("abc\n");//打印出来是abc+换行 
	return 0;
}

Commonly used strings

Escape character Paraphrase
\? Use when writing consecutive question marks to prevent them from being parsed into three-letter words
\’ Used to represent character constants'
\" Used to denote double quotation marks inside a string
\\ Used to indicate a backslash to prevent it from being interpreted as an escape sequence
\a Warning character, buzzer
\b Backspace
\f Paper feed
\n Newline
\r Carriage return
\t Horizontal tab
\ v Vertical tab
\ddd ddd represents 1~3 octal numbers. Such as: \130 X
\ xdd dd represents 2 decimal digits. Such as: \x30 0
#include <stdio.h>
#include <string.h>
int main() 
{
    
    
	printf("%d\n",strlen("c:\test\32\test.c"));//计算后面字符串的长度
	return 0;
}
//答案:13
//答案解析,\t 是一个字符;\32也是一个字符,表示的是作为8进制代表的那个10进制数字,作为ASCII码值,对应的字符。即将8进制的32转换为10进制为3*8+2=26。通过查表可知ASCII表中26表示的是→
//"c:\test\32\test.c"变成了
//c
//:
//\t---横向制表
//e
//s
//t
//\32---查ASCII表,可知打印出来是→
//\t
//e
//s
//t
//.
//c
//所以字符串长度一共是13

Guess you like

Origin blog.csdn.net/wsq_666/article/details/114271223