extern C Summary

Source: Internet
Author: User

noun explanation

1.extern

extern translation is external, used before a variable or function, so that its visible range widened, this is why called extern Bar. So what does it do for you? When used in a variable or function, prompt the compiler to look for its definition in other modules.

As an example:

functions defined in source file a are invisible (that is, inaccessible) in other source files. In order to call this function in the source file b , an external declaration should be added to the head of b :
(extern) function prototype;    
in this way, the source file B can also call that function.   

2. Declarations and definitions

The statement (Declaration) simply declares that there is such a thing, but it does not involve actions such as memory allocation, which are done elsewhere, so it is not very useful, but rather it plays a very important role, First it declares the type of a variable or function, letting the program know the type of a variable, or the argument and return value type of a function.

Definition can be understood as a superset of the declaration, or it can be understood as a subset of the definition, the definition of memory allocation at the same time, so the declaration can be made more than once, but the definition will only be done once.

Important explanation

1.extern function

This will be easier when you define a function, such as

Int Fun (void)

The compiler will default it to

extern int fun (void)

So the difference between adding or not adding extern before a function is small.

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 fileA.Cneed to referenceB.Cfunctions, such as in theB.Cthe prototype isint fun (int mu), then you canA.Cdeclared inextern int Fun(int mu), and then you can use Funto do any thing. Just like the declaration of a variable,extern int Fun(int mu) can be placed inA.CAnywhere in the region, and not necessarily onA.Cscope of the file scope. For references to functions in other modules, the most common method is to include the header files for these function declarations. UseexternWhat is the difference between a function that contains a header file and a reference? externis much simpler to refer to than include header files! externthe use of the method is straightforward, to refer to which function is usedexternwhich function is declared. This is probablyKISSa manifestation of the 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. In largeCDuring the process of compiling the program, this difference is very obvious.

Attached examples:

File_01.hint Add (int, int);//Obviously this is a global function that has properties that are called by extern. File_01.c#include "File_01.h" int Add (int a, int b) {return a + B;} Main.c#include <stdio.h>int Main (int argc, char** argv) {int a = 4;int b = 5;extern int add (int, int);//This line can be placed before the call Anywhere, extern can also be omitted int c = Add (A, b);p rintf ("%d\n", c);}

  

2.extern Variables

Simply say:

extern int var;// This is a declaration that can be made multiple times

int var;// This is a definition

extern int var = 0;// This is not only a declaration, but also a definition

For example, if the fileA.Cneed to referenceB.CMedium VariableInt v, you canA.Cdeclared inextern int v, and then you can reference the variablev. It is important to note that the referenced variablevthe Link property must be an outer link (External), that is to sayA.Cto refer tov, not only depends on theA.Cdeclared inextern int v, also depends on the variablevcan be quoted in itself. This involvesCanother topic of language-the scope of variables. can be other modules toexternthe variable that the modifier refers to is usually a global variable. And the important point is thatextern int vcan be placed inA.CAnywhere in the region, for example, you canA.Cthe functions in Fundeclaration at the beginning of a definitionextern int v, and then you can refer to the variablev, but only in the function FunScope ReferencesvIt's still a matter of variable scope. For this, many people are wary of using it. Seemsexterndeclarations can only be used for file scopes.

Attached examples:

File_01.hextern int G_var;
/* Here is a declaration that the definition is implemented in a. c file, which is similar to a function, except that the function is the default with extern,
So usually do not notice, extern if omitted, then it is equivalent to the. h file is defined, then directly G_var = 9, equivalent to assignment (not initialized),
But there are warning. *///file_01.c#include "File_01.h"
int G_var = 9;//Here is the actual definition, and initialization (should not be an assignment). main.c#include <stdio.h>extern int G_var;
/* Here extern can be omitted would be more magical, so after omitting is still a declaration can do a simple validation, if here int g_var = 0; Then a redefinition error occurs. */
int main (int argc, char** argv) {printf ("%d\n", G_var);}

  

3.extern Arrays & extern pointers

An extern char a[] = "ABCD" is defined in a source file;

Can be declared in another file:extern char* A; it?

Attached examples:

File_01.hextern Char g_var[];//file_01.c#include "File_01.h" char g_var[] = "ABCD"; Main.c#include <stdio.h>//extern char* G_var;
If you use the pointer form, the runtime will have a segment error extern char g_var[];
/* If two rows are valid at the same time, the error is reported: Main.c:7: error:conflicting types for ' G_var ' main.c:6: error:previous declaration of ' G_var ' is here from On the wrong hand, it is clear that the two declarations have a type conflict, stating that the compiler does not equate char* and char[].
Do not believe the two lines are declared as char* or char[] will be successful through the compilation. */
int main (int argc, char** argv) {printf ("%s\n", G_var),/*printf ("%s\n", G_var);
The above with a pointer form, where the direct segment error, indicating that the connector is not at all the G_var and file_01.c defined in the G_var as a matter,
They are two types, oddly, if the above-mentioned pointers are compiled to pass, it means that the compiler thinks that G_var is a variable that has already been defined.
is the file_01.c definition of that, but the time of the operation of the error, in the end, the original G_var is directly assigned to a value composed of the contents of the array,
Because the original value here is ABCD, is interpreted as char[], that is, G_var equals the first address of the array, and now to be interpreted as char*, so it is clear that
That is equivalent to G_var = "ABCD", that is, G_var = 0x64636261, otherwise if the normal way of thinking should be first to G_var initialization, and then assign value,
The value is an array header address, but now there is no action to initialize the variable G_var alone, or the variable is already there when the array is initialized.
In the final analysis, arrays and pointers are not the same internal structure caused. If you want to print with this pointer, you can also hit the first four bytes in the group.
Because the size of a pointer variable is four bytes, you can print unsigned db = (unsigned) g_var;char* ptr = (char*) (&db);p rintf ("%s\n", ptr); */

  

4.extern "C"

Calling C library functions in C+ + requires extern "C"in the C+ + program . "declares the function to be referenced. This is for the linker, telling the linker to link with the C function specification when linking. The main reason for this is that C+ + and C programs have different naming rules in the target code after compilation.

extern C Summary

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.