Text: Understanding the keyword extern in the C language
Recently wrote a C program, compile the variable duplicate definition of the error, you see no errors found. Using Google found that the extern
understanding is not thorough, I searched this article, written well. I made a poor translation. (Original: http://www.geeksforgeeks.org/understanding-extern-keyword-in-c/
)
I am sure this article will be of great help to beginners in C, because it will make them better and more proficient in C language. So let me start by talking about the extern
use of keywords in variables and functions. The most basic keywords extern
extend the visibility of variables and functions. This may be the reason why it is named extern
.
Almost everyone knows the meaning of declaring and defining variables (functions), but for the sake of the integrity of this article, I want to find out about them. 声明
a variable (function) simply indicates that the variable (function) exists somewhere in the program and does not allocate memory for them. However, declaring a variable (function) plays an important role in explaining the type of variable (function). Therefore, when a variable is declared, the program knows the type of the variable, and in the case of the function declaration, the program knows the parameter and return type of the function. This is called a declaration. Come 定义
, when we define a variable (function), it also allocates memory for that variable (function) In addition to the function of the Declaration. Therefore, we can assume that a declaration is a subset of definitions. From the above note, it is obvious that a variable (function) can be declared multiple times and can only be defined once.
Now go back to our main goal: to understand the keywords in the C language extern
. I have explained 声明
and worked 定义
because we have to use them to understand the keywords extern
. Let's begin by understanding a simple situation that extern
functions on a function. By default, a function is declared and defined with a extern
prefix, which means that when a function is declared and defined, it is not written before extern
, and it is also the default. For example, the following code.
1 |
int foo(int arg1, char arg2); |
The declared function does not precede it extern
, but the compiler adds the keyword in front of it extern
, as follows
1 |
extern int foo(int arg1, char arg2); |
The definition of a function is the same as above (the definition of a function refers to a function body). So every time we define a function, there's always a default keyword in front of it extern
. Since the declaration can be repeated multiple times, the definition can only be completed once, and we can see that the declaration of a function can be repeated multiple times in more than one c/h file or a single c/h file. But the function actually defines only once ( 仅在一个文件中
). Such a function is visible throughout the program and can be called anywhere in the file. (through the Declaration of a function, the compiler knows where the function is defined at compile time.)
The second case, the extern
effect on the variable. How do I declare a variable without defining it? This is an important question to understand the extern
role of variables, the answer is as follows:
Above, an integer variable var
is declared (no definition, no memory allocated). And, as needed, we can declare multiple times.
Now, how do you define a variable? The answers are as follows:
Here, an integer variable is declared and defined (the var
declaration is a subset of the definition, the definition of a variable contains a declaration), and the variable is also var
allocated memory. Now, to our surprise, when we define or declare a function, there is a default in front of it extern
, but the case for variables is different. If variables are the same as functions, they will never allocate memory, they are only declared. Therefore, when we want to declare and not define a variable, we need to add the keyword extern
. In addition extern
, the visibility of variables extends to the entire program, where we know where variables are declared and defined, and can be used anywhere throughout the program. Here are a few examples to understandextern
123456 |
int var; int main(void) {var = ten; return 0;} |
Analysis: Successful program compilation, var
is a global variable (hidden potential declaration)
12345 |
extern int var; int main(void) {return 0;} |
Analysis: The program was compiled successfully, it var
is only declared here, other places are not used var
, so no problem.
123456 |
extern int var; int main(void) {var = ten; return 0;} |
Parsing: Program compilation error, because the variable is declared, var
but not elsewhere defined, in essence, not to var
allocate memory, the program also assigns it a value.
1234567 |
#include "somefile.h" extern int var; int main(void) {var = ten; return 0;} |
Analysis: If somefile.h
a variable is defined in the file file var
, the program will compile successfully.
123456 |
extern int var = 0; int main(void) {var = ten; return 0;} |
Analysis: Guess whether the above code compiles successfully? Here according to the standard of C can be compiled through. Declaring a variable and initializing it with a value, the memory of that variable is allocated, that is, the variable is defined. (Some compilers report a warning: warning: ‘extern‘ variable has an initializer
)
So, we can come to the conclusion that:
The declaration can be multiple times, defined only once.
Keywords extern
are used to extend the visibility of variables and functions.
Because the function exists by default extern
, it does not need to be defined and declared extern
.
When a variable is used extern
, it simply declares no definition.
When a variable is extern
declared and initialized, it is the same as the definition of a variable.
Reprint to understand the keyword extern in C language