看下面的代码:
#include <stdio.h>
int a = 1;
int b = 2;
int c = a+b;
int main() {
printf("c is %d\n", c);
return 0;
}
gcc -o test test.c 编译时出现错误:initializer element is not constant
-----
原因: 全局变量c的值不能在编译时确定,要在执行是确定(编译原理。。)
解决办法:
#include <stdio.h>
int a = 1;
int b = 2;
int c; //只声明
int main() {
c = a + b; //在main函数里赋值
printf("c is %d\n", c);
return 0;
}