Differences between global variables, local variables, static global variables, and static local variables in C Language

Source: Internet
Author: User

1. c ++ variables have different scopes based on different lifecycles of the defined positions. The scopes can be divided into six types:

Global scope, local scope, statement scope, Class scope, namespace scope and file scope.

From the scope:

1> global variables have a global scope. Global variables can be defined in only one source file to act on all source files. Of course, other source files that do not contain the definition of a global variable must use the extern keyword to declare the global variable again.

2> static local variables have local scopes. They are initialized only once, and exist until the first initialization ends until the program running ends, the difference between global variables and global variables is that global variables are visible to all functions, while static local variables are always visible only to the defined function bodies.

3> A local variable only has a local scope. It is an automatic object (auto). It does not exist all the time but only exists during function execution, after a function is called and executed, the variable is revoked and the memory occupied by the function is also withdrawn.

4> static global variables also have a global scope. The difference between static global variables and global variables is that if a program contains multiple files, it acts on the files defining it and cannot act on other files, the variable modified by the static keyword has the file scope. In this way, even if two different source files define static global variables with the same name, they are also different variables.

2. From the memory allocation perspective:
1> global variables, static local variables, and static global variables are all allocated space in the static storage area, while local variables are allocated space in the stack.

2> global variables are static storage, and static global variables are also static storage. The two are not different in storage methods. The difference between the two lies in that the scope of non-static global variables is the entire source program. When a source program is composed of multiple source files, non-static global variables are valid in each source file. The static global variable limits its scope, that is, it is valid only in the source file defining the variable, and cannot be used in other source files of the same source program. Because the scope of static global variables is limited to one source file, they can only be shared by functions in the source file. Therefore, errors in other source files can be avoided.

1) static variables will be placed in the static data storage area (globally visible) of the program, so that the original values can be maintained during the next call. This is the difference between stack variables and heap variables.
2) The variable uses static to notify the compiler that it is only visible within the scope of the variable. This is the difference between it and global variables.

From the above analysis, we can see that after a local variable is changed to a static variable, its storage mode is changed, that is, its survival time is changed. After changing a global variable to a static variable, it changes its scope and limits its scope of use. Therefore, the description of static plays different roles in different places. Attention should be paid.

TIPS:

A. if the global variable is only accessed in a single c file, you can change the variable to a static global variable to reduce the coupling between modules;

B. If the global variable is only accessed by a single function, you can change the variable to the static local variable of the function to reduce the coupling between modules;

C. When designing and using functions that access dynamic global variables, static global variables, and static local variables, you need to consider re-import, because they are all stored in the static data storage area, which is globally visible;

D. If we need a reentrant function, we must avoid using static variables in the function (such a function is called a function with the "internal memory" function)

E. static variables must be used in a function. For example, if the return value of a function is of the pointer type, the address of the static local variable must be used as the return value. If the return value is of the auto type, the returned result is an error pointer.

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.