这似乎很微不足道,但对以下行为的有点严格的解释将有助于我理解很多。extern所以我会很感激你的回答。
在下面的示例程序中,我在extern函数x(之后,期望声明链接到它,然后它失败并给出以下错误:main()main()88xmain()printf()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声明一个变量,没有任何警告或错误?如果有效,它到底是什么意思?
2)既然我们在函数内部声明x了extern,它没有显示错误,那么为什么这个声明没有链接到函数内部变量的定义,而是在外部定义变量时?冲突的存储类声明auto-vs-extern是原因吗?