C language knowledge Summary (4), C language knowledge Summary
Variable Scope
C language divides variables into local variables and global variables based on different variable scopes.
1. Local Variables
1> definition: a variable defined within a function is called a local variable. The format parameter is also a local variable.
2> scope: A local variable is valid only within the function that defines it. That is, a local variable is used only within the function that defines it. Other functions cannot use it.
2. Global Variables
1> definition: variables defined outside all functions are called global variables.
2> scope: the scope of a global variable ranges from the position of the defined variable to the end of the source program. That is, the global variable can be shared by other functions after the defined position.
Storage Type of Variables
* The storage type of variables refers to where the variables are stored. There are three places for storing variables: common memory, runtime stack, and hardware register. The storage type of a variable determines when the variable is created, when it is destroyed, and how long its value can be maintained, that is, the life cycle of the variable.
* The C language divides variables into automatic variables, static variables, and register variables based on their storage types.
1. Automatic Variables
1> definition: Automatic variables are stored in the stack.
2> what are automatic variables? The local variables modified by the keyword auto are all automatic variables. However, this keyword is rarely used, which is basically useless, because all local variables are automatic variables by default.
3> life cycle: the automatic variable is created only when the program executes the code block (function) that declares the automatic variable. After the code block (function) of the automatic variable is executed, these automatic variables are automatically destroyed. If a function is called repeatedly, these automatic variables are re-created each time.
2. Static variables
1> definition: static variables are stored in static memory, that is, they do not belong to stacks.
2> what are static variables:
3> lifecycle: a static variable is created before the program runs. It always exists throughout the entire running period until the program ends.
1 # include <stdio. h> 2 3 int a; 4 5 void test () {6 static int B = 0; 7 B ++; 8 9 int c = 0; 10 c ++; 11 12 printf ("B = % d, c = % d \ n", B, c); 13} 14 15 int main () {16 int I; 17 // call test function 18 for (I = 0; I <3; I ++) {19 test (); 20} 21 22 return 0; 23} three times in a row}
* The variables a and B in the 3rd rows are static variables. The variables c in the 6th rows and I in the 9th rows are automatic variables.
* Because the variable B in the first row is a static variable, it will be created only once and its lifecycle will continue until the end of the program. Because it will only be created once, the Code in line 6th will only be executed once. The value of variable B will not be reinitialized to 0 when the test function is called again.
* Note: although the variable B of row 6th is a static variable, it only changes its storage type (that is, its lifecycle) and does not change its scope, variable B can only be used within the test function.
* We repeatedly call the test function three times in the main function. The output result is: B = 1, c = 1.
B = 2, c = 1
B = 3, c = 1
3. register variables
1> definition: variables stored in hardware registers are called register variables. Register variables are more efficient than variables stored in the memory (by default, both automatic variables and static variables are stored in the memory)
2> which variables are register variables:
The automatic variables modified by the keyword register are register variables.
Only automatic variables can be register variables, but not global variables and static local variables.
Register variables are only applicable to int, char, and pointer type variables.
3> lifecycle: Because the register variable itself is an automatic variable, the register variable in the function occupies the value stored in the register when calling the function. When the function ends, the register is released and the variable disappears.
4> note:
Due to the limited number of registers in the computer, too many register variables cannot be used. If the register is saturated, the program automatically converts the register variable to automatic variable processing.
To increase the computing speed, some frequently used automatic variables are usually defined as register variables, so that the program allocates registers for it as much as possible, instead of memory.
Struct
Form:
Struct name {
Type name 1 member name 1;
Type name 2 member name 2;
......
Type name n member name n;
};
Struct variable definition
1. First define the struct type and then define the variable
struct Student { char *name; int age;};struct Student stu;
2. Define both struct types and variables
struct Student { char *name; int age;} stu;
3. directly define the struct type variable, omitting the type name
struct { char *name; int age;} stu;
Attention of struct
Recursive definition of struct itself is not allowed
The structure body can contain other structures.
Defines the struct type, but describes the composition of the type, and does not allocate storage space to it, just as the system does not allocate space for the int type itself. Only when a variable of the struct type is defined will the system allocate storage space to the variable.
The memory space occupied by the struct variable is the sum of the memory occupied by its members, and each member is arranged in the order defined in the memory.
Structure Memory Structure
1. The memory occupied by the struct is related to the declared sequence of its members. For example:
1 struct stu1 {2 char a; // 1 byte 3 char B; // 1 byte 4 int c; // 4 byte 5 }; 6 struct stu2 {7 char a; 8 int c; 9 char B; 10}; 11 12 sizeof (stu1) --> 8 ?; // Related to the system 13 sizeof (stu2) --> 12 ?; // RELATED TO THE SYSTEM
2. Concepts of width and alignment:
1) the width of each member can be known by sizeof. For example, bool and char are 1, short is 2, int Is 4, and string is 8. 2) pre-compiled command # pragma pack (n), n =, 16 is used to specify the "alignment coefficient", # pragma pack () is used to restore the default. 3) data member alignment rule: the first member offset of a struct (or union) is 0, and the offset of each data member is the minimum value of its width and alignment coefficient. 4) structure alignment rules: the structure itself should also be alignment, alignment should follow the maximum width of its members and the minimum value of alignment coefficient.
Resolution (# pragma pack (4 )):
1 struct stu1 {2 char a; // 1 byte offset 0 3 char B; // 1 byte offset 1 4 // Since the alignment coefficient is 4, here we need to add two bytes. 5 int c; // 4 bytes offset 4 6}; 7 sizeof (stu1) --> 8 8 struct stu2 {9 char a; // 1 byte, offset 0, occupies 1 byte 10 // because the alignment coefficient is 4, here we need to add 3 bytes of 11 int c; // 4 bytes, offset 412 char B; // 1 byte, offset 813 // because the alignment coefficient is 4, add 3 bytes 14} Here; 15 sizeof (stu2) --> 12
Struct Initialization
Place the initial values of each member in a pair of braces {} sequentially and separate them with commas to assign values one by one.
For example, initialize the Student struct variable stu.
struct Student { char *name; int age;};struct Student stu = {"Zhy", 23};
The initialization assignment can only be performed while defining variables. The initialization assignment and the definition of variables cannot be separated. The following method is incorrect:
struct Student stu;stu = {"Zhy", 23};
Use of struct
Generally, the operations on struct variables are performed in the unit of members. The reference format is as follows:Struct variable name. member name
If a member is also a struct variable, you can use the member operator "." To access the lowest-level member continuously.
Assign values to all struct variables of the same type.
Struct Array
Definition Method
Struct Student {char * name; int age;}; struct Student stu [5]; // The first struct Student {char * name; int age;} stu [5]; // type 2 struct {char * name; int age;} stu [5]; // Type 3
Initialization
struct { char *name; int age;} stu[2] = { {"Zhy", 23}, {"zz", 18} };
Struct as function parameters
When a struct variable is passed as a function parameter, the values of all Members are actually passed, that is, the values of the members in the real parameter are assigned to the corresponding parameter members one by one. Therefore, changes to the form parameter do not affect the real parameter.
Pointer to struct
* Each struct variable has its own storage space and address, so the pointer can also point to the struct variable
* Struct pointer variable definition form: struct name * pointer variable name
* With pointers to struct, there are three access methods for struct members.
Struct variable name. member name
(* Pointer variable name). Member name
Pointer variable name-> member name
# Include <stdio. h> int main (int argc, const char * argv []) {// define a struct type struct Student {char * name; int age ;}; // define a struct variable struct Student stu = {"Zhy", 23}; // define a pointer variable struct Student * p pointing to the struct; // point to the struct variable stu p = & stu;/* at this time, you can access the struct member in three ways * // Method 1: struct variable name. member name printf ("name = % s, age = % d \ n", stu. name, stu. age); // Method 2: (* pointer variable name ). member name printf ("name = % s, age = % d \ n", (* p ). name, (* p ). age); // method 3: pointer variable name-> member name printf ("name = % s, age = % d \ n", p-> name, p-> age); return 0 ;}
Enumeration
Concept: enumeration is a basic data type in C, not a constructor type. It can be used to declare a group of constants. When a variable has several fixed possible values, you can define this variable as an enumeration type.
For example, you can use an enumeration type variable to describe the week, because the week is only 7, maybe from Monday to Sunday.
Enumeration type definition
The general format is: enum enumeration name {enumeration element 1, enumeration element 2 ,......};
1. First define the enumeration type and then the enumeration variable
enum Season {spring, summer, autumn, winter};enum Season s;
2. Define both enumeration types and enumeration Variables
enum Season {spring, summer, autumn, winter} s;
3. Omit the enumeration name and define the enumeration variable directly.
enum {spring, summer, autumn, winter} s;
The preceding three methods define the enumerated variable s.
Enumeration usage notes
1> the C language compiler processes enumeration elements (such as spring and summer) as Integer constants, which are called enumeration constants.
2> the values of enumeration elements depend on the order in which enumeration elements are arranged during definition. By default, the value of the first enumeration element is 0, the second is 1, and 1 is added sequentially.
enum Season {spring, summer, autumn, winter};
That is to say, the value of spring is 0, the value of summer is 1, the value of autumn is 2, and the value of winter is 3.
3> you can also change the value of an enumeration element when defining an enumeration type.
enum season {spring, summer=3, autumn, winter};
An enumeration element without a specified value. Its value is 1 added to the previous element. In other words, the spring value is 0, the summer value is 3, the autumn value is 4, and the winter value is 5.
Enumeration basic operations
Enum Season {spring, summer, autumn, winter} s; s = spring; // equivalent to s = 0; s = 3; // equivalent to s = winter;