1.auto type. 1) can only be used to modify a local variable, indicating that the variable is stored on the stack. 2) The default local variable is the aotu type, so it is usually omitted.
2.register. You can only modify the local variables, and upgrade the memory heavy variables to the cpu register for storage, so the access speed will be very fast. The variables that store cpu are fast but limited in number and are usually used for optimization. Variables for frequent use are generally not used. It must be noted that variables modified with register cannot go to the address value because the variable is stored in the register and has no address.
3.extern Used to modify a variable or function, extern can be placed before the variable or function to indicate that the variable or function is defined in another file, prompting the compiler to find its definition in other modules when it encounters this variable or function. Note: Because the declaration function has no function body (and ends with a semicolon), you can omit the extern when declaring the function. However, extern is usually omitted when the header file (.h file) is declared. If it is declared in other c files, it is recommended to add extern. (1) Do not define global variables in the header file. Global variables need to be defined in the .c file, and the header file can be declared using extern. (2) The function declared by the file may not be extern, and it shall not be defined in the header file.
4.static There are two main usages: (1) limited scope, such as Modify global variables - global static variables; Modification function - static function. (2) Keep the variable content persistent; Modify local variables - local static variables.
*Modify global variables - global static variables By adding the keyword static before the global variable, the global variable is defined as a global static variable (qualified scope). The global static variable scope is restricted and is intended to be visible within the file defining it, precisely from the beginning of the definition to the end of the file. Add the keyword ststic before the function return type, and the function becomes a static function. A static function is only visible in the file that defines it and cannot be used by other files.
使用原理:(1) A function does not want to be referenced by other modules, it is modified using static; (2) Different files may have the same function name. In this case, using static to modify can solve the problem of duplicate names.
*Modify local variables - local static variables By adding the keyword static before the local variable, the local variable becomes a local static variable. Scope: The scope is either a local scope. When the function or statement block that defines it ends, the scope ends. But when the local variable static variable leaves the scope and is not destroyed, but remains in memory, the function is called again, the same value as when the previous call exited. Local variables are defined on the stack, and the variables are destroyed after the function exits. Local static variables are defined in the static area, which is already in memory when the program starts running.
5. Memory layout The c language program is mainly divided into four areas during memory execution: (1) code area, (2) static area, (3) heap area, and (4) stack area (in most cases, we only need to understand these four The district is ok).
码区: Code area, usually refers to a memory area used to store program execution code. The size of the area in this section is determined before the program is executed. And the memory area is usually read-only. In the code snippet, it is also possible to include some read-only constant variables, such as string constants.
静态区: The content stored in the static area is determined when the program is loaded into the memory, and disappears from the memory when the program exits. All global variables/global static variables and local static variables are stored in the static area, and the life cycle is occupied during the running of the program; the variable life period stored in the static area starts from the program running time until the end of the program.
栈区: Stack stack is a kind of advanced back-end structure, which is automatically allocated by the compiler to release memory, store parameter values of functions, values of local variables, and so on.
(1) The function call is advanced; (2) The function is pushed onto the stack from the high address to the low address; (3) The space occupied by the function call into the stack is equal to the space to be released when the stack is popped. (4) The depth of the function call stack is determined by the space allocated on the function stack. If a larger array is allocated, the stack will be deeper; (5) The array cannot be allocated too large array (such as 2M to try), otherwise the stack will overflow, causing the program to crash. (6) The life cycle of the stack variable, which starts at the time of execution of the function until the stack exits at the end of the function call (lifecycle and scope are irrelevant).
Heap area: Also called the dynamic memory allocation area, which is the area where the programmer is responsible for allocating and releasing memory. The difference between the stack area and the stack area: (1) The space allocated by the stack area should not be too large, generally not more than 2M; the heap area can allocate a large memory space, as long as the memory is large enough (no problem is allocated for 100M/1000M). (2) The variable space of the stack area is automatically allocated and released by the system; the heap area must be applied by the programmer (malloc) and released (free), and the active release will cause memory leaks.