0

I used a predefined char variable in a case in my switch and got this error case label does not reduce to an integer

char player = 'X';
  switch(.....){
    case player:
.
.
.
.

I need a solution for this.

nikhilbalwani
  • 817
  • 7
  • 20

2 Answers2

1

From the C11 standard:

The expression of each case label shall be an integer constant expression

player is not a "constant expression".

Please note that in C qualifying a variable as const, does not make it a "constant expression" in the sense of the C standard.

A label either need to be an integer literal, or a enum, which in fact is an integer.

Community
  • 1
  • 1
alk
  • 69,737
  • 10
  • 105
  • 255
0

What you want is:

char player = 'X';
switch(player){
    case 'X':
    case 'Y':
    case 'Z':

(A char is an encoding and the encoding is an int)

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41