extra comma

Discussion in 'C Programming' started by aarklon@gmail.com, Feb 16, 2008.

  1. Guest

    Hi all,

    why does C language permits an extra comma in initializer list

    ex:- int days[] = {
    31,28.31,30,31,30,
    31,31,30,31,30,31,
    }
    i have heard it is for the purpose of automatic code generation
    is there any other purpose than this, if so why ...????
    , Feb 16, 2008
    #1
    1. Advertising

  2. said:

    > Hi all,
    >
    > why does C language permits an extra comma in initializer list
    >
    > ex:- int days[] = {
    > 31,28.31,30,31,30,


    Between 31 and 28 you meant ,, not ..

    > 31,31,30,31,30,31,
    > }
    > i have heard it is for the purpose of automatic code generation


    That's supposed to be the reasoning behind such lamenesses, yes. But
    observe:

    i = 0;
    printf(" %d", day);
    while(i++ < 12)
    {
    printf(", %d%s", day, (i % 6) == 5 ? "\n" : "");
    }

    So, as you can see, it isn't actually difficult to generate the code
    without the trailing comma.

    > is there any other purpose than this, if so why ...????


    No, it's just catering for the lazy.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Feb 16, 2008
    #2
    1. Advertising

  3. Guest

    On Feb 16, 7:14 pm, Richard Heathfield <> wrote:
    > said:
    >
    > > Hi all,

    >
    > > why does C language permits an extra comma in initializer list

    >
    > > ex:- int days[] = {
    > > 31,28.31,30,31,30,

    >
    > Between 31 and 28 you meant ,, not ..

    How do you know? 28.31 is a valid value for initializing in integer
    context.

    >
    > > 31,31,30,31,30,31,
    > > }
    > > i have heard it is for the purpose of automatic code generation

    >
    > That's supposed to be the reasoning behind such lamenesses, yes. But
    > observe:
    >
    > i = 0;
    > printf(" %d", day);
    > while(i++ < 12)
    > {
    > printf(", %d%s", day, (i % 6) == 5 ? "\n" : "");
    >
    > }
    >
    > So, as you can see, it isn't actually difficult to generate the code
    > without the trailing comma.

    Modulus or even the conditional check could be expensive somewhere I
    guess.
    Notice it's not only allowed in an "initializer list" but anywhere
    where {elements} is used. (I don't know the proper term for it)
    For example, printf("%zu\n", sizeof (char[]){1,2,3,});
    , Feb 16, 2008
    #3
  4. Martin Guest

    On Feb 16, 4:55 pm, wrote:
    > why does C language permits an extra comma in initializer list
    > ...
    >  i have heard it is for the purpose of automatic code generation
    > is there any other purpose than this, if so why ...????


    According to K&R2 (p196) it is "a nicety for neat formatting."

    --
    Martin
    Martin, Feb 16, 2008
    #4
  5. said:

    > On Feb 16, 7:14 pm, Richard Heathfield <> wrote:
    >> said:
    >>
    >> > Hi all,

    >>
    >> > why does C language permits an extra comma in initializer list

    >>
    >> > ex:- int days[] = {
    >> > 31,28.31,30,31,30,

    >>
    >> Between 31 and 28 you meant ,, not ..

    > How do you know?


    Call me psychic if you like.

    > 28.31 is a valid value for initializing in integer
    > context.


    Yes, but on this occasion it was not what was intended.

    <snip>

    >> So, as you can see, it isn't actually difficult to generate the code
    >> without the trailing comma.

    > Modulus or even the conditional check could be expensive somewhere I
    > guess.


    My % could have been avoided if necessary, but in any case it was to deal
    with the newline, not the comma.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Feb 16, 2008
    #5
  6. Ben Pfaff Guest

    Richard Heathfield <> writes:

    > said:
    >> why does C language permits an extra comma in initializer list
    >> i have heard it is for the purpose of automatic code generation

    >
    > That's supposed to be the reasoning behind such lamenesses, yes. But
    > observe:
    >
    > i = 0;
    > printf(" %d", day);
    > while(i++ < 12)
    > {
    > printf(", %d%s", day, (i % 6) == 5 ? "\n" : "");
    > }


    Observe:

    int array[] = {
    #ifdef ELEMENT_ONE
    1,
    #endif
    #ifdef ELEMENT_TWO
    2,
    #endif
    #ifdef ELEMENT_THREE
    3,
    #endif
    #ifdef ELEMENT_FOUR
    4,
    #endif
    };

    versus:

    int array[] = {
    #ifdef ELEMENT_ONE
    1
    # if (defined(ELEMENT_TWO) || defined(ELEMENT_THREE) \
    || defined(ELEMENT_FOUR))
    ,
    # endif
    #endif
    #ifdef ELEMENT_TWO
    2,
    # if defined(ELEMENT_THREE) || defined(ELEMENT_FOUR)
    ,
    # endif
    #endif
    #ifdef ELEMENT_THREE
    3
    # if defined(ELEMENT_FOUR)
    ,
    # endif
    #endif
    #ifdef ELEMENT_FOUR
    4,
    #endif
    };

    I know which one I would prefer to maintain.
    --
    "The expression isn't unclear *at all* and only an expert could actually
    have doubts about it"
    --Dan Pop
    Ben Pfaff, Feb 16, 2008
    #6
  7. Ben Pfaff Guest

    Richard Heathfield <> writes:

    > Ben Pfaff said:
    >
    >> Richard Heathfield <> writes:

    >
    > <snip>
    >
    >> I know which one I would prefer to maintain.

    >
    > Surely the whole point of generating the code automatically is that you
    > don't have to maintain it?


    The code that you snipped was not an example of automatically
    generated code.
    --
    "Welcome to the wonderful world of undefined behavior, where the demons
    are nasal and the DeathStation users are nervous." --Daniel Fox
    Ben Pfaff, Feb 16, 2008
    #7
  8. Ben Pfaff said:

    > Richard Heathfield <> writes:


    <snip>

    > I know which one I would prefer to maintain.


    Surely the whole point of generating the code automatically is that you
    don't have to maintain it?

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Feb 16, 2008
    #8
  9. rzed Guest

    Richard Heathfield <> wrote in
    news::

    > said:
    >
    >> Hi all,
    >>
    >> why does C language permits an extra comma in initializer list
    >>
    >> ex:- int days[] = {
    >> 31,28.31,30,31,30,

    >
    > Between 31 and 28 you meant ,, not ..
    >
    >> 31,31,30,31,30,31,
    >> }
    >> i have heard it is for the purpose of automatic code
    >> generation

    >
    > That's supposed to be the reasoning behind such lamenesses, yes.
    > But observe:
    >
    > i = 0;
    > printf(" %d", day);
    > while(i++ < 12)
    > {
    > printf(", %d%s", day, (i % 6) == 5 ? "\n" : "");
    > }
    >
    > So, as you can see, it isn't actually difficult to generate the
    > code without the trailing comma.
    >
    >> is there any other purpose than this, if so why ...????

    >
    > No, it's just catering for the lazy.
    >


    And what's wrong with that? A compiler is just catering to those
    too lazy to code machine language directly.

    One benefit of allowing the extra comma (albeit for the lazy) is
    that it allows you to manually reorder lists of items in an editor
    without having to go back and add a comma here and take it away
    there.

    I don't see the harm in allowing the trailing comma, and there is
    at least some productivity benefit.

    --
    rzed
    rzed, Feb 16, 2008
    #9
  10. Ben Pfaff Guest

    Richard Heathfield <> writes:

    > Ben Pfaff said:
    >
    >> Richard Heathfield <> writes:
    >>
    >>> Ben Pfaff said:
    >>>
    >>>> Richard Heathfield <> writes:
    >>>
    >>> <snip>
    >>>
    >>>> I know which one I would prefer to maintain.
    >>>
    >>> Surely the whole point of generating the code automatically is that you
    >>> don't have to maintain it?

    >>
    >> The code that you snipped was not an example of automatically
    >> generated code.

    >
    > Oh. I see.
    >
    > <bright smile, a la Dory from "Finding Nemo">
    >
    > Well then - perhaps you ought to automate it! :)


    Fortunately, I have no need to do so, because trailing commas are
    allowed in initializer lists (and elsewhere).
    --
    char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
    ={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
    =b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
    2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
    Ben Pfaff, Feb 16, 2008
    #10
  11. Ben Pfaff said:

    > Richard Heathfield <> writes:
    >
    >> Ben Pfaff said:
    >>
    >>> Richard Heathfield <> writes:

    >>
    >> <snip>
    >>
    >>> I know which one I would prefer to maintain.

    >>
    >> Surely the whole point of generating the code automatically is that you
    >> don't have to maintain it?

    >
    > The code that you snipped was not an example of automatically
    > generated code.


    Oh. I see.

    <bright smile, a la Dory from "Finding Nemo">

    Well then - perhaps you ought to automate it! :)

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Feb 16, 2008
    #11
  12. rzed said:

    > Richard Heathfield <> wrote in
    > news::
    >
    >> said:


    <snip>

    >>> is there any other purpose than this, if so why ...????

    >>
    >> No, it's just catering for the lazy.

    >
    > And what's wrong with that? A compiler is just catering to those
    > too lazy to code machine language directly.


    Right. And keyboards are lame, too - we should be using plugboards.

    > One benefit of allowing the extra comma (albeit for the lazy) is
    > that it allows you to manually reorder lists of items in an editor
    > without having to go back and add a comma here and take it away
    > there.


    True enough. In fact, that's catering for the good kind of lazy (yes, there
    are at least two kinds). Of course, manually reordering lists is easy
    enough even if you don't like the trailing comma. Reorder, remove last
    comma, recompile, and the editor will tell you where to insert the comma.
    :)

    > I don't see the harm in allowing the trailing comma, and there is
    > at least some productivity benefit.


    I certainly wouldn't want to see the feature /removed/ from the language.
    Had I been consulted as to its original addition, I'd probably have argued
    against it, but hey - it's there, it works, it does no particular harm, so
    no big deal. (Of course, removing it from the Standard would do no harm
    either, since the standardisation process is effectively dead in the water
    now.)

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Feb 16, 2008
    #12
  13. Ben Pfaff said:

    > Richard Heathfield <> writes:
    >

    <snip>

    >> Well then - perhaps you ought to automate it! :)

    >
    > Fortunately, I have no need to do so, because trailing commas are
    > allowed in initializer lists (and elsewhere).


    Um, I only suggested that because it really, really, really looked like a
    good candidate for automation. Very repetitive, easy for people to screw
    up during maintenance... it's a natural.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Feb 16, 2008
    #13
  14. Ben Pfaff Guest

    Richard Heathfield <> writes:

    > Ben Pfaff said:
    >
    >> Richard Heathfield <> writes:
    >>> Well then - perhaps you ought to automate it! :)

    >>
    >> Fortunately, I have no need to do so, because trailing commas are
    >> allowed in initializer lists (and elsewhere).

    >
    > Um, I only suggested that because it really, really, really looked like a
    > good candidate for automation. Very repetitive, easy for people to screw
    > up during maintenance... it's a natural.


    Hmm. I appreciate the thought, but I haven't noticed that
    similar lists have been a problem in practice in the projects
    that I'm involved in, as long as they don't grow absurdly large.
    If they do grow too large, then automation is certainly a
    reasonable option.
    --
    char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
    ={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
    =b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
    2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
    Ben Pfaff, Feb 16, 2008
    #14
  15. Richard Heathfield <> writes:
    > Ben Pfaff said:
    >> Richard Heathfield <> writes:

    >
    > <snip>
    >
    >> I know which one I would prefer to maintain.

    >
    > Surely the whole point of generating the code automatically is that you
    > don't have to maintain it?


    I think Ben's point (not that I'd presume to speak for him) is that
    the trailing-comma rule can make it easier to maintain the code that
    generates the code.

    If you want to generate an initializer list and you already have all
    the information in an array, then the trailing-comma rule doesn't help
    much; it's easy enough to know when you're about to write the last
    element and drop the comma. It's *slightly* more convenient to be
    able to generate a comma after every item, but not enough to be worth
    marring the language.

    But if you don't know whether the current item is the last one until
    after you've written it (say, you're reading input from a file), then
    being able to write the trailing comma after every item can be more
    significantly convenient.

    It's still not impossible to avoid the trailing comma (if nothing
    else, you can build an array or list internally and write the whole
    initializer only when it's complete), and I still have mixed feelings
    about whether the rule is worthwhile, but it's not completely useless.

    An interesting (I think) point is that I've never heard anyone
    complain that semicolons are used as terminators rather than as
    separators, as they are in Pascal. The fact that we can (and must)
    write:
    {
    statement1;
    statement2;
    }
    rather than
    {
    statement1;
    statement2
    }
    makes programs easier to maintain when we want to add a third
    statement. But the idea that a comma should be a terminator rather
    than a separator seems somehow unnatural -- even though they're both
    separators in written English.

    To summarize:

    { statement1; statement2; } /* good */
    { statement1; statement2 } /* bad */
    { initializer1, initializer2, } /* bad */
    { initializer1, initializer2 } /* good */

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Feb 16, 2008
    #15
  16. Thad Smith Guest

    Ben Pfaff wrote:
    > Richard Heathfield <> writes:
    >
    >> said:
    >>> why does C language permits an extra comma in initializer list
    >>> i have heard it is for the purpose of automatic code generation

    >> That's supposed to be the reasoning behind such lamenesses, yes. But
    >> observe:
    >>
    >> i = 0;
    >> printf(" %d", day);
    >> while(i++ < 12)
    >> {
    >> printf(", %d%s", day, (i % 6) == 5 ? "\n" : "");
    >> }

    >
    > Observe:
    >
    > int array[] = {
    > #ifdef ELEMENT_ONE
    > 1,
    > #endif
    > #ifdef ELEMENT_TWO
    > 2,
    > #endif
    > #ifdef ELEMENT_THREE
    > 3,
    > #endif
    > #ifdef ELEMENT_FOUR
    > 4,
    > #endif
    > };
    >
    > versus:
    >
    > int array[] = {
    > #ifdef ELEMENT_ONE
    > 1
    > # if (defined(ELEMENT_TWO) || defined(ELEMENT_THREE) \
    > || defined(ELEMENT_FOUR))
    > ,
    > # endif
    > #endif
    > #ifdef ELEMENT_TWO
    > 2,
    > # if defined(ELEMENT_THREE) || defined(ELEMENT_FOUR)
    > ,
    > # endif
    > #endif
    > #ifdef ELEMENT_THREE
    > 3
    > # if defined(ELEMENT_FOUR)
    > ,
    > # endif
    > #endif
    > #ifdef ELEMENT_FOUR
    > 4,
    > #endif
    > };


    Ben meant to omit the comma following "4", of course. Perhaps the mistake
    helps illustrate his point.

    Richard Heathfield wrote:
    > Ben Pfaff said:
    >> I know which one I would prefer to maintain.

    >
    > Surely the whole point of generating the code automatically is that you
    > don't have to maintain it?


    Ben's example was for code that is manually written and maintained, not
    automatically generated. I think his point has merit.

    --
    Thad
    Thad Smith, Feb 16, 2008
    #16
  17. "Keith Thompson" <> wrote in message
    > An interesting (I think) point is that I've never heard anyone
    > complain that semicolons are used as terminators rather than as
    > separators, as they are in Pascal. The fact that we can (and must)
    > write:
    > {
    > statement1;
    > statement2;
    > }
    > rather than
    > {
    > statement1;
    > statement2
    > }
    > makes programs easier to maintain when we want to add a third
    > statement. But the idea that a comma should be a terminator rather
    > than a separator seems somehow unnatural -- even though they're both
    > separators in written English.
    >

    Most people don't know the rules for semicolons in written English. The
    semi-colon can be used to introduce a list, or to separate two parts of a
    sentence which are themselves valid sentences (periods).

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
    Malcolm McLean, Feb 16, 2008
    #17
  18. Ben Pfaff Guest

    "Malcolm McLean" <> writes:

    > Most people don't know the rules for semicolons in written
    > English. The semi-colon can be used to introduce a list, or to
    > separate two parts of a sentence which are themselves valid sentences
    > (periods).


    Ordinarily, a colon, not a semicolon, would be used in English to
    introduce a list.
    --
    Ben Pfaff
    http://benpfaff.org
    Ben Pfaff, Feb 16, 2008
    #18
  19. "Ben Pfaff" <> wrote in message
    news:...
    > "Malcolm McLean" <> writes:
    >
    >> Most people don't know the rules for semicolons in written
    >> English. The semi-colon can be used to introduce a list, or to
    >> separate two parts of a sentence which are themselves valid sentences
    >> (periods).

    >
    > Ordinarily, a colon, not a semicolon, would be used in English to
    > introduce a list.
    >

    I mean terminate a comma-separated list.

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
    Malcolm McLean, Feb 16, 2008
    #19
  20. Richard Heathfield wrote:

    > good candidate for automation. Very repetitive, easy for people to screw
    > up during maintenance...


    Not in my experience, no. Perhaps you just worked with lower grade
    maintenance droids?
    gd&r.

    --
    Mark McIntyre

    CLC FAQ <http://c-faq.com/>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
    Mark McIntyre, Feb 17, 2008
    #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. Jason Miles
    Replies:
    1
    Views:
    600
    Jürgen Exner
    May 28, 2004
  2. Ryan Moore

    add a comma

    Ryan Moore, Aug 27, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    3,229
    William F. Robertson, Jr.
    Aug 28, 2003
  3. Martin Pahlplatz

    server.mappath with comma in foldername?

    Martin Pahlplatz, Nov 11, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    518
    Martin Pahlplatz
    Nov 11, 2003
  4. Sriram Rajagopalan

    Extra Comma in enum is Valid?

    Sriram Rajagopalan, Aug 9, 2005, in forum: C Programming
    Replies:
    5
    Views:
    2,124
    Martin Ambuhl
    Aug 9, 2005
  5. mathieu
    Replies:
    3
    Views:
    593
    Bo Persson
    Sep 4, 2009
Loading...

Share This Page