Pelles C forum

C language => Beginner questions => Topic started by: Alessio on October 06, 2009, 01:06:33 PM

Title: switch case
Post by: Alessio on October 06, 2009, 01:06:33 PM
Hi,

please see this code:

Code: [Select]
//
#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 ?

Quote
0 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.


Title: Re: switch case
Post by: JohnF on October 06, 2009, 01:14:49 PM
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
Title: Re: switch case
Post by: Alessio on October 06, 2009, 03:42:23 PM
Thank you.
Should this topic moved to bug reports ?
Title: Re: switch case
Post by: JohnF on October 06, 2009, 04:12:49 PM
I think so yes.

John