Usage of extern in C language

Source: Internet
Author: User
Tags modifier variable scope

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


1. Declarations of extern-modified variables. For example, if the file a.c needs to refer to the variable int v in B.C, you can declare the extern int v in A.C, and then you can refer to the variable v. It should be noted here that the linked property of the referenced variable v must be an outer link (external), that is, the A.C to refer to V, not only depends on the declaration of extern int V in A.C, but also on the variable v itself can be referenced. This involves another topic of the C language-the scope of the variable. Variables that can be referenced by other modules with the extern modifier are usually global variables. It is also important to note that the extern int v can be placed anywhere in the A.C, such as you can declare extern int v at the beginning of the function fun definition in A.C, and then you can refer to the variable V, only to refer to V in the function fun scope, This is also an issue with variable scope. For this, many people are wary of using it. It seems that the extern declaration can only be used for file scopes.

2. extern modifier function declaration. 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 needs to refer to a function in B.C, such as an int mu in B.C, then you can declare the extern int fun (int mu) in a.c, and then you can use fun to do anything. Just like the declaration of a variable, the extern int fun (int mu) 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. What is the difference between using extern and including a header file to reference a function? extern is much simpler to refer to than include header files! 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.

3. 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 for the linker, telling the linker to link with the C function specification when linking. The main reason is that C + + and C programs have different naming rules in the target code after compilation is complete.

Usage of extern in C language

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.