(1) local variable
a variable defined within a function is an internal variable that is valid only in the scope of this function, that is, only within this function. These variables cannot be used outside of this function, they are called local variables.
1. The variables defined in main function main are also valid only in the main function, not because they are defined in the main function and are valid throughout the file or program.
2. Variables with the same name can be used in different functions, which represent different objects and do not interfere with each other.
3. The formal parameter is also a local variable.
4. Within a function, you can define variables in compound statements that are valid only in this compound statement.
(2) Global variables
variables defined outside the function are external variables, external variables are global variables, and global variables can be shared by other functions in this file. Its effective range starts at the location where the variable is defined and ends with the source file.
1. Set the role of global variables: increase the channel of data connection between functions.
2. Do not use global variables when they are not necessary, because
a. Global variables occupy the storage unit during the full execution of the program.
B. It reduces the versatility of a function by
C. Excessive use of global variables can reduce the clarity of the program.
3. If the external variable is defined at the beginning of the file, the external variable can be used throughout the file scope, if not defined at the beginning of the file. The scope of action specified above is limited to the end of the file. If the function before the definition point wants to refer to the external variable, you should use the keyword extern as the external variable description in the function.
4. If the external variable has the same name as a local variable in the same source file, the external variable does not work within the scope of the local variable.
(3) static variable
The scope of the static variable depends on the position of the static variable, and if it is in the function, the scope is the function.
static global variable, only in this file can be used, although the entire program contains multiple files, but static global variables can only be used in the definition of the file, Can not be used in other files in the program. It is an external variable that defines a static type of storage because the scope is from the definition point to the end of the program, and the difference is that the storage type determines the storage location, and the static variables are stored in the data area of the memory before the program starts running before A fixed byte is allocated, the size of the bytes allocated during the program is not changed, and only after the program is run is the memory consumed.
"Attention"
how does the operating system and the compiler judge global and local variables?
the operating system and compiler get the type of the variable based on the memory area in which the program runs. The global data of the program is placed in the global data area of the allocated memory, and the local data of the program is placed in the stack area.   &NBSP
Attention "
<1>static global variables are different from ordinary global variables.
Global variables (external variables) before the description of the static to form the global variable. The global variable itself is static storage, and static global variables are, of course, static storage mode. The two are not different in storage. The difference between the two is that the scope of the Non-static global variable is the entire source program, when a source program consists of multiple source files, non-static Global variables are valid in each source file. A static global variable restricts its scope, that is, only valid in the source file where the variable is defined. It cannot be used in other source files of the same source program. Because the scope of a static global variable is limited to one source file and can only be public for functions within that source file, you can avoid causing errors in other source files. From the above analysis, we can see that Changing a local variable to a static variable changes the way it is stored, which changes its lifetime. Changing the global variable to a static variable changes its scope and limits its use. The
<2>static function differs from a normal function. The
static function differs from the normal function scope. Only in this file. Functions that are used only in the current source file should be described as internal functions (static). Intrinsic functions should be described and defined in the current source file. For functions that can be used outside of the current source file, you should specify in a header file that the source file that you want to use these functions should contain this header file.
<3>static What is the difference between a local variable and a normal local variable. The
static local variable is initialized only once, and the next time it is based on the last result value;