"C + + considerations" 2 variable Declaration and definition

Source: Internet
Author: User

It is not possible to put all the C + + programs in a file, so C + + supports a separate compilation (separate compilation) mechanism, which allows the program to be divided into several files, each file can be compiled independently. This shows that when your program has a lot of files, modify one of the files after you do not need to recompile all the files, only need to compile the modified, and then link them together.

How does C + + support split-compilation? By separating the declaration and the definition.

The Declaration (Declaration) makes the name known to the program, and a file must contain a declaration of that name if it wants to use a name defined elsewhere.

Definition is responsible for creating the entity associated with the name.

A declaration statement consists of a basic data type (base type) and a list of the declarator (declarator) immediately following it.

The definition also needs to request storage space, or it may assign an initial value to the variable.

If you want to declare a variable instead of defining it, add the variable with the keyword extern keyword tag before the variable name to assign an initial value, but doing so also offsets the effect of extern. An extern statement, if it contains an initial value, is no longer a declaration and becomes a definition.

externint i;  // 声明i而非定义iint j;  // 声明并定义j

Inside the function body, an error is raised if an attempt is made to initialize a variable that is marked by the extern keyword.

// 错误int main(){    externint i;    int i=10;    return0;}

Variables can and may only be defined once, but can be declared more than once.

// 这样是可以得externint i;externint i;externint i;int main(){    int i=10;    return0;}

If you want to use the same variable in more than one file, you must detach the declaration and definition. But at this point the definition of the variable must appear and only appear in one file, while the other file used by the variable must be declared, but definitely cannot be defined repeatedly.

In addition, C + + also makes type checking during the compile phase (type checking), so it is also known as C + + as a static type (statically typed) language. The more complex the program, the more static type persistence helps to find the problem. Then, if the compiler must know the type of each entity object, it requires that we declare its type before using a variable.

"C + + considerations" 2 variable Declaration and definition

Related Article

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.