Huh it's getting so muddy.The following IBM Support Portal link seems to suggest that the reason we can't use const
qualified variables as real constants is because their life-time is not the same as that of the program itself.It seems to say that only about local variables as global variables have the same lifetime as the program. (IBMLINK).Here's what exactly it says :
An object that is declared const is guaranteed to remain constant for its lifetime, not throughout the entire execution of the program. For this reason, a const object cannot be used in constant expressions.
But in the following program,since the lifetime of the const
qualified variable is the same as that of the execution of the program, why I still get the error when I use it after case
in the switch-case statement,where a constant is expected?It gives the following error:
|11|error: case label does not reduce to an integer constant|
#include<stdio.h>
const int x=2;
int main(void)
{
switch(2)
{
case 1:
printf("Hello");
break;
case x:
printf("Hola");
}
}