[英]How does extern work?
extern
is a storage class in C. How exactly does it work? extern
是C中的存储类。它究竟是如何工作的? The output of the code given below is 20. How is this the output? 下面给出的代码输出是20.这是如何输出的?
#include <stdio.h>
int main()
{
extern int a;
printf("%d", a);
return 0;
}
int a=20;
It means three things: 这意味着三件事:
extern
, or with an initialiser, or in your case, both). extern
,或者使用初始化,或者在你的情况下,都是)。 Specifically, your extern int a;
具体来说,你的
extern int a;
declares that the variable exists, but doesn't define it at that point. 声明该变量存在,但不会在该点定义它。 At this point, you can use it, and the linker will make sure your use refers to the definition.
此时,您可以使用它,链接器将确保您的使用引用定义。 Then you have the required definition,
int a=20;
然后你有了所需的定义,
int a=20;
at the end, so all is well. 最后,一切都很好。
extern
in this case indicates that the symbol a
is defined in a different location, such as a different module. 在这种情况下,
extern
表示符号a
在不同的位置定义,例如不同的模块。 So the linker looks for a symbol with the same name in all of the modules that are linked, and if one exists then it sets the address to your local variable a
with the address to the externally defined variable. 因此,链接器在所有链接的模块中查找具有相同名称的符号,如果存在,则将地址设置为本地变量
a
,并将地址设置为外部定义的变量。 Since you have another a
defined outside of your main()
function, the a
inside your main()
function is (basically) the same variable as the one outside. 既然你有另一
a
你的外部定义main()
函数,在a
你中main()
函数(基本)为外面的一个相同的变量。
Since the global a
is initialized before the main function executes, the value is 20 by the time you access it. 由于全局
a
在main函数执行之前被初始化,因此在您访问它时该值为20。
extern means i declare a variable, just like you implement a function in a source file and declare the prototype in a header to allow other source file to use it. extern意味着我声明一个变量,就像在源文件中实现一个函数一样,并在头文件中声明原型以允许其他源文件使用它。
If you put a global variable in a source file, and use a header to declare it with the extern keyword, each source file including the header will see the variable. 如果将全局变量放在源文件中,并使用标头使用extern关键字声明它,则包含标头的每个源文件都将看到该变量。
The linker will do the job to tie everything just as it does with functions 链接器将完成工作,就像处理函数一样
extern
as a storage class specifier tells the compiler that the object being declared is not a new object, but has storage elsewhere, ie, is defined elsewhere. extern
作为存储类说明符告诉编译器声明的对象不是新对象,但在其他地方有存储,即在其他地方定义。 You can try this experiment with your code to see how it works. 您可以尝试使用代码进行此实验,以了解其工作原理。 Leave out the keyword
extern
in your declaration of int a
in main()
. 在
main()
的int a
声明中省略关键字extern
。 Then your printf()
will print some garbage value, as it would be a new definition of an int
with the same identifier, which would hide the global a
declared elsewhere. 然后你的
printf()
将打印一些垃圾值,因为它将是具有相同标识符的int
的新定义,这将隐藏在其他地方声明的全局a
。
You use extern
to tell the compiler that the variable is defined elsewhere. 您使用
extern
告诉编译器该变量是在别处定义的。 Without extern
in your program compiler would define another variable a
(in addition to this in the global scope) in your main()
function that would be printed uninitialized. 如果程序中没有
extern
,编译器会在main()
函数中定义另一个未初始化的变量a
(除了在全局范围内)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.