C language storage types and comparison of the scope and survival scope of each storage type, the scope of c language variables
There are four types of storage in C language: `auto`,` extern`, `register`, and` static`. The storage type indicates which segment of the process the variable is to allocate memory space. You can allocate memory for the variable. There are data area, BBS area, stack area and heap area in space.
Automatic storage type (`auto`)
The automatic storage type modifier specifies that a local variable is automatic, which means that every time a block of the statement that defines the variable is executed, a new copy of the variable will be generated in memory and the Perform initialization **. In fact, if not specified, the storage type of local variables is automatic by default, so you can add or not `auto`.
1 // The default storage type of the function is also auto.
2 int main ()
3 {
4 // In this example, no matter whether the declaration of m contains auto, the execution effect is the same, that is, the default is auto,
5 auto int m = 99;
6 printf ("% d \ n", m);
7 return 0;
8 }
Static storage variables (`static`)
Variables declared as static types, whether global or local, are stored in the data area, and their life cycle is the entire program. If they are static local variables, their scope is within a pair of {}, if they are static global Variable whose scope is the current file. If a static variable is not initialized, it is automatically initialized to 0. ** Static variables can be initialized only once. In subsequent runs, they will retain the values that the program last used. **
1 // Compare the difference between auto type variables and static type variables in the execution process
2 #include <stdio.h>
3 int main ()
4 {
5 int i;
6 for (i = 0; i <3; i ++)
7 {
8 auto int m = 20;
9 static int n = 20;
10 printf ("m% d is% d,", i, m);
11 printf ("n% d is% d;", i, n);
12 printf ("\ n");
13 m ++;
14 n ++;
15}
16 return 0;
17}
operation result:
m0 is 20, n0 is 20;
m1 is 20, n1 is 21;
m2 is 20, n2 is 22;
External storage variables (`extern`)
Generally, `extern` is used to declare a variable defined in another translation unit. That is, `extern` is used to declare in the current file that global variables defined in other files in the current project are referenced. If the global variable is not initialized, it will be stored in the BBS area, and its value will be automatically assigned to 0 during compilation. If it has been initialized, it will be stored in the data area. Global variables, whether they are initialized or not, have a life cycle during the entire program. In order to save memory space, when extern is used to declare global variables defined in other files in the current file, no memory space will be allocated for them. .
Register storage type (`register`)
A variable declared as a register storage type is the same as an automatic variable except that the program cannot get its address. Variables declared as register are resident in the CPU registers after being transferred from the memory to the CPU registers, so accessing the register variables will greatly improve the efficiency, because the process of transferring variables from the memory to the registers is eliminated Several instruction cycles.
Comparison of storage types
As can be seen from this table, there are three types of identifier scopes for C programs: * local, global, file *. The scope of an identifier determines which statements in the program can use it, in other words, the visibility of the identifier in other parts of the program. Usually, the scope of an identifier is implicitly stated by its position in the program.