Understand local, global, static, dynamic, and external variables

Source: Internet
Author: User

Understand local, global, static, dynamic, and external variables

1. All variables and local variables

Local variables: variables defined in the function. The scope is the function's internal eg: void fun () {int x ;}

All variables: can be defined only once and referenced multiple times. The scope is the entire file.

 

If the names of global variables and local variables are duplicate in the same source file, all variables do not work in the scope of local variables.

 

Keywords:

All variables: Out-of-Function Definition + global visibility + global data zone stored in the memory + 0 when not initialized

Local variables: defined in the function + visible in the function + stored in the stack, the function exit variable disappears + the Explicit initialization content is unpredictable

 

2. Static and Dynamic Storage Variables

 

Static storage variables: variables that occupy fixed memory permanently during the running of the program + are stored in the stack, and the life cycle is the whole program.

Dynamic storage variables: temporary and Dynamic Distribution of storage space variables + stored in the heap zone, automatically released after the function is completed

 

Therefore, the instruction code of the program is placed in the code area, and the static storage variable is placed in the static data area (such as global variables ), the dynamic storage variables of the program are stored in the dynamic data area (such as the form parameters of the function and the return address of the function call)

 

3. Static local variables, static global variables, and external variables (extern)

 

Static local variables: The difference from local variables is that the variable still exists but cannot be used by other functions when the function exits. When you enter the function again, you can use the last saved result. It can be understood that local variables are cached and valid in the original scope. (Only initialized once. The next initialization depends on the previous result value)

 

Eg:

 

# Include <stdio. h>

 

Voidfun1 (int v ){

Static int a = v;

Printf ("% d",)

}

 

Intmain (){

Fun1 (100 );

Fun1 (200 );

}

 

The result of the two executions is 100, and the initialization of a for the second call is the previous value, reflecting the storage

 

Static global variables and extern are valid only in the defined source file. The difference between static global variables and global variables is: global variables and the external variables declared as extern ), used by other source files. Static global variables cannot be used. They can only be used in the source file.

 

The scope of any function modified with static is only the current source file, but this function is invisible to the outside.

 

Note:

 

A program divides the memory blocks allocated to the operating system into four areas:

1. Code area: stores the code blocks of various functions of a program.

2. Global Data zone: stores global data and static data (one initialization and multiple calls)

3. Heap zone: Dynamic Data

4. Stack zone: Local Data


Storage of local variables, global variables, and external variables

Variable category:
Global variables and local variables can be divided according to the scope.
According to the life cycle, there can be divided into static storage and dynamic storage, specifically divided into automatic (auto), static (static), register (register), external (extern ).
Static storage is used to allocate a fixed storage space during the running of the program. Dynamic Storage is used to dynamically allocate storage space as needed during the running of the program.

Each variable has two attributes: scope and storage class. These attributes are used together to describe a variable. The relationships between these variables and storage locations are as follows:
External variables (global variables), static external variables, and static local variables are stored in the static storage area.
Automatic local variables (local variables are automatic local variables by default) and function parameters are stored in the dynamic storage area. Both static and dynamic storage areas belong to the user area in the memory.
However, register variables are stored in CPU registers rather than memory.

First, describe several attributes related to the scope:
Local variables: the variables defined in a function are internal variables, which are valid only within the scope of the function, that is, they can be used only in the function, these variables cannot be used outside of this function. Such variables are called "local variables ".
Global variables: variables defined outside the function can be shared by other functions in the source file. The valid range is from the position of the defined variable to the end of the source file, this type of variable is called "global variable ".

The following describes the attributes related to the storage type:
Atuo: When declaring local variables, if static is not specified, auto is used by default. These variables are dynamically allocated to the storage space and the data is stored in the dynamic storage area.
Static: When declaring a local variable, use the keyword static to specify the local variable as a "static local variable", so that the original value will not disappear after the function call ends, that is, the occupied storage units are not released. The existing value of this variable is the value at the end of the last function call when the callback function is called.
Register: When declaring dynamic local variables or function parameters, you can declare the variables as register, so that the compilation system allocates a register for the variables instead of the memory space, this method can improve the performance of programs that frequently call certain local variables. (Register operation speed is much higher than memory)
Extern: used to extend the scope of a global variable. For example, if the function wants to reference an external variable but the external variable is defined after the function, the function needs to use extern to declare the variable, in this way, the global variables defined after the function can be used. In addition, extern can declare external variables in Multi-file programs.

Variables are divided from different dimensions to form a variety of complex relationships, so you need to focus on learning programming.

What are static variables, static variables = global variables, dynamic variables = local variables?

To put it simply, a static variable is a variable name marked with a static variable name.
For example, static int;
Global variables are equivalent to static variables. Their differences can be simply understood as the difference in scope. Static variables are in the scope of a function (the main function is the scope of the main function, A local function is the scope of a local function. A global function is a variable written in a blank space (not a local function) outside the main function. Its life cycle is generated before the main function, after the main function runs, data is recycled.
The difference between dynamic variables and static variables is:
For example
Void (){
Static int I = 0; // static
I ++;
}
Void B (){
Int I = 0; // dynamic
I ++;
}
The dynamic variable is that the system recycles the memory after each call, and the memory is allocated for the next call,
The lifetime of static variables will be reclaimed by the system only after the main function is run.

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.