2

What is the reason for this?

const int a = 0;
static int b = a * 5;  // compile error
int main()
{
    const int x = 1;
    static int y = x * 10;  // compile error
}
zxgear
  • 1,168
  • 14
  • 30

1 Answers1

2

According to C standard:

All the expressions in an initializer for an object that has static or thread storage duration shall be constant expressions or string literals.

This is valid C++ code though.

AlexD
  • 32,156
  • 3
  • 71
  • 65
  • 2
    Specifically `a` is not a constant expression, it actually is a variable. (It would be a "constant" in C++; this limitation is why people use `#define` a lot in C.) – Lynn Apr 12 '15 at 00:58