In C language, the literal meaning of static can easily lead us astray; in fact, it has three roles.
(1) First, let's introduce its first and most important role: hiding.
When we compile multiple files at the same time, all global variables and functions without the static prefix have global visibility. To understand this statement, let me give an example. We need to compile two source files simultaneously: one is a.c, and the other is main.c.
The following is the content of a.c:
a.c file code
void msg()
{
printf("Hello\n");
}
The following is the content of main.c:
main.c file code
{
extern char a; // extern variable must be declared before use
printf("%c ", a);
(void)msg();
return 0;
}
The program output is:
A Hello
You might ask: why can the global variable a and function msg defined in a.c be used in main.c? As mentioned earlier, all global variables and functions without the static prefix have global visibility, and other source files can also access them. In this example, a is a global variable and msg is a function, and neither has the static prefix, so they are visible to the other source file main.c.
If static is added, they will be hidden from other source files. For example, if static is added before the definitions of a and msg, main.c will not see them. Using this feature, we can define functions and variables with the same name in different files without worrying about naming conflicts. static can be used as a prefix for functions and variables. For functions, the effect of static is limited to hiding, while for variables, static has the following two additional roles.
(2) The second role of static is to maintain the persistence of variable content.
Variables stored in the static data area are initialized when the program first starts running, and this is the only initialization. There are two kinds of variables stored in the static storage area: global variables and static variables. However, compared with global variables, static can control the visible scope of variables. In the final analysis, static is still used for hiding. Although this usage is not common, I will still give an example.
Example
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\t\tlocal static\n");
for(; count <= 10; ++count)
printf("%d\t\t%d\n", count, fun());
return 0;
}
The program output 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 to default initialize to 0.
In fact, global variables also have this property, because global variables are also stored in the static data area. In the static data area, the default value of every byte in memory is 0,0x00and sometimes this feature can reduce the programmer's workload. For example, when initializing a sparse matrix, we can set all elements one by one to 0,0and then assign the few elements that are not 0.0If defined as static, the initial operation of setting to 0 is omitted. For another example, if we want to use a character array as a string, but find it troublesome to add00 at the end of the character array every time.\0If the string is defined as static, this trouble is saved, because it is already 0 there.\0 。
Let's do a small experiment to verify.
Example
int a;
int main(void)
{
int i;
static char str[10];
printf("integer: %d; string: (begin)%s(end)", a, str);
return 0;
}
The program output is as follows:
integer: 0; string: (begin)(end)
Finally, let's summarize the three roles of static in one sentence. First, the most important function of static is hiding; second, because static variables are stored in the static storage area, they have persistence and a default value of 0.
Source: https://www.cnblogs.com/dc10101/archive/2007/08/22/865556.html