声明:K&R

前端之家收集整理的这篇文章主要介绍了声明:K&R前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在阅读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用其他变量初始化.

猜你在找的C&C++相关文章