Declaration and definition of C + + variables finally figured out.

Source: Internet
Author: User

1. Definition of variable: the definition of a variable is used to allocate storage space for a variable, and you can also specify an initial value for the variable. In a program, variables have and have only one definition.

2. Declaration of variables: used to indicate to the program the type and name of the variable. A variable can be declared multiple times in a program, but only once.

3. The two linkages and differences:

(1) Definition is also a declaration, because when defining a variable we also show the program its type and name;

(2) But a declaration is not a definition, you can declare a variable by using the extern keyword without defining it. Declarations that do not define variables include the object name, the object type, and the keyword extern before the object type;

Cases:

extern int i;//declares but does not define

The int i;//Declaration also defines

The extern declaration is not a definition, nor does it allocate storage space. In fact, it simply illustrates that the variable is defined elsewhere in the program.

Note: If the declaration has an initialization, then it can be treated as a definition, at which point the declaration is also defined, even if the declaration is marked extern,

For example: the extern double pi = 3.1416;//declaration is also defined, although this sentence uses extern, but this statement defines the PI, allocating and initializing the storage space.

Note: an extern declaration can contain an initializer only if it is outside the function.

Note: Since the extern declaration of the initialization is already defined, and the variable can only be defined once in the program, any subsequent definition of the variable is wrong:

extern double pi = 3.1416;//defines the

Double pi;//redefined, illegal

Note: in the C + + language, variables must be defined only once, and variables must be defined or declared before they can be used.

4. Why it is necessary to distinguish between declarations and definitions:

C + + programs are usually made up of many files. To allow multiple files to access the same variable, C + + distinguishes between declarations and definitions. Any variable that is used in multiple files needs to have both a definition and a declaration. In this case, the variable is defined in one file and only the declaration of the variable can be included in the other file that has changed (no longer contains the definition because the variable can only be defined once).




Declaration and definition of C + + variables finally figured out.

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.