Description of static, global, and local variables/functions in C language)

Source: Internet
Author: User
The concepts of "static" and "Global" in C language are easily confused. I can avoid them when writing code, whether at school or after I work, at first glance, I thought it was a good programming habit. In fact, it was because I was not fully familiar with the C language. I believe some people, like me, were reluctant to mention static and extern. Although all kinds of programming specifications require that we do not use global variables as much as possible, pay attention to the wording and "try as much as possible", which means that sometimes it cannot be avoided, static variables do not have to be mentioned.

 

The understanding of a programming language is a bit like cleaning the room. The more unwilling you want to sweep the corner, the more likely it will cause your anxiety. Therefore, it is necessary to clean it. Some of the text below comes from the Internet, books, and personal experiences, and some experiments are used to verify each conclusion.

 

Let's talk about the meaning of static modification: the significance of static modification is mainly reflected in a storage and scope limitation;

1,One-time storage: the one-time storage function of static modification is mainly reflected in variables.

A)       Static local variables are initialized only once. The next Initialization is based on the previous result value. It is a bit similar to the static member variables of the class in c ++, that is, no matter how many instance objects are generated for this type, all objects share a static variable. No matter how many times the function is called, the static variable is initialized only once and is not destroyed because it exceeds its lifetime, it is only invisible to the outside. Here is an example:

VoidFun1 (intV)
{
    StaticIntValue = v;
   StaticIntValue = V;
}
IntMain (intArc, char* ARGs [])
{
  Fun1 (50 );
  Fun1 (100 );
}

The result is: value: 50.Value: 50
It indicates that the initialization value when fun1 () is called for the second time uses the value of the previous value. The storage space of value in the static zone is not because of fun1 () and is released, that is, a storage;

B)      Sdf

2,Scope limitation: the scope limitation function of static modification is reflected in both functions and variables;

A)       For a function, the scope of any function modified with static is only the current source file, but this function is invisible to the outside, that is, only functions in the same source file can call this static function. Conversely, if a function is called only by other functions in the same source file, the function should be declared as static, the benefit of doing so is that the naming conflicts between functions of different source files can be solved to some extent;

B)      Static global variables are valid only in the current source file and invisible to the outside, and cannot be referenced by external files;

 

Next, we will analyze the relationship between global variables and static variables:

As the name implies, global variables refer to variables that can be referenced globally. Compared with local variables, global variables are also called external variables. Like static variables, global variables are located in the static data zone and are defined in one place, multiple references: Use the keyword "extern" to reference the variable "external.

Global variables can also be static. As mentioned above, static global variables mean that they are not referenced by "external". They are the global variables in a single source file, that is, the global variables in the compilation phase, instead of global variables in the connection phase.

 

Through the above analysis, we can draw the following conclusions:

1,The difference between a static function and a common function is that a static function cannot be called by a function other than the same source file.

2,The difference between static local variables and common local variables is that static local variables are initialized only once, And the next Initialization is actually the last variable;

3,The difference between static global variables and common global variables is that the scope of static global variables is limited to the source files.

 

Supplement: If a static global variable is defined in the header file (this is not a good habit and is required in rare cases ), this means that every source file containing this header file will contain such an independent global variable.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.