Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An Analysis of the Application of static keywords in C language

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "C language static keyword application case analysis". In daily operation, I believe that many people have doubts about C language static keyword application case analysis. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "C language static keyword application case analysis". Next, please follow the editor to study!

First, preface

Introduce the static keyword in detail

Second, know more than one document

In order to understand the function of the static modifier function, we need to understand the relevant contents of multiple files

1. Creation of multiple files

Let me 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 you choose.

.h: we call it a header file, which generally contains function declarations, variable declarations, macro definitions, header files, etc. (header)

.c: we call it a source file, which generally contains function implementations, variable definitions, etc. (.c: C language)

Multi-file is a .h file that contains multiple .c files, such as main.c test1.c test2.c teset3.c. ...

2. Why should there be multiple files?

In a company's large-scale project, the function expected to be realized by the product is often very complex, so the function is usually modularized, so it is convenient for us to reuse the code, modify and maintain the code and cooperate with many people. naturally, we need multiple .c files in one program.

3. Why should there be a header file?

When simply using source files and organizing the project structure, the larger the project, the more complex the maintenance cost will become!

So when we organize the project structure, we will use header files to reduce the maintenance cost of large projects.

Add: the meaning of # pragma once in the header file

When you create a .h header file, you will find that the compiler automatically adds # pragma once at the beginning of the header file.

I believe that many friends have had or are deeply confused about this thing. In fact, it is used to prevent the header file from being included repeatedly. Take a chestnut.

As shown above: I included the header file in test.h, but I also included test.h and stdio.h in main.c, which caused the stdio.h to be included twice, so that the program made two copies of the contents of stdio.h when compiling, resulting in code redundancy, and # pragma once will check whether the header file has been included, if so, it is not copying.

Another method to prevent repeated inclusion of header files (involves preprocessing content, don't talk about it for the time being, students can take it as an understanding)

4. The concrete embodiment of multiple files in the code.

In the figure above, we define a global variable and a function in the test.c file, then declare them in the test.h file, and finally print and call the global variables and functions 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 to explain the role of static below)

Third, the most misnamed keyword-static

Static overall exposition

The figure above is MSDN's interpretation of static, which translates as: when a variable is modified, 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 initializes it to 0 unless a different value is specified. When you modify a variable or function in the scope of a file, the static keyword specifies that the variable or function has an internal link (its name is not visible outside the file in which it is declared). This passage doesn't have any specific concepts to read. Next, I'll take you to learn more about static from the three objects that static plays.

1. Static modifies local variables

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

Figure 2: after we modify a with static, we find that the screen prints from 1 to 10, just as an is not destroyed after each call to the test function, but continues to be used, and the next time the test function is called, a directly carries out the + + operation on the previous basis.

Therefore, the function of static to modify local variables is to change the life cycle of local variables, in essence, to change the storage location of local variables, so that local variables no longer open up space on the stack, but directly open up space on the static area, so that local variables have the same life cycle as global variables, that is, with the generation and destruction of the whole program.

More in-depth understanding of the role of static modifying local variables: figure 3, our program from a source file (.c file) to an executable program (.exe file) needs to be run through compilation links, and the compilation process is divided into three stages: 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 sentences of assembly code. In figure 3, however, we can observe that only static int a = 0 There is no corresponding assembly code for this statement, that is, the C language skips this statement directly when compiling.

In essence: in the compilation phase of the compilation process, the compiler will allocate space for the local variables modified by static, so the C program will directly skip the static-modified statement in the process of running, that is, static int a = 0 in the second or above or even the first call to the test function; this statement will not be executed.

Supplement: memory distribution:

To figure this out, we first need to know what the memory layout looks like:

As shown in the figure, there is a specific division of memory on the left and a general division of memory on the right. In the C language stage, we only need to remember the diagram on the right, from which we can see that the memory development of local variables is on the stack area. the characteristic of the stack area is to enter the code block to open up space and leave the code block to free space, so the scope and life cycle of local variables are only in the code block. On the other hand, variables with static directly open up space in the static zone, so the life cycle of the variable is extended.

2. Static modifies global variables

Comparative analysis of figure 1 and figure 2: I defined a global variable g_val in Add.c. Because the global variable has external link properties, I only need to declare g_val in test.c to use it normally, but when I used static to modify g_val, we found that the compiler said that g_val was an external symbol that could not be parsed.

So the function of static to modify the global variable is to change the external link attribute of the global variable (which can be accessed in other source files) to make it an internal connection attribute (which can only be accessed within this file), giving us the impression that the scope of the global variable has become smaller.

3. Static modification function

Comparative analysis of figure 1 and figure 2: here is very similar to static decorating global variables. I have defined an Add function in Add.c, because the function also has external link properties, so I only need to declare the Add function in test.c to use it normally, but when I use static to modify the Add function, we find that the compiler says that Add is an external symbol that cannot be parsed.

So the function of the static modifier function is to change the external link attribute of the function (which can be accessed in other source files) to make it an internal connection attribute (which can only be accessed within this file), giving us the impression that the scope of the function has become smaller.

At this point, the study of "static keyword application case analysis of C language" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report