switch why?

Discussion in 'C Programming' started by vim, May 15, 2006.

  1. vim

    vim Guest

    #include<stdio.h>
    int main()
    {
    int a=10;
    switch(a)
    {
    case '1':
    printf("ONE\n");
    break;
    case '2':
    printf("TWO\n");
    break;
    defa1ut:
    printf("NONE\n");
    }
    return 0;
    }
    My expectation :------The output is NONE.
    But the output is different why?
     
    vim, May 15, 2006
    #1
    1. Advertising

  2. vim

    Vladimir Oka Guest

    vim opined:

    > #include<stdio.h>
    > int main()
    > {
    > int a=10;
    > switch(a)
    > {
    > case '1':
    > printf("ONE\n");
    > break;
    > case '2':
    > printf("TWO\n");
    > break;
    > defa1ut:
    > printf("NONE\n");
    > }
    > return 0;
    > }
    > My expectation :------The output is NONE.
    > But the output is different why?


    Different in what way?

    You probably meant:

    case 1:
    ...
    case 2:

    `int`s are quite a different beast from `char`s.

    Also, it's `default`.

    --

    <http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
     
    Vladimir Oka, May 15, 2006
    #2
    1. Advertising

  3. vim

    pemo Guest

    vim wrote:
    > #include<stdio.h>
    > int main()
    > {
    > int a=10;
    > switch(a)
    > {
    > case '1':
    > printf("ONE\n");
    > break;
    > case '2':
    > printf("TWO\n");
    > break;
    > defa1ut:
    > printf("NONE\n");
    > }
    > return 0;
    > }
    > My expectation :------The output is NONE.
    > But the output is different why?


    >But the output is different why?


    Does it say 'Syntax Error'?


    --
    ==============
    Not a pedant
    ==============
     
    pemo, May 15, 2006
    #3
  4. vim

    vim Guest

    It will compile correctly.But the answer is not the expected one
     
    vim, May 15, 2006
    #4
  5. "pemo" <> wrote:
    >vim wrote:
    >> #include<stdio.h>
    >> int main()
    >> {
    >> int a=10;
    >> switch(a)
    >> {
    >> case '1':
    >> printf("ONE\n");
    >> break;
    >> case '2':
    >> printf("TWO\n");
    >> break;
    >> defa1ut:
    >> printf("NONE\n");
    >> }
    >> return 0;
    >> }
    >> My expectation :------The output is NONE.
    >> But the output is different why?

    >
    >>But the output is different why?

    >
    >Does it say 'Syntax Error'?


    "defa1ut:", althought different from "default:", is a valid label. I
    would expect a warning about "unreachable code", but no syntax error.
    (I have not compiled the OP's sample)
     
    Roberto Waltman, May 15, 2006
    #5
  6. On 15 May 2006 08:30:45 -0700, "vim" <> wrote:

    >It will compile correctly.But the answer is not the expected one


    You mistyped "default" (with a lower case 'L') as "defau1t" (with the
    digit '1')
     
    Roberto Waltman, May 15, 2006
    #6
  7. vim

    pemo Guest

    Roberto Waltman wrote:
    > "pemo" <> wrote:
    >> vim wrote:
    >>> #include<stdio.h>
    >>> int main()
    >>> {
    >>> int a=10;
    >>> switch(a)
    >>> {
    >>> case '1':
    >>> printf("ONE\n");
    >>> break;
    >>> case '2':
    >>> printf("TWO\n");
    >>> break;
    >>> defa1ut:
    >>> printf("NONE\n");
    >>> }
    >>> return 0;
    >>> }
    >>> My expectation :------The output is NONE.
    >>> But the output is different why?

    >>
    >>> But the output is different why?

    >>
    >> Does it say 'Syntax Error'?

    >
    > "defa1ut:", althought different from "default:", is a valid label. I
    > would expect a warning about "unreachable code", but no syntax error.
    > (I have not compiled the OP's sample)


    LOL, - nicely spotted


    --
    ==============
    Not a pedant
    ==============
     
    pemo, May 15, 2006
    #7
  8. vim

    Eric Sosman Guest

    vim wrote On 05/15/06 11:21,:
    > #include<stdio.h>
    > int main()
    > {
    > int a=10;
    > switch(a)
    > {
    > case '1':
    > printf("ONE\n");
    > break;
    > case '2':
    > printf("TWO\n");
    > break;
    > defa1ut:
    > printf("NONE\n");
    > }
    > return 0;
    > }
    > My expectation :------The output is NONE.
    > But the output is different why?


    Y'know, I was struck by the artificiality of the
    problem in your earlier thread about "problem with
    initialization" but decided to give a straight answer
    anyhow. Now I see that you're just a troll who's found
    a C puzzle book somewhere.

    vim, there's an old saying about jokes: Use it once
    and you're a wit; use it twice and you're a half-wit.
    I suggest you quit before you get denormalized.

    --
     
    Eric Sosman, May 15, 2006
    #8
  9. vim wrote:
    > #include<stdio.h>
    > int main()
    > {
    > int a=10;
    > switch(a)
    > {
    > case '1':
    > printf("ONE\n");
    > break;
    > case '2':
    > printf("TWO\n");
    > break;
    > defa1ut:

    Should be
    def1at3:
    or
    flatulate:
    > printf("NONE\n");
    > }
    > return 0;
    > }
    > My expectation :------The output is NONE.


    Just change your expectation to match reality. The output *should* be "42".

    > But the output is different why?
    >

    Gnomes or sprites. It's hard to tell, since both of them are known to
    be l33t hax0rs. Consult your Tome of Magickal Computation.
     
    Clever Monkey, May 15, 2006
    #9
  10. Roberto Waltman wrote:
    >
    > "pemo" <> wrote:
    > >vim wrote:

    [...]
    > >> defa1ut:
    > >> printf("NONE\n");

    [...]
    > "defa1ut:", althought different from "default:", is a valid label. I
    > would expect a warning about "unreachable code", but no syntax error.
    > (I have not compiled the OP's sample)


    If you turn up the warning level on your compiler (assuming that your
    compiler actually has that ability) you may get a warning to appear.

    --
    +-------------------------+--------------------+-----------------------------+
    | Kenneth J. Brody | www.hvcomputer.com | |
    | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------------+
    Don't e-mail me at: <mailto:>
     
    Kenneth Brody, May 15, 2006
    #10
  11. "vim" <> writes:
    > It will compile correctly.But the answer is not the expected one


    *What* will compile correctly?

    Read <http://cfaj.freeshell.org/google/>.

    If you want to post puzzles, that's ok, but please let us know that
    they're puzzles rather than actual code you're having problems with.
    Some of us might be willing to provide help if you need it, but not be
    interested in working on a puzzle.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, May 15, 2006
    #11
  12. Keith Thompson wrote:
    > "vim" <> writes:
    >> It will compile correctly.But the answer is not the expected one

    >
    > *What* will compile correctly?
    >
    > Read <http://cfaj.freeshell.org/google/>.
    >
    > If you want to post puzzles, that's ok, but please let us know that
    > they're puzzles rather than actual code you're having problems with.
    > Some of us might be willing to provide help if you need it, but not be
    > interested in working on a puzzle.
    >

    Speaking of which, I'm not usually interested in "puzzles" since I have
    my own problems to solve at work (for better or for worse, I am the C
    guy by attrition -- none of the young coders are taught it in school
    anymore), but are any of the C puzzle books worth the time?

    To be more clear, is the purpose to highlight places coders tend to be
    weakest and shine some lights into the darker corners?

    This is not a spurious question. At least, I hope it's not. I'm
    reading "Expert C Programming" by van der Linden right now and finding
    that my understanding of the language is certainly deepening.

    Of course, I like van der Linden's chatty style and appreciate the
    humour. Are any of these puzzle books similar in this manner?
     
    Clever Monkey, May 15, 2006
    #12
  13. vim

    Guest

    Clever Monkey wrote:

    > Speaking of which, I'm not usually interested in "puzzles" since I have
    > my own problems to solve at work (for better or for worse, I am the C
    > guy by attrition -- none of the young coders are taught it in school
    > anymore), but are any of the C puzzle books worth the time?


    If you want some help, recent Georgia Tech graduates will know at least
    some C.

    > To be more clear, is the purpose to highlight places coders tend to be
    > weakest and shine some lights into the darker corners?


    Well, I can certainly imagine mistyping "default" and spending more
    time than I'd like finding it :)
     
    , May 15, 2006
    #13
  14. vim wrote:
    > #include<stdio.h>
    > int main()
    > {
    > int a=10;
    > switch(a)
    > {
    > case '1':
    > printf("ONE\n");
    > break;
    > case '2':
    > printf("TWO\n");
    > break;
    > defa1ut:
    > printf("NONE\n");
    > }
    > return 0;
    > }
    > My expectation :------The output is NONE.
    > But the output is different why?


    Some immediate possibilities:
    1) You misspelled "default"
    2) Your implementation uses a character encoded in which either '1' or
    '2' has an encoding of 10.
     
    Martin Ambuhl, May 15, 2006
    #14
  15. "vim" <> writes:
    > #include<stdio.h>
    > int main()
    > {
    > int a=10;
    > switch(a)
    > {
    > case '1':
    > printf("ONE\n");
    > break;
    > case '2':
    > printf("TWO\n");
    > break;
    > defa1ut:
    > printf("NONE\n");
    > }
    > return 0;
    > }
    > My expectation :------The output is NONE.
    > But the output is different why?


    If you're going to try to fool us by misspelling "default", at least
    spell it as "defau1t" rather than "defa1ut".

    Did you *really* expect the output to be "NONE", or did you already
    know the answer before you posted the puzzle?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, May 15, 2006
    #15
  16. vim

    Neil Guest

    Roberto Waltman wrote:
    > On 15 May 2006 08:30:45 -0700, "vim" <> wrote:
    >
    >> It will compile correctly.But the answer is not the expected one

    >
    > You mistyped "default" (with a lower case 'L') as "defau1t" (with the
    > digit '1')



    That is twice you took the time to type "But the answer is not the
    expected one"
    What did you expect?
    What did you get?
    How do you expect anyone to help?

    Did you know that '1' != 1 ?
     
    Neil, May 16, 2006
    #16
    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. Brian J. Sayatovic

    Why constant ints in switch case expressions?

    Brian J. Sayatovic, Jul 2, 2003, in forum: Java
    Replies:
    22
    Views:
    21,929
    Dale King
    Jul 9, 2003
  2. Replies:
    36
    Views:
    1,547
    Chris Uppal
    Jun 6, 2005
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    980
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,226
    Smokey Grindel
    Dec 2, 2006
  5. Switch Within A Switch

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

Share This Page