gcc: error: case label does not reduce to an integer constant

B

Ben Pfaff

Mark McIntyre said:
Er, these are all literals (as far as I'm concerned). I suspect we have a
nomenclature disagreement.

You'll have to define "literal" for the rest of us then. The
standard only uses it in the phrases "string literal" and
"compound literal".
 
K

Keith Thompson

Mark McIntyre said:
Thats a literal in my book.

Get a new book.

As Ben Pfaff points out, the standard only uses the term in the
phrases "string literal" and "compound literal". Based on the usage
of the term in other languages, I tend to think of a literal as a
single token representing a constant value, what C usually calls a
"constant" (integer constant, character constant, etc.) (though that
excludes C's compound literals).

The term is admittedly ambiguous, but I suspect you're the only person
around here who's going to think that 2+2 is a "literal". The
standard has a perfectly good term for the concept we're discussing,
"constant expression".
 
J

Joe Wright

Keith said:
Get a new book.

As Ben Pfaff points out, the standard only uses the term in the
phrases "string literal" and "compound literal". Based on the usage
of the term in other languages, I tend to think of a literal as a
single token representing a constant value, what C usually calls a
"constant" (integer constant, character constant, etc.) (though that
excludes C's compound literals).

The term is admittedly ambiguous, but I suspect you're the only person
around here who's going to think that 2+2 is a "literal". The
standard has a perfectly good term for the concept we're discussing,
"constant expression".

Imagine my surprise..

/* Constant Literal Expression? */
#include <stdio.h>
int main(void) {
int i;
for (i = 0; i < 4; ++i) {
switch(i+1) {
case 1: puts("case 1"); break;
case 1+1: puts("case 2"); break;
case 1+1+1: puts("case 3"); break;
case 2+2: puts("case 4"); break;
}
}
return 0;
}

...prints

case 1
case 2
case 3
case 4
 
L

lonny.nettnay

I'm using Google Groups. It must have dropped the space.
Still liked the program. :)

Lonny
 
L

Lawrence Kirby

On Thu, 30 Dec 2004 01:07:17 +0000, Mark McIntyre wrote:

....
Er, these are all literals (as far as I'm concerned). I suspect we have a
nomenclature disagreement.

I can understand why you might call something like 5 or 'X' a literal (the
standard calls them constants). Given that 3<<2 is an expression that
happens to contain 2 literals. I cna see no sense in which the overall
expression could be called a literal, unless you are using the term
"literal" to mean "constant expression". But that is a non-obvious
association.

Lawrence
 
M

Mark McIntyre

You'll have to define "literal" for the rest of us then. The
standard only uses it in the phrases "string literal" and
"compound literal".

So? Last time I looked, the Standard wasn't a dictionary of computing
terms.

FWIW as far as I think of it, a literal value is one typed in, literally.
 
K

Keith Thompson

Mark McIntyre said:
So? Last time I looked, the Standard wasn't a dictionary of computing
terms.

No, it isn't, but there are plenty of dictionaries of computing terms.
Do you know of one that defines "literal" in a manner consistent with
your usage?
FWIW as far as I think of it, a literal value is one typed in, literally.

I don't see how that makes 3<<2 a literal and x+3 not a literal. In
any case, you're using the term in a way that nobody else does. I'm
not absolutely saying that you're wrong and everyone else is right,
but assuming that you're right and everyone else is wrong is not
likely to help communication, which is after all the point of this
newsgroup.

Does your concept of "literal" match what the C standard calls a
"constant expression"? If so, perhaps we could just all agree to call
them "constant expressions" and move on.
 
D

dcorbit

If you know what all the cases are, you can do a perfect hash for the
input strings and then switch on the hash.

This is the C-FAQ version:

20.17: Is there a way to switch on strings?

A: Not directly. Sometimes, it's appropriate to use a separate
function to map strings to integer codes, and then switch on
those. Otherwise, of course, you can fall back on strcmp() and
a conventional if/else chain. See also questions 10.12, 20.18,
and 20.29.

References: K&R1 Sec. 3.4 p. 55; K&R2 Sec. 3.4 p. 58; ISO
Sec. 6.6.4.2; H&S Sec. 8.7 p. 248.
 
M

Mark McIntyre

I don't see how that makes 3<<2 a literal and x+3 not a literal.

x isn't a value. Its a symbol.
In any case, you're using the term in a way that nobody else does.

I should have defined my meaning in my original post. My mistake.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top