473,799 Members | 3,098 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

const int still not enough for case statement?

6 New Member
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 7307
Harry2o
6 New Member
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 Recognized Expert Moderator Expert
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
Harry2o
6 New Member
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 Recognized Expert Moderator Expert
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 Recognized Expert Top Contributor
@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
2371
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 statment that will output based upon the value of the option evaluated. This example only shows one case statement but there are eight case statements. Thanks for any insight! Tom If Not objRs.EOF Then
6
20075
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 will test a query for a null value. Here is my statement: SELECT CASE (SELECT MAX(SEQ) + 1
21
7657
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 case 1-35: work? If this would work, will it consider all numbers between 1 and 35 inclusive of 1 and 35? Please let me know if the following will work below: switch(state){ case 1-35: case 37:
6
4703
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 <iomanip> #include <iostream> using namespace std;
2
3845
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 ' Created : 2-5-2004 - Mark Mullins ' Modified :
12
21093
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 record (3232,'Raindeer') based on the condition that the
1
21691
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 "01" : dWarrExpDateMonth="Jan" : dRetailDateMonth="Jan" case "02" : dWarrExpDateMonth="Feb" : dRetailDateMonth="Feb" End Select
7
3541
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 <stdio.h>
13
11862
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 just curious to find out is it effective to use this method?? or is there is any other alternative method present so that execution time and code size can be reduced?? Thanks in advance.
0
9687
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9541
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10482
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10251
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10225
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9072
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7564
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4139
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 we have to send another system

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.