Why can't constants have commas?

P

Peter Ammon

My code obfuscator gave me this:

char buff[1, 9];

to which gcc retorted:

"ISO C90 forbids variable-size array 'buff'"

and checking the standard, it appears that commas are indeed forbidden
from being in a constant expression.

Why's that? Wouldn't it make more sense to allow commas as long as the
left and right operands are also constant expressions? Calling the
expression 1,9 "variable-size" is pretty silly.

-Peter
 
C

Chris Torek

... checking the standard, it appears that commas are indeed forbidden
from being in a constant expression.

Why's that? Wouldn't it make more sense to allow commas as long as the
left and right operands are also constant expressions? Calling the
expression 1,9 "variable-size" is pretty silly.

Asking for sense from the Standards Committees may be going a bit
overboard. :)

Seriously, even if the left operand is *not* constant, the
expression itself could still be a constant. For instance:

i = (printf("hello\n"), 2);

is certain to set i to 2. In this case, however, there is also
a required side-effect (writing "hello\n" to stdout). As such,
the parenthesized expression is clearly unsuitable for use as a
static initializer:

static int x = (printf("hello\n"), 2); /* WRONG */

but might be allowed as a constant in "more general" positions:

printf("I say ");
{
int i, x[(printf("hello\n"), 2)];
for (i = 0; i < sizeof x / sizeof *x; i++)
x = printf("Hello hello\n");
printf("I don't know why you say goodbye\n");
printf("I say hello\n");
}

So perhaps, once one starts down this road, a committee subgroup
might not be able to agree as to precisely what is allowed when. :)
 
A

Artie Gold

Peter said:
My code obfuscator gave me this:

char buff[1, 9];

to which gcc retorted:

"ISO C90 forbids variable-size array 'buff'"

and checking the standard, it appears that commas are indeed forbidden
from being in a constant expression.

Why's that? Wouldn't it make more sense to allow commas as long as the
left and right operands are also constant expressions? Calling the
expression 1,9 "variable-size" is pretty silly.

"1, 9" is an *expression* in C, not a constant.

If what you're trying to do is declare a multidimensional array, that
would be:

char buff[1][9];

Please look this up in whatever C book you have -- *and* (as you should
have done *before* posting) read the FAQ.

http://www.eskimo.com/~scs/C-faq/top.html

HTH,
--ag
 
E

E. Robert Tisdale

Peter said:
My code obfuscator gave me this:

char buff[1, 9];

to which gcc retorted:

"ISO C90 forbids variable-size array 'buff'"

and checking the standard, it appears that
commas are indeed forbidden from being in a constant expression.

Why's that? Wouldn't it make more sense to allow commas
as long as the left and right operands are also constant expressions?
Calling the expression 1,9 "variable-size" is pretty silly.
> cat buff.c
char buff[1, 9];
> gcc -Wall -std=c99 -pedantic -c buff.c
buff.c:1: error: parse error before ',' token
> gcc --version
gcc (GCC) 3.4.2
 
S

Stuart Gerchick

Peter Ammon said:
My code obfuscator gave me this:

char buff[1, 9];

to which gcc retorted:

"ISO C90 forbids variable-size array 'buff'"

and checking the standard, it appears that commas are indeed forbidden
from being in a constant expression.

Why's that? Wouldn't it make more sense to allow commas as long as the
left and right operands are also constant expressions? Calling the
expression 1,9 "variable-size" is pretty silly.

-Peter

In C the size of an array must be constant. 1,9 is not a constant.
 
P

Peter Ammon

Artie said:
Peter said:
My code obfuscator gave me this:

char buff[1, 9];

to which gcc retorted:

"ISO C90 forbids variable-size array 'buff'"

and checking the standard, it appears that commas are indeed forbidden
from being in a constant expression.

Why's that? Wouldn't it make more sense to allow commas as long as
the left and right operands are also constant expressions? Calling
the expression 1,9 "variable-size" is pretty silly.

"1, 9" is an *expression* in C, not a constant.

Thank you for this correction. What I really intended to say was
"constant expression," not constant.
If what you're trying to do is declare a multidimensional array, that
would be:

char buff[1][9];

Please look this up in whatever C book you have -- *and* (as you should
have done *before* posting) read the FAQ.

http://www.eskimo.com/~scs/C-faq/top.html

I cannot find any discussion of why expressions with commas cannot be
constant expressions in K&R, C Unleashed, or the FAQ.

-Peter
 
P

Peter Ammon

E. Robert Tisdale said:
Peter said:
My code obfuscator gave me this:

char buff[1, 9];

