0x01
extern is used before the declaration of a variable or function to indicate that "this variable/function is defined elsewhere and is referenced here."
0x02
The declaration of the extern modifier variable.
Example: If you need to refer to the variable int v in B.C in A.C, you can declare the extern int v in A.C, and then you can refer to the variable V; Note that the linked property of the referenced variable v must be an outer link (external), That is, A.C to refer to the variable V, not only depends on the declaration of extern int V in A.C, but also on the variable v itself can be referenced. Here's another topic---the scope of a variable. Variables that can be referenced by extern to other modules are usually global variables.
Another point is that extern int v can be placed anywhere in the A.C, for example, you can declare the extern int v at the beginning of the function func () definition in A.C, and then you can refer to the variable V, except that the variable v is only referenced in the Func () scope ( This is also a variable scope problem, for this point, many people are concerned about the use, as if the extern declaration can only be used for file scope.
0x03
Declaration of the extern modifier function.
In essence, there is no difference between a variable and a function. A function name is a pointer to the beginning of a functional binary block. If the file A.C to reference a function in B.C, such as the prototype in B.C is an int func (int m), then you can declare the extern int func (int m) in A.C, and then you can use Func () to do anything. Just like the declaration of a variable, the extern int func (int m) can be placed anywhere in the A.C, not necessarily in the scope of the A.C file scope,
For references to functions in other modules, the most common method is to include the header files for these function declarations. Use extern and include header files to refer to the difference between functions: extern is much more indirect than including a header file. The use of extern is straightforward, which function you want to refer to by using extern to declare which function. This is probably a reflection of the kiss principle. One obvious benefit of this is that it will speed up the process of compiling the program (to be exact, preprocessing) and saving time. This difference is very noticeable during the large C program compilation process.
0x04
In addition, the extern modifier can be used to indicate the invocation specification of C or C + + functions. For example, invoking C library functions in C + + requires that the function to be referenced is declared in the C + + program with extern "C". This is used by the linker to tell the linker to link with the C function specification when linking. The main reason is that C + + and C programs are compiled with different naming rules in the target code.
0x05
A brief example:
1#include <stdio.h>2#include <stdlib.h>3 4 intx =0;5 inty =5;6 intfunc1 ()7 {8 externp, q;9printf"P is%d, q is%d\n", p, q);Ten return 0; One } A - intp =8; - intQ =Ten; the intMain () - { - func1 (); -printf"x is%d, y is%d\n", x, y); +}
Output Result:
Usage of extern in C language