473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

switch and friends

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;
}

May 4 '07 #1
12 1361
gopala wrote:
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.
May 4 '07 #2
gopala wrote:
Hey people,
Can somebody explain me how this fragment works ?
Does it?
I came across
this question in a competition and was really blank about this at the
time.
I hope it lost, because of, at least, ...
/* Actual code in competition */
[...]
scanf("%d");
May 4 '07 #3
ak
On May 4, 8:39 am, gopala <krishna....@gmail.comwrote:
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

May 4 '07 #4
ak wrote:
Please mention what was exactly asked in the competition
The question was to write the output of that fragment.
Martin Ambuhl wrote:
Does it?
It does. That is the reason I'm posting ;)

Clever Monkey wrote:
>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 :)

May 4 '07 #5
ak
On May 4, 8:58 am, Clever Monkey <spamt...@clevermonkey.org.INVALID>
wrote:
gopala wrote:
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

May 4 '07 #6
gopala <kr*********@gmail.comwrote:
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

May 4 '07 #7
gopala wrote:
>
.... snip ...
>
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");
Do some reasonable indentation and it should self explain.

switch(0) {
case 1: for (; i < 5; i++)
case 10: { printf("%d", i);
while(i%3 != 0)
case 5: printf("%d", ++i);
default: printf("*");
}
}

Those case and default mutterings are really labels for the switch
clause. Many arguments to switch will cause undefined actions.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net
--
Posted via a free Usenet account from http://www.teranews.com

May 4 '07 #8
Beej Jorgensen wrote:
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) ?

May 5 '07 #9
gopala <kr*********@gmail.comwrote:
>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

May 5 '07 #10
On May 5, 11:45 am, Beej Jorgensen <b...@beej.uswrote:
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 :)

May 6 '07 #11
On May 5, 5:03 pm, gopala <krishna....@gmail.comwrote:
Beej Jorgensen wrote:
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) ?
Me too. The standard doesn't seem to address this in normative
text. (The examples are non-normative, meaning they could be
erroneous).

May 6 '07 #12
Old Wolf <oldw...@inspire.net.nzwrote:
... 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.

--
Peter

May 6 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Travel Mug | last post by:
I'm curious. How many of you are switching from VS.NET and c# to monodevelop and mono? It seems like the low cost savings ( FREE ) and getting all functionality with superior windowing widgets...
6
by: (Pete Cresswell) | last post by:
I'm makeing a little DB to help manage high school class reunions. One feature I'm trying to implement is "Friends". e.g. Fred Smith is a friend of Joe Jones and Bill Anderson. We record...
6
by: Sridhar_Gadda | last post by:
Hello friends, I am new to C and I have written a code but #include <stdio.h> main() { init_funtion(); }
11
by: zhangzhan | last post by:
hello.everyone,i am an chinese.i study in shanxi unniversity,as i like english very much,i also wanr to make friends with a foriger. .....i really don't kown what i should to say ,as i first came...
3
by: shaun roe | last post by:
Hello, I am working in a framework with certain restrictions; in particular I have a 'data' class with getter and setter methods, and a some 'filling' classes which know how to insert the data to...
2
by: kokul | last post by:
Hi friends, I get a value from the html page using cgi->param function. Then i want to pass this value to access database. But it showing some error...I'll explain the details.. sub my_func...
8
by: Srinu | last post by:
Hi all, If we compile the below piece of code, it gets compiled. But gives weird result. switch(x) { int y=2; case 1:
6
by: kokul | last post by:
Dear Friends, I want to take multi lines from a text box in the webpage and pass to my script which saves this into a database. I wrote code like this my $desc = $cgi->param('comment'); ...
2
by: LeXave | last post by:
Hi all I've got the following problem in my database structure : I have a table "User". Each user can have one or more friends which are users too. And I have no idea about the way I must think...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.