Chapter 2nd variables and basic Types 2--the difference between declaration and definition

Source: Internet
Author: User

The difference between a variable declaration and a definition

We are in the program design, always use the definition of variables and the declaration of variables, but sometimes we are not very clear about the concept, know how it is used, but do not know how a thing, the following I will simply introduce their differences are as follows: (Hope my guidance to you benefit)

There are two scenarios for declaring a variable:

1, one is the need to set up storage space. For example, int A has established storage space at the time of declaration. ----definition

2, the other is no need to establish storage space. For example: extern int a where variable A is defined in another file. -----statement

The former is a "declarative declaration (defining declaration)" or "definition", and the latter is a "declarative declaration (Referncing declaration)", which, broadly speaking, contains a definition, That is, a definition is a special case of a declaration, so not all declarations are definitions , for example: int A It is both a declaration and a definition. For extern A, however, it is simply a declaration, not a definition. In general, we often describe the declaration of space-building as a "definition", and a declaration that does not require the creation of a storage space. It is obvious that the declaration we are referring to here is narrower, that is, a narrow declaration, that is, a declaration of a non-defining nature, for example: in the main function:

1 intMain ()2 3  {4         extern intA;5             //this is a declaration, not a definition, and declaring a is an external variable that has already been defined.6             //Note: The variable type can be removed when declaring an external variable such as: extern A;7Dosth ();//Execute function8 }9 Ten     intA//is the definition of an external variable that defines a as an integral type

The definition of an external variable is not the same as the declaration of an external variable, the definition of an external variable can only be used once, its position is outside of all functions, and the declaration of an external variable in the same file may be multiple times, and it can be within a function (which function is declared in that function) or outside the function ( Before the definition point of the external variable). The system allocates storage space based on the definition of an external variable, rather than on the declaration of an external variable. For external variables, initialization can only be done in definition, not in declarations. The so-called "declaration", whose function is to declare that the variable is an external variable that has been defined in the past, is simply a "declaration" for "advance" reference to the variable. extern is declared only and does not make any definition.

(The ultimate purpose of our statement is to use it in advance, that is, before it is defined, if it is not necessary to declare it separately if it is not required to be used in advance, so is the function, so the declaration does not allocate storage space, and the storage space is allocated only when defined.) )

Using static to declare a variable has two functions:

(1) For a local variable with a static declaration, the space allocated for the variable is always present throughout the execution period of the program.

(2) If the external variable is declared with static, the function of the variable is limited to this file module.

--------------------------------------------------------------------header file definition and declaration

Note the definition of the variable cannot be placed in the header file!!! The general situation in the head file only the declaration of variables, because the header file to be included in other files (that is, # # #), if the definition is placed in the header file, you can not avoid multiple definitions of variables, C + + does not allow multiple definitions of variables, a program in the definition of the specified variable only once, the declaration can be numerous

However, with three exceptions, the definition of the three entities can also be placed in the header file.

1. The definition of a const variable that the value is known at compile time can be placed in the header file

such as: const int num (10);

2. Class definitions can be placed in the header file

3.inline function

These three entities can be defined in more than one source file, as long as the definitions in each source file are the same.

/--------------------------------------------------------------------header file definition and declaration

Chapter 2nd variables and basic Types 2--the difference between declaration and definition

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.