In C, users can use three types of storage space: program area, static storage area, and dynamic storage area.
- The program stores machine commands of executable programs;
- The static storage area stores the variables of the fixed storage units needed during the program running, such as global variables.
- The dynamic storage area stores the variables that dynamically allocate content space as needed during the program running process, such as formal parameters and local variables.
Variable Storage refers to the way data is stored in memory. There are two types of variable storage: static storage and dynamic storage. There are four types: auto, static, register, and extern)
- Local variable Storage Method
There are three storage methods for local variables: automatic, static, and register.
- Automatic Variable
- Static variables
- Register variable
Automatic variables: if the local variables in the function are not explicitly declared as static storage, they are dynamically allocated storage space and the data is stored in the dynamic storage area. When a function is called, The system allocates storage space to the data, which is released at the end of the function call. Auto variables use the keyword auto as the declaration of the storage class, which can be omitted
Static variables: the memory space occupied by the automatic variables after the function call is completed is released. Sometimes, you want the value of the local variables in the function to remain unchanged after the function call is completed, that is, the occupied content space is not released. When the variable has a value in the next call, you can declare the local variable as a "static local variable" and declare it with the keyword "static ".
Note: local static variables are allocated to storage units in the static storage area and are not released during the entire program running. Local static variables are assigned values during program compilation, if the value is assigned only once, the initial value has been determined when the program is running. In the future, no value is assigned when the function is called, but the value at the end of the callback function is retained. The initial value is 0 (integer variable) or a null character (character type variable). Other functions cannot be referenced.
Register variable: the C language allows you to place the value of a local variable in a register in the CPU. When necessary, you can directly read data from the Register without having to read data in the memory. This variable is called a register variable, declare with the keyword "register"
Note: Only Local Automatic variables and formal parameters can be used as register values.
- Storage of global variables
- External global variables
- Static global variables
Global variables are allocated memory units in the static storage area. There are two types of storage: External type (extern) and static type (static)
External global variables: global variables are defined outside the function, and their scopes start from the definition of variables to the end of the program file. If the function before the definition point wants to reference the variable, declare the variable with the keyword extern before the reference and declare the variable as an external global variable.
Note: extern cannot be used to initialize variables. The function of extern is to expand the scope of global variables. When the system compiles extern, we first look for the definition of global variables in this article. If it cannot be found, definitions of global variables in other files at link time
Static global variables: when designing a program, you sometimes do not want some global variables to be referenced by other files. In this case, you can use the keyword static to declare global variables.
Note: whether or not to perform static declaration on global variables, global variables are stored in static mode. static global variables declared in static mode are used, functions defined in this file before global variables cannot be referenced.