In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "what is the function of the c language keyword static". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the function of the c language keyword static".
Keyword static
C code is organized in files. In all the source files of a source program, an external variable (note that it is not a local variable) or a function can only be defined once in a source program. If there is a duplicate definition, the compiler will report an error. With the mutual reference and independent relationship between variables and functions in different source files, the extern and static keywords are generated.
Static is used to describe static variables.
1. If it is defined outside the function, then the effect is similar to the global variable, that is, the variable specified by static can be used in the current c program file.
2. If it is defined within the function, then the variable will only be initialized once, and even if the function is called again, the static variable will not be initialized again, so the value of this variable will always be saved, and when we call the function again, it will still be the result of the last function call saved.
So what is the purpose of the keyword static?
Few people can answer this simple question completely. In the C language, the keyword static has three obvious functions:
1)。 In the function body, a variable that is declared static maintains its value while the function is called.
2)。 Within the module (but outside the function), a variable declared static can be accessed by the function used in the module, but not by other functions outside the module. It is a local global variable.
3)。 Within a module, a function that is declared static can only be called by other functions within the module. That is, this function is limited to the local scope of the module in which it is declared.
Most people can answer the first part correctly, some can answer the second part correctly, and few people can understand the third part. This is because you don't understand the benefits and importance of localizing data and code scope.
Expand:
1. What is the difference between static global variables and ordinary global variables?
The description of a global variable (external variable) followed by a static constitutes a static global variable.
The global variable itself is static storage mode, and static global variable is also static storage mode. There is no difference in the way they are stored.
The difference between the two is that the scope of the non-static global variable is the whole source program. When a source program is composed of multiple source files, the non-static global variable is valid in each source file. On the other hand, the static global variable limits its scope, that is, it is valid only in the source file in which the variable is defined, and cannot be used in other source files of the same source program. Because the scope office of static global variables is limited to one source file and can only be shared by functions within that source file, you can avoid causing errors in other source files.
Static global variables are initialized only once to prevent them from being referenced in other file units.
2. What is the difference between static local variables and ordinary local variables?
Changing a local variable to a static variable changes the way it is stored, that is, its lifetime. Changing a global variable to a static variable changes its scope and limits its scope of use.
Thank you for your reading. the above is the content of "what is the function of the c language keyword static". After the study of this article, I believe you have a deeper understanding of the role of the c language keyword static, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.