Attributes of variables (global variables, local variables, dynamic variables, static variables, etc)

Source: Internet
Author: User

Attributes of Variables 

1, Variable Classification

L according to scope: it can be divided into global variables and local variables.

L according to the life cycle: it can be divided into static storage and dynamic storage, specifically divided into automatic (auto), static (static), register (Register), external (extern ).

Static storageInA fixed storage space is allocated during the running of the program, and a dynamic storage is used.YesDynamically allocates storage space as needed during program running.

 

2, Variable scope and storage class relationship

Each variable hasScopeAndStorage TypeTwoAttribute,These attributes are used together to describe a variable.The relationships between these variables and storage locations are as follows:

LExternal variables (global variables), static external variables, and static local variables are stored in the static storage area.

L automatic local variables (The local variable is an automatic local variable by default.), Function parametersStored in the dynamic storage area (stack Area).

LBoth static and dynamic storage areas belong to the user areas in the memory.

LThe register variables are stored inCPURegister rather than memory.

 

3And several attributes related to the scope:

LLocal variable: The variables defined in a function are internal variables, which are valid only within the scope of the function. That is to say, they can be used only within the function, these variables cannot be used outside of this function. Such variables are called "local variables ".

LGlobal 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 ".

 

4And storage type-related attributes:

LAtuo:When declaring local variables, if static is not specified, the default values are auto. These variables are dynamically allocated to the storage space and the data is stored in the dynamic storage area.

LStatic:When declaring a local variable, use the keyword static to specify the local variable as a "static local variable". In this way, the original value will not disappear after the function call, that is, the occupied storage unit will not be released, when the callback function is called, the existing value of this variable is the value at the end of the last function call.

L register:When declaring dynamic local variables or function parameters, you can declare the variablesRegister,In this way, the compilation system allocates a register instead of memory space for the variable. This method can improve the performance of programs that frequently call certain local variables.(Register operation speed is much higher than memory)

L extern: usedExtensionThe scope of the global variable.

1) 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.

2) In addition, extern can declare external variables in Multi-file programs.


From: http://blog.csdn.net/21aspnet/article/details/2560072

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.