Declaration and definition of variables and usage of the keyword extern

Source: Internet
Author: User

Variable declaration and definition:

AOf a variable allocates
Storage for the variable and may also specify an initial value for the variable.
There must be one and only one definition of a variable in a program.

ADeclarationMakes known the type
And name of the variable to the program. A definition is also a declaration:
When we define a variable, we declare its name and type.
We can declare a name without defining it by using
ExternKeyword.
A declaration that is not also a definition consists of the object's name and
Its Type preceded by the keyword
Extern:

In many encoding rules, variables and function declarations are placed in the header file, and their corresponding definitions are placed in the source file.
However, many people, especially beginners, have unclear definitions and definitions. They feel that Declaration is definition and definition is declaration. This obfuscation
It's not surprising, because variables are defined at the same time in most cases.
E.g.
// Main1.cint var; // The Declaration also defines int main (INT argc, char ** argv []).
{
...
} The following is a pure declaration without definition.
E.g.
// Main2.c
Extern int val; // declare only this is an external variable. The specific function of extern is described later.
Extern int val1 = 1; // declare and define
External variables Int main (INT argc, char ** argv [])
{
...
} The function declaration and definition are similar. It is also easier to differentiate.
E.g.
// Main3.c
Void func () // the Declaration is also defined
{
...
} Int main (INT argc, char ** argv [])
{
...
Func (); // You can compile the link.
...
} If you declare a function only, you do not need to write the function body. You only need to declare the information of the function type and function parameters.
E.g.
// Main4.c
Void func (char); // declare only int main (INT argc, char ** argv [])
{
...
Func (); // compilation can be successful. If no function is defined later, the link will fail.
...
} Void func (char * Str) // function definition. If this part of the definition is not in this file, it can be compiled in main, but cannot be linked.
{
...
} The reason why the function definition and declaration are better differentiated is that the function must have a function body compiler to allocate space for it, and the variable only needs a name and its type compiler.
You can allocate space to it.
So it can be said that the declaration only tells the compiler that the declared variables and functions exist, but no space is actually allocated to them. Code
When the variables or functions previously declared are used, the compiler will not report errors during compilation, but will report errors during linking, because the compiler will
Find the memory addresses of these variables and functions. Because they are declared but not defined, the linker certainly cannot find them, so an error is reported. And define them.
Then the compiler will allocate space for them, and they will have their own addresses. Then they will be connected.
Therefore, the definition is to allocate space, so the definition can only be one time. The Declaration does not allocate space. You can declare it multiple times. Extern keyword
I wrote this short article today because someone asked me how to use extern. They knew how to use extern, but they didn't know the principle of extern.
In main2.c, the extern int Val is used to tell the compiler that the variable is defined in other files (foreign aid ).
Never be surprised to see its name. The compiler believes in its own people, so if we see the Val variable during compilation, we will think it exists and will not report an error. Only
When the link is connected, the linker will go to other OBJ files to find the Val variable definition (Address). If it is found, the link goes smoothly; otherwise, an error is reported. Because the compiler only needs to know
The variable name can be declared, so the extern int Val can be written as the extern Val (that is, the variable type is omitted ).
E.g.
// Main2.c
Extern int val; // declare that Val is an external variable and can also be written as extern val;
Int main (INT argc, char ** argv [])
{
Val = 10;
...
} // Another. c
Int val;
It is no problem to compile the links of these two files together.
Not all variables can be declared with extern. Only variables with global variables and without static declarations can be declared as extern. so if you don't want the global variables in your source file to be referenced by other files, you can add a static declaration to the variables.
E.g.
Extern val; // an error is reported when the link is connected, because Val is declared as static.
Int main (INT argc, char ** argv [])
{
Val = 10;
...
} // Another. c
Static int val;
This is the only difference between static global variables and non-static global variables.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.