C + + Prime: The relationship between variable declarations and definitions

Source: Internet
Author: User

In order to allow the program to be split into several logical parts, the C + + language supports a separate compilation mechanism, allowing the program to be split into several files, each of which can be compiled independently.

To support split compilation, C + + separates declarations and definitions. A declaration makes a name known to the program, and a file must contain a declaration of that name if it wants to use a name defined elsewhere. The definition is responsible for creating the entity associated with the name.

Variable declarations specify the type and name of the variable, define additional storage space for the request, and may assign an initial value to the variable.

To declare a variable instead of defining it, add the keyword extern before the variable name, and do not show the Initialize variable:

extern int i;    // declaring I rather than defining int J;              // declare and define

Any initialization declaration that contains a display is defined as:

extern Double 3.1416;    // definition

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

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

If you want to use the same variable in multiple files, you must declare and define the detach. At this point, the definition of the variable must appear and appear in only one file, while the other file used by the variable must be declared and never be defined repeatedly.

C + + Prime: The relationship between variable declarations and definitions

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.