Extern usage and extern usage

Source: Internet
Author: User

Extern usage and extern usage

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 ". (Extern can be placed before a variable or function to indicate that the definition of a variable or function is in another file, prompting the compiler to find its definition in other modules when it encounters this variable and function)

Generally, the extern usage is as follows:

Its main usage is:

  • Use extern when declaring variables in other files in this file
  • Use extern when using c library functions in cpp Program Files

Header file. h

extern volatile SERV103_LINK servLink;extern YaoXinStrucType *pstYx;static int ycSpace = 500;static int yxSpace = 1000;static int sjbgSpace = 10*1000;static int gzbgSpace = 30*1000;
Then you need to reference it in A. cpp file.

One. cpp (multiple cpp)

Iec103_serv_send_sjbg (SERV103_LINK *) & servLink, sjbgData, 1 );

ST_RET iec103_serv_send_zjbg (SERV103_LINK * servLink, SoeItemType * zjbgData, ST_UCHAR sendReason );


Note: I call it in different cpp


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.


Functions and variables are the same: (the above example is used in functions)

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.




Extern usage

Extern is used for global variables. Since you have defined a (int) in the main function, that is, the scope of a is main. Otherwise, the scope will not work soon.
If you want to use the int a in main to f, that is not acceptable, but you can say that a (int) Declaration (Definition) is a global variable, then use extern (if f and main are not in the same format. cpp)
As for the specific usage of extern, I have answered this question and the answer is very detailed. zhidao.baidu.com/question/96882132.html
(Snpsprcsac) You can check it out!
. H and. cpp is "independent", but not completely independent ,. use extern in h. the variable (constant) defined in cpp cannot be switched over (I also mentioned this problem in extern,
Generally, if a program has a class, the class is defined in *. h, and the corresponding class is implemented in *. cpp (with the same name!
The last question you asked was not very clear!

Use of extern in C language. It is better to have an example.

I used extern several times, mainly to declare the role of external variables. I also used it in the header file. For example, you have a project. c, lib. c, lib. h, three files, in lib. h contains lib. c variable definition, such as a [4] array, that is, this variable is lib. c global variables when I am in the project. when used in file c, even if a [4] is a global variable, it is only useful in this file, and other files cannot be used. But what should I do with project. c? In the project. in c, use extern to declare a [4]. It will automatically find a [4] in another file ], this is also why if a file has a global variable or a function is used only in this file, it should be added with static to prevent other files from being declared by the extern. The extern statement does not allocate memory. You can only find the definition in other files. I have done this experiment in the file project. in c, use extern to declare a [4], and at the same time in lib. in c, extern declares a [4], which leads to an error prompt during the link, saying a [4] is not found ]. So in general, extern is used to use variables elsewhere.

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.