Both functions and variables have a declaration and a definition of two notation. The purpose of a declaration is to tell other code the name of the variable, whereas the definition has a function that is more than a declaration, and allocates memory for that variable in addition to the name.
For a function, adding no extern has no relation, because the function body must be defined, and the extern will be ignored, and no function body is the declaration,
int add (int a, int b);
equivalent to extern int add (int a, int b);
If it's a variable.
int A; Defines a variable a
extern int A; Declares a variable a
A variable can have any declaration, but there can be only one definition, the global variable is defined in multiple files, only one file is non-extern, and the rest must be declared with an extern representation.
Description: In the. h file, it is generally used to declare some variables. Not defined, all definitions and implementations are in. C or. cpp files.
So, pay special attention to
When defining global variables, it is important to pay special attention when it is a global variable:
The declaration of a global variable is generally written in the. h file, plus extern, because it is not defined as a variable, so it is not. This global variable can be accessed by all files that include the. h file. However, the definition of this variable must be in a. C or. cpp file. And there can only be one definition of this variable.
Correlation and description of C + + #include. h extern