初始元素是不是在C常量

初始元素是不是在C常量

问题描述:

可能重复:结果
  Error “初始元素不是常数rdquo;的试图用常量来初始化变量时

我从JavaScript / PHP / Python的未来,也许我失去了一些东西,这里是code:

I'm coming from javascript/php/python and probably I'm missing something, here is the code:

const int a = 50;
const int c = 100;
const int d = 100;
int endX = c + a;
int endY = d;
int startX, startY, b;

我得到

ex1.4.c:6:错误:初始元素不是恒定的结果
  ex1.4.c:7:错误:初始元素不是常数

ex1.4.c:6: error: initializer element is not constant
ex1.4.c:7: error: initializer element is not constant

有人做过解释?

如果您声明endX为全局变量错误是有道理的。

If you are declaring endX as a global variable the error makes sense.

原因是,全局变量在编译时初始化,您要初始化endX为必须在执行时间来完成的操作。

The reason is that global variables are initialized in compiling time, and you are trying to initialize endX as an operation that must be done in execution time.