为什么这个程序的输出只能通过使用 extern 来?为什么没有它呢?

why the output of this program comes only by using extern ? Why not without it?

#include<stdio.h>  
int main ()
{

   printf("%d\n",z);
   return 0;

}  
int z=25;

why is output to this code is showing an error ?

您在 C 中声明 functions/variables 计数的 顺序。在你的代码中,当编译器解析你的代码时,它遇到了尚未声明的符号z

因此,您需要将 int z = ... 放在第一次使用 z 之前,也就是放在 main 之前。

关键字extern告诉编译器该变量已在另一个文件中声明,因此将在链接时解析,即当所有文件组装成一个程序时。所以对于这个文件的编译,未解析的符号z可以忽略=>没有编译错误

试试这个:

#include<stdio.h>  
int z=25;

int main ()
{

   printf("%d\n",z);
   return 0;

}