In-depth understanding of the three functions of the C language static keyword

static modified local variables

insert image description hereinsert image description hereinsert image description here

Figure 1: The a defined in the test function is a local variable. The local variable opens up space on the stack area. The use of the stack area is that it automatically opens up space for the variable when it enters the life cycle of the variable, and automatically destroys the corresponding space when it leaves the life cycle of the variable. , so here a will be redefined and initialized to 0 every time the test function is called, so 10 1s are printed on the screen;

Figure 2: After we decorate a with static, we find that the screen prints 1 to 10, as if a is not destroyed after each call to the test function, but continues to be used. The next time the test function is called, a is directly in the previous Based on the ++ operation.
Therefore, the role of static modifying local variables is to change the life cycle of local variables, essentially changing the storage location of local variables, so that local variables no longer open up space on the stack area, but directly open up space in the static area. Thus, local variables have the same life cycle as global variables, that is, they are generated and destroyed with the entire program.

A deeper understanding of the role of static modification of local variables : Figure 3, our program changes from a source file (.c file) to an executable program (.exe file) that needs to be compiled, linked and run in three stages, and the compilation stage is divided into three parts There are three stages of preprocessing, compilation and assembly . In the assembly stage, the compiler will convert our C language code into assembly code, and each C language statement corresponds to multiple lines of assembly code. However, in Figure 3, we can It is observed that only static int a = 0; this statement has no corresponding assembly code, that is to say, C language will skip this statement directly when compiling.
Essentially: in the compilation phase of the compilation link, the compiler will allocate space for the statically modified local variables, so the C program will directly skip the statically modified statement during the running process, that is, in the second and The above statement will not even be executed when the test function is called for the first time, static int a = 0;.

static modifies global variables

insert image description hereinsert image description here

Figure 1 and Figure 2 Comparative analysis: I defined a global variable g_val in Add.c, because the global variable has an external link attribute, so I only need to declare g_val in test.c and it can be used normally, but when When I used static to modify g_val, we found that the compiler said that g_val is an unresolved external symbol;
so the role of static modifying global variables is to change the external link attributes of global variables (which can be accessed in other source files), Making it an internal link property (which can only be accessed inside this file) gives us the impression that the scope of the global variable is reduced.

static decorated function

insert image description hereinsert image description here

Comparative analysis of Figure 1 and Figure 2: This is very similar to the static modification of global variables. I defined an Add function in Add.c, because the function also has external link attributes, so I only need to declare the Add function in test.c After that, it can be used normally, but when I use static to modify the Add function, we find that the compiler says that Add is an unresolved external symbol;
so the role of the static modified function is to change the function's external linkage attribute (which can be found in be accessed in other source files), making it an internal connection attribute (can only be accessed within this file), giving us the feeling that the scope of the function has become smaller.

Summarize

  • The role of static modification of local variables: changing the life cycle of local variables essentially changes the storage location of local variables, so that local variables no longer open up space on the stack area, but directly open up space in the static area, thus making Local variables have the same life cycle as global variables, that is, they are created and destroyed with the entire program.
  • The role of static modifier global variables: changes the external link attribute of the global variable (which can be accessed in other source files), making it an internal link attribute (which can only be accessed within this file).
  • The role of the static modifier function is to change the external linkage attribute of the function (which can be accessed in other source files), making it an internal linkage attribute (which can only be accessed within this file).

Guess you like

Origin blog.csdn.net/m0_62391199/article/details/124093244