Definition of "programming language" variables and differences from extern declarations

Source: Internet
Author: User

The difference between a variable's definition and a declaration

First, the definition of a variable must occur before the variable declaration (extern), the declaration of a variable can have more than one, but the definition of the variable is only one, the declaration of the variable affects the reference and scope of the variable, and the definition of the variable is only a unilateral creation of variables and initialization assignment, etc. If the variable is not initialized when it is redefined, it will be randomly assigned when the file is compiled.

1) Variables allocate memory space when defined , and may also be assigned an initial value

2) variables are declared without allocating memory space , and the declaration of a variable simply declares that the variable exists and may refer to a declared variable in a location in another file, with the function of declaring that the variable is behind the defined variable.

Variables are divided into variable names and variable values

The address of the variable name

is formed during the compilation process.

in the During compilation, when the system discovers that you have defined a variable and assigns it an initial value, the variable is allocated the appropriate storage space, and the name of the variable and the address of the first byte of the storage space (the value storage unit address) exists in a variable name table. By the type of the variable, you know that the variable occupies the size of the storage space

The variable name is stored in the stack symbol table, the symbol is also a file, and this file also occupies memory space, in memory is a stack to save the information, that is, the stack space stored parameter name

When we use this variable, we will go to the variable table to check the name of the variable, if not found, indicating that the variable is not defined, the corresponding error message, if found, will know the variable in the storage space of the address, so that the storage space to take the value of the variable.

Variable Value

Relative address of a value type

As the variable is defined, it generates its own storage unit, and the relative address is its own storage unit address, such as int a , and the memory cells are created in- memory, which is then assigned to a Storage unit, the relative address is the address of the a storage unit

Use of variables

When using this variable, the variable name table will be used to find out if there is a definition, if there is a definition in the variable name table according to the address of the storage unit (relative address) to find the corresponding storage space, the value is taken out.

Initialization of variables

Before using variables, you should assign an initial value to the variable, and a good compiler will find the variables that are used without being defined, but the variables do not have to be initialized, variables defined outside the function (global variables), or inside functions with static The variables defined by the keyword are initialized to zero by the system before the programmer assigns the initial value, which is the result of clearing the BSS segment after initializing the stack space, if Auto (auto variable) inside the function , If you do not explicitly initialize the variables, they will have undefined values, which are unknown values stored in the storage unit, and all must be guaranteed to be assigned the initial value before using it.

Left and right values of variables

Value type (rvalue: constant); reference type (lvalue: specific data type with corresponding memory identifier)

      rvalue   is used to calculate the contents of the corresponding storage unit based on the relative address. That is, the use of the variable to take its corresponding storage unit value (constant), and finally represents the value inside the memory unit, such as b = a  ; in a

The lvalue calculates its corresponding storage unit according to the relative address, which is used to assign a value to the storage unit, such as B = A; which B is the lvalue operation, and the explicit Lvalue expression is an identifier with the appropriate data type and storage space, and the indirect operator *  and the address operator . & and so on, but & The result of the fetch operator is the right value

Definition of "programming language" variables and differences from extern declarations

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.