简体   繁体   English

使以下代码合法的'extern'关键字是什么?

[英]What does the 'extern' keyword does that makes the following code legal?

Why does the following code compile? 为什么以下代码会编译? when is the 'num' variable getting its value? num变量何时获得其值?

#include <stdio.h>

extern int num;

void main()
{
    printf("%d", num); //prints 3
}

int num = 3;

Example: Live Code 示例: 实时代码

In your code, 在您的代码中

 extern int num;

is called a declaration . 被称为声明 OTOH, 太太

int num = 3;

is the definition with explicit initialization. 是具有显式初始化的定义。 This statement sets the value. 该语句设置值。 This value is decided at the compile time and set just before main() starts it's execution. 该值在编译时确定,并在main()开始执行之前设置。

That said, void main() should be int main(void) , at least to conform to the standard. 也就是说, void main()应该是int main(void) ,至少要符合标准。

It works because you've declared num so it can be named in the function's code, and because you've defined it at namespace scope so that it's initialised at static-initialisation time, which is just before main is executed. 之所以起作用,是因为您已声明num以便可以在函数的代码中对其进行命名,并且因为已在名称空间范围内对其进行了定义 ,以便在静态初始化时(即在执行main之前)对其进行初始化。

In that sense, your program as currently written is largely indistinguishable from the following: 从这个意义上讲,您当前编写的程序与以下内容基本没有区别:

#include <stdio.h>

int num = 3;

void main()
{
    printf("%d", num); //prints 3
}

"Where" you initialise the variable isn't particularly relevant, as long as that occurs in time before you try to use it. 初始化变量的“位置”并不特别相关,只要在您尝试使用变量之前及时进行初始化即可。

By the way, you must make main return int , not void . 顺便说一句,您必须使main返回int ,而不是void

extern marks a declaration of a variable that's defined later. extern标记了稍后定义的变量的声明。 The definition, the int num = 3; 定义中, int num = 3; part, is what actually allocates the memory for the variable and sets it's value (it also doubles as a declaration). 部分是实际为变量分配内存并设置其值的内容(它也作为声明的两倍)。 C/C++ are declare-before-use, if you didn't do that extern int num; C / C ++是在使用前声明的,如果您没有执行extern int num; then num wouldn't be declared at the point you use it. 那么在使用时将不会声明num You could also of course drop the extern line and move the definition of num up to above main() . 当然,您也可以删除extern行并将num的定义上移到main()以上。

There's two things that come into play here. 这里有两件事在起作用。 During compile, num has to be declared at a point in the source file before any use. 在编译期间,必须在使用前在源文件中的一点声明num As for when it gets it's value assigned, that comes during program loading. 至于何时获得它的值分配,这是在程序加载期间出现的。 All variables at file scope (outside any functions) like num have their memory allocated and their values if any initialized before main() is called. 文件范围内(所有函数之外)的所有变量(如num均已分配其内存,并且其值(如果在main()调用之前进行了初始化main()也已被分配。 If they aren't given a value in their definition they may contain anything, so don't make any assumptions about the value of uninitialized variables. 如果在定义中未给它们赋值,则它们可能包含任何内容,因此不要对未初始化变量的值做任何假设。 You also can't make any assumptions about the order they're initialized in, so don't refer to other file-scope variables when initializing a variable like this. 您也不能对它们的初始化顺序做任何假设,因此在初始化这样的变量时不要引用其他文件作用域变量。

extern is used with the declaration. extern与声明一起使用。 It tells the compiler that the thing will be defined elsewhere. 它告诉编译器该事物将在其他地方定义。 It's useful when you have multiple files using the same variable or function. 当您有多个文件使用相同的变量或函数时,此功能很有用。 You would put an extern declaration in a header file, and define the shared variable/function in a source file. 您可以将extern声明放在头文件中,并在源文件中定义共享变量/函数。

If you remove extern from your code you will be defining num twice, which isn't allowed. 如果从代码中删除extern ,则将两次定义num ,这是不允许的。 To get rid of the extern declaration, just put your int num = 3; 要摆脱extern声明,只需将您的int num = 3; at the top of your file. 在文件的顶部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM