What is the meaning of the GCC warning “case label value exceeds maximum value for type”?
-
06-07-2019 - |
Question
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?
Solution
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.
OTHER TIPS
Well, KEY_F(9) would be 273 (see curses.h) which exceeds the range of char (-128,127).
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.
It looks like KEY_F(9) must evaluate to something that is outside the range of a char.
What everyone else said regarding the range for char.
I remember this from my early days writing C... you're probably calling decode_input from a loop, right? If the user presses something like F9, you're going to get two bytes in the keyboard buffer - and the first byte will be 0x0.