Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Why can't constants have commas?

Reply
Thread Tools

Why can't constants have commas?

 
 
Peter Ammon
Guest
Posts: n/a
 
      10-27-2004
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
 
Reply With Quote
 
 
 
 
Chris Torek
Guest
Posts: n/a
 
      10-28-2004
In article <news:clpci2$hkg$>
Peter Ammon <> 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 (40°39.22'N, 111°50.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.
 
Reply With Quote
 
 
 
 
Artie Gold
Guest
Posts: n/a
 
      10-28-2004
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."
 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      10-28-2004
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
 
Reply With Quote
 
Stuart Gerchick
Guest
Posts: n/a
 
      10-28-2004
Peter Ammon <> wrote in message news:<clpci2$hkg$>...
> 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.
 
Reply With Quote
 
Peter Ammon
Guest
Posts: n/a
 
      10-28-2004
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.
 
Reply With Quote
 
Peter Ammon
Guest
Posts: n/a
 
      10-28-2004
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.
 
Reply With Quote
 
Peter Ammon
Guest
Posts: n/a
 
      10-28-2004
Stuart Gerchick wrote:
> Peter Ammon <> wrote in message news:<clpci2$hkg$>...
>
>>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.
 
Reply With Quote
 
dandelion
Guest
Posts: n/a
 
      10-28-2004

"Peter Ammon" <> wrote in message
news:%h3gd.36079$. com...
> Stuart Gerchick wrote:
> > Peter Ammon <> wrote in message

news:<clpci2$hkg$>...
> >
> >>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...



 
Reply With Quote
 
Robert Harris
Guest
Posts: n/a
 
      10-28-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What use do you have in using constants over variables? amun25dringer11@gmail.com C++ 6 12-08-2007 02:49 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Why are string constants null on postback? Brad Wood ASP .Net 1 07-15-2005 08:43 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57