Understanding `extern` keyword in C - 了解 C 语言中的 `extern` 关键字

Understanding extern keyword in C - 了解 C 语言中的 extern 关键字

Basically, the extern keyword extends the visibility of the C variables and C functions. That’s probably the reason why it was named extern.
基本上,extern 关键字扩展了 C 变量和 C 函数的可见性。这可能就是它被命名为 extern 的原因。

Though most people probably understand the difference between the declaration and the definition of a variable or function, for the sake of completeness, I would like to clarify them.
尽管大多数人可能理解变量或函数的 声明定义 之间的区别,但是为了完整起见,我想对其进行澄清。

Declaration of a variable or function simply declares that the variable or function exists somewhere in the program, but the memory is not allocated for them. The declaration of a variable or function serves an important role - it tells the program what its type is going to be. In case of function declarations, it also tells the program the arguments, their data types, the order of those arguments, and the return type of the function. So that’s all about the declaration.
变量或函数的声明只是声明该变量或函数存在于程序中的某个位置,但未为其分配内存。变量或函数的声明起着重要的作用 - 告诉程序其类型将是什么。如果是函数声明,它还会告诉程序参数,其数据类型,这些参数的顺序以及函数的返回类型。这就是声明。

Coming to the definition, when we define a variable or function, in addition to everything that a declaration does, it also allocates memory for that variable or function. Therefore, we can think of definition as a superset of the declaration (or declaration as a subset of definition).
关于定义,当我们定义一个变量或函数时,除了声明所做的一切外,它还为该变量或函数分配内存 因此,我们可以将定义视为声明的超集 (或声明作为定义的子集)。

A variable or function can be declared any number of times, but it can be defined only once. (Remember the basic principle that you can’t have two locations of the same variable or function).
变量或函数可以声明多次,但只能定义一次。(请记住基本原则,即您不能同时拥有两个变量或函数的位置)。

Now back to the extern keyword. First, Let’s consider the use of extern in functions. It turns out that when a function is declared or defined, the extern keyword is implicitly assumed. When we write.
现在回到关键字 extern。首先,让我们考虑在函数中使用 extern。事实证明,在声明或定义函数时,将隐式假定关键字 extern。当我们写的时候。

int foo(int arg1, char arg2);

The compiler treats it as:
编译器将其视为:

extern int foo(int arg1, char arg2);

Since the extern keyword extends the the function’s visibility to the whole program, the function can be used (called) anywhere in any of the files of the whole program, provided those files contain a declaration of the function. (With the declaration of the function in place, the compiler knows the definition of the function exists somewhere else and it goes ahead and compiles the file). So that’s all about extern and functions.
由于关键字 extern 将函数的可见性扩展到整个程序,因此该函数可以在整个程序的任何文件中的任何位置使用 (调用),只要这些文件包含该函数的声明即可。(在适当的位置声明了函数,编译器知道该函数的定义在其他地方存在,然后继续编译该文件) 这就是有关 extern 和函数的全部内容。

Now let’s consider the use of extern with variables. You would do something like this:
现在,我们考虑将 extern 与变量一起使用。您将执行以下操作:

extern int var;

Here, an integer type variable called var has been declared (it hasn’t been defined yet, so no memory allocation for var so far). And we can do this declaration as many times as we want. So far so good.
在这里,已经声明了一个称为 var 的整数类型变量 (尚未定义,因此到目前为止尚未为 var 分配内存)。我们可以根据需要进行多次声明。到目前为止,一切都很好。

Now, how would you define var? You would do this:
现在,您将如何定义 var ?您可以这样做:

int var;

