Distinguish the difference between variable declarations and variable declarations and definitions

Reprinted https://www.cnblogs.com/GavinDai/archive/2011/10/24/2222735.html

The difference between variable declarations and definitions

We in the programming, all the time used to define and declare a variable of variables, we can sometimes not very clear on this concept, know how to use it, but I do not know how one would do, I would simply following put their differences are described below :( I hope you pointing benefit)

Declare variables there are two cases:

1, one is the need to create storage space. For example: int a statement at the time had already established storage space.

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

The former is a "defining statement (defining declaration)" or as "the definition (Definition)", and the latter is the "Reference Statement (referncing declaration)", the statement contains a broad definition of perspective, i.e. the definition is declared a special case, not all declarations are defined, for example: int a statement it is both, but it is also defined. However, it is not just a statement extern a definition of terms. Under normal circumstances we so often described, to declare the establishment of a space called the "definition", but the statement does not need to create a volume called "statement." Obviously, we refer here to the declaration is a relatively narrow range, i.e., the statement in the narrow sense, i.e. non-declaration defines the properties, for example: in the main function:

int main () {
extern int A;
// This is a statement rather than a definition, a statement A is already defined external variables
// NOTE: As may be removed when the variable type declaration external variable: extern A;
dosth (); // perform functions
}
int A; // definition defines the external integer variable A

"Definitions" external variables and "Statement" external variables is not the same, the definition of external variables can only have one, it's location is outside of any function, while the same external variable declaration in a file can be multiple times it can (which function to use on the statement in the function) in function of the outside can also function (defined point before the external variables). The system allocates storage in accordance with the definition of external variables (rather than external variables declared in accordance with). For external variables in terms of initialization can only be performed in the "definition", rather than in the "Statement". The so-called "declaration", its role is to declare the variable is already defined external variables in the back, just to "advance" and the variable is referenced as the "statement" only. extern only make a statement, without any definition.

(Our ultimate aim is to advance the use of the statement, that the definition used before, if you do not use a separate statement in advance is not necessary, and is so variable, function, too, so the statement does not allocate storage space, only the definition will allocation of storage space.)

Use static to declare a variable's role is twofold:

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

(2) an external variable declared with the static, then the effect of the variable module is limited to the present document.

We in the programming, all the time used to define and declare a variable of variables, we can sometimes not very clear on this concept, know how to use it, but I do not know how one would do, I would simply following put their differences are described below :( I hope you pointing benefit)

Declare variables there are two cases:

1, one is the need to create storage space. For example: int a statement at the time had already established storage space.

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

The former is a "defining statement (defining declaration)" or as "the definition (Definition)", and the latter is the "Reference Statement (referncing declaration)", the statement contains a broad definition of perspective, i.e. the definition is declared a special case, not all declarations are defined, for example: int a statement it is both, but it is also defined. However, it is not just a statement extern a definition of terms. Under normal circumstances we so often described, to declare the establishment of a space called the "definition", but the statement does not need to create a volume called "statement." Obviously, we refer here to the declaration is a relatively narrow range, i.e., the statement in the narrow sense, i.e. non-declaration defines the properties, for example: in the main function:

int main () {
extern int A;
// This is a statement rather than a definition, a statement A is already defined external variables
// NOTE: As may be removed when the variable type declaration external variable: extern A;
dosth (); // perform functions
}
int A; // definition defines the external integer variable A

"Definitions" external variables and "Statement" external variables is not the same, the definition of external variables can only have one, it's location is outside of any function, while the same external variable declaration in a file can be multiple times it can (which function to use on the statement in the function) in function of the outside can also function (defined point before the external variables). The system allocates storage in accordance with the definition of external variables (rather than external variables declared in accordance with). For external variables in terms of initialization can only be performed in the "definition", rather than in the "Statement". The so-called "declaration", its role is to declare the variable is already defined external variables in the back, just to "advance" and the variable is referenced as the "statement" only. extern only make a statement, without any definition.

(Our ultimate aim is to advance the use of the statement, that the definition used before, if you do not use a separate statement in advance is not necessary, and is so variable, function, too, so the statement does not allocate storage space, only the definition will allocation of storage space.)

Use static to declare a variable's role is twofold:

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

(2) an external variable declared with the static, then the effect of the variable module is limited to the present document.

Guess you like

Origin www.cnblogs.com/ArChieve/p/11403634.html