473,513 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

switch statement in C



{
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 '07 #1
10 1788
ko********@yahoo.com 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.
Apr 10 '07 #2
ko********@yahoo.com 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) ks***@mib.org <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"
Apr 10 '07 #3
ko********@yahoo.com 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.
Apr 10 '07 #4
Martin Ambuhl <ma*****@earthlink.netwrites:
ko********@yahoo.com 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) ks***@mib.org <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"
Apr 10 '07 #5
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 10 '07 #6
ko********@yahoo.com 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 <fo*@bar.comwrites:".

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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"
Apr 11 '07 #7

Keith Thompson wrote:
I wrote the above. Please don't snip attribution lines (i.e., lines
of the form "So-and-so <fo*@bar.comwrites:".

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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 '07 #8
ko********@yahoo.com wrote:
Keith Thompson wrote:
>I wrote the above. Please don't snip attribution lines (i.e., lines
of the form "So-and-so <fo*@bar.comwrites:".

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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

Apr 11 '07 #9
Keith Thompson <ks***@mib.orgwrote:
Martin Ambuhl <ma*****@earthlink.netwrites:
ko********@yahoo.com 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
Apr 11 '07 #10
On Apr 11, 5:53 am, r...@hoekstra-uitgeverij.nl (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
Apr 11 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
10882
by: Myster Ious | last post by:
Polymorphism replaces switch statements, making the code more compact/readable/maintainable/OO whatever, fine! What I understand, that needs to be done at the programming level, is this: a...
35
8292
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except...
17
2792
by: prafulla | last post by:
Hi all, I don't have a copy of C standard at hand and so anyone of you can help me. I have always wondered how switch statements are so efficient in jumping to the right case (if any)? Can...
5
6098
by: Alvin Bruney | last post by:
Is a switch more efficient than an if statement? I observe thru the debugger that a switch statement jumps directly to its case handler where as an if statement examines all conditions...
3
19727
by: pgraeve | last post by:
I am a convert from VB to C# so bear with me on this "conversion" question C# switch statement seems to be the closest relative to VB's Select Case. I used VB's Select Case statement liberally. ...
27
5592
by: Yuriy Solodkyy | last post by:
Hi VS 2005 beta 2 successfully compiles the following: using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program {
25
18351
by: v4vijayakumar | last post by:
'continue' within switch actually associated with the outer 'while' loop. Is this behavior protable? int ch = '\n'; while (true) { switch(ch) { case '\n': cout << "test"; continue; } }
12
12258
by: | last post by:
Is it fine to call another method from Switch? Eg. Switch (stringVar) { case ("a"): somVar = "whatever"; Another_Method(); //call another method return;
2
1444
by: Phillip B Oldham | last post by:
What would be the optimal/pythonic way to subject an object to a number of tests (based on the object's attributes) and redirect program flow? Say I had the following: pets = {'name':...
13
11771
by: Satya | last post by:
Hi everyone, This is the first time iam posting excuse me if iam making any mistake. My question is iam using a switch case statement in which i have around 100 case statements to compare. so...
0
7259
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7158
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7380
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5683
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5085
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1592
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.