Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > switch statement in C

Reply
Thread Tools

switch statement in C

 
 
koolj96825@yahoo.com
Guest
Posts: n/a
 
      04-10-2007


{
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

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      04-10-2007
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.
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      04-10-2007
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) kst- <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"
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      04-10-2007
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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-10-2007
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) kst- <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"
 
Reply With Quote
 
koolj96825@yahoo.com
Guest
Posts: n/a
 
      04-10-2007
> 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.

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-11-2007
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) kst- <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"
 
Reply With Quote
 
koolj96825@yahoo.com
Guest
Posts: n/a
 
      04-11-2007

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) kst- <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.

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      04-11-2007
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) kst- <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

 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      04-11-2007
Keith Thompson <kst-> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
My Switch statement get stuck on the last statement bthumber ASP .Net 5 01-29-2009 07:56 PM
if statement that, when false, skips first statement in its block, executes second? Jay McGavren Java 11 01-16-2006 05:49 PM
How do I do a conditional statement in a constant statement? tkvhdl@gmail.com VHDL 3 12-16-2005 06:13 PM
Which of switch statement and if-else statement takes less time to execute? swaroophr@gmail.com C Programming 21 08-02-2005 09:24 AM
exec "statement" VS. exec "statement" in globals(), locals() tedsuzman Python 2 07-21-2004 08:41 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57