By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,359 Members | 1,481 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,359 IT Pros & Developers. It's quick & easy.

Why can't constants have commas?

P: n/a
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
Nov 14 '05 #1
Share this Question
Share on Google+
27 Replies


P: n/a
Peter Ammon wrote:
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
--
Artie Gold -- Austin, Texas

"If you don't think it matters, you're not paying attention."
Nov 14 '05 #2

P: n/a
In article <news:cl**********@news.apple.com>
Peter Ammon <pe*********@rocketmail.com> wrote:
... 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[i] = 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. :-)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #3

P: n/a
Peter Ammon wrote:
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
Nov 14 '05 #4

P: n/a
Peter Ammon <pe*********@rocketmail.com> wrote in message news:<cl**********@news.apple.com>...
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.
Nov 14 '05 #5

P: n/a
Artie Gold wrote:
Peter Ammon wrote:
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
--
Pull out a splinter to reply.
Nov 14 '05 #6

P: n/a
E. Robert Tisdale wrote:
Peter Ammon wrote:
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.

--
Pull out a splinter to reply.
Nov 14 '05 #7

P: n/a
Stuart Gerchick wrote:
Peter Ammon <pe*********@rocketmail.com> wrote in message news:<cl**********@news.apple.com>...
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?

--
Pull out a splinter to reply.
Nov 14 '05 #8

P: n/a

"Peter Ammon" <ge******@splintermac.com> wrote in message
news:%h*******************@newssvr21.news.prodigy. com...
Stuart Gerchick wrote:
Peter Ammon <pe*********@rocketmail.com> wrote in message news:<cl**********@news.apple.com>...
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...

Nov 14 '05 #9

