Summary of variable attributes

Source: Internet
Author: User
Tags variable scope

There are five attributes of a variable:

(1) name (description must be displayed)

(2) type (the description must be displayed)

(3) Storage category (default mode or display description (Use: auto, register, static, extren ))

(4) scope (determined by the storage category)

(5) lifetime (determined by the storage class)

The first two do not need to be explained, as the name suggests. The following three concepts are detailed:

  • Storage Class of Variables

Definition: refers to the system's requirements on variable storage methods.

Category: There are two types of storage:

(1) static storage: The system allocates storage space for variables while the program is running. That is, once assigned, it does not change until the entire program is running.

(2) Dynamic Storage: The system dynamically allocates storage space to variables during the program running. That is, during the running of the program, you can dynamically allocate resources according to program requirements.

The storage class not only describes the storage unit of the variable, but also the time and scope of the variable's survival.

There are four types:

Before discussing the four delimiters, note the fact that the global variables and functions defined in a program file are external by default! That is, cross-file, but why do we need to add extern? I will talk about it later.

(1) auto: A local variable without instructions. When the lifetime of a variable ends, the system automatically releases its storage space.

(2) register variable register: To improve program execution efficiency, local variable values can be stored in registers (instead of memory.

(3) static variables: they can act on local variables and global variables. Therefore, they can be divided into local static variables and Global static variables. Static refers to the lifetime, while local or global refers to However. Note: because the global variable scope is originally the whole program by default, the file can be used (public). Once static modification is added, the variable or function name is hidden to make it private to the file (module ).

(4) variables declared with extern: global variables. if they are to be used in other files, they must be declared, method --> Use extern for external Declaration (usually put at the beginning of the file, and the keyword extern is omitted for functions ).

In general, the storage class of the variable + the location of the defined variable = scope and lifetime of the Variable

  • Variable Scope
Definition: function, that is, effective meaning; domain is range. Linking refers to the effective range of a variable, which reflects the characteristics of a variable from the perspective of space. Classification (6 types): (1) global variable scope (including global variables and Global static variables); (2) local variable scope; (3) Statement scope; (4) class scope; (5) namespace scope; (6) File Scope.
  • Lifetime of a Variable
Definition: refers to the time period from variable creation to deletion, which reflects the characteristics of the variable from the time perspective. Category (3): (1) Dynamic Survival: data stored in the heap area. Creation and deletion are all done by the programmer. (2) Local Survival: refers to the data stored in the "Stack zone. (3) Static Survival: refers to the data stored in the (global) Data zone. Programs start to exist as soon as they are run. Once the program is finished, they are automatically released by the system. Ps: Now I will answer: Why do I need to add extern? Full Description: since the global variable (different from the static global variable) is dynamic by default, that is, the whole project of the scope, why does an extern Declaration need to be added to another file to use it? A: first define the extern: extern can be placed before a variable or function to indicate that the definition of a variable or function is in another file, prompt that the compiler should look for definitions in other modules when encountering secondary variables or functions. Therefore, extern only serves as a prompt, which works with the compiler. From the perspective of compilers: Modern compilers generally adopt file-based compilation, that is, each file is transparent (that is, invisible) during compilation ). This tells us that although the global variables are dynamic by default, that is, the scope can be transferred across files to the entire project, the modern compiler limits this default condition during compilation. But in the Link phase, each file is "integrated". Therefore, the scope of the global variables defined in the file is extended to the whole project after the link is completed. At the same time, due to the link stage, if there are global variables with the same name in different files, a redefinition error will occur, that is, the global variables defined in each file of a program cannot be the same. Therefore, redefinition of variables cannot solve the problem, so we can only adopt the global variables defined in one file. If other files require them, declare them with extern to prompt the compiler, the definition of this variable must be found in other files of the program. Because the compiler's "eyes" are not long enough, it does not realize that although a variable identifier is not defined in this file, it may be defined in other files, so it needs to be prompted, as a result, extern is a convention.

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.