Scope and Storage Class of user identifiers in C Language (3), identifier
3. global variables and their scopes and survival periods
Global variables only have one static category. You can use the extern and static specifiers for global variables.
3.1 Scope and lifetime of global variables
A global variable is a variable defined at any position outside the function. Its scope starts from the position defined by the variable and ends at the end of the entire source file.
The use of global variables is equivalent to opening up another channel for data transmission between functions. The lifetime of the global variable is the entire program running period. If the global variable has the same name as the local variable in a function, the global variable is blocked in the function and the local variable is accessed in the function, global variables with the same name do not have any relationship.
Although the global variables have a large scope and long lifetime, they seem convenient and flexible to use. However, note that, unless necessary, global variables are generally not recommended, the reasons are as follows:
(1) whether or not the global variables occupy the memory space during the entire program running;
(2) global variables must be defined outside the function, reducing the versatility of the function and affecting the independence of the function.
(3) using global variables may lead to unexpected changes in values of global variables due to negligence or improper use, resulting in hard-to-find errors.
3.2 extend the scope of global variables with the extern specifier within the same compilation unit
When a global variable is defined, the function that consumes it should be described with extern in the function that consumes it, so as to notify the Compilation Program: this variable is a global variable that has been defined externally. It has been assigned a storage unit and does not need to be opened for it. At this time, its scope starts from the extern description and extends to the end of the function.
Note: The description of global variables is different from that of global variables. The definition of a variable (opening a storage unit) can only appear once. When defining a global variable, the extern specifier cannot be used, it can appear in the desired place multiple times, and extern must be used for description.
Int x, y;/* defines the format of global variables */
Extern int x, y;/* Format of Global variables */
3.3 extend the scope of global variables with the extern specifier in different compilation units
). Generally, each source file that can be compiled separately becomes a "compilation unit ".
When a program is composed of multiple compilation units and the same global variable needs to be consumed in each file, if a required global variable with the same name is defined in each file, the "Duplicate definition" error will occur during "Link. In this case, there is no exception when compiling each file separately. The compilation program will open up storage space for each file separately by definition, and when connecting, an error message will be displayed, the same variable name is repeatedly defined.
The solution to the above problem is generally: define all global variables in one file, and use extern to describe these variables in other files that use these global variables, declare that these variables have been defined in other compilation units and notify the Compilation Program not to open up storage units for them.
3.4 static global variables
When a global variable is described using the static specifier, this variable can be called a "static" global variable. Static global variables are limited to the current compilation unit and cannot be referenced by other compilation units. Static description limits the extension of the global variable scope and achieves information hiding. This is very helpful for writing a large program with many compilation units, and programmers do not have to worry about confusion caused by the duplicate names of global variables.