What is the meaning of the GCC warning "case label value exceeds maximum value for type"?
20,521
Solution 1
A char is a number between -128 and 127. KEY_F(9) probably is a value outside of that range.
Use:
- unsigned char, or
- int, or
- (char) KEY_F(9)
Or even better, use a debugger and determine sizeof(KEY_F(9)) to make sure it's a byte and not a short.
Solution 2
Well, KEY_F(9) would be 273 (see curses.h) which exceeds the range of char (-128,127).
Solution 3
In this case, KEY_F(9)
is evaluating to something outside the range of char
. The switch
statement is assuming that because its argument is a char
, that all case labels will be also. Changing the switch
to read switch((unsigned int)ch)
will cure it.
Author by
Alistair
Updated on July 09, 2022Comments
-
Alistair almost 2 years
My code looks like this:
char * decode_input(char ch) { switch(ch) { case 'g': return "get"; break; case KEY_F(9): return "quit"; break; default: return "unknown"; break; } }
Any clues?
-
RBerteig almost 15 yearsCasting KEY_F(9) to char might loose information depending on the implementation of that macro. Also, char is either signed or unsigned depending on the platform defaults and often compiler switches.
-
RBerteig almost 15 yearsThe range of char depends on whether char is signed or unsigned. However, if curses.h is the source of the macro, then it is outside the range either way.
-
razzed almost 15 yearsTrue true, and it's been a while since I played in C. I always used types for string characters anyway and always used unsigned char for the type, so I haven't run into this ever...
-
razzed almost 15 yearsNote that casting to char will remove the compiler error, but it will likely not behave correctly!