Huawei C language Face test
How do I refer to a global variable that has already been defined?
Answer: extern
You can use the reference header file in a way that you can also use the extern keyword, if you use a reference header file to refer to a global variation declared in a header file, assuming that you write the variable incorrectly, the error occurs during compilation, and if you use an extern reference, suppose you make the same mistake, There will be no error during compilation and error during connection
3. In addition, the extern modifier can be used to indicate the invocation specification of a C or C + + function. For example, calling C library functions in C + + requires the use of extern "C" in the C + + program to declare the function to be referenced. This is for the linker, tell the linker to link in the C function specification. The main reason is that C + + and C program after the compilation is completed in the target code different naming rules
1.extern often has such a role in variable declarations that you declare a global variable in the *.c file, and if the global variable is to be referenced, it is placed in the *.h and declared with extern.
2. If the declaration of a function has a keyword extern, it simply implies that the function may be defined in another source file, with no other effect. That is, the following two function declarations do not differ:
extern void Print_externifo (); and Voidprint_externifo ();
The use of extern in functions:
If the C/cpp file that defines the function declares the defined function in the corresponding header file, then you need to include the header file in the other C/cpp files to use these functions.
If you do not want to include the header file, declare the function in C/cpp. Generally speaking, a function declared in this file is not "extern", and a function declared in another file is "extern", so that a function that calls another file definition in this file does not have to include the header file include "*.h" to declare the function, and it can be used directly after the declaration.
As an example:
#vimprint_extern. C
#include <stdio.h>
extern void Print_extern ()//can be used here without extern
{
printf (" Helloextern test!! \ n ");
}
#vim extern_test.c
#include <stdio.h>
extern void Print_extern ()//In fact, it's OK to use no extern.
Main ()
{
print_extern ();
}
Compile:
#gcc –c print_extern.c–o print_extern.o
#gcc print_extern.o extern_test.c–o extern_test
#./extern_test
View a full set of articles: Http://www.bianceng.cn/Programming/C/201212/34807.htm