The definition of a variable allocates a storage space for the variable and may assign an initial value to the variable. The variable can only be defined once in the program (such as int I; or Int I = 1 ;) the declaration of variables mainly declares the variable name and the variable type. It is also a declaration that no space is allocated. When defining a variable, we specify its name and type. We cannot declare a variable using extern without defining it. It can be declared multiple times in the program. (For example, extern int I ;).
As long as the Declaration has an initial value, it is considered to be defined, regardless of whether extern (for example, extern int I = 2 is defined)
---- Reference from: C ++ Primer:
Another global variable is automatically assigned 0 by the compiler by default, and the local variable must be assigned a value manually.
I. Generally, define a t in A. CPP and declare it with extern in a_pub.h. Use it in B. cpp include a_pub.h, as shown below:
1. First case: Compilation Error
// Out_pub.h
Extern int T = 1; // defined
// Out. cpp
Int t; // defines a global variable. The default value is 0. You can also change it to int T = 1;
// Test. cpp
# Include <iostream>
# Include "out_pub.h"
Int main ()
{
STD: cout <t <STD: Endl;
Return 0;
}
2. Case 2:
// Out_pub.h
Extern int t; // declaration. Multiple declarations are allowed.
Extern int t; // It is also normal to add more.
// Out. cpp
Int t; // defines a global variable. The default value is 0. You can also change it to int T = 1;
// Test. cpp
# Include <iostream>
# Include "out_pub.h"
Int main ()
{
STD: cout <t <STD: Endl;
Return 0;
}
In the second program above, the main function defines another int T = 3 without errors, because the internal definition shields the external (global) Definition.
The extern function can also be declared. The specific usage is the same as that of the declared variable.
2. It can also be defined in a. cpp and referenced directly in B. cpp.
// Out. cpp
Int t; // defines a global variable. The default value is 0. You can also change it to int T = 1;
// Test. cpp
# Include <iostream>
Extern int t; // declare an external one. If it is changed to extern int I = 2, it is redefined.
Int main ()
{
STD: cout <t <STD: Endl;
Return 0;
}
3. Another use of extern: Used in the C ++ compiler to compile the C function of extern.
Cause: C ++ combines the function name and parameter to generate an intermediate function to solve the polymorphism. If C is used, an intermediate function is also generated, so an error occurs during compilation.
Example:
// Out. h
# Ifdef _ cplusplus // This section of code references the online ...,
Extern "C "{
# Endif
# Include <stdio. h> // If out. h is used directly # include <stdio. h>
Extern void AA (); // extern void AA ();
// Error: unresolved external symbol "Void _ cdecl AA (void )"(? Aa @ yaxxz), In the vc6 debugging result
# Ifdef _ cplusplus
}
# Endif
// Out. c
# Include "Out. H"
Void AA ()
{
Printf ("AA is running ");
}
// Test. cpp
# Include <iostream>
Using namespace STD;
# Include "Out. H"
Int main ()
{
AA ();
Return 0;
}
4. By default, const is a local variable, even if it is declared globally, and the initial value must be assigned during definition. To make an external reference, you must add extern.
Example:
// A. cpp
Extern const int A = 100;
// B. cpp
Extern const int A; // reference
The reason why const is stored in the head file is that it is a local variable by default:During compilation, the const variable is replaced by a constant expression. No space is stored as a const variable. If the const variable is not initialized using a constant expression, it should not be defined in head, but in CPP, and then declared in head with extern