extra comma

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • aarklon@gmail.com

    extra comma

    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 ...????
  • Richard Heathfield

    #2
    Re: extra comma

    aarklon@gmail.c om 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[i]);
    while(i++ < 12)
    {
    printf(", %d%s", day[i], (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

    Comment

    • vippstar@gmail.com

      #3
      Re: extra comma

      On Feb 16, 7:14 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
      aark...@gmail.c om 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[i]);
      while(i++ < 12)
      {
      printf(", %d%s", day[i], (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 "initialize r 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,});

      Comment

      • Martin

        #4
        Re: extra comma

        On Feb 16, 4:55 pm, aark...@gmail.c om 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

        Comment

        • Richard Heathfield

          #5
          Re: extra comma

          vippstar@gmail. com said:
          On Feb 16, 7:14 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
          >aark...@gmail. com 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

          Comment

          • Ben Pfaff

            #6
            Re: extra comma

            Richard Heathfield <rjh@see.sig.in validwrites:
            aarklon@gmail.c om 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[i]);
            while(i++ < 12)
            {
            printf(", %d%s", day[i], (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(ELEMEN T_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

            Comment

            • Richard Heathfield

              #7
              Re: extra comma

              Ben Pfaff said:
              Richard Heathfield <rjh@see.sig.in validwrites:
              <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

              Comment

              • Ben Pfaff

                #8
                Re: extra comma

                Richard Heathfield <rjh@see.sig.in validwrites:
                Ben Pfaff said:
                >
                >Richard Heathfield <rjh@see.sig.in validwrites:
                >
                <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

                Comment

                • Richard Heathfield

                  #9
                  Re: extra comma

                  Ben Pfaff said:
                  Richard Heathfield <rjh@see.sig.in validwrites:
                  >
                  >Ben Pfaff said:
                  >>
                  >>Richard Heathfield <rjh@see.sig.in validwrites:
                  >>
                  ><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

                  Comment

                  • Ben Pfaff

                    #10
                    Re: extra comma

                    Richard Heathfield <rjh@see.sig.in validwrites:
                    Ben Pfaff said:
                    >
                    >Richard Heathfield <rjh@see.sig.in validwrites:
                    >>
                    >>Ben Pfaff said:
                    >>>
                    >>>Richard Heathfield <rjh@see.sig.in validwrites:
                    >>>
                    >><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);in t main(void){unsi gned long b[]
                    ={0x67dffdff,0x 9aa9aa6a,0xa77f fda9,0x7da6aa6a ,0xa67f6aaa,0xa a9aa9f6,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)bre ak;else default:continu e;if(0)case 1:putchar(a[i&15]);break;}}}

                    Comment

                    • Keith Thompson

                      #11
                      Re: extra comma

                      Richard Heathfield <rjh@see.sig.in validwrites:
                      Ben Pfaff said:
                      >Richard Heathfield <rjh@see.sig.in validwrites:
                      >
                      <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_Keit h) <kst-u@mib.org>
                      Nokia
                      "We must do something. This is something. Therefore, we must do this."
                      -- Antony Jay and Jonathan Lynn, "Yes Minister"

                      Comment

                      • Thad Smith

                        #12
                        Re: extra comma

                        Ben Pfaff wrote:
                        Richard Heathfield <rjh@see.sig.in validwrites:
                        >
                        >aarklon@gmail.c om 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[i]);
                        >while(i++ < 12)
                        >{
                        > printf(", %d%s", day[i], (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(ELEMEN T_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

                        Comment

                        • Malcolm McLean

                          #13
                          Re: extra comma


                          "Keith Thompson" <kst-u@mib.orgwrote 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.


                          Comment

                          • Ben Pfaff

                            #14
                            Re: extra comma

                            "Malcolm McLean" <regniztar@btin ternet.comwrite s:
                            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

                            Comment

                            • Malcolm McLean

                              #15
                              Re: extra comma


                              "Ben Pfaff" <blp@cs.stanfor d.eduwrote in message
                              news:877ih4lfdn .fsf@blp.benpfa ff.org...
                              "Malcolm McLean" <regniztar@btin ternet.comwrite s:
                              >
                              >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.


                              Comment

                              Working...