Key points of the fourth course of C language
(1) : review pointer size: the pointer size can only be 4 bytes (32b) in x86 and 8 bytes (64b) in x64. Because the pointer stores the first address.
(2) struct: a new type is designed and designed by the program developer.
struct student { };//Semicolon required
struct student { char s_id[10]; char s_name[10]; char s_sex[5]; char s_age; } int main() { int a = 10; struct student s1={"09001","yhping","man",23};//Initialize the structure with curly brackets {} printf("id:%s \n",s1.s_id);//s1 is the structure variable To access printf("name:%s \n",s1.s_name);//%Print string s printf("sex:%s \n",s1.s_sex); printf("age:%n \n",s1.s_age); return =0; }
The following two methods are equivalent and can be accessed by pointer:
struct student*sp=&s1; //sp=&s1; //*sp=s1; printf("id: \n,"(*sp).s_id);//. takes precedence over * so parentheses (* sp) must be used printf("id: \n,"sp->s_id);//The two are equivalent. The second - > pointer is simpler
(3) Documents
//Read file: "disk" -- copy file data to "buffer" -- read buffer data to "program"
//Write file: "program" -- write data to "buffer" -- write buffer data to "disk" file.
//Because memory access is much faster than disk.
//Files are divided into: text files (converted to corresponding ASCII code values) and binary files (directly used without conversion).
#include <stdio,h> int main() { int a = 0, b = 0; scanf_s("%d,%d",&a,&b);//stdin extracts the data format and scanf from the std buffer_ Same format in S printf("a=%d b=%d \n",a,b);//stdout return 0; } //stdin--buffer--stdout
Enter 12,23 (the format in scanf_s is comma separated)
Output a=12 b=23
(4) Keywords
size of (used to calculate the number of bytes) is not a macro or function, but a keyword.
#include <stdio,h> int main() { int a = 10; int size=sizeof(++a);//int size=4 still prints integer bytes and does not parse the expression + + a printf("size:%d \n",size);//4 printf("a=%d,\n",a); return 0; }
size=4
a=10
#include <stdio,h> int main() { int a = 10; int size=sizeof(a+10.0);//Change to double type int size=8 printf("size:%d \n",size);//4 printf("a=%d,\n",a); return 0; }
10
010
0x10
0b1010: all are integer size=4
12.23: double type size=8;
#include<stdio.h> #include<string.h> int main() { char str[]={"yhping"};//The string must end with \ 0 and take up a space int size = sizeof(str);//type+num int len = strlen(str); printf("size:%d \n",size);//6 + 1 = 7 calculation \ 0 printf("len:%d \n",len);//6 array length does not count \ 0 return 0; }
typedef converts legal variable definitions into other type declarations.
unsigned char u_int8;//Variable name typedef unsigned char u_int8;//Convert from variable name to type name
The space applied by the array is the product of the bytes occupied by its array type and the number of data in the array.
int short[10];//Output is 2 * 10 = 20
Static static keyword (extend its life, move from. stack area to. data area)
Modifies global variables, local variables, and functions.
void fun(int x) { int a = 0;//After the function ends, a is the local variable and returns the stack frame Stack area static int b = 0;//The service life is extended only once, and the initialization accumulation is 1 B data area a +=1;//++a b +=1;//++b printf("a = %d,b= %d,\n",a,b); } int main() { for(int i=10;i > 0;i--) { fun(i); } return 0; }
After running, a=1 remains unchanged, and the static keyword static extends its service life from b = 1 to B = 2 to 10.
(5) bool type
//bool only has true and false. In c language, only 0 is false, and non-0 is true.
//Import header file # < stdpool, H > cpp files are used directly.
//bool occupies one byte 0000 0000 false 0000 0001 true.
int main() { char cha = '0';//The ASCII value of character 0 is 48 true, that is, the bool value is 0000 0001 char chb = '\0';//Null character 0 flash bool: 0000 char chc = 0;//0 flase int *p = NULL;//Null finger macro is defined as 0 flash return 0; }
int main() { bool x = 1;//true x -= 1;//Flash only 0 is false, and non-0 is true x -= 1;//true x -= 1//true return 0; }
Relational expression: the operation result is bool value
Priority:
//X greater than or equal to 10 and less than or equal to 20 can be written as: x > = 10 & & x < = 20; Do not put mathematical concepts into c language concepts. Mathematics has infinite concepts, while each variable in c language has a range. Do not treat mathematical expressions as expressions in c language.
//For example, 5 / 2 = 2 integer / integer = integer; 5 / 2.0 = 2.5 integer / floating point number = floating point number.
//Arithmetic priority is higher than comparison priority, that is, a+b > c is first a+b and then greater than c.
Logical expression: the running result is bool value
int main() { int a = 10; bool x = flase; x = !!a; printf("%d,\n",x);//1 a is true! a false!! a is true x is 1 printf("%d,\n",a);//The value of 10 a is not affected and is still 10 return 0; }