The difference between variable declarations and definitions

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

The difference between variable declarations and definitions of
our 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 thing, here I will simply 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:

main int () {
extern int A;
// This is declaration not defined, A is a declaration has a defined external variables
// NOTE: variable type can be removed, such as when an external variable declarations: extern A;
doSth (); // function execution
}
int a; // is defined, and a is defined integer external variable
external variable "definition" and "statement" external variables are not identical, external variables defined only once, it the location is outside of any function, while the same external variable declaration in a file can be many times, it can be within a function (which function to use on the statement in the function) can also be outside the function (in external variables defined point before). 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 blog.csdn.net/ZDT_zdh/article/details/90065835