InProgramDuring design, variables are always defined and declared, but sometimes we are not very clear about this concept and know how it is used, but we do not know how it will take a while, below I will briefly introduce their differences as follows: (I hope my guidance will benefit you)
There are two scenarios for variable declaration:
1. One is to create a bucket. For example, int A has created a bucket When declaring it.
2. You do not need to create a bucket. For example, extern int A, where variable A is defined in other files.
The former is "defining Declaration" or "Definition", while the latter is "referncing Declaration )", in a broad sense, a declaration contains definitions, that is, definitions are a special case of declarations, so not all declarations are definitions. For example, int A is both declaration and definition. However, for extern A, it only declares that it is not a definition. In general, we often describe the definition of a bucket, rather than the definition of a bucket ". Obviously, the declaration we refer to here is narrow in scope, that is, narrow Declaration, that is, non-definition declaration, for example: in the main function:
Int main (){
Extern int;
// This is a declaration, not a definition. Declaring a is an external variable that has been defined.
// Note: When declaring an external variable, you can remove the variable type, for example, extern;
Dosomething (); // execute a function
}
Int A; // defines an external variable whose value is an integer.
The "Definition" of an external variable is different from the "Declaration" of an external variable. An external variable can only be defined once, and its position is outside all functions, the external variables in the same file can be declared multiple times, and can be declared within the function (which function should be declared in that function) it can also be outside the function (before the external variable's definition point ). The system allocates storage space according to the definition of external variables (rather than the declaration of external variables. For external variables, initialization can only be performed in "Definition", rather than in "Declaration. The so-called "Declaration" serves to declare that the variable is an external variable that has been defined later. It is only a "Declaration" to "reference the variable in advance. Extern is declared only and is not defined.
(The final purpose of our declaration is to be used in advance, that is, before the definition. If we do not need to use it in advance, there is no need to declare it separately. This is true for variables and functions, therefore, it is declared that no bucket will be allocated. Only the bucket will be allocated when it is defined .)
Using static to declare a variable has two functions:
(1) If a local variable is declared as static, the space allocated for the variable will always exist throughout the execution period of the program.
(2) When an external variable is declared as static, the function of this variable is limited to the module of this file.