switch and friends

G

gopala

Hey people,
Can somebody explain me how this fragment works ? I came across
this question in a competition and was really blank about this at the
time.
Actually I traced the program later but I still have doubts as to why
the compiler seems to neglect "case, default .. " keywords. Thanks in
advance.

#include <stdio.h>

void func() /*Code by me after some tracing */
{
char str[] = " ++c dna c evol I";
int sz = sizeof(str)/sizeof(char);
int i=sz-1;
printf("\n");
switch(8)
{
case 2:while(i)
{
default:printf("%c",str[i--]);}
}
}


int main()
{
int i=0;
/* Actual code in competition */
switch(0)
{
case 1: for(;i<5;i++)
case 10:{ printf("%d",i);
while(i%3!=0)
case 5: printf("%d",++i);
default: printf("*");
}
}
printf("\n\n\n");
func();
scanf("%d");
return 0;
}
 
C

Clever Monkey

gopala said:
Hey people,
Can somebody explain me how this fragment works ? I came across
this question in a competition and was really blank about this at the
time.
Actually I traced the program later but I still have doubts as to why
the compiler seems to neglect "case, default .. " keywords. Thanks in
advance.

#include <stdio.h>

void func() /*Code by me after some tracing */
{
char str[] = " ++c dna c evol I";
int sz = sizeof(str)/sizeof(char);
int i=sz-1;
printf("\n");
switch(8)
{
case 2:while(i)
{
default:printf("%c",str[i--]);}
}
}
All sorts of fun can be had with switch statements. I admit I often
have a hard time reading the code to decipher what it does, but a famous
example is Duff's Device: http://en.wikipedia.org/wiki/Duff's_device

This code looks like it uses a similar method to walk backwards through
a string to print the _secret_ message.
 
A

ak

Hey people,
Can somebody explain me how this fragment works ? I came across
this question in a competition and was really blank about this at the
time.
Actually I traced the program later but I still have doubts as to why
the compiler seems to neglect "case, default .. " keywords. Thanks in
advance.

#include <stdio.h>

void func() /*Code by me after some tracing */
{
char str[] = " ++c dna c evol I";
int sz = sizeof(str)/sizeof(char);
int i=sz-1;
printf("\n");
switch(8)
{
case 2:while(i)
{
default:printf("%c",str[i--]);}
}

}

int main()
{
int i=0;
/* Actual code in competition */
switch(0)
{
case 1: for(;i<5;i++)
case 10:{ printf("%d",i);
while(i%3!=0)
case 5: printf("%d",++i);
default: printf("*");
}}

printf("\n\n\n");
func();
scanf("%d");
return 0;



}- Hide quoted text -

- Show quoted text -

Please mention what was exactly asked in the competition

Ciao,
AK
 
G

gopala

ak said:
Please mention what was exactly asked in the competition
The question was to write the output of that fragment.


Martin said:
It does. That is the reason I'm posting ;)

Clever said:
All sorts of fun can be had with switch statements. I admit I often
have a hard time reading the code to decipher what it does, but a famous
example is Duff's Device: http://en.wikipedia.org/wiki/Duff's_device
Thanks for the link :)
 
A

ak

gopala said:
Hey people,
Can somebody explain me how this fragment works ? I came across
this question in a competition and was really blank about this at the
time.
Actually I traced the program later but I still have doubts as to why
the compiler seems to neglect "case, default .. " keywords. Thanks in
advance.
#include <stdio.h>
void func() /*Code by me after some tracing */
{
char str[] = " ++c dna c evol I";
int sz = sizeof(str)/sizeof(char);
int i=sz-1;
printf("\n");
switch(8)
{
case 2:while(i)
{
default:printf("%c",str[i--]);}
}
}

All sorts of fun can be had with switch statements. I admit I often
have a hard time reading the code to decipher what it does, but a famous
example is Duff's Device:http://en.wikipedia.org/wiki/Duff's_device

This code looks like it uses a similar method to walk backwards through
a string to print the _secret_ message.- Hide quoted text -

- Show quoted text -

Great link.....a concept added to my knowledge...Thanks a lot!
Do post more links like this one!

Cheers,
AK
 
B

Beej Jorgensen

gopala said:
switch(8)
{
case 2:while(i)
{
default:printf("%c",str[i--]);}
}

switch(8) hits the default, right? It's sort of like this:

goto bdefault;
{
bcase2: while(i)
{
bdefault: printf("%c", str[i--]);}
}

which is sorta like this, if I might completely spaghettify the code:

goto bdefault;
{
bcase2: if (!i) goto alldone;

bdefault: printf("%c", str[i--]); goto bcase2;

alldone: ;
}

It's my understanding that the the switch is just jumping into the
middle of the loop body, and the loop picks up "normally" from there.
C99 6.8.6.1p3 gives an example of this.

HTH,
-Beej
 
G

gopala

Beej said:
It's my understanding that the the switch is just jumping into the
middle of the loop body, and the loop picks up "normally" from there.
C99 6.8.6.1p3 gives an example of this.

Thanks a lot for making me aware of this. I could understand
something. But I still have doubts regd "why" the loop picks up
normally though it doesn't pass throught the loop statement
(while,for) ?
 
B

Beej Jorgensen

gopala said:
But I still have doubts regd "why" the loop picks up normally though it
doesn't pass throught the loop statement (while,for) ?

The best answer I have for this is that at the end of a while block,
program execution unconditionally jumps back to the while statement,
just like always.

It doesn't matter what has happened before then... the statement at the
end of the while hasn't been tracking anything--the conditional is
tested at the start of the while loop. All the end of the while loop
knows is that once you get here, no matter how that happened, it's time
to go back to the start again.

Someone else might be able to offer a better explanation here.

-Beej
 
G

gopala

The best answer I have for this is that at the end of a while block,
program execution unconditionally jumps back to the while statement,
just like always.

It doesn't matter what has happened before then... the statement at the
end of the while hasn't been tracking anything--the conditional is
tested at the start of the while loop. All the end of the while loop
knows is that once you get here, no matter how that happened, it's time
to go back to the start again.

Hey thats a nice explanation. Thanks a lot for explaining me :)
 
O

Old Wolf

Thanks a lot for making me aware of this. I could understand
something. But I still have doubts regd "why" the loop picks up
normally though it doesn't pass throught the loop statement
(while,for) ?

Me too. The standard doesn't seem to address this in normative
text. (The examples are non-normative, meaning they could be
erroneous).
 
P

Peter Nilsson

Old Wolf said:
... examples are non-normative, meaning they
could be erroneous).

Meaning they need not be 'normal'. But if they are not
correct, then a defect report should be raised.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top