Understanding of C language Variables

Source: Internet
Author: User

In essence, the variable isProgramSpace used to store information. The word "this space" must contain two additional attributes. First, the stored content can beCode(2) Its address is accessible (otherwise, it cannot be accessed using code ). This leads to a series of issues related to variables.

A variable has two attributes. The first is its data type, which is the type of data that the space can store, and the second is its space-time attribute. This article only covers the latter. Is the life cycle of the variable, and null is the scope of the variable.

InCThere are two types of variables: external variables defined outside any function and automatic variables defined within a function.

Although in most C The book talks about global variables. However, in fact C In the standard, no global variables are defined. C Is useful. This reflects C An important idea of the Creator is to limit the scope of variables as much as possible. They want to limit the lifetime of variables. This idea runs through the whole C Standards have also become one of the most important aspects of structured program design.

External variables are source file-level variables. That is to say, its scope is limited to the source file that defines it. In the source file A An external variable defined in B It must be accessed in the file B Start with a keyword Extern Declare it. Pay attention to the definition and declaration of the two words, in the classic C In the book, we are very careful not to mention the area. The essence of definition is creation. In essence, it is to apply for a bucket. The statement does not trigger storage allocation. The declaration here only makes the source file A The scope of a variable in extends to the source file. B .

In a project, external variables must be defined in a source file. In other source files that need to be accessedExternDeclare it.

In the project, if you define a variable in a source file and declare it in all other source files, this variable becomes a global variable. ThereforeCGlobal variables are made by yourself.

in group development, if you do not want your defined external variables to be accessed by others, you can add modifiers static . In this case, , the scope of this external variable is limited to the source file that defines it. If extern declare it and an error will be reported during compilation. An external static variable is a real file-level variable.

the life cycle of an external variable is the entire running period of the project (here only refers to the statically allocated variable, dynamic Allocation is different .). Some books call it "permanent ".

automatic variables are classified into two levels. The function header defines the function level, and the scope is the entire function; the code block level is defined in the Code block header, and the scope is limited to this code block. The automatic variable is created when the machine executes the function (or code block) that defines it, and disappears automatically when it leaves the function (or code block. This function (or code block) is created again. This is the meaning of "automatic. Therefore, the life cycle of an automatic variable is limited to one running process of the function (or code block. If the modifier static is an automatic static variable. The scope of the automatic static variable remains unchanged, and the life cycle changes to permanent (the entire project runtime ). An automatic static variable must have an initial value as the initial value of the variable at the first run of the function (or code block. For subsequent operations, the initial values of each operation are the final values of the previous operation.

variables of different scopes can have the same name. If the scopes of two variables with the same name are mutually exclusive (for example, the two external static variables of the two source files), of course, they are not bounded. If one scope contains another scope, blocking will occur when the program is running. When the variable enters the inner layer, the variable in the outer layer is blocked by the variable with the same name in the inner layer. In this case, internal variables are actually accessed. When the inner layer ends, the outer variable is revealed again.

in C ++ in, variables can only be defined in the header of a code block. What is the significance of this? Assume that a variable needs to be defined in a code block, but that variable is used only at the end of the code block. If you define a variable only when it is used, you can limit its scope to the maximum extent. The other advantage is that the Code is more readable. If the code block is large, the definition and reference are pasted together, which facilitates the program's understanding. New C This is also extended by the standard.

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.