The difference between a variable declaration and a definition

Source: Internet
Author: User

Original: http://www.cnblogs.com/GavinDai/archive/2011/10/24/2222735.html

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.

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

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:

int main () {
extern int A;
This is a declaration, not a definition, and declaring a is an external variable that has already been defined.
Note: The variable type can be removed when declaring an external variable such as: extern A;
Dosth (); Execute function
}
int A; 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.

Original 2:http://blog.csdn.net/sjxbf/article/details/6310150

"C++primer" in the fourth edition of the 2.3.5 Section said:

① variable definition: Used to allocate storage space for a variable, and to specify an initial value for the variable. In a program, variables have and have only one definition.

② variable declaration: Used to indicate to the program the type and name of the variable.

The ③ definition is also a declaration: When a variable is defined, we declare its type and name.

④extern keyword: The variable name is declared without defining it by using the extern keyword.

1. The definition is also a declaration, and the extern declaration is not defined, that is, no storage space is allocated. extern tells the compiler that the variable is defined elsewhere.

For example: extern int i; Declaration, not definition

int i; Declaration, also defined

2. If the declaration has an initialization, it is defined, even if it is preceded by extern. The extern declaration can be initialized only if it is outside the function.

For example: extern double pi=3.1416; Defined

3. The Declaration and definition of a function is relatively simple, with {} is the definition, otherwise it is declared.

For example: extern double max (double d1,double D2); Statement

4. Unless there is an extern keyword, it is the definition of a variable.

For example: extern int i; Statement

int i; Defined

Programming Style:

1. Do not put the variable definition into the. h file, which can easily lead to duplicate definition errors.

2. Try to limit the variable definition to the source file scope using the static keyword, unless the variable is designed to be global.

3. A variable can be declared in the header file, which is declared when the header file is included.

Summarize:

Variables must be defined or declared before they are used.

In a program, a variable can be defined only once, but it is declared more than once.

Defines the allocation of storage space, and the declaration does not.

C + + programs typically consist of many files, in order for multiple files to access the same variables, C + + distinguishes between declarations and definitions.

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.

A declaration (declaration) is used to indicate to a program the type and name of a variable. Definition is also a declaration: When defining a variable, we declare its type and name. You can declare a variable name without defining it by using extern. Declarations that do not define variables include the object name, the object type, and the keyword extern before the object type.

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. A variable can be declared multiple times in a program, but only once.

A declaration can have an initialization only if it is also defined, because only the definition allocates storage space. The initialization must have storage space to initialize. If the declaration has an initialization, it can be treated as a definition, even if the declaration is marked as extern.

Any variable that is used in multiple files requires a declaration that is separate from the definition. In this case, a file contains the definition of a variable, and the other file that uses the variable contains the declaration of the variable (not the definition).

--------------------------------------------------------------------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

The difference between a variable declaration and a 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.