News:

Download Pelles C here: http://www.smorgasbordet.com/pellesc/

Main Menu

switch case

Started by Alessio, October 06, 2009, 01:06:33 PM

Previous topic - Next topic

Alessio

Hi,

please see this code:

//
#include <stdio.h>

//
int main(void)
{
    //
    int i;

//
for ( i = 0; i < 10; i++ )
{
switch ( i )
{
case 0:
case 1:
case 2:
case 5:
printf("%d is in case!\n", i);
break;

default:
printf("%d is NOT in case!\n", i);
break;
}
}

return 0;
}


Why I get this output ?

Quote0 is in case!
1 is in case!
2 is in case!
3 is in case!
4 is in case!
5 is in case!
6 is NOT in case!
7 is NOT in case!
8 is NOT in case!
9 is NOT in case!

Why 3, 4 is "in case" ?

Thanks,
Alessio.



JohnF

#1
In C you need a break in each case unless you want fall-through.

   case 0:
     break;

EDIT: sorry I see what you are saying - the answer is I don't know. I've checked with previous versions of PellesC and get the same result.

EDIT1:
It would appear that PellesC is is not generating the right code. I've checked with two other compilers which both give the result you expect.

John

Alessio

Thank you.
Should this topic moved to bug reports ?

JohnF