Why can't constants have commas?

Discussion in 'C Programming' started by Peter Ammon, Oct 28, 2004.

  1. Peter Ammon

    Peter Ammon Guest

    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
     
    Peter Ammon, Oct 28, 2004
    #1
    1. Advertising

  2. Peter Ammon

    Chris Torek Guest

    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 = 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.
     
    Chris Torek, Oct 28, 2004
    #2
    1. Advertising

  3. Peter Ammon

    Artie Gold Guest

    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."
     
    Artie Gold, Oct 28, 2004
    #3
  4. 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
     
    E. Robert Tisdale, Oct 28, 2004
    #4
  5. 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.
     
    Stuart Gerchick, Oct 28, 2004
    #5
  6. Peter Ammon

    Peter Ammon Guest

    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.
     
    Peter Ammon, Oct 28, 2004
    #6
  7. Peter Ammon

    Peter Ammon Guest

    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.
     
    Peter Ammon, Oct 28, 2004
    #7
  8. Peter Ammon

    Peter Ammon Guest

    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.
     
    Peter Ammon, Oct 28, 2004
    #8
  9. Peter Ammon

    dandelion Guest

    "Peter Ammon" <> wrote in message
    news:%h3gd.36079$...
    > 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...
     
    dandelion, Oct 28, 2004
    #9
  10. 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
     
    Robert Harris, Oct 28, 2004
    #10
  11. Peter Ammon

    Xenos Guest

    "Robert Harris" <> wrote in message
    news:6h6gd.59590$...
    > 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.
     
    Xenos, Oct 28, 2004
    #11
  12. Peter Ammon

    xarax Guest

    "Peter Ammon" <> wrote in message
    news:%h3gd.36079$...
    > 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?


    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!
     
    xarax, Oct 28, 2004
    #12
  13. On Wed, 28 Oct 2004, Chris Torek wrote:
    >
    > 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.


    [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
     
    Arthur J. O'Dwyer, Oct 28, 2004
    #13
  14. Peter Ammon

    Ben Pfaff Guest

    Peter Ammon <> 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:
    web: http://benpfaff.org
     
    Ben Pfaff, Oct 28, 2004
    #14
  15. Peter Ammon

    Kevin Bracey Guest

    In message <>
    "Arthur J. O'Dwyer" <> 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/
     
    Kevin Bracey, Oct 28, 2004
    #15
  16. Peter Ammon

    Dan Pop Guest

    In <4180d582$0$32061$4all.nl> "dandelion" <> writes:


    >"Peter Ammon" <> wrote in message
    >news:%h3gd.36079$...
    >> 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.


    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:
    Currently looking for a job in the European Union
     
    Dan Pop, Oct 28, 2004
    #16
  17. Peter Ammon

    Bart Guest

    On Wed, 27 Oct 2004 16:55:46 -0700, Peter Ammon
    <> 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
     
    Bart, Oct 28, 2004
    #17
  18. Peter Ammon

    Old Wolf Guest

    Peter Ammon <> 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.
     
    Old Wolf, Oct 28, 2004
    #18
  19. (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) <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.
     
    Keith Thompson, Oct 28, 2004
    #19
  20. Peter Ammon

    Peter Ammon Guest

    dandelion wrote:
    > "Peter Ammon" <> wrote in message
    > news:%h3gd.36079$...
    >
    >>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...


    Surely no weirder than the unary + operator!

    --
    Pull out a splinter to reply.
     
    Peter Ammon, Oct 29, 2004
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. AviraM
    Replies:
    2
    Views:
    6,430
    Manish Pandit
    Sep 28, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,071
    Smokey Grindel
    Dec 2, 2006
  3. dmitrey
    Replies:
    25
    Views:
    679
    Jorgen Grahn
    Mar 23, 2007
  4. PerlFAQ Server
    Replies:
    0
    Views:
    96
    PerlFAQ Server
    Jan 7, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    88
    PerlFAQ Server
    Feb 26, 2011
Loading...

Share This Page