switch statement in C

K

koolj96825

{
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
 
I

Ian Collins

{
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.
 
K

Keith Thompson

{
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/>.
 
M

Martin Ambuhl

{
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.
 
K

Keith Thompson

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


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.
 
K

koolj96825

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.
 
K

koolj96825

Keith said:
I wrote the above. Please don't snip attribution lines (i.e., lines

--
Keith Thompson (The_Other_Keith) (e-mail address removed) <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.
 
C

CBFalconer

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
 
R

Richard Bos

Keith Thompson said:
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
 
D

Dave Hansen

On Apr 11, 5:53 am, (e-mail address removed) (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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top