Why is "switch-case" considering a #define'd identifier as constant but not a const-qualified variable?
c, c-preprocessor, constants, switch-statement
Solution
In C `const` does not mean constant but read-only. A `const` qualified variable is never a constant and cannot be used where a constant expression is required.
Note that you can also use `enum` constants for the `case` constant expression as `enum` constants are real constants.
Problem
This program produces the following error: case label does not reduce to an integer constant| I simply fail to understand why an explicitly declared constant `third_cond`, assigned initializer `3`, that too inside the function body, is not considered a constant for `case 3` while a `#define`'d identifier `second_value` is considered a constant at the same time.The program works fine if I use the literal `3` instead of `third_cond` or simply `#define` `third_cond` as `3`. Please explain why it's so, unless this question is a duplicate. I checked,but didn't find an answer. ``` #include<stdio.h> #define second_cond 2 int main() { const int third_cond=3; int choice; printf("Enter your choice\n"); scanf("%d",&choice); switch(choice) { case 1: printf("Beggar"); break; case second_cond: printf("Mugger"); break; case third_cond: printf("Lugger"); break; default: printf("Bugger"); } } ```