to which gcc retorted:

"ISO C90 forbids variable-size array 'buff'"

and checking the standard, it appears that
commas are indeed forbidden from being in a constant expression.

Why's that? Wouldn't it make more sense to allow commas
as long as the left and right operands are also constant expressions?
Calling the expression 1,9 "variable-size" is pretty silly.

cat buff.c
char buff[1, 9];
gcc -Wall -std=c99 -pedantic -c buff.c
buff.c:1: error: parse error before ',' token
gcc --version
gcc (GCC) 3.4.2

Curious regression; the error message emitted by my version (gcc 3.3)
seems much more informative.
 
P

Peter Ammon

Stuart said:
Peter Ammon said:
My code obfuscator gave me this:

char buff[1, 9];

to which gcc retorted:

"ISO C90 forbids variable-size array 'buff'"

and checking the standard, it appears that commas are indeed forbidden
from being in a constant expression.

Why's that? Wouldn't it make more sense to allow commas as long as the
left and right operands are also constant expressions? Calling the
expression 1,9 "variable-size" is pretty silly.

-Peter


In C the size of an array must be constant. 1,9 is not a constant.

1+9 is a constant expression. Why not have 1,9 a constant expression as
well?
 
D

dandelion

Peter Ammon said:
Stuart said:
My code obfuscator gave me this:

char buff[1, 9];

to which gcc retorted:

"ISO C90 forbids variable-size array 'buff'"

and checking the standard, it appears that commas are indeed forbidden
from being in a constant expression.

Why's that? Wouldn't it make more sense to allow commas as long as the
left and right operands are also constant expressions? Calling the
expression 1,9 "variable-size" is pretty silly.

-Peter


In C the size of an array must be constant. 1,9 is not a constant.

1+9 is a constant expression. Why not have 1,9 a constant expression as
well?

To what end? It would invariably evaluate to 9. Hence a comma in a constant
expression would seem a bit weird...
 
R

Robert Harris

Stuart said:
[snip]
In C the size of an array must be constant. 1,9 is not a constant.
No. See paragraph 6.7.5.2 of the standard: an array that isn't an object
with static storage duration can have a variable length declaration. But
1,9 is not a variable - it is an expression.

Robert
 
X

Xenos

Robert Harris said:
Stuart said:
[snip]
In C the size of an array must be constant. 1,9 is not a constant.
No. See paragraph 6.7.5.2 of the standard: an array that isn't an object
with static storage duration can have a variable length declaration. But
1,9 is not a variable - it is an expression.

Robert

"variable length" does not mean the expression has to literally be a
variable.
 
X

xarax

Peter Ammon said:
Stuart said:
My code obfuscator gave me this:

char buff[1, 9];

to which gcc retorted:

"ISO C90 forbids variable-size array 'buff'"

and checking the standard, it appears that commas are indeed forbidden
from being in a constant expression.

Why's that? Wouldn't it make more sense to allow commas as long as the
left and right operands are also constant expressions? Calling the
expression 1,9 "variable-size" is pretty silly.

-Peter


In C the size of an array must be constant. 1,9 is not a constant.

1+9 is a constant expression. Why not have 1,9 a constant expression as
well?

If you had something like this:

enum stuff { gork = (1,9) };

void poo(void)
{
char buff[gork];
}

Would that be legal? If so, then substituting
(1,9) for gork in the buff declaration should
also be legal.


--
----------------------------
Jeffrey D. Smith
Farsight Systems Corporation
24 BURLINGTON DRIVE
LONGMONT, CO 80501-6906
http://www.farsight-systems.com
z/Debug debugs your Systems/C programs running on IBM z/OS for FREE!
 
A

Arthur J. O'Dwyer

[big snip]
static int x = (printf("hello\n"), 2); /* WRONG */

Yeah, well, in this case Peter isn't asking for it to be constant;
function calls are definitely not constant initializers, whether in
static-variable initializations or in array sizes. But the sequence
<const-expression> , <const-expression>
never has weird side effects, so that's not a problem.

I thought maybe it had something to do with the fact that the comma
operator introduces a sequence point---what does it mean to have a
sequence point in the middle of a variable declaration?! But it appears
that C has no problem with the sequence points in

int a[1&&9];
int b[1||9];

Oh, and commas would create slightly icky interactions in compound
initializers and in 'enum' declarations, but that could be handled the
same way it's handled in function argument lists---have the "top-level"
commas be separators and the "lower-level" commas be operators.

enum { foo = 1, bar };
enum { baz = (1, bar) };

So I have absolutely no idea what's going on.

-Arthur
 
B

