Static function (modifier function, local variable, global variable)
In the C language, the literal meaning of static can easily lead us astray. In fact, it has three functions.
(1) first introduce the first and most important one: hiding.
When we compile multiple files at the same time, all global variables and functions without the static prefix are globally visible. I will give an example to illustrate this sentence. We need to compile two source files, a. c and main. c at the same time.
The contents of A.C are as follows:
Char a = 'a'; // global variable
Void msg ()
{
Printf (Hello );
}
The content of main. c is as follows:
Int main (void)
{
Extern char a; // extern variable must be declared before use
Printf (% c, );
(Void) msg ();
Return 0;
}
The running result of the program is:
A Hello
You may ask: why can the global variables a and msg defined in a. c be used in main. c? As mentioned above, all global variables and functions without the static prefix have global visibility, and other source files can also be accessed. In this example, a is a global variable, msg is a function, and there is no static prefix. Therefore, it is visible to other source files main. c.
If static is added, other source files are hidden. For example, if static is added before the definitions of a and msg, main. c will not be able to see them. This feature allows you to define functions with the same name and variables in different files without worrying about name conflicts. Static can be used as the prefix of functions and variables. For a function, the role of static is limited to hiding. For a variable, static has the following two functions.
(2) The second role of static is to keep the variable content persistent. Variables stored in the static data area will be initialized at the beginning of the program, which is also the only initialization. There are two types of variables stored in the static storage area: global variables and static variables, but compared with global variables, static can control the visible range of variables. static is used to hide the variables. Although this is not common, I will give an example.
# Include
Int fun (void ){
Static int count = 10; // In fact, this assignment statement has never been executed.
Return count --;
}
Int count = 1;
Int main (void)
{
Printf (global local static );
For (; count <= 10; ++ count)
Printf (% d, count, fun ());
Return 0;
}
The running result of the program is:
Global local static
1 10
2 9
3 8
4 7
5 6
6 5
7 4
8 3
9 2
10 1
(3) The third role of static is that the default Initialization is 0. In fact, global variables also have this attribute, because global variables are also stored in the static data zone. In the static data area, the default values of all bytes in the memory are 0x00. In some cases, this feature can reduce the workload of programmers. For example, to initialize a sparse matrix, we can set all elements to 0 one by one, and assign values to elements other than 0. If it is defined as static, the first 0 operation is saved. Another example is to use a character array as a string, but it is too troublesome to add ''at the end of the character array each time. If the string is defined as static, it saves the trouble because it is ''. Try a small experiment.
# Include
Int;
Int main (void)
{
Int I;
Static char str [10];
Printf (integer: % d; string: (begin) % s (end), a, str );
Return 0;
}
The running result of the program is as follows:
Integer: 0; string: (begin) (end)
Finally, we will summarize the three functions of static in one sentence. First, the main function of static is to hide. Secondly, because static variables are stored in the static storage area, they have persistence and default value 0.
The above content is written by Mr. Write in the blog Park, which is quite clear and easy to understand and can be archived for convenient review. Address: http://www.cnblogs.com/dc10101/archive/2007/08/22/865556.html
1. What is the difference between static global variables and common 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.
Static global variables are modified only once to prevent being referenced in other file units;
2. What is the difference between static local variables and common local variables?
After a local variable is changed to a static variable, its storage mode is changed, that is, its lifetime is changed. After changing a global variable to a static variable, it changes its scope and limits its scope of use.
Static local variables are initialized only once, and the next time is based on the previous result value;
3. What is the difference between a static function and a common function?
Static functions have different scopes than normal functions. They are only available in this file. The function used only in the current source file should be regarded as an internal function (a static-modified function). The internal function should be described and defined in the current source file. For functions that can be used outside the current source file, it should be described in a header file that the source file to use these functions must contain this header file.