1. All variables and local variables
Local variables: variables defined in the function. The scope is the function's internal eg: void fun () {int X ;}
All variables: can be defined only once and referenced multiple times. The scope is the entire file.
If the names of global variables and local variables are duplicate in the same source file, all variables do not work in the scope of local variables.
Keywords:
All variables: Out-of-Function Definition + global visibility + global data zone stored in the memory + 0 when not initialized
Local variables: defined in the function + visible in the function + stored in the stack, the function exit variable disappears + the Explicit initialization content is unpredictable
2. Static and Dynamic Storage Variables
Static storage variables: variables that occupy fixed memory permanently during the running of the program + are stored in the stack, and the life cycle is the whole program.
Dynamic storage variables: temporary and Dynamic Distribution of storage space variables + stored in the heap zone, automatically released after the function is completed
Therefore, the instruction code of the program is placed in the code area, and the static storage variable is placed in the static data area (such as global variables ), the dynamic storage variables of the program are stored in the dynamic data area (such as the form parameters of the function and the return address of the function call)
3. Static local variables, static global variables, and external variables (extern)
Static local variables: The difference from local variables is that the variable still exists but cannot be used by other functions when the function exits. When you enter the function again, you can use the last saved result. It can be understood that local variables are cached and valid in the original scope. (Only initialized once. The next initialization depends on the previous result value)
Eg:
# Include <stdio. h>
Voidfun1 (INT v ){
Static int A = V;
Printf ("% d",)
}
Intmain (){
Fun1 (100 );
Fun1 (200 );
}
The result of the two executions is 100, and the initialization of A for the second call is the previous value, reflecting the storage
Static global variables and extern are valid only in the defined source file. The difference between static global variables and global variables is: global variables and the external variables declared as extern ), used by other source files. Static global variables cannot be used. They can only be used in the source file.
The scope of any function modified with static is only the current source file, but this function is invisible to the outside.
Note:
A program divides the memory blocks allocated to the operating system into four areas:
1. Code area: stores the code blocks of various functions of a program.
2. Global Data zone: stores global data and static data (one initialization and multiple calls)
3. Heap zone: Dynamic Data
4. Stack zone: Local Data
In-depth understanding of local, global, static, dynamic, and external variables