I am trying to do a simple function to return values based on the input...shown below. char func (unsigned char add) { signed char i; switch (add) { case 0: i = 0; break; case 1: i = 1; break; case 2, 3, 4, 9: i = 9; break; case 10: i = 10;break; default: i = 99;break; } return i; } When i try to call func(2), it automatically jumps to the default.... Does the Keil C compiler support the multiple list case structure? MG
"Does the Keil C compiler support the multiple list case structure?" Does ANSI 'C' define any such thing? What does your K&R say? read carefully the definition of the 'case' label...
Also, read-up on the comma operator in 'C'. Note that Hitex currently has a special offer on the full ANSI spec: http://www.hitex.co.uk/newsletter0603/
Does the Keil C compiler support the multiple list case structure? No. Another Hitech extension? For multiple cases that have the same body, write the (ANSI standard) code thus:
switch (x) { case 2 : case 3 : case 7 : case 9 : i = 9; break; }
"Case labels have to be literals, don't they? I don't think the comma operator is allowed there, any more than any other sort of expression." K&R says (say?), "a constant expression" - so the comma operator probably is allowed (though pointless!)
Thanks. The follwing code does work. switch (inputvar) { case a: case b: case c: etc etc callfunction(); break; case d: callotherfunction(); break; } The original code does not generate any errors btw. I guess could implement this with an if condition as follows if ((var == a)|(var == b)|..... callfunction();