28

I was looking at this SO question and got to thinking about const ints versus #defines and realized I don't actually understand why the compiler can't deal with this. Could someone shed some light as to why the following code

const int FOO = 10;

int main(int argc, char** argv)
{
    switch(argc)
    {
        case FOO: { printf("foo\n"); }
        default:  { printf("default\n"); }
    }
}

results in

error: case label does not reduce to an integer constant

I read the ISO-C99 spec which states in 6.8.4.2.3 that

The expression of each case label shall be an integer constant expression and no two of the case constant expressions in the same switch statement shall have the same value after conversion.

I understand why the case expression must be constant, but not why only a literal makes the compiler (gcc 4.2.1) happy.

Community
  • 1
  • 1
nall
  • 15,899
  • 4
  • 61
  • 65
  • Interestingly that code appears to compile and run just fine on gcc-4.3.4: http://ideone.com/n1bmIb Edit: Ah.. but only as C++, not C. – GrahamS Dec 12 '12 at 13:33

1 Answers1

28

A constant expression is not the same as a const-qualified type value, even though technically the value is known by the compiler at the point of the case statement.

Imagine what would happen if another file declared extern const int FOO and tried to use it the same way. The compiler wouldn't know what FOO was because it was defined in another file. Even though it has a constant value, it is not a constant expression.

dreamlax
  • 93,976
  • 29
  • 161
  • 209
  • 14
    `extern` example doesn't make anyting clear and doesn't really explain anything. In C++ language you can also declate an `extern` constant, just like in C, yet in C++ it is perfectly legal to use `const int` objects in constant expressions, including case labels (not `extern` ones, of course). The only true answer to the original question is that it was done that way historically. From the very beginning in C the term "constant" meant literal numerical values, not `const` objects. Why? Just because. – AnT stands with Russia Nov 11 '09 at 04:48
  • 1
    Well, the extern example was the one I couldn't come up with that the compiler couldn't deal with. Your comment is valuable, though. Thanks. – nall Nov 11 '09 at 04:51
  • So, is there no way to avoid using magic numbers in case statements? – memmons Jan 17 '11 at 22:53
  • @Harkonian: You could use preprocessor macros, i.e. `#define FOO 10`. – dreamlax Jan 17 '11 at 23:24
  • I'd say the second part of the spec description is more important here. Even if compiler *could* somehow construct the necessary switch code to reuse const values determined at linking time, there'll be no guarantee that they will have actually different values. Even if you can do all that, the actual `switch` code will be supposedly more complex than a set of `if`s, so there's no actual benefit supporting it. Just my thoughts. – Michał Górny Sep 03 '11 at 07:56
  • 3
    The extern issue is a nice explanation thanks. But two questions: why doesn't C at least allow this for local `static const int`? And how does C++ cope with this? – GrahamS Dec 12 '12 at 14:05