Use of the extern keyword in C Language

Source: Internet
Author: User
In C, the modifier extern is used before the declaration of a variable or function to describe & ldquo; this variable/function is defined elsewhere and must be referenced here ;. 1. extern modifier variable declaration. For example, if file a. c needs to reference the intv variable in B. c, you can declare externintv in a. c, and then you can reference the variable v. Note that the link property of the referenced variable v must be external, that is, a. c must be referenced to v.

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

1. extern modifier variable declaration. For example, if the file a. c needs to reference the int v variable in B. c, you can declare extern int v in a. c, and then you can reference the variable v. Note that the link property of the referenced variable v must be external, that is,. c. the declaration of extern int v in c also depends on whether the variable v itself can be referenced. This involves another C language topic-variable scope. Variables that can be referenced by other modules with the extern modifier are generally global variables. It is also important that extern int v can be placed in. anywhere in c, for example, you can. in c, the function fun declares the extern int v at the beginning of the definition, and then you can reference the variable v, except that v can only be referenced in the function fun scope, this is still a problem of variable scope. Many people are concerned about this. It seems that the extern Declaration can only be used in the file scope.

2. extern modifier function declaration. In essence, variables and functions are no different. The function name is a pointer to the beginning of the binary block of the function. If the file. c. Reference B. c functions, such as B. the original type of c is int fun (int mu. declare extern int fun (int mu) in c, and then you can use fun to do anything. Just like the declaration of variables, extern int fun (int mu) can be placed anywhere in a. c, not necessarily within the scope of a. c's file. The most common method for referencing functions in other modules is to include the header files of these function declarations. What is the difference between using extern and containing header files to reference functions? The reference method of extern is much simpler than that of header files! The use of extern is straightforward. to reference a function, use extern to declare a function. This is probably a manifestation of the KISS Principle! An obvious advantage of doing so is that it will accelerate the compilation (preprocessing) process of the program and save time. This difference is obvious during compilation of large C Programs.

3. In addition, the extern modifier can be used to indicate the call specifications of C or C ++ functions. For example, to call a C-Database Function in C ++, you must declare the function to be referenced using extern "C" in C ++. This is for the linker and tells the linker to use the C Function Specification for link. The main reason is that C ++ and C programs have different naming rules in the target code after compilation.

Use http://www.linuxidc.com/Linux/2012-10/73003.htm of the extern keyword 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.