P: n/a
Stuart Gerchick wrote:
[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
Nov 14 '05 #10

P: n/a
"Peter Ammon" <ge******@splintermac.com> wrote in message
news:%h*******************@newssvr21.news.prodigy. com...
Stuart Gerchick wrote:
Peter Ammon <pe*********@rocketmail.com> wrote in message news:<cl**********@news.apple.com>...
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!

Nov 14 '05 #11

P: n/a

"Robert Harris" <ro*****************@blueyonder.co.uk> wrote in message
news:6h*******************@fe1.news.blueyonder.co. uk...
Stuart Gerchick wrote:
[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.
Nov 14 '05 #12

P: n/a

On Wed, 28 Oct 2004, Chris Torek wrote:

Peter Ammon <pe*********@rocketmail.com> wrote:
... 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.

[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
Nov 14 '05 #13

P: n/a
Peter Ammon <pe*********@rocketmail.com> writes:
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.
--
Ben Pfaff
email: bl*@cs.stanford.edu
web: http://benpfaff.org
Nov 14 '05 #14

P: n/a
In message <Pi**********************************@unix40.andre w.cmu.edu>
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote:
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;

--
Kevin Bracey, Principal Software Engineer
Tematic Ltd Tel: +44 (0) 1223 503464
182-190 Newmarket Road Fax: +44 (0) 1728 727430
Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
Nov 14 '05 #15

P: n/a
In <41***********************@dreader18.news.xs4all.n l> "dandelion" <da*******@meadow.net> writes:

"Peter Ammon" <ge******@splintermac.com> wrote in message
news:%h*******************@newssvr21.news.prodigy .com...
Stuart Gerchick wrote:
> Peter Ammon <pe*********@rocketmail.com> wrote in messagenews:<cl**********@news.apple.com>... >
>>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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #16

P: n/a
On Wed, 27 Oct 2004 16:55:46 -0700, Peter Ammon
<pe*********@rocketmail.com> wrote:
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
Nov 14 '05 #17

P: n/a
Peter Ammon <ge******@splintermac.com> wrote:
E. Robert Tisdale wrote:
Peter Ammon wrote:
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.
Nov 14 '05 #18

P: n/a
ol*****@inspire.net.nz (Old Wolf) writes:
[...]
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.

--
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.
Nov 14 '05 #19

P: n/a
dandelion wrote:
"Peter Ammon" <ge******@splintermac.com> wrote in message
news:%h*******************@newssvr21.news.prodigy. com...
Stuart Gerchick wrote:
Peter Ammon <pe*********@rocketmail.com> wrote in message
news:<cl**********@news.apple.com>...
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!

--
Pull out a splinter to reply.
Nov 14 '05 #20

P: n/a
In <1h********************************@4ax.com> Bart <48**@freeuk.com> writes:
On Wed, 27 Oct 2004 16:55:46 -0700, Peter Ammon
<pe*********@rocketmail.com> wrote:
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.


What part of "My code obfuscator gave me this" was too difficult for you
to understand?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #21

P: n/a
In <ln************@nuthaus.mib.org> Keith Thompson <ks***@mib.org> writes:
ol*****@inspire.net.nz (Old Wolf) writes:
[...]
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.


I kinda doubt it, given the "wide" availability of conforming C99
implementations.

Most of the code using VLAs is GNU C code using GNU C VLAs, rather than
C99 code using C99 VLAs (the two kinds of VLAs are not semantically
equivalent).
The committee isn't going to break existing code like that.


VLAs should have been introduced in C89. They are the right solution
for any kind of code doing matrix processing in C, but they're coming
far too late.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #22

P: n/a
On 29 Oct 2004 16:56:03 GMT, Da*****@cern.ch (Dan Pop) wrote:
What part of "My code obfuscator gave me this" was too difficult for you
to understand?

Dan


Actually, I saw the 'obfuscator' bit after I posted my message.. Even
so, I think the content still stands.

It seems that ' char buff[a,b]' just means 'buff[b]' in C? In which
case I would expect the compiler to remark on the 'a,' bit, which it
has (with a misleading error message). And also in this case a
'de-obfuscator' could just remove the extraneous code.

(I also thought that 'char buff...' seemed quite sensible output of
the obfuscator..)

Bart.
Nov 14 '05 #23

P: n/a

"Dan Pop" <Da*****@cern.ch> wrote in message
news:cl***********@sunnews.cern.ch...
In <41***********************@dreader18.news.xs4all.n l> "dandelion"

<da*******@meadow.net> writes:
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.


Which is at least something different than I wrote.
Hence a comma in a constant expression would seem a bit weird...


Bogus conclusion.


Not a conclusion at all. Merely a statement of my view that such a
declaration would be a bit weird.

So your "Bogus conclusion" conclusion is Bogus.

Thankyouverymuch...
Nov 14 '05 #24

P: n/a

"Peter Ammon" <ge******@splintermac.com> wrote in message
news:yH*******************@newssvr14.news.prodigy. com...
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!


About that weird, yes.
Nov 14 '05 #25

P: n/a
Kevin Bracey <ke**********@tematic.com> wrote in message news:<6c****************@tematic.com>...
In message <Pi**********************************@unix40.andre w.cmu.edu>
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote:
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;

But think of the possibilities that could open up for the IOCCC...
Nov 14 '05 #26

P: n/a
In <4p********************************@4ax.com> Bart <48**@freeuk.com> writes:
It seems that ' char buff[a,b]' just means 'buff[b]' in C?
It *would*, if the comma operator were allowed in constant expressions.
In which
case I would expect the compiler to remark on the 'a,' bit, which it
has (with a misleading error message).
The compiler must complain, because a,b is not a valid constant
expression. It need not generate a meaningful complaint, however :-)
And also in this case a
'de-obfuscator' could just remove the extraneous code.
Most of the changes performed by obfuscators can be undone.
(I also thought that 'char buff...' seemed quite sensible output of
the obfuscator..)


Any obfuscator that doesn't translate all local names in combinations
of two characters (O and 0 or 1 and l) is not a real obfuscator.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #27

P: n/a
On Thu, 28 Oct 2004 13:57:48 GMT, "xarax" <xa***@email.com> wrote:
"Peter Ammon" <ge******@splintermac.com> wrote in message
news:%h*******************@newssvr21.news.prodigy. com...
Stuart Gerchick wrote: <snip>
In C the size of an array must be constant. 1,9 is not a constant.

(For C99 no longer for automatic arrays, as already noted.)
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.


No. enum values also must be (integer) constant expressions.

- David.Thompson1 at worldnet.att.net
Nov 14 '05 #28

This discussion thread is closed

Replies have been disabled for this discussion.