2 local variables and their scope and lifetime 2.1 auto variables
When you define a variable inside a function or in a compound statement, if you do not specify a storage class or use the auto specifier, the default variable defined by the system is an automatic category. So:
float A; Equivalent to auto float A;
The storage unit of the auto variable is allocated in the dynamic storage area of the memory. When entering the function body, the system automatically allocates storage units for the auto variable, and the storage units are freed automatically when exiting. This type of local variable is scoped from the defined position to the end of the function body. Because of the frequent invocation of the function, the location of the storage unit allocated for a variable in the dynamic storage area changes with the program's operation, and the initial value of the variable is changed, so the values of the automatic variable with no initial number are indeterminate.
If the automatic variable is assigned the initial value by defining the statement, the operation of assigning the initial value is carried out in the process of running the program, and every time the function body is entered, the specified initial value is assigned. The most prominent advantage of using such local variables is that information can be isolated between functions, and the use of the same name variable in different functions does not interfere with each other, thus avoiding errors caused by accidental assignment affecting other functions.
2.2 Register variable
The register variable is also an automatic class variable, and it differs from the auto variable only in that the variable described by register suggests that the compiler keep the value of the variable in the register of the CPU, rather than the internal deposit element as a generic variable. When the program is running, accessing the value stored in the register is much faster than accessing the value stored in memory. Therefore, when the program is running at a higher speed, it is useful to specify the few variables that are frequently referenced as register variables, which helps to improve the speed of the program.
Description
(1) The number of registers in the CPU is limited, so only a small amount of register variables can be described. In a function, the number of allowable descriptions for a register variable depends not only on the type of CPU, but also on the C compiler used. The auto variable is automatically processed when there are not enough registers to hold the specified variable, or if the compiler considers the specified variable to be unsuitable for the register. Therefore, the register description is only a suggestion for compiling a program, not a mandatory one.
(2) Since the value of the register variable is placed in the register instead of in memory, the register variable does not have an address and cannot be evaluated for address operations.
(3) The Register variable description should be as close as possible to its use, in order to improve the efficiency of register utilization. This can be achieved by placing the description and use of the register variable in a compound statement.
2.3 Local variables for static storage classes
When you use static inside a function body (or compound statement) to describe a variable, you can call the variable a static local variable. The scope of a static local variable is still the same as that of the auto, register class, but there are two points in nature that differ from the latter:
(1) During the whole program operation, static local variables occupy a permanent storage unit in the static storage area of memory. Even after exiting the function, the static local variable still uses the original storage unit the next time the function is entered. Because these storage units are not freed, the values in these storage units are preserved so that the original values in the storage unit can continue to be used. The lifetime of a static local variable is extended until the end of the program run.
(2) The initial value of a static local variable is given at compile time, not during program execution (the automatic variable is assigned an initial value during program execution). The C compiler automatically assigns an initial value of 0 to a static local variable that is not assigned an initial value.
The above characteristics of static local variables are useful for writing functions that must retain local variable values between function calls.
Scope and storage classes for user identifiers in the C language (2)