Introduction to three keywords in C Language storage and three keywords in C Language Storage
1. static
This keyword can be used in three ways:
(1) The first type is to modify local variables to make them static local variables. Static local variables are stored in the data segment/bss segment. The scope is the code block scope, and the life cycle is the program lifecycle, the link property is connectionless. Static local variables are initialized only once. The values used later are the values after the previous use (similar to global variables)
(2) The second type is used to modify global variables to make them static global variables. Static global variables are stored in the data segment/bss segment. The scope is the file scope, and the life cycle is the program life cycle, the link property is an external link. Static-modified global variables are valid only in the defined source files and are not available in other source files of the same source program. This feature can be used to avoid the problem of cognominal variables.
(3) The third type is to modify the function to make it a static function. Static global variables are stored in the data segment/bss segment. The scope is the file scope, and the life cycle is the program life cycle, the link property is internal link. The static modifier function is only valid in the defined source file and cannot be used in other source files of the same source program. This feature can be used to avoid the problem of duplicate names of multiple file functions.
(4) In a program with multiple source files, the static keyword should be added to the functions or global variables used only in one source file.
2. auto
This keyword has only one usage:
It can only be used to modify local variables to make them automatic local variables. Automatic local variables are stored on the stack. In fact, we usually define local variables as automatic local variables, but we omit the auto keyword; it can be seen that defining common local variables is the local variables modified by the auto keyword by default.
3. extern
(1) This keyword is often used to declare a global variable. The goal is to define a global variable in a. c In a multi-File Project and use this global variable in B. c;
(2) because the C language program is compiled with a single. the c file is compiled in units. If it is directly used without declaration, the compiler reports an error. The solution is to use the extern keyword Declaration (note that the value cannot be assigned during Declaration; otherwise, an error occurs: Repeated definition)
(3) It should be in B. c declares this global variable before using this global variable, telling the compiler that I have defined this global variable in another file, and the prototype is the same as the declaration, so that the linker will go to another one when linking. o file.