switch case question

Discussion in 'C Programming' started by Alessio, Oct 6, 2009.

  1. Alessio

    Alessio Guest

    Hi, please consider following code:

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

    What should be correct result ?
    With lcc compilier I get that 3 and 4 are "NOT in case", with PelleC
    compiler I get that only 6,7,8, and 9 are "NOT in case"...

    Thank you,
    Alessio.
    Alessio, Oct 6, 2009
    #1
    1. Advertising

  2. Alessio

    jacob navia Guest

    Alessio a écrit :
    > Hi, please consider following code:
    >
    > #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;
    > }
    >
    > What should be correct result ?
    > With lcc compilier I get that 3 and 4 are "NOT in case", with PelleC
    > compiler I get that only 6,7,8, and 9 are "NOT in case"...
    >


    With lcc-win I get:
    0 is in case!
    1 is in case!
    2 is in case!
    3 is NOT in case!
    4 is NOT 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!

    and that is the correct output.
    jacob navia, Oct 6, 2009
    #2
    1. Advertising

  3. Alessio

    Mark Bluemel Guest

    On 6 Oct, 13:06, Alessio <> wrote:
    > Hi, please consider following code:
    >
    > #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;
    >
    > }
    >
    > What should be correct result ?


    What does your reading of the language specification and/or a good
    reference suggest should be the correct result?

    > With lcc compilier I get that 3 and 4 are "NOT in case",


    Does "lcc" generate code that reports only 3 and 4 are "NOT in case"?
    If so, then it's broken. However, I suspect you mean that with lcc you
    get 3,4,6,7,8,9 are "NOT in case", which is correct.

    > with PelleC compiler I get that only 6,7,8, and 9 are "NOT in case"...


    Then PelleC seems to be generating incorrect code.

    Note: it's often clearer to post your source, the output from a
    compilation and the output from a run rather than a summary like this.
    To illustrate, the example below shows that gcc works as I'd expect :-

    $ cat alessio.c
    #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;
    }

    $ cc -o alessio alessio.c
    $ ./alessio
    0 is in case!
    1 is in case!
    2 is in case!
    3 is NOT in case!
    4 is NOT 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!
    Mark Bluemel, Oct 6, 2009
    #3
  4. Alessio

    Alessio Guest

    Kenneth Brody ha scritto:

    > (I am assuming that your reference to lcc with 3 and 4 means that they,
    > _in_ _addition_ _to_ 6, 7, 8, and 9, are "NOT in case", rather than
    > _only_ 3 and 4.)
    >


    yes, you're right.

    Alessio
    Alessio, Oct 6, 2009
    #4
  5. Alessio

    gwowen Guest

    Just to confirm the initial bug report...

    C:\Program Files\PellesC>type foo.c
    #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;

    }

    C:\Program Files\PellesC>.\Bin\cc foo.c
    foo.c
    ..\Bin\polink.exe foo.obj

    C:\Program Files\PellesC>.\foo.exe
    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!

    -----------------------

    YIKES!
    gwowen, Oct 6, 2009
    #5
  6. On Tue, 06 Oct 2009 10:25:26 -0400, gwowen <> wrote:

    > Just to confirm the initial bug report...
    >
    > C:\Program Files\PellesC>type foo.c

    <snip>
    >
    > C:\Program Files\PellesC>.\Bin\cc foo.c
    > foo.c
    > .\Bin\polink.exe foo.obj
    >
    > C:\Program Files\PellesC>.\foo.exe

    <snip>

    Two things strike me, here.
    1) Do you really want to be putting your own source files in
    C:\Program Files ???? This seems like a very strange practice.

    2) I don't know anything about Pelles C, but if it were me,
    before running the above test, I would do
    del foo.*
    to make sure there's no leftover foo.exe doing anything funny.
    I find it hard to imagine a C compiler with any amount of
    user base getting things wrong as badly as your output would
    indicate. There's a wikipedia page for Pelles C, which says
    that it's based on a modified version of lcc, and the initial
    message says that lcc got it right.

    --
    Morris Keesan --
    Morris Keesan, Oct 6, 2009
    #6
  7. Alessio

    jacob navia Guest

    Gareth Owen a écrit :
    > "Morris Keesan" <> writes:
    >
    >> 1) Do you really want to be putting your own source files in
    >> C:\Program Files ???? This seems like a very strange practice.

    >
    > Given that I only installed it for the test, and then deleted it
    > straight away, I think it'll be OK. I did it there so I didn't even
    > have to set the PATH, etc.
    >
    > Normally no, I wouldn't do that. It's a bloody stupid thing to do.
    >
    >> 2) I don't know anything about Pelles C, but if it were me,
    >> before running the above test, I would do
    >> del foo.*
    >> to make sure there's no leftover foo.exe doing anything funny.

    >
    > Fresh install, see above.
    >
    >> I find it hard to imagine a C compiler with any amount of
    >> user base getting things wrong as badly as your output would
    >> indicate.

    >
    > Hence the "YIKES".


    Look, compiler writing is a difficult business. Please post a
    bug report to the author. Pelles C is based on lcc, and lcc
    doesn't have this bug. Probably Pelles introduced this bug
    later.
    jacob navia, Oct 7, 2009
    #7
  8. Alessio

    Alessio Guest

    jacob navia ha scritto:

    > Look, compiler writing is a difficult business. Please post a
    > bug report to the author.


    Done.

    Alessio
    Alessio, Oct 7, 2009
    #8
  9. Alessio

    gwowen Guest

    On Oct 7, 11:25 am, jacob navia <> wrote:

    > Pelles C is based on lcc...
    > lcc doesn't have this bug...
    > Probably Pelles introduced this bug later.


    Probably?
    gwowen, Oct 8, 2009
    #9
  10. Alessio

    Richard Bos Guest

    Gareth Owen <> wrote:

    > "Morris Keesan" <> writes:


    > > 2) I don't know anything about Pelles C, but if it were me,
    > > before running the above test, I would do
    > > del foo.*
    > > to make sure there's no leftover foo.exe doing anything funny.

    >
    > Fresh install, see above.
    >
    > > I find it hard to imagine a C compiler with any amount of
    > > user base getting things wrong as badly as your output would
    > > indicate.

    >
    > Hence the "YIKES".


    Odd. Must be a recent change. I have PellesC 2.90.8 installed here,
    which is from 2004 or so, and it gives the correct output.

    Richard
    Richard Bos, Oct 8, 2009
    #10
    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. gane kol

    switch case with enum

    gane kol, May 6, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    7,297
    Kevin Spencer
    May 6, 2005
  2. Brian J. Sayatovic

    Why constant ints in switch case expressions?

    Brian J. Sayatovic, Jul 2, 2003, in forum: Java
    Replies:
    22
    Views:
    21,815
    Dale King
    Jul 9, 2003
  3. vic

    switch case statement

    vic, Mar 3, 2004, in forum: Java
    Replies:
    9
    Views:
    5,770
  4. Andy

    Switch..Case Statement Question.

    Andy, Feb 28, 2005, in forum: Javascript
    Replies:
    21
    Views:
    180
    Dr John Stockton
    Mar 7, 2005
  5. Switch Within A Switch

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

Share This Page