Detailed explanation of C language keywords (2) Take you to a comprehensive understanding of the static keyword

I. Introduction

Hello everyone, welcome to the second part of the C language in-depth analysis column - keyword detailed explanation. In this article, we will introduce the static keyword in detail, which requires us to master the global variables I mentioned in the previous article, The related concepts of local variables, scope and life cycle, if you are vague about these concepts, you can move to my last blog first. The following is the blog link.
Detailed explanation of C language keywords (1)

2. Recognize multiple files

In order to understand the role of the static modification function, we need to understand the content of multiple files

1. Creation of multiple files

Here I will first introduce the creation of the header file: the creation of the header file is very similar to the creation of the .c file, except that the c++ file is changed to .h when it is selected.
insert image description here
insert image description here
H: We call it a header file, which generally contains functions Declarations, variable declarations, macro definitions, header files, etc. (header)
.c: We call it source files, which generally include function implementations, variable definitions, etc. (.c:c language)
Multiple files are under a .h file, Contains multiple .c files, such as main.c test1.c test2.c teset3.c  …

2. Why do you need multiple files?

In a large-scale project of a company, the functions to be implemented by the expected product are often very complex, so the functions are generally modularized, so as to facilitate code reuse, code modification and maintenance, and multi-person collaboration. Naturally We need multiple .c files in one program

3. Why do you need a header file?

Simply using source files, when organizing the project structure, the larger and more complex the project, the maintenance cost will become higher and higher!
Therefore, we will use header files when organizing the project structure to reduce the maintenance cost of large projects.

Supplement : The meaning of #pragma once in the header file

When you create a .h header file, you will find that the compiler will automatically add #pragma at the beginning of the header file.
insert image description here
Once I believe that many small partners have deep doubts about this thing in the past or now, in fact, it uses To prevent the header file from being repeatedly included, for example,
insert image description here
insert image description here
as shown above: I include the header file <stdio.h> in test.h, but in main.c I also include both test.h and stdio.h , which causes stdio.h to be included twice, so that the program copies the contents of stdio.h twice when compiling, resulting in code redundancy, and #pragma once will check whether the header file has been included. , if it is not copying.
Another way to prevent the repeated inclusion of header files (involving preprocessing content, I won't talk about it for the time being, students can take it as an understanding)
insert image description here

4. The specific embodiment of multiple files in the code

here is the quote
In the above figure, we define a global variable and a function in the test.c file, then declare it in the test.h file, and finally print and call the global variable and function in the main.c file, We can find that this approach is feasible, that is: global variables and functions can be accessed across files (this conclusion will be used when explaining the role of static below)

3. The most misnamed keyword - static

static overall description

here is the quote

The above picture is MSDN's explanation of static. The translation is: when modifying a variable, the static keyword specifies that the variable has a static duration (allocated at the beginning of the program and released at the end of the program) and initialized to 0 unless specified other values. When modifying a variable or function in file scope, the static keyword specifies that the variable or function has internal linkage (its name is not visible outside the file in which it is declared). There is no specific concept in this passage. Next, I will introduce you to a detailed understanding of static from the three objects that static functions.

1, static modification of 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;.

Supplement: Memory distribution :

To figure out this problem, we must first know what the memory layout is: as shown in the
insert image description here
figure, the left side is the specific division of memory, and the right side is the approximate division of memory. In the C language stage, we only need to remember the diagram on the right, from As we can see in the figure, the memory development of local variables is on the stack area, and the stack area is characterized by entering the code block to open up space and leaving the code block to release the space, so the scope and life cycle of local variables are only in the code block. However, variables using static directly open up space in the static area, so the life cycle of the variables is extended.

2, static modification of 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.

3, static modification 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.

4. Summary

1. Global variables and functions can be accessed across files. Because there are projects of a certain scale, they must have multiple files. Between multiple files, data "interaction" must be carried out later (test.h test.c main.c ), if it cannot be accessed across files, the cost of data "interaction" will be very high, so the C language specifies that global variables and functions can be accessed across files when it is designed.
2. The role of static modification of local variables: changing the life cycle of local variables , which 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, so that local variables have the same life cycle as global variables, that is, with Generate and destroy with the entire program.
3. The role of static modification of 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).
4. The role of the static modifier function is to change the external link property of the function (which can be accessed in other source files), making it an internal link property (which can only be accessed within this file).

Guess you like

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