1.Dynamic Storage and static storage
Variables can be divided into global variables and local variables from the perspective of scope (I .e. from space.
From another perspective, from the perspective of the time when the variable value exists (that is, the lifetime), it can be divided into static storage and dynamic storage.
Static storage:A fixed storage space is allocated during the running of the program.
Dynamic Storage:Dynamically allocates storage space as needed during the program running.
User buckets can be divided into three parts:
1) Procedure area;
2) static storage area;
3) Dynamic Storage zone;
Global variables are all stored in the static storage area. When the program starts to execute, the global variables are allocated to the storage area, and the program is released after the row is complete. They occupy a fixed storage unit during program execution without Dynamic Allocation and release;
The dynamic storage area stores the following data:
1) function form parameters;
2) Automatic variables (local variables without static Declaration );
3) field protection and return address during function calling;
For the above data, dynamic storage space is allocated when the function starts calling, and the space is released when the function ends.
In C, each variable and function has two attributes: Data Type and data storage type.
2. autoVariable
Local variables in the function, if not specifically declared as static storage classes, are dynamically allocated storage space, and data is stored in the dynamic storage area. The form parameters in the function and the variables defined in the function (including the variables defined in the composite statement) belong to this class. When the function is called, the system will allocate storage space to them, these buckets are automatically released at the end of the function call. These local variables are called automatic variables. Auto variables use the keyword auto for the storage class declaration.
The keyword auto can be omitted. If auto is not written, it is implicitly set to "Automatic Storage category", which is a dynamic storage method.
3.Declare local variables with static
Sometimes you want the local variable value in the function to keep the original value without disappearing after the function call. In this case, you should specify the local variable as "static local variable" and declare it with the keyword "static.
[Example 1.8] evaluate the static local variable value.
F (int)
{
Auto B = 0;
Static c = 3;
B = B + 1;
C = c + 1;
Return (a + B + c );
}
Int main (void)
{
Int a = 2, I;
For (I = 0; I <3; I ++)
Printf ("% d", f ());
}
Description of static local variables:
1) Static local variables belong to the static storage class and are allocated to storage units in the static storage area. The program is not released during the entire running period. Automatic variables (that is, dynamic local variables) belong to the dynamic storage class, occupying the dynamic storage space, and are released after the function call ends.
2) assign an initial value to a static local variable during compilation, that is, assign the initial value only once. Assign the initial value to an automatic variable when the function is called, and re-assign the initial value to each callback function, it is equivalent to executing a value assignment statement.
3) if initial values are not assigned when defining local variables, the initial values 0 (For numeric variables) or null characters (for character variables) are automatically assigned to static local variables during compilation ). For automatic variables, if the initial value is not assigned, its value is an uncertain value.
[Example 1.9] print the factorial value from 1 to 5.
Int fac (int n)
{
Static int f = 1;
F = f * n;
Return (f );
}
Int main (void)
{
Int I;
For (I = 1; I <= 5; I ++)
Printf ("% d! = % D \ n ", I, fac (I ));
}
4. registerVariable
To improve efficiency, the C language allows you to place the value of a local variable in a register in the CPU. This variable is called a "register variable" and is declared using the keyword register.
[Example 2.0] use register variables.
Int fac (int n)
{
Register int I, f = 1;
For (I = 1; I <= n; I ++)
F = f * I;
Return (f );
}
Int main (void)
{
Int I;
For (I = 0; I <= 5; I ++)
Printf ("% d! = % D \ n ", I, fac (I ));
}
Note:
1) only local automatic variables and formal parameters can be used as register variables;
2) The number of registers in a computer system is limited, and multiple register variables cannot be defined;
3) Local static variables cannot be defined as register variables.