switch statement in C

Discussion in 'C Programming' started by koolj96825@yahoo.com, Apr 10, 2007.

  1. Guest

    {
    const int state_black = 1 ;
    const int state_white = 2 ;

    int state ;

    state = state_black ;

    switch(state)
    {

    case state_black:
    break ;

    case state_white:
    break;
    }
    }

    why is this not acceptable?
    why can't declaring the variable const make it suitable to use with
    the case label?

    Thanks in advance
    , Apr 10, 2007
    #1
    1. Advertising

  2. Ian Collins Guest

    wrote:
    >
    > {
    > const int state_black = 1 ;
    > const int state_white = 2 ;
    >
    > int state ;
    >
    > state = state_black ;
    >
    > switch(state)
    > {
    >
    > case state_black:
    > break ;
    >
    > case state_white:
    > break;
    > }
    > }
    >
    > why is this not acceptable?
    > why can't declaring the variable const make it suitable to use with
    > the case label?
    >

    In C, for some reason a const variable isn't classed as a compile time
    constant. Which is why we have to resort to enums and smelly #defines.

    --
    Ian Collins.
    Ian Collins, Apr 10, 2007
    #2
    1. Advertising

  3. writes:
    > {
    > const int state_black = 1 ;
    > const int state_white = 2 ;
    >
    > int state ;
    >
    > state = state_black ;
    >
    > switch(state)
    > {
    >
    > case state_black:
    > break ;
    >
    > case state_white:
    > break;
    > }
    > }
    >
    > why is this not acceptable?
    > why can't declaring the variable const make it suitable to use with
    > the case label?


    See question 11.8 in the comp.lang.c FAQ, <http://www.c-faq.com/>.

    --
    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."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Apr 10, 2007
    #3
  4. wrote:
    >
    > {
    > const int state_black = 1 ;
    > const int state_white = 2 ;
    >
    > int state ;
    >
    > state = state_black ;
    >
    > switch(state)
    > {
    >
    > case state_black:
    > break ;
    >
    > case state_white:
    > break;
    > }
    > }
    >
    > why is this not acceptable?


    state_black and state_white are variables, not constants, and switch
    labels must use compile-time constants.

    > why can't declaring the variable const make it suitable to use with
    > the case label?


    Because the variable is still a variable, and not a compile-time constant.
    Martin Ambuhl, Apr 10, 2007
    #4
  5. Martin Ambuhl <> writes:
    > wrote:
    >> {
    >> const int state_black = 1 ;
    >> const int state_white = 2 ;
    >> int state ;
    >> state = state_black ;
    >> switch(state)
    >> {
    >> case state_black:
    >> break ;
    >> case state_white:
    >> break;
    >> }
    >> }
    >> why is this not acceptable?

    >
    > state_black and state_white are variables, not constants, and switch
    > labels must use compile-time constants.
    >
    >> why can't declaring the variable const make it suitable to use with
    >> the case label?

    >
    > Because the variable is still a variable, and not a compile-time constant.


    state_black and state_white are "variables", sort of, except that they
    aren't allowed to vary.

    I think using the term "variable" to something that can't legally be
    modified may be a bit misleading. (A cast can be used to get around
    the "const" qualification and attempt to modify it, but any attempt to
    do so invokes undefined behavior.)

    The standard uses the term "object", not "variable".

    As question 11.8 in the FAQ explains, the "const" qualifier really
    means "read-only", not "constant". Quoting the FAQ:

    The const qualifier really means ``read-only''; an object so
    qualified is a run-time object which cannot (normally) be assigned
    to. The value of a const-qualified object is therefore not a
    constant expression in the full sense of the term, and cannot be
    used for array dimensions, case labels, and the like. (C is unlike
    C++ in this regard.) When you need a true compile-time constant,
    use a preprocessor #define (or perhaps an enum).

    For the above, it would have made more sense to declare:

    enum state { state_black = 1, state_white = 2 };

    The "= 1" and "= 2" could be omitted if you don't care about the
    values, as long as they're distinct.

    --
    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."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Apr 10, 2007
    #5
  6. Guest

    > For the above, it would have made more sense to declare:
    >
    > enum state { state_black = 1, state_white = 2 };
    >
    > The "= 1" and "= 2" could be omitted if you don't care about the
    > values, as long as they're distinct.


    I like this solution. I will use it. Thanks.

    The example code is not an actual case, just an example.
    , Apr 11, 2007
    #6
  7. writes:
    >> For the above, it would have made more sense to declare:
    >>
    >> enum state { state_black = 1, state_white = 2 };
    >>
    >> The "= 1" and "= 2" could be omitted if you don't care about the
    >> values, as long as they're distinct.

    >
    > I like this solution. I will use it. Thanks.

    [...]

    You're welcome.

    I wrote the above. Please don't snip attribution lines (i.e., lines
    of the form "So-and-so <> writes:".

    --
    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."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Apr 11, 2007
    #7
  8. Guest

    Keith Thompson wrote:
    > I wrote the above. Please don't snip attribution lines (i.e., lines
    > of the form "So-and-so <> writes:".
    >
    > --
    > 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."
    > -- Antony Jay and Jonathan Lynn, "Yes Minister"


    Oops, it was an unintensional oversight. Sorry about that. Well, at
    least you'll hear less of me in four days. My internet connection
    runs out. I'll need to sponge for a while.

    Have a good day.
    , Apr 11, 2007
    #8
  9. CBFalconer Guest

    wrote:
    > Keith Thompson wrote:
    >
    >> I wrote the above. Please don't snip attribution lines (i.e., lines
    >> of the form "So-and-so <> writes:".
    >>
    >> --
    >> 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."
    >> -- Antony Jay and Jonathan Lynn, "Yes Minister"

    >
    > Oops, it was an unintensional oversight. Sorry about that. Well, at
    > least you'll hear less of me in four days. My internet connection
    > runs out. I'll need to sponge for a while.


    Please snip sigs. This is the part following the "-- " line.

    --
    <http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
    <http://www.securityfocus.com/columnists/423>
    <http://www.aaxnet.com/editor/edit043.html>

    "A man who is right every time is not likely to do very much."
    -- Francis Crick, co-discover of DNA
    "There is nothing more amazing than stupidity in action."
    -- Thomas Matthews


    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Apr 11, 2007
    #9
  10. Richard Bos Guest

    Keith Thompson <> wrote:

    > Martin Ambuhl <> writes:
    > > wrote:
    > >> why can't declaring the variable const make it suitable to use with
    > >> the case label?

    > >
    > > Because the variable is still a variable, and not a compile-time constant.

    >
    > state_black and state_white are "variables", sort of, except that they
    > aren't allowed to vary.
    >
    > I think using the term "variable" to something that can't legally be
    > modified may be a bit misleading.


    Programming law #14: variables won't; constants aren't.

    Richard
    Richard Bos, Apr 11, 2007
    #10
  11. Dave Hansen Guest

    On Apr 11, 5:53 am, (Richard Bos) wrote:
    [...]
    > Programming law #14: variables won't; constants aren't.


    "The primary purpose of the DATA statement is to give names to
    constants; instead of referring to pi as 3.141592653589793 at every
    appearance, the variable PI can be given that value with a DATA
    statement and used instead of the longer form of the constant. This
    also simplifies modifying the program, should the value of pi change."
    -Early FORTRAN manual for Xerox Computers

    Regards,

    -=Dave
    Dave Hansen, Apr 11, 2007
    #11
    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. _
    Replies:
    1
    Views:
    410
    Enrique
    Feb 19, 2004
  2. vic

    switch case statement

    vic, Mar 3, 2004, in forum: Java
    Replies:
    9
    Views:
    5,752
  3. Replies:
    21
    Views:
    1,028
    Giannis Papadopoulos
    Aug 2, 2005
  4. bthumber
    Replies:
    5
    Views:
    405
    Alexey Smirnov
    Jan 29, 2009
  5. Switch Within A Switch

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

Share This Page