NO

Author Topic: switch case  (Read 2887 times)

Alessio

  • Guest
switch case
« on: October 06, 2009, 04:21:07 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.



Online AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: switch case
« Reply #1 on: October 06, 2009, 10:22:15 PM »

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" ?

I have tried some similar cases which work correct. In your case Pelles C generate only code which checks the if the value is less then 0 or above 5 for the jump to default.  If you use 0, 1, 3 and 5 Pelles C checks also the limits and jump then over a tabelle of jumping-addresses.

I think Pelle has to look at this.
best regards
 Alex ;)

intuitas

  • Guest
Re: switch case
« Reply #2 on: October 08, 2009, 06:27:53 PM »
Hi
I haven't done exhaustive testing but 0,1,2 and 5; 0,1,2 and 6; 0,1,2 and 7 all produce errors.
Every other combination I have tried seems to work correctly including 0,1,2 and 8; and 0,1,2 and 9.
If the increment is altered the "in" case sequences of 0, 1,2 and 5(6 or 7) all still produce errors.
Removal of any one of the four "in" case statements eliminates the errors - as does adding a fifth.
I defined a second integer (j) and interposed a simple assignment statement between the fourth "in" case statement and the printf - to no effect - however interposing it between any of the four "in" case statements eliminated the errors.

I dont know if any of the above helps - but it was an interesting exercise and I tried quite a few meaningless variations along the way.

Regards

Paul