0

I did this code only for learning purpose. But while doing so I found a problem. Here x is constant integer,still compiler is giving me error. I am using gcc compiler. Please explain what is the reason of this error and how to avoid it.

#include<stdio.h>
int main()
{
int const  x = 10;
int y = 20;
switch(y)
{
    case x:      //error: case label does not reduce to an integer constant
    printf("value of x: %d\n",x);   
    break;
}
}
Tonmoy
  • 557
  • 2
  • 6
  • 20

5 Answers5

2

The grammar for a switch statement in C is the following:

selection-statement:
switch ( expression ) statement
  labeled-statement:
  case constant-expression : statement
  default : statement

Therefore, you can only use constant expressions as "case value". Constant expression is not the same as constant variable. In other words - sorry, but you cannot do that.

Community
  • 1
  • 1
  • This is clearly a duplicate. Why post an answer? – RedX Oct 02 '13 at 14:53
  • @RedX: Because answering this was easier than quicker for me than researching for duplicates. Nobody prevents you from voting this a dupe. –  Oct 02 '13 at 17:15
2

you can use the preprocessor as a workaround:

#define X 10
// ...
case X:
jev
  • 2,023
  • 1
  • 17
  • 26
  • but as far as i know preprocessor is also constant not a constant qualifier – Tonmoy Oct 02 '13 at 14:52
  • This is clearly a duplicate. Why post an answer? – RedX Oct 02 '13 at 14:53
  • although the original question is closely related to the one linked, I thought this answer (which is not a duplciate of another answer) still may be useful to ciscumvent the inconvenience – jev Oct 02 '13 at 15:21
1

You might know that x is a constant but the compiler cannot guarantee it: it is still possible to modify x in C. One way is by taking its address (via a pointer) and dereferencing it.

In C you can only switch on literal integral types; more formally, a constant expression.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

In this case , you have to use a if statement becasue C's switch() does not take an expression. It takes a constant.

CS Pei
  • 10,869
  • 1
  • 27
  • 46
0

case statements need constants. You could accomplish something similar to what you're doing with a macro.

#define TEN 10
#include<stdio.h>
int main()
{
int const  x = TEN;
int y = 20;
switch(y)
{
    case TEN:      //error: case label does not reduce to an integer constant
    printf("value of x: %d\n",x);   
    break;
}
}
Joe
  • 46,419
  • 33
  • 155
  • 245
  • This is clearly a duplicate. Why post an answer? – RedX Oct 02 '13 at 14:54
  • Duplicate of what? `devnull` and `jev`'s answer came after mine. It's not a duplicate if it was first. – Joe Oct 02 '13 at 14:56
  • Of another qustion on the site, see the votes for close. – RedX Oct 02 '13 at 14:57
  • Oh, you mean the question's a duplicate? It didn't occur to me to check. I've flagged it. – Joe Oct 02 '13 at 14:58
  • That question's answer explains why but doesn't give a satisfactory solution IMHO @RedX. – Joe Oct 02 '13 at 15:11
  • Then maybe add an answer there? This is specially important as in future all clicks on this topic might be redirected there. – RedX Oct 02 '13 at 15:22