为什么在main()中声明一个extern变量有效,但在main()中也不能定义它呢? [英] Why does declaring an extern variable inside main() works,but not defining it inside main() as well?

查看:337
本文介绍了为什么在main()中声明一个extern变量有效,但在main()中也不能定义它呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎很琐碎,但对以下行为的某种严格的解释将有助于我对extern的理解,因此,我感谢您的回答.

在下面的示例程序中,我在函数(main())中声明了extern变量x.现在,如果我在main()之后的文件作用域中定义变量并将8分配给但是如果我在printf()之后的main()中定义变量x,则期望extern声明链接到它,那么它将失败并给出出现以下错误:

test.c||In function 'main':|
test.c|7|error: declaration of 'x' with no linkage follows extern declaration|
test.c|5|note: previous declaration of 'x' was here|
||=== Build finished: 1 errors, 0 warnings ===|


#include<stdio.h>

int main()
{
extern int x;
printf("%d",x);
int x=8;   //This causes error
} 
//int x=8;   //This definition works fine when activated

我只看到代码中的一个错误,即语句int x=8意味着我们再次将x声明为具有auto存储类的变量.我不理解.您能告诉我以下内容:

1)为什么我们允许在函数内内声明extern变量而没有任何警告或错误?如果有效,这到底是什么意思?/p>

2)由于我们在函数内部将x声明为extern并且没有显示错误,所以为什么此声明未链接到函数内部的变量定义,但是看外面,何时在外面定义变量?发生冲突的存储类声明auto-vs-extern是这个原因吗?

解决方案

extern变量声明是对编译器的承诺,即在其他地方存在 global 变量的定义.局部变量不符合对编译器的承诺,因为它们对链接器不可见.从某种意义上讲,extern声明与函数的正向声明相似:您对编译器说:我知道该函数已经在这里,所以让我现在使用它,让链接器负责定位实际的实现". >

This seems very trivial but a somewhat rigorous explanation for the following behavior will help my understanding of extern a lot.So I'll appreciate your answers.

In the following sample program,I've declared an extern variable x inside a function (main()).Now if I define the variable at file scope right after main() and assign 8 to it, then the program works fine and 8 is printed.But if I define the variable x inside main() after the printf(),expecting the extern declaration to link to it, then it fails and gives the following error:

test.c||In function 'main':|
test.c|7|error: declaration of 'x' with no linkage follows extern declaration|
test.c|5|note: previous declaration of 'x' was here|
||=== Build finished: 1 errors, 0 warnings ===|


#include<stdio.h>

int main()
{
extern int x;
printf("%d",x);
int x=8;   //This causes error
} 
//int x=8;   //This definition works fine when activated

I see only one fault in the code,that the statement int x=8 means we are declaring x again as a variable with auto storage class.Rest I don't understand.Can you tell me the following:

1) Why are we allowed to declare an extern variable inside a function,without any warning or error?If valid,what exactly does it mean?

2) Since we declared x as extern inside the function and it showed no error,why then this declaration doesn't link to the definition of the variable inside the function,but looks outside,when the variable is defined outside? Is conflicting storage-class declaration auto-vs-extern the reason for this?

解决方案

extern variable declaration is a promise to the compiler that there would be a definition of a global variable some place else. Local variables do not qualify as fulfillments of the promise to the compiler, because they are invisible to linkers. In a sense, extern declarations are similar to forward declarations of functions: you say to the compiler "I know this function is there, so let me use it now, and let linker take care of locating the actual implementation".

这篇关于为什么在main()中声明一个extern变量有效,但在main()中也不能定义它呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