The role of 'static' in C language
Category Programming Techniques
In the C language, the literal meaning of 'static' can easily mislead us. In fact, it has three main functions.
(1) Let's first introduce its first and most important function: 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, I will give an example. We are compiling two source files at the same time, one is a.c, and the other is main.c.
Here is the content of a.c:
a.c file code
char a = 'A'; // global variable
void msg()
{
printf("Hello\n");
}
Here is the content of main.c:
main.c file code
int main(void)
{
extern char a; // extern variable must be declared before use
printf("%c ", a);
(void)msg();
return 0;
}
The program's 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, 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, it 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. This feature can be used to 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 role of 'static' is limited to hiding, while for variables, 'static' also has the following two functions.
(2) The second function of 'static' is to maintain the persistence of variable content.
Variables stored in the static data area are initialized when the program first starts, and it is the only time they are initialized. There are two types of variables stored in the static storage area: global variables and 'static' variables. However, compared with global variables, 'static' can control the visibility range of variables. After all, 'static' is still used for hiding. Although this usage is not common, I will still give an example.
Example
#include <stdio.h>
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's 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 function of 'static' is to initialize by default to 0.
In fact, global variables also have this attribute because global variables are also stored in the static data area. In the static data area, all bytes in memory have a default value of 0x00. Sometimes this feature can reduce the workload of programmers. For example, initializing a sparse matrix, we can set all elements to 0 one by one, and then assign a few elements that are not 0. If defined as static, it saves the operation of setting 0 at the beginning. For example, if you want to use a character array as a string but find it troublesome to add \0
at the end of the character array each time. If the string is defined as static, this trouble is saved because it is already \0
.
Let's do a small experiment to verify it.
Example
#include <stdio.h>
int a;
int main(void)
{
int i;
static char str[10];
printf("integer: %d; string: (begin)%s(end)", a, str);
return 0;
}
The program's output is:
integer: 0; string: (begin)(end)
Finally, a one-sentence summary of the three functions of '