473,406 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

const int still not enough for case statement?

I stumbled upon the gcc error "case label does not reduce to an integer constant" when trying to use a const int variable in a case statement.

Basically what is discussed in this thread: http://bytes.com/topic/c/answers/617...ant-expression

Nevertheless all the provided solutions don't work for me (except using #define apparently).

If I just have a simple program like:
Expand|Select|Wrap|Line Numbers
  1. int main( int argc, char** argv ) {
  2.  int switcher = 0;
  3.  const int testconst = 1337;
  4.  
  5.  switch( switcher ) {
  6.   case testconst:
  7.    break;
  8.  }
  9. }
it (3.3.5 as well as 4.3.3) still gives me that error.

What am I doing wrong?
Oct 13 '09 #1
5 7277
Hmm ... I guess it's because GCC doesn't allow that whereas g++ does (and why it worked in the other thread).
Oct 13 '09 #2
Banfa
9,065 Expert Mod 8TB
Is this C or C++ code?

it makes a difference, in C the const qualifier is really just a hint to the compiler that the variable can not be changed the symbol still represents a variable which can not be used where a constant value is required. You can not use such a variable as the size specifier when declaring an array either.

That is why in many C programs you tend to find actual constant values #defined rather than declared as const variables.

In C++ the const keyword has a much stronger meaning. It creates an actual constant value that on the whole can be used in switch cases and array declarations as long the variable has internal linkage (as opposed to external linkage).

A const with external linkage can not be used because its value has to be resolved by referring to the memory location in which it is stored, which can not be done at compile time, so it doesn't actually reduce to a constant value at compile time.
Oct 13 '09 #3
It is C code.

And I understand what you're saying, but I would rather not use a switch statement than having to use #defines only for that reason. (#defines prevent various compiler warnings/errors, such as type checking and so forth)

Would you see using enums (even if it's just one constant you want to use) as a reasonable workaround or solution?
Oct 13 '09 #4
Banfa
9,065 Expert Mod 8TB
You could use enums but they are little better than #defines except that they can only represent integer values.

While #defines are not type safe since C is less type safe than say C++ anyway I do not think you will find much advantage over using #defines rather than const int or enums.

However if you wish to give a more specific example of the sort of error you think wont be caught by use of #define (or a link) I will consider it.
Oct 13 '09 #5
donbock
2,426 Expert 2GB
@Harry2o
That might be true for C++, but are you sure there is a limitation like this for C? I can't think of any C compiler type checking warnings/errors that are fooled by #defines that expand to literal numbers.

Are you familliar with the C literal number suffix notation?
For instance: 24uL is an unsigned long constant with the value "24".
Oct 13 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Tom | last post by:
I would like to know if an .asp case statement can contain HTML elements. I am building an application that I would like to have dynamic choices. The dynamic part would be built in the a case...
6
by: ryan.mclean | last post by:
Hi all, first, let me preface this by saying that I am very new to sql server, coming from oracle. Here is my problem: I would like to have a case statement (similar to decode in oracle) that...
21
by: Andy | last post by:
Can someone tell me if the following Switch...Case construct is valid? I'm wanting to check for multiple values in the Case statement without explicitly listing each values. So for example, will...
6
by: deanfamily11 | last post by:
I've set up a case statement to have my program determine where on the Cartesian plane a point the user enters is located. I keep getting the C2051 error when I compile. Any help? #include...
2
by: Mark Mullins | last post by:
have code below: Function ClassColl(strClass As Variant, strColl As Variant) As String ' Comments : ' Parameters : strClass ' strColl ' Returns : String Description '...
12
by: rAinDeEr | last post by:
Hi, I have a table with 2 columns ** CREATE TABLE test (emp_num DECIMAL(7) NOT NULL,emp_name CHAR(10) NOT NULL) and i have inserted a number of records. ** Now, I want to insert a new...
1
by: microsoft.public.dotnet.languages.vb | last post by:
Hi All, I wanted to know whether this is possible to use multiple variables to use in the select case statement such as follows: select case dWarrExpDateMonth, dRetailDateMonth case...
7
by: sam_cit | last post by:
Hi Everyone, In the following code, i have a common action for three switch cases, is there any other better way to write the three values in a single case? Thanks in advance #include...
13
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...

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.