switch and friends

Discussion in 'C Programming' started by gopala, May 4, 2007.

  1. gopala

    gopala Guest

    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;
    }
    gopala, May 4, 2007
    #1
    1. Advertising

  2. 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.
    Clever Monkey, May 4, 2007
    #2
    1. Advertising

  3. 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");
    Martin Ambuhl, May 4, 2007
    #3
  4. gopala

    ak Guest

    On May 4, 8:39 am, 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--]);}
    > }
    >
    > }
    >
    > 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
    ak, May 4, 2007
    #4
  5. gopala

    gopala Guest

    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 :)
    gopala, May 4, 2007
    #5
  6. gopala

    ak Guest

    On May 4, 8:58 am, Clever Monkey <>
    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
    ak, May 4, 2007
    #6
  7. gopala <> wrote:
    > 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
    Beej Jorgensen, May 4, 2007
    #7
  8. gopala

    gopala Guest

    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) ?
    gopala, May 5, 2007
    #8
  9. gopala <> wrote:
    >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
    Beej Jorgensen, May 5, 2007
    #9
  10. gopala

    gopala Guest

    On May 5, 11:45 am, Beej Jorgensen <> wrote:
    > 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 :)
    gopala, May 6, 2007
    #10
  11. gopala

    Old Wolf Guest

    On May 5, 5:03 pm, gopala <> wrote:
    > 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).
    Old Wolf, May 6, 2007
    #11
  12. Old Wolf <> wrote:
    > ... 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
    Peter Nilsson, May 6, 2007
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Buster Copley
    Replies:
    5
    Views:
    545
    Gianni Mariani
    Jul 7, 2003
  2. Replies:
    2
    Views:
    584
  3. Replies:
    0
    Views:
    641
  4. Spiros Bousbouras

    Reading from files and range of char and friends

    Spiros Bousbouras, Mar 10, 2011, in forum: C Programming
    Replies:
    123
    Views:
    1,905
    James Kuyper
    Mar 31, 2011
  5. Switch Within A Switch

    , Apr 22, 2006, in forum: Javascript
    Replies:
    7
    Views:
    88
    Lasse Reichstein Nielsen
    Apr 22, 2006
Loading...

Share This Page