Ben Pfaff

Peter Ammon said:
checking the standard, it appears that commas are indeed forbidden
from being in a constant expression.

Why's that? Wouldn't it make more sense to allow commas as long as
the left and right operands are also constant expressions?

The comma operator is only useful if and when its left operand
expression has a side-effect. Constant expressions do not have
side effects. Therefore, the comma operator is not useful in
constant expressions and it might as well be forbidden to avoid
confusion.
 
K

Kevin Bracey

In message <[email protected]>
"Arthur J. O'Dwyer said:
Yeah, well, in this case Peter isn't asking for it to be constant;
function calls are definitely not constant initializers, whether in
static-variable initializations or in array sizes. But the sequence
<const-expression> , <const-expression>
never has weird side effects, so that's not a problem.

I thought maybe it had something to do with the fact that the comma
operator introduces a sequence point---what does it mean to have a
sequence point in the middle of a variable declaration?! But it appears
that C has no problem with the sequence points in

int a[1&&9];
int b[1||9];

I have heard the theory that it's a specific and deliberate design decision,
with the express purpose of making the beginner's error

int c[1,9];

a constraint violation. Otherwise, silly code like this would actually
compile and run:

int a[5,5], x, y;

for (x=0; x<5; x++)
for (y=0; y<5; y++)
a[x,y] = x*y;
 
D

Dan Pop

Peter Ammon said:
Stuart said:
Peter Ammon <[email protected]> wrote in message
My code obfuscator gave me this:

char buff[1, 9];

to which gcc retorted:

"ISO C90 forbids variable-size array 'buff'"

and checking the standard, it appears that commas are indeed forbidden
from being in a constant expression.

Why's that? Wouldn't it make more sense to allow commas as long as the
left and right operands are also constant expressions? Calling the
expression 1,9 "variable-size" is pretty silly.

-Peter


In C the size of an array must be constant. 1,9 is not a constant.

1+9 is a constant expression. Why not have 1,9 a constant expression as
well?

To what end? It would invariably evaluate to 9.

Just as 1+9 invariably evaluates to 10, yet it is a valid constant
expression and can be used in an array declaration.
Hence a comma in a constant expression would seem a bit weird...

Bogus conclusion.

Dan
 
B

Bart

My code obfuscator gave me this:

char buff[1, 9];

to which gcc retorted:

"ISO C90 forbids variable-size array 'buff'"


I'd imagine an Integer constant is needed here.

What is the "1" in 1,9 doing here anyway? Declaring an array indexed
1..9? Declaring a 2 dimensional array [0..1] [0..8]? I'm not surprised
the compiler complained, so would I!

I'm not too knowledgeable about C so not sure what A,B might mean
except evaluate A then B, or vice versa. But that doesn't explain
meaning of [1,9] in this context.

Bart
 
O

Old Wolf

Peter Ammon said:
E. Robert Tisdale said:
Peter said:
My code obfuscator gave me this:
char buff[1, 9];
to which gcc retorted:

"ISO C90 forbids variable-size array 'buff'"
cat buff.c
char buff[1, 9];
gcc -Wall -std=c99 -pedantic -c buff.c
buff.c:1: error: parse error before ',' token
gcc --version
gcc (GCC) 3.4.2

Curious regression; the error message emitted by my version (gcc 3.3)
seems much more informative.

I think the difference is, Trollsdale's is at file scope
whereas yours was inside a function (where VLA is permitted in C99).

BTW this whole thread shows how ridiculous VLA is, the sooner
the standards committee retracts their mistake, the better.
 
K

Keith Thompson

BTW this whole thread shows how ridiculous VLA is, the sooner
the standards committee retracts their mistake, the better.

Don't hold your breath. VLAs have been implemented, and code has been
written that depends on them. The committee isn't going to break
existing code like that.
 
P

Peter Ammon

dandelion said:
Stuart said:
Peter Ammon <[email protected]> wrote in message
My code obfuscator gave me this:

char buff[1, 9];

to which gcc retorted:

"ISO C90 forbids variable-size array 'buff'"

and checking the standard, it appears that commas are indeed forbidden

from being in a constant expression.

Why's that? Wouldn't it make more sense to allow commas as long as the
left and right operands are also constant expressions? Calling the
expression 1,9 "variable-size" is pretty silly.

-Peter


In C the size of an array must be constant. 1,9 is not a constant.

1+9 is a constant expression. Why not have 1,9 a constant expression as
well?


To what end? It would invariably evaluate to 9. Hence a comma in a constant
expression would seem a bit weird...

Surely no weirder than the unary + operator!
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top