Home  >  Article  >  php教程  >  How to use extern in C language

How to use extern in C language

高洛峰
高洛峰Original
2016-12-19 14:38:311805browse

In C language, the modifier extern is used before the declaration of a variable or function to indicate "this variable/function is defined elsewhere and should be referenced here."

1. Extern modifies the declaration of variables. For example, if file a.c needs to reference the variable int v in b.c, you can declare extern int v in a.c, and then you can reference the variable v. What needs to be noted here is that the link attribute of the referenced variable v must be an external link (external). That is to say, for a.c to reference v, it not only depends on the declaration of extern int v in a.c, but also depends on the variable v itself. can be quoted. This involves another topic in the C language-the scope of variables. Variables that can be referenced by other modules with the extern modifier are usually global variables. Another very important point is that extern int v can be placed anywhere in a.c. For example, you can declare extern int v at the beginning of the definition of function fun in a.c, and then you can refer to the variable v. That's all. You can only reference v in the scope of function fun. This is still a problem of variable scope. Regarding this point, many people have concerns when using it. It seems that the extern statement can only be used in file scope.

2. extern modified function declaration. Essentially, there is no difference between variables and functions. The function name is a pointer to the beginning of the function's binary block. If file a.c needs to reference a function in b.c, for example, the prototype in b.c is int fun (int mu), then you can declare extern int fun (int mu) in a.c, and then you can use fun to do anything. Just like the declaration of a variable, extern int fun (int mu) can be placed anywhere in a.c, and does not necessarily have to be placed in the file scope of a.c. The most common way to reference functions in other modules is through header files containing declarations of those functions. What is the difference between using extern and including a header file to reference a function? The extern reference method is much simpler than including the header file! The use of extern is straightforward. Use extern to declare which function you want to reference. This is probably a manifestation of the KISS principle! An obvious benefit of doing this is that it will speed up the process of program compilation (preprocessing, to be precise) and save time. This difference is very obvious during the compilation of large C programs.

3. In addition, the extern modifier can be used to indicate the calling specification of C or C++ functions. For example, to call a C library function in C++, you need to use extern "C" to declare the function to be referenced in the C++ program. This is used by the linker to tell the linker to use the C function specification when linking. The main reason is that the naming rules in the target code after C++ and C programs are compiled are different.



For more articles related to the usage of extern in C language, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Summary of extern usageNext article:Summary of extern usage