In this line, an integer type variable called var has been both declared and defined (remember that definition is the superset of declaration). Since this is a definition, the memory for var is also allocated. Now here comes the surprise. When we declared/defined a function, we saw that the extern keyword was present implicitly. But this isn’t the case with variables. If it were, memory would never be allocated for them. Therefore, we need to include the extern keyword explicitly when we want to declare variables without defining them. Also, as the extern keyword extends the visibility to the whole program, by using the extern keyword with a variable, we can use the variable anywhere in the program provided we include its declaration the variable is defined somewhere.
在这一行中,已声明和定义了一个名为 var 的整数类型变量 (请记住,定义是声明的超集)。由于这是一个定义,因此还分配了 var 的内存。现在来了惊喜。当我们声明/定义一个函数时,我们看到 extern 关键字是隐式存在的。但是变量不是这种情况。如果是这样,将永远不会为他们分配内存。因此,当我们要声明变量而不定义变量时,需要显式包括 extern 关键字。另外,随着 extern 关键字将可见性扩展到整个程序,通过将 extern 关键字与变量一起使用,我们可以在程序中的任何位置使用该变量,只要我们将其声明包括在某个位置即可。

Now let us try to understand extern with examples.

1. Example 1:

int var; 
int main(void) 
{ 
var = 10; 
return 0; 
}

This program compiles successfully. var is defined (and declared implicitly) globally.
该程序编译成功。var 是全局定义 (并隐式声明) 的。

2. Example 2:

extern int var; 
int main(void) 
{ 
return 0; 
}

This program compiles successfully. Here var is declared only. Notice var is never used so no problems arise.
该程序编译成功。这里仅声明 var。请注意,从未使用过 var,因此不会出现任何问题。

3. Example 3:

extern int var; 
int main(void) 
{ 
var = 10; 
return 0; 
}

This program throws an error in compilation because var is declared but not defined anywhere. Essentially, the var isn’t allocated any memory. And the program is trying to change the value to 10 of a variable that doesn’t exist at all.
该程序在编译时引发错误,因为声明了 var 但未在任何地方定义 var。本质上,该变量没有分配任何内存。该程序正在尝试将根本不存在的变量的值更改为 10。

4. Example 4:

#include "somefile.h" 
extern int var; 
int main(void) 
{ 
var = 10; 
return 0; 
}

Assuming that somefile.h contains the definition of var, this program will compile successfully.
假设 somefile.h 包含 var 的定义,该程序将成功编译。

5. Example 5:

extern int var = 0; 
int main(void) 
{ 
var = 10; 
return 0; 
}

Well, here comes another surprise from C standards. They say that…if a variable is only declared and an initializer is also provided with that declaration, then the memory for that variable will be allocated–in other words, that variable will be considered as defined. Therefore, as per the C standard, this program will compile successfully and work.
好吧,这是 C 标准带来的另一个惊喜。他们说…如果仅声明了一个变量,并且该声明还提供了一个初始化程序,则将分配该变量的内存,换句话说,该变量将被视为已定义。因此,根据 C 标准,该程序将成功编译并运行。

So that was a preliminary look at the extern keyword in C.
因此,这是 C 语言中的 extern 关键字的初步介绍。

In short, we can say (简而言之,我们可以说):
A declaration can be done any number of times but definition only once. (声明可以进行多次,但定义只能进行一次。)
the extern keyword is used to extend the visibility of variables/functions. (extern 关键字用于扩展变量/函数的可见性。)
Since functions are visible throughout the program by default, the use of extern is not needed in function declarations or definitions. Its use is implicit. (由于默认情况下函数在整个程序中都是可见的,因此在函数声明或定义中不需要使用 extern。它的使用是隐式的。)
When extern is used with a variable, it’s only declared, not defined. (当 extern 与变量一起使用时,仅声明而不是定义。)
As an exception, when an extern variable is declared with initialization, it is taken as the definition of the variable as well. (作为例外,当用初始化声明 extern 变量时,它也被当作变量的定义。)

References

https://www.geeksforgeeks.org/understanding-extern-keyword-in-c/

发布了454 篇原创文章 · 获赞 1733 · 访问量 103万+

猜你喜欢

转载自blog.csdn.net/chengyq116/article/details/104432984