Scope global variable local variable static variable external variable constant volatile variable

Source: Internet
Author: User
Scope global variable local variable static variable external variable constant volatile variable

Scope:

The scope rule tells us the valid range of a variable, where it is created, and where it is destroyed (that is, beyond the scope ). The effective scope of a variable starts from its definition point and ends with the first Closed parentheses that are closest to the variable before it is defined. That is to say, the scope is determined by the last pair of parentheses of the variable.

Global variables:

Global variables are defined externally in all function bodies and can be used in the part of the program (or even the code in other files. Global variables are not affected by the scope. problems can be used (that is, the life cycle of global variables ends until the end of the program ). If the extern keyword is used in one file to declare the global variables in another file, this file can use this data.

Local variables:

Local variables appear in a scope and they are confined to a function. Local variables are often called automatic variables because they are automatically generated when they enter the scope and disappear when they leave the scope. The keyword auto can explicitly describe this problem, but the default local variable is

Auto, so there is no need to declare it as auto.

Register variable

A register variable is a local variable. The keyword register tells the compiler to "access this variable as quickly as possible ". Accelerating Access depends on reality, but, as the name implies, this is often done by placing variables in registers. This cannot be ensured to be changed to the register, or even to Improve the access speed. This is just a hint of the compiler.

Register variables are restricted. It is impossible to get or calculate the address of the register variable. The register variable can only be declared in one block (it cannot be a global or static register variable ). However, you can use the register variable as a formal parameter in a function (that is, in the parameter table.

Generally, we should not speculate on the compiler optimizer because it may be better than ours. Therefore, it is best to avoid using the keyword register.

Static variables

The static keyword has some unique meanings. Generally, the defined local variables in a function disappear when the function scope ends. When this function is called again, the storage space of the variable is re-created and its value is re-initialized. To make the value of a local variable still exists throughout the life cycle of the program, we can define the local variable of the function as static and give it an initialization. Initialization is only executed when the function is called for the first time, and the values of variables remain unchanged between function calls. In this way, the function can "remember" some information fragments between function calls.

We may wonder why global variables are not used. The advantage of static variables is that they are unavailable outside the function range, so they cannot be easily changed. This will make the error localization.

External variables

Extern tells the compiler that there is a variable and function, even if the compiler does not see it in the current file. This variable or function may be defined in a file or after the current file. For example, extern int I; the compiler will knowIIt must exist somewhere as a global variable. When the compiler sees the definition of variable I, it does not see other declarations, So it knows that it has found the same declared I before the file.

Constant

Const tells the compiler that this name represents a constant. Both internal and user-defined data types can be defined as Const. If you define an object as a constant and try to change it, the compiler will generate an error. In C ++, a const must have an initial value.

Volatile variable

Const tells the compiler that "this will not change" (this is to allow the compiler to execute additional optimizations), while volatile tells the compiler that "It doesn't know when to change ", it prevents the compiler from making any Optimization Based on the stability of the variable.

Refer to Chapter 3 of volume 1 of thinking in C ++

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.