NO

Author Topic: switch case  (Read 1856 times)

Alessio

  • Guest
switch case
« 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.



JohnF

  • Guest
Re: switch case
« Reply #1 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
« Last Edit: October 06, 2009, 03:07:02 pm by JohnF »

Alessio

  • Guest
Re: switch case
« Reply #2 on: October 06, 2009, 03:42:23 pm »
Thank you.
Should this topic moved to bug reports ?

JohnF

  • Guest
Re: switch case
« Reply #3 on: October 06, 2009, 04:12:49 pm »
I think so yes.

John