【发布时间】:2019-11-28 02:18:27
【问题描述】:
我在主函数之后再次声明了全局变量,但它仍然影响主函数。我知道 C 允许在第一次声明未初始化变量时再次声明全局变量(它在 c++ 中不起作用)。如果我在 main 函数之后分配值,它仍然可以在 c 中出现两个警告,但在 c++ 中会出错。
我已经调试了代码,但它从未到达int a=10; 行。
#include <stdio.h>
#include <string.h>
int a;
int main()
{
printf("%d",a);
return 0;
}
/*a=10 works fine with following warnings in c.
warning: data definition has no type or storage class
warning: type defaults to 'int' in declaration of 'a' [-Wimplicit-int]|
but c++ gives the following error
error: 'a' does not name a type|
*/
int a=10;
输出是10。
【问题讨论】:
-
嗨,最后重新声明'a'的目的是什么?
-
这肯定会有所帮助。 stackoverflow.com/questions/17388431/…
标签: c global-variables