Definitions of local variables, global variables, and static variables

Source: Internet
Author: User

(1) local variables
Variables defined in a function are internal variables, which are valid only within the scope of the function. That is to say, they can only be used in this function, and they cannot be used outside of this function, they are called local variables.
1. the variables defined in the main function are only valid in the main function, not in the entire file or program because they are defined in the main function.
2. variables with the same name can be used in different functions. They represent different objects and do not interfere with each other.
3. Form parameters also make local variables.
4. Within a function, you can define variables in the composite statement. These variables are only valid in this statement.
(2) global variables
Variables defined outside the function are external variables and external variables are global variables. global variables can be shared by other functions in this file, its effective range starts from the location of the defined variable to the end of the source file.
1. Setting global variables adds channels for data connection between functions.
2. We recommend that you do not use global variables when necessary, because
A. global variables occupy storage units during all program execution.
B. It reduces the versatility of functions C. using too many global variables will reduce program clarity.
3. if the external variable is defined at the beginning of the file, the external variable can be used throughout the file range. If the definition at the beginning of the file is no longer defined, the scope specified above is limited to the definition point to the end of the file. if the function before defining a vertex wants to reference this external variable, use the keyword extern in this function as an external variable description.
4. If the external variables and local variables in the same source file have the same name, the external variables do not work within the scope of the local variables.
(3) static variables
The scope of a static variable depends on the position of the static variable. If it is in a function, the scope is the function.
Static global variables can only be used in this file. Although the entire program contains multiple files, static global variables can only be used in the file that defines them, but it cannot be used in other files in the program. it is an external variable that defines the storage type as static. Its scope is from the definition point to the end of the program. The difference is that the storage type determines the storage location, static variables are stored in the data area of the memory. They are allocated fixed bytes before the program starts running. The size of allocated bytes remains unchanged during the program running, the occupied memory is released only after the program runs.

Attention]
How does the operating system and compiler determine global and local variables?
The operating system and compiler obtain the type of the Variable Based on the memory area of the program running. The global data of the program is placed in the global data area of the allocated memory, and the local data of the program is placed in the stack area.
Attention]
<1> what is the difference between static global variables and common global variables?
The description of global variables (external variables) is preceded by static to form a static global variable. The global variables themselves are static storage methods, and static global variables are also static 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 the functions in the source file, so it can avoid errors in other source files. 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.
<2> what is the difference between a static function and a common function?
Static functions have different scopes than normal functions. Only in this file. The function used only in the current source file should be declared as an internal function (static). The internal function should be described and defined in the current source file. for functions that can be used outside the current source file, it should be described in a header file that the source file to use these functions must contain this header file.
<3> what is the difference between static local variables and common local variables?
Static local variables are initialized only once, and the next time is based on the previous result value;

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.