Sequence Programming in 122-C Language

1. Constant: the quantity that cannot be changed
○1 Digital constant. For example
12,
23.4, 12.5f 12 = 23; //error 23.4 = 45.6; //error

○2 Character constant. For example,'\0', '0','A','a','\n'
corresponds to the ASCII code value 0 48 65 97 10 (line feed)
'0' = '1'; //error, equivalent to 48 = 49;

○3 String constant. ”0”, ”abcd”

○4 Macro definition. Macro definition is character replacement.
Format: #define macro name value
For example: #define PI 3.14
PI = 3.14159 //error, equivalent to 3.14 = 3.14159;

○5 const constant
const int ca = 10;
ca = 20; //error

2. Variable, the amount that can be modified.
Definition: data type variable name;
for example: int a = 0; a = 10;//ok
Identifier naming rules: 1. It is composed of letters, underscore _, and numbers.
2. It can only start with a letter or underscore.
3. It cannot be used. Keyword
4. Case-sensitive (case sensitive)
legal identifiers: abc, a1, _a, a_b, A, Float
illegal identifiers: 1a, a*b, float

3. Data type

char (1 byte) short (2 bytes) int (integer default) (2 or 4 bytes, 4 bytes are main) long (4 bytes) long long (8 bytes) float (4 bytes) double (Decimal default) (8 bytes)
The value range of char type is -128~127;
the value range of unsigned char type is 0~255;
char is an integer like mini

char ch = ‘a’;
printf("%c,%d\n",ch,ch); //输出: a,97
printf("%c,%d\n",98,98); //输出:b,98

4. Detailed explanation of output function printf

printf("%d,%d\n",98,'b');//输出 98,98. %d 用于输出十进制整数
printf("%c,%c\n",99,'c');//输出 c,c. %c 用于输出字符
printf("%f,%f\n",12.5f,23.4);//输出 12.500000,23.400000. 
//%f 用于输出 float 和 double
printf("%s\n","abc");//输出 abc.%s 用于输出字符串
char *str = "hello"; 
printf("%s\n",str);//输出 hello
printf("%x\n",20);//输出 14. %x 用于输出十六进制数字.0x14==20
printf("%x,%X\n",180,180);//输出 b4,B4
printf("%08x\n",100);//考试重点。输出 00000064. 08 表示输出 8 个十六进制数,不足左边补 0

Common errors are as follows

printf("%d\n",12.5);//error 格式化符和数据不匹配,应该使用%f
printf("%d\n"); //error 漏写输出的数据

5. Input function scanf to explain
scanf ("formatting character", address list);

int a;
int b;
scanf("%d%d",&a,&b);//从键盘输入 10 20.读取成功
scanf("%d%d",&a,&b);//从键盘输入 10,20.读取 b 失败
scanf("%d,%d",&a,&b);//从键盘输入 10 20.读取 b 失败
scanf("%d,%d",&a,&b);//从键盘输入 10,20.读取成功
scanf("%d%d",a,b);//从键盘输入 10 20.程序崩溃
scanf("%d,%d\n",&a,&b);//从键盘输入 10 20.程序停不下来,直到输入\n
float c;
scanf("%d",&c);//error .格式化符和数据不匹配,读取 float 使用%f,读取 double 用%lf

6. Other input and output functions
getchar: read a character
putchar: output a character
gets: read a line of characters, this function is very dangerous, it is not recommended to use
puts: output a line of characters

char ch;
ch = getchar();//从键盘读取一个字符存放在 ch 中
putchar(ch); //输出 ch 中的字符
char str[100];
gets(str); //读取一行的字符存放在 str 中
puts(str);//输出 str

What is the result of executing the following code fragment?
Insert picture description here
The printf function actually has a return value, the return value is an integer. If successful, it returns the number of output characters (the return value is the number of output characters, including numbers, letters, punctuation marks, spaces, etc.), and returns a negative value if the output fails

Guess you like

Origin blog.csdn.net/LINZEYU666/article/details/111872970