I. Static variables
Static variables are roughly divided into three usage types:
1. It is used in local variables to become static local variables. Static local variables have two usages: Memory Function and global survival.
2. It is used for global variables and is mainly used to restrict the global variables from being called by other files.
3. Used for the member in the class. indicates that the member belongs to this class but does not belong to any specific object in the class.
1. Static local variables
Static local variables are stored in static mode and have the following features:
(1) the lifetime of a static local variable is defined as the entire source program in the function, but its scope is still the same as that of an automatic variable. It can only be used within the function that defines the variable. After exiting the function, although the variable still exists, it cannot be used.
(2) An initial value such as an array can be assigned to the static local volume of the constructor class. If the initial value is not assigned, the system automatically assigns the value 0.
(3) If an initial value is not assigned to a static local variable of the basic type, the system automatically assigns the value 0. If the initial value is not assigned to the automatic variable, the value is not fixed. According to the characteristics of static local variables, it can be seen that it is a kind of lifetime for the entire source program. Although this function cannot be used after it is defined, it can continue to be used when the function is called again, and the value left after the previous call is saved. Therefore, when you call a function multiple times and require that the values of some variables be retained between calls, you can consider using static local variables. Although global variables can also achieve the above purpose, global variables sometimes cause unexpected side effects, so it is best to use local static variables.
Example:
Int fun (){
Static int A = 1;
A ++;
}
When you enter this function for the first time, variable A is initialized to 1! And then auto-increment 1. After entering this function, a will not be initialized again, and only perform auto-increment 1 operation. Before the static invention, the same function should be achieved, only global variables can be used:
Int A = 1;
Int fun (){
A ++;
}
2. Static global variables
The description of global variables (external variables) is preceded by static to form a static global variable. Global variables are static storage, and static global variables are also static storage. The two are not different in storage methods. The difference between the two lies in that the scope of non-static global variables is the entire source program. When a source program is composed of multiple source files, non-static global variables are valid in each source file. The static global variable limits its scope, that is, it is valid only in the source file defining the variable, and cannot be used in other source files of the same source program. Because the scope of static global variables is limited to one source file, they can only be shared by functions in the source file. Therefore, errors in other source files can be avoided. From the above analysis, we can see that after a local variable is changed to a static variable, its storage mode is changed, that is, its survival time is changed. After changing a global variable to a static variable, it changes its scope and limits its scope of use. Therefore, the description of static plays different roles in different places.
3. Static class member variables
The static keyword has two meanings. You can check the context to determine
A: The variable is a static storage variable, indicating that the variable is stored in the static storage area.
B, indicates that the variable is an internal connection (in this case, the variable is not in any {}, just like the global variable, and static is added at this time), that is, in other. in the CPP file, the variable is invisible (you cannot use it ).
Ii. Static functions-internal and external functions
When a source program is composed of multiple source files, the C language divides the function into internal functions and external functions based on whether the function can be called by functions in other source files.
1 internal function (also known as static function)
A function defined in a source file can only be called by functions in this file, but cannot be called by functions in other files of the same program. This function is called an internal function.
To define an internal function, you only need to add a "static" keyword before the function type, as shown below:
Static function name (function parameter table)
{......}
The keyword "static" is "static" in Chinese. Therefore, internal functions are also called static functions. However, the meaning of "static" here is not the storage method, but the scope of the function is limited to this file.
The advantage of using internal functions is that when different people write different functions, you don't have to worry about whether your own defined functions will have the same name as the functions in other files, because the same name does not matter.
2. External Functions
External Function Definition: when defining a function, if the keyword "static" is not added or the keyword "extern" is appended, this function is an external function:
[Extern] function name (function parameter table)
{......}
When calling an external function, you need to describe it:
[Extern] function name of the function type (parameter type table) [, function name 2 (parameter type table 2)…];
[Case] external function application.
(1) file mainf. c
Main ()
{Extern void input (...), Process (...), Output (...);
Input (...); Process (...); Output (...);
}
(2) file subf1.c
......
Extern void input (......) /* Define external functions */
{......}
(3) file subf2.c
......
Extern void process (......) /* Define external functions */
{......}
(4) file subf3.c
......
Extern void output (......) /* Define external functions */
{......}