variable declarations and variable definitions
Variable definitions: Used to allocate storage space for a variable, and to specify an initial value for a variable. In a program, a variable has and has only one definition.
Variable declaration: Used to indicate the type and name of a variable to a program.
A definition is also a declaration, an extern declaration is not a definition or a declaration: When defining a variable we declare its type and name. An extern declaration is not defined: it is not defined by declaring a variable name by using the extern keyword.
[note]
Variables must be defined or declared before they are used.
In a program, a variable can be defined only once, but may be declared multiple times.
Defines the allocation of storage space, and the declaration does not.
C + + programs typically consist of many files, and C + + distinguishes declarations and definitions in order for multiple files to access the same variables. A
variable's definition (definition) is used to allocate storage space for a variable, and you can specify an initial value for a variable. In a program, a variable has and has only one definition.
a declaration (declaration) is used to indicate the type and name of a variable to a program. The definition is also a declaration: When defining a variable we declare its type and name. You can declare a variable name by using extern without defining it. Declarations that do not define variables include object names, object types, and the keyword extern before the object type.
extern declarations are not defined and do not allocate storage space. In fact it simply means that the variable is defined elsewhere in the program. A variable in a program can be declared multiple times, but only once.
a declaration can have an initializer only if the declaration is also defined, because only the definition allocates storage space. The initialization must have storage space to initialize. If a declaration has an initialization, it can be treated as a definition, even if the declaration is marked as extern.
any variable that is used in multiple files requires a declaration that is separate from the definition. In this case, a file contains the definition of the variable, and other files that use the variable contain the declaration (not the definition) of the variable.
How to distinguish variable declarations and definitions clearly
extern notifies compiler variables to be defined elsewhere
1.extern tells the compiler that the variable is defined elsewhere.
For example:
extern int i; Declaration, not defining
int i; Declaration, also defined, not initialized
a declaration with an initialization must be defined
2. If the declaration has an initialization, it is treated as a definition, even if extern is added to the front.
An extern declaration can be initialized only if it is outside the function.
For example:
extern double pi=3.141592654; Defined
declaration and definition of functions
3. The difference between the declaration and definition of a function is simpler, with {} is the definition, otherwise it is the declaration.
For example:
extern double Max (double d1,double D2); Statement
unless there is an extern keyword, it is defined as a variable.
4. Unless there is an extern keyword, it is the definition of a variable.
For example:
extern int i; Declaration
int i;//definition
Modular design style of the program
Summary
1. Do not put variable definitions into. h files, which can easily lead to duplicate definition errors.
never define a variable in the. h file. The difference between defining a variable and declaring a variable is defining an operation that produces a memory allocation. Is the concept of the Assembly phase; The declaration only tells the module that contains the declaration to look for external functions and variable 2 from other modules during the connection phase
. Use the static keyword to limit the definition of a variable to the scope of the source file. Unless the variable is designed to be global.
3. You can declare a variable in the header file and declare the variable when it is included in the header file.
Modular Essentials
(1) The module is one. c file and a. h file, the header file (. h) is the declaration of the Module Interface,
(2) a module to provide other modules to call the external functions and data in. h file should be labeled as extern keyword Declaration;
(3) Functions and global variables within a module should be declared with the static keyword at the beginning of the. c file, and
(4) Never define variables in the. h file. The difference between defining a variable and declaring a variable is that defining the operation that produces the memory allocation is the concept of the assembly phase, whereas the declaration simply tells the module containing the declaration to look for external functions and variables from other modules during the connection phase.
General situation in the bottom of the file only the declaration of variables, because the header file to be included in other files (that is, #include), if you put the definition of the header file, you can not avoid multiple definition of variables, C + + does not allow multiple definitions of variables, a program in the definition of the specified variable only once, the declaration can be countless times.
But with three exceptions, the definition of the entity in three can also be placed in the header file.
1. The definition of a const variable that is known at compile time can be placed in the header file
such as: const int num;
2. Class definition can put 3.inline function in header file
These three entities can be defined in multiple source files, as long as the definitions in each source file are the same. Sample Programs
#include <stdio.h>
#include <stdlib.h>
// is defined, the variables defined in external variables//c for integer A are defined by default is extern ,
// so generally speaking int a = <==> extern int a = ten;
/*extern */int a = ten;
If the declaration has an initialization, it is treated as a definition, even if extern is added to the front.
//The extern declaration can be initialized only if it is outside the function.
int main (void)
{
extern int A; declares an external extern int variable a
// This is a declaration rather than a definition, declaring a is a defined external variable
// Note: You can remove the variable type when declaring an external variable such as: extern A;
printf ("A =%d\n", a);
return exit_success;
}
In this program, we define a variable outside the function
Note extern int a = 10; can be initialized only if the extern declaration is outside the function.
And we'll talk about that later.
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
int A; defines a variable that does not initialize
int b = ten; define a variable and initialize extern int C at the same time ; declares an external extern int-type variable a
printf ("a =%d\n", a);
printf ("B =%d\n", b);
printf ("c =%d\n", c);
extern int d = ten;
return exit_success;
}
In this program,
int A; is a definition, but uninitialized, printing his value is indeterminate, so compile-time will report an uninitialized exception.
int B = 10; is a definition and is correctly initialized, there is no problem printing the value of B.
But it's a statement, if you want to read and write to C, and we don't have a definition of C, so the grammar check is fine, but when you link, the connector can't find the address of C.
Obviously for D, we mentioned earlier that if you declare an initializer, you are treated as a definition, even if you add extern to the front. However, the extern declaration can be initialized only if it is outside the function.
Now this definition is clearly considered by the GCC compiler to be wrong.