天天看点

C: extern 关键字总结                                         C extern keyword

                                         C extern keyword

一、declaration and definition 

     The declaration of a variable / function simply declares that the variable / function exists somewhere in the program but the memory is not allocated for them, and it only is the type of this variable/function. Therefore, when a variable is declared, the program knows the data type of that variable. In the case of function declaration, the program knows what are the arguments to that functions, their data type, the order of arguments and the return type of the function. 

The definition is that when we define a variable/function, apart from the role of declaration, it also allocates memory for that variable/function. 

From above explanation, it should be obvious that a variable/function can be declaredany number of times but it can be defined only once(Remember the basic principle that you can't have two locations of the same variable/function).  

二、extern keyword

1. use extern before a C function

  By default, the declaration and definition of  a c function have extern prepended with them. It means even though we don't use extern with the declaration/definition of C function, it is present there. For example, when we write:

                                                                    int foo(int arg1, char arg2);

There is an extern present in the beginning which is hidden and the compiler treats it as below.

                                                                    extern int foo(int arg1,char arg2);

Same is the case with the definition of a C function (Definition of a C function means writing the body of the function). Therefore whenever we define a C function, an extern is present there in the beginning o f the function definition. Since the declaration can be done any number of times and definition can be done only once, we can notice that declaration of a function can be added in several C/H files or in a single C/H file several times. But we notice the actual definition of the function only once (i.e in one file only). And as the extern extends the visibility to the whole program, the function can be used (called) anywhere in any of the files of the whole program provided the declaration of the function is known. (By knowing the declaration of the function, C compiler knows that the definition of the function exists and it goes ahead to compile the program). So that is all about extern with c functions.

2.use extern before a variable 

Let us explain it through under these examples. For example:

                                                                                 extern int var;

 Here, an integer type variable called var has been declared (remember no definition i.e no memory allocation for var so far). And we can do this declaration as many times as needed (remember that declaration can be done any number of times).

Now we define a variable. For example: 

                                                                                 int var;

 Here, an integer type variable called var has been declared and defined, and the memory for var is also allocated. Now here comes the surprise, when we declared/defined a C function, we saw that an extern was present by default.  While defining a function, we can prepend it with extern without any issues. But it is not the case with  C variables. If we put the presence of extern in variable as default  then the memory for them will not be allocated ever, they will be declared only. Therefore we put extern explicitly for C variables when we want to declare them without defining them. Also, as the extern extends the visibility to the whole program, by externing a variable we can use variable anywhere in the program provided we know the declaration of them and the variable is defined somewhere.

Now let us understand extern with examples. 

Example1:

                                                                                  int var;

                                                                                  int main()

                                                                                  {

                                                                                       var =10;

                                                                                       return 0;  

                                                                                   } 

Analysis: This program is compiled successfully. Here var is defined (and declared implicitly) globally.

Example2:

                                                                                   extern int var;

                                                                                   int main()

                                                                                   {

                                                                                       return 0;

                                                                                     }

Analysis: This program is compiled successfully. Here var is declared only. Notice var is never used so no problems.

Example 3:

                                                                                     extern int var;

                                                                                     int main()

                                                                                      {

                                                                                             var =10;

                                                                                              return 0;

                                                                                       }

Analysis: This program throws error in compilation. Because var is declared but not defined anywhere. Essentially, the var is not allocated any memory. And the program is trying to change the value to 10 of a variable that does not exist at all.

Example 4:

                                                                                   #include "somefile.h"

                                                                                   extern int var;

                                                                                   int main()

                                                                                  {

                                                                                          var=10;

                                                                                          return 0;

                                                                                    }

Analysis: Supposing that somefile.h has the definition of var. This program will be compiled successfully.

Example 5:

                                                                                  extern int var=0;

                                                                                  int main()

                                                                                 {

                                                                                       var =10;

                                                                                       return 0;

                                                                                }

Analysis: If a variable is only declared and an initializer is also provided with that declaration, then the memory for that variable will be allocated. i.e. that that variable will be considered as defined. Therefore, as per(as per:按照、依据、如同) the C standard, this program will compile successfully and work.

In short, we  summarize as follows.

1. Declaraton can be done any number of times but definition only once.

2."extern" keyword is used to extend the visibility of variables/functions

3.Since functions are visible through out the program by default. The use of extern is not needed in function declaration/definition. Its use is redundant.

4.When extern is used with a variable, it's only declared not defined.

5. As an exception, when an extern variable is declared with initialization, it is taken as definition of the variable as well. 

继续阅读