我正在阅读K& R的“C编程语言”第2章:“类型,运算符和表达式”,第2.4节,在那里我发现了以下语句:
If the variable in question is not automatic,the initialization is
done once only,conceptually before the program starts executing,and
the initializer must be a constant expression.An explicitly
initialized automatic variable is initialized each time the function
or block it is in is entered; the initializer may be any expression.
以上几行不太清楚它们是什么意思?
解决方法
int a = 5; int b = a; //error,a is not a constant expression int main(void) { static int c = a; //error,a is not a constant expression int d = a; //okay,a don't have to be a constant expression return 0; }
只有d是一个自动变量,所以只允许d用其他变量初始化.