当我创建一个单独的块(新的一对花括号)时,在 main() 函数内部,如下所示:
int main(void){
int x = 10;
{
extern int y;
printf("\tNo. is %d\n", y);
int y = 20;
}
}
当我编译这段代码时,我遇到了一个错误:
test.c: In function ‘main’:
test.c:12:9: error: declaration of ‘y’ with no linkage follows extern declaration
int y = 20;
test.c:9:16: note: previous declaration of ‘y’ was here
extern int y;
但
如果 int y 的定义位于 main 函数的末尾,则代码编译并运行得很好。
这个错误背后的原因可能是什么?根据我的书,如果一个变量被声明为 extern,那么我们可以在定义它之前使用它,编译器将在整个文件中搜索变量的定义。