Re: comma operator

Discussion in 'C Programming' started by Keith Thompson, May 15, 2009.

  1. Franken Sense <> writes:
    > In Dread Ink, the Grave Hand of Franken Sense Did Inscribe:
    > [misposted to clp.misc]
    > [x-posted to clc; f-up set to clc]
    >> I'm reading up on perl today and find this in perldoc perlop:
    >>
    >> Comma Operator
    >>
    >> Binary "," is the comma operator. In scalar context it
    >> evaluates its left argument, throws that value away, then
    >> evaluates its right argument and returns that value. This is
    >> just like C's comma operator.
    >>
    >> // end excerpt
    >>
    >> What is C's comma operator?


    It evaluates its left argument, throws that value away, then evaluates
    its right argument and yields that value.

    See your C textbook.

    (Note that not all commas are comma operators; in particular, the
    commas separating arguments in a function call or macro invocation are
    not operators.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, May 15, 2009
    #1
    1. Advertising

  2. In Dread Ink, the Grave Hand of Franken Sense Did Inscribe:

    [misposted to clp.misc]
    [x-posted to clc; f-up set to clc]
    > I'm reading up on perl today and find this in perldoc perlop:
    >
    > Comma Operator
    >
    > Binary "," is the comma operator. In scalar context it evaluates its
    > left argument, throws that value away, then evaluates its right
    > argument
    > and returns that value. This is just like C's comma operator.
    >
    > // end excerpt
    >
    > What is C's comma operator?



    --
    Frank

    My father grew up in the Great Depression - his mother's.
    ~~ Al Franken
    Franken Sense, May 15, 2009
    #2
    1. Advertising

  3. On May 15, 8:46 am, Keith Thompson <> wrote:
    > (Note that not all commas are comma operators; in particular, the
    > commas separating arguments in a function call or macro
    > invocation are not operators.)


    I've wondered if my rule is correct:
    The comma in
    A, B
    is The Comma Operator if and only that is equivalent to
    (A, B)

    For example, only the second comma in
    printf(A, (B, C))
    is The Comma Operator because that is equivalent to
    printf(A, ((B, C)))
    but not
    printf((A, (B, C)))

    James
    James Dow Allen, May 15, 2009
    #3
  4. Keith Thompson

    luserXtrog Guest

    On May 15, 2:49 am, James Dow Allen <> wrote:
    > On May 15, 8:46 am, Keith Thompson <> wrote:
    >
    > > (Note that not all commas are comma operators; in particular, the
    > > commas separating arguments in a function call or macro
    > > invocation are not operators.)

    >
    > I've wondered if my rule is correct:
    > The comma in
    >      A, B
    > is The Comma Operator if and only that is equivalent to
    >      (A, B)
    >
    > For example, only the second comma in
    >      printf(A, (B, C))
    > is The Comma Operator because that is equivalent to
    >      printf(A, ((B, C)))
    > but not
    >      printf((A, (B, C)))
    >
    > James


    I find it easier to think of the comma as a semicolon without
    a period. It terminates the expression without terminating the
    statement.

    --
    lxt
    luserXtrog, May 15, 2009
    #4
  5. luserXtrog <> writes:
    [...]
    > I find it easier to think of the comma as a semicolon without
    > a period. It terminates the expression without terminating the
    > statement.


    Ah, but it doesn't terminate the expression; it only terminates a
    subexpression.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, May 15, 2009
    #5
  6. Keith Thompson

    Willem Guest

    Richard Heathfield wrote:
    ) Yes, it does. An expression is "a sequence of operators and operands
    ) that specifies computation of a value, or that designates an object
    ) or a function, or that generates side effects, or that performs a
    ) combination thereof". A comma operator always terminates such a
    ) sequence.

    So, what would you call this then:

    ((1, 2, 3) + (4, 5, 6))


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
    Willem, May 15, 2009
    #6
  7. Richard Heathfield <> writes:
    > Keith Thompson said:
    >> luserXtrog <> writes:
    >> [...]
    >>> I find it easier to think of the comma as a semicolon without
    >>> a period. It terminates the expression without terminating the
    >>> statement.

    >>
    >> Ah, but it doesn't terminate the expression; it only terminates a
    >> subexpression.

    >
    > Yes, it does. An expression is "a sequence of operators and operands
    > that specifies computation of a value, or that designates an object
    > or a function, or that generates side effects, or that performs a
    > combination thereof". A comma operator always terminates such a
    > sequence.


    You're right, of course. (The standard's definition of "expression"
    is broken, but that's a separate point.)

    Incidentally, that's true only because the comma operator has a lower
    precedence than any other operator.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, May 15, 2009
    #7
  8. Keith Thompson <> writes:
    > Richard Heathfield <> writes:
    >> Keith Thompson said:
    >>> luserXtrog <> writes:
    >>> [...]
    >>>> I find it easier to think of the comma as a semicolon without
    >>>> a period. It terminates the expression without terminating the
    >>>> statement.
    >>>
    >>> Ah, but it doesn't terminate the expression; it only terminates a
    >>> subexpression.

    >>
    >> Yes, it does. An expression is "a sequence of operators and operands
    >> that specifies computation of a value, or that designates an object
    >> or a function, or that generates side effects, or that performs a
    >> combination thereof". A comma operator always terminates such a
    >> sequence.

    >
    > You're right, of course. (The standard's definition of "expression"
    > is broken, but that's a separate point.)
    >
    > Incidentally, that's true only because the comma operator has a lower
    > precedence than any other operator.


    I may be wrong about that. I'll think about it when I have more time.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, May 15, 2009
    #8
  9. Richard Heathfield <> writes:
    > Willem said:
    >> Richard Heathfield wrote:
    >> ) Yes, it does. An expression is "a sequence of operators and
    >> operands ) that specifies computation of a value, or that
    >> designates an object ) or a function, or that generates side
    >> effects, or that performs a ) combination thereof". A comma
    >> operator always terminates such a ) sequence.
    >>
    >> So, what would you call this then:
    >>
    >> ((1, 2, 3) + (4, 5, 6))

    >
    > I would call 1 an expression, which is terminated by a comma
    > operator.

    [...]

    I would call 1 an expression. But I wouldn't call it "a sequence of
    operators and operands". Here it's an operand of the comma operator,
    but in isolation:

    1;

    there are no operators, and therefore 1 is not an operand.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, May 15, 2009
    #9
  10. Keith Thompson

    Willem Guest

    Richard Heathfield wrote:
    ) Willem said:
    )> So, what would you call this then:
    )>
    )> ((1, 2, 3) + (4, 5, 6))
    )
    ) I would call 1 an expression, which is terminated by a comma
    ) operator.
    ) I would call 2 an expression, which is terminated by a comma
    ) operator.
    ) I would call 4 an expression, which is terminated by a comma
    ) operator.
    ) I would call 5 an expression, which is terminated by a comma
    ) operator.
    )
    ) I would call the whole thing an expression, too.
    )
    ) What would you call these expressions?

    Subexpressions, obviously. They're all parts of the whole thing,
    which is an expression.

    What I wanted to know is what you call the operands of the '+' operator.


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
    Willem, May 15, 2009
    #10
  11. Keith Thompson

    jameskuyper Guest

    Willem wrote:
    > Richard Heathfield wrote:
    > ) Yes, it does. An expression is "a sequence of operators and operands
    > ) that specifies computation of a value, or that designates an object
    > ) or a function, or that generates side effects, or that performs a
    > ) combination thereof". A comma operator always terminates such a
    > ) sequence.
    >
    > So, what would you call this then:
    >
    > ((1, 2, 3) + (4, 5, 6))


    1 Primary expression (6.5.1p1)
    2 Primary expression
    1, 2 Expression (6.5.17p1)
    3 Primary expression
    1, 2, 3 Expression
    (1, 2, 3) Primary expression
    4 Primary expression
    5 Primary expression
    4, 5 Expression
    6 Primary expression
    4, 5, 6 Expression
    (4, 5, 6) Primary expression
    (1, 2, 3) + (4, 5, 6) Additive expression (6.5.6p1)
    ((1, 2, 3) + (4, 5, 6)) Primary expression

    In each case, I've classified the type of expression as tightly as
    possible, but every one of those also qualifies as a simple
    "expression" as defined in 6.5.17p1.

    The standard uses the term "subexpression", but never defines it. My
    understanding of the term, which is consistent with every use of that
    term which occurs in the standard, is that a subexpression is any
    expression which is not a full expression (6.8p4). If I'm right about
    that, then all of the above expressions, except possibly the last one,
    can also be described as a subexpressions. Whether the last one is
    full expression depends upon the context in which it occurs.

    How would you classify it?
    jameskuyper, May 15, 2009
    #11
  12. Keith Thompson

    luserXtrog Guest

    On May 15, 2:14 pm, Twirlip of the Mists <> wrote:
    > On Thu, 14 May 2009 18:46:44 -0700, Keith Thompson wrote:
    > > Franken Sense <> writes:
    > >> In Dread Ink, the Grave Hand of Franken Sense Did Inscribe:

    >
    > >>> What is C's comma operator?

    >
    > > It evaluates its left argument, throws that value away, then evaluates
    > > its right argument and yields that value.

    >
    > Follow up question: What do you use it for?
    >
    > I was first introduced to the comma operator in for loops to initialize
    > more than one variable (and where the right argument's value is thrown
    > away). For example:
    >
    >   for (a = 0, b = 0 ; a < x ; a++) {
    >     /* Do stuff */
    >   }
    >
    > (Also sometimes in the third expression of the for loop)
    >
    > I then found it useful for while loops when I had to find out the value
    > to test in an indirect manner:
    >
    >   while (set_ptr(&ptr), ptr != NULL) {
    >     /* Do other stuff */
    >   }
    >
    > I then started using it in all while loops where I had to first go and
    > get the value to test, because I liked the consistency and it looks
    > cleaner. So I replaced things like this:
    >
    >   while ((c = getc(fp)) != EOF) {
    >     /* Do more other stuff */
    >   }
    >
    > With this:
    >
    >   while (c = getc(fp), c != EOF) {
    >     /* Haven't I done this stuff already?  */
    >   }
    >
    > And then I ran out of uses.
    >


    Commas can also be useful to eliminate braces (should one so desire).

    while (c = getc(fp), c != EOF)
    dothis(c), dothat(c);

    And in macros, it can reduce the need for do-while to make the
    macro behave "function-like".

    #define BOO(c) something(c), \
    somethingelse(c), \
    printf("boogabooga!\n")

    (As always, add extra parens where it matters.)

    It's use, in general is to do what would otherwise have to be
    done in multiple statements but within a single simple statement
    where for one reason or another a single statement is syntactically
    required. Semi-w/o-dot sums it up concisely.

    --
    lxt
    luserXtrog, May 15, 2009
    #12
  13. Franken Sense <> writes:

    > In Dread Ink, the Grave Hand of Richard Heathfield Did Inscribe:

    <snip>
    >> ... The thing that precedes a comma operator is always an
    >> expression. Therefore, the comma operator always terminates an
    >> expression - as do all binary operators. Even = terminates an
    >> expression; in x = y, = terminates the expression x. (This isn't
    >> true for unary operators, however. In x = -y; the unary minus
    >> operator doesn't terminate an expression. Neither does & in p =
    >> &obj. But I think it's true for all binary operators.)

    >
    > If we have
    > (t=3, t+2)
    > then t = 5.


    Do you mean t will be 5, i.e. in C notation t == 5? If so, no. The
    expression has value 5 and t will be 3 afterwards (in fact it will be
    3 before the expression finishes).

    > If we had
    > (t=3, t)
    > , how is t not an lvalue?


    Eh? What have lvalues to do with it?

    If t is an lvalue, putting it on the right-hand side of a comma
    operator stops it being one. This is true of several forms of
    expression in C that could, plausibly, be defined as being lvalues.
    Assignment, comma and conditional expressions could all be defined to
    yield lvalues but the don't (C++ begs to differ in all three cases).

    --
    Ben.
    Ben Bacarisse, May 16, 2009
    #13
  14. Franken Sense <> writes:
    [...]
    > If we have
    > (t=3, t+2)
    > then t = 5.


    No, we don't. It's equivalent to:

    ((t = 3), (t + 2))

    After evaluation, t == 3, and the expression yields the value 5.

    > If we had
    > (t=3, t)
    > , how is t not an lvalue?


    Who said it wasn't? I snipped some context, but nobody upthread
    mentioned lvalues.

    That expression stores the value 3 in t, and yields the value 3. The
    first occurrence of t is an lvalue. The second is as well, but it
    occurs in a context that doesn't require an lvalue. (The standard's
    definition of "lvalue" is sufficiently broken that it's arguable
    whether the second occurence of t actually is an lvalue.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, May 16, 2009
    #14
  15. In Dread Ink, the Grave Hand of Richard Heathfield Did Inscribe:

    > Keith Thompson said:
    >
    >> Keith Thompson <> writes:
    >>> Richard Heathfield <> writes:
    >>>> Keith Thompson said:
    >>>>> luserXtrog <> writes:
    >>>>> [...]
    >>>>>> I find it easier to think of the comma as a semicolon without
    >>>>>> a period. It terminates the expression without terminating the
    >>>>>> statement.
    >>>>>
    >>>>> Ah, but it doesn't terminate the expression; it only terminates
    >>>>> a subexpression.
    >>>>
    >>>> Yes, it does. [...]
    >>>
    >>> You're right, of course. (The standard's definition of
    >>> "expression" is broken, but that's a separate point.)
    >>>
    >>> Incidentally, that [the comma operator always terminates an
    >>> expression is] true only because the comma operator has a
    >>> lower precedence than any other operator.

    >>
    >> I may be wrong about that. I'll think about it when I have more
    >> time.

    >
    > You're wrong. The thing that precedes a comma operator is always an
    > expression. Therefore, the comma operator always terminates an
    > expression - as do all binary operators. Even = terminates an
    > expression; in x = y, = terminates the expression x. (This isn't
    > true for unary operators, however. In x = -y; the unary minus
    > operator doesn't terminate an expression. Neither does & in p =
    > &obj. But I think it's true for all binary operators.)


    If we have
    (t=3, t+2)
    then t = 5.

    If we had
    (t=3, t)
    , how is t not an lvalue?
    --
    Frank

    ....it's obviously how his disease manifests itself, any kind of substance
    dependency is very deep, issues of self esteem, you can just tell that he's
    a really insecure and vulnerable person -- and I love him. You know,
    sometimes I listen to him on the radio, and he's very judgmental, he's a
    very angry person, and I just want to remind him that anytime you have a
    finger pointing at someone else, there's three pointing back at you.
    ~~ Al Franken
    Franken Sense, May 16, 2009
    #15
  16. Keith Thompson

    Willem Guest

    Richard Heathfield wrote:
    ) Willem said:
    )
    )<snip>
    )
    )> What I wanted to know is what you call the operands of the '+'
    )> operator.
    )
    ) I would certainly have no problem with describing them as
    ) expressions.

    In that case, I probably misunderstood your point.

    It appears from crossthread that the standard never defines
    'subexpression', am I correct ?

    This whole subthread started with the question if 'a comma always
    terminates (an expression that is) a subexpression', IIRC, or perhaps with
    the slightly too strongly stated 'a comma doesn't terminate expressions,
    only subexpressions'. The latter is obviously wrong, as a subexpression is
    also an expression in itself, but the former is debatable.


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
    Willem, May 16, 2009
    #16
  17. Keith Thompson

    Richard Bos Guest

    Willem <> wrote:

    > Richard Heathfield wrote:
    > ) Willem said:
    > )> So, what would you call this then:
    > )>
    > )> ((1, 2, 3) + (4, 5, 6))
    > )
    > ) I would call 1 an expression, which is terminated by a comma
    > ) operator.


    > ) What would you call these expressions?
    >
    > Subexpressions, obviously. They're all parts of the whole thing,
    > which is an expression.
    >
    > What I wanted to know is what you call the operands of the '+' operator.


    I'd call it many things, depending on context; and one of those things
    I'd call it is an expression.

    Richard
    Richard Bos, May 16, 2009
    #17
  18. In Dread Ink, the Grave Hand of Keith Thompson Did Inscribe:

    > Richard Heathfield <> writes:
    >> Willem said:
    >>> Richard Heathfield wrote:
    >>> ) Willem said:
    >>> )
    >>> )<snip>
    >>> )
    >>> )> What I wanted to know is what you call the operands of the '+'
    >>> )> operator.
    >>> )
    >>> ) I would certainly have no problem with describing them as
    >>> ) expressions.
    >>>
    >>> In that case, I probably misunderstood your point.
    >>>
    >>> It appears from crossthread that the standard never defines
    >>> 'subexpression', am I correct ?

    >>
    >> Yes. My C89 draft uses the word twice, and doesn't define it on
    >> either occasion; n1256 mentions it slightly more often, but again
    >> doesn't define it at any point.

    >
    > Probably the authors felt it wasn't necessary to define it. In
    > general, a subfoo is a foo that's part of another foo.
    >
    > That doesn't always apply (subhuman, submarine, substandard), but IMHO
    > it's clear enough from the context.


    sub-par. What do you do with that word?

    I love golf. I understand my entire world by getting in a good drive, a
    reasoned-approach, and probablt a two-putt. When golfers play well, they
    end up at minus ten.

    "Well that's another subpar performance for Tiger Woods."
    --
    Frank

    I'm going to die homeless, penniless, and 30 pounds overweight.
    ~~ Al Franken
    Franken Sense, May 17, 2009
    #18
  19. "Franken Sense" <> wrote in message
    news:pe387t1hsxra$...
    > In Dread Ink, the Grave Hand of Keith Thompson Did Inscribe:
    >
    >> Richard Heathfield <> writes:
    >>> Willem said:
    >>>> Richard Heathfield wrote:
    >>>> ) Willem said:
    >>>> )
    >>>> )<snip>
    >>>> )
    >>>> )> What I wanted to know is what you call the operands of the '+'
    >>>> )> operator.
    >>>> )
    >>>> ) I would certainly have no problem with describing them as
    >>>> ) expressions.
    >>>>
    >>>> In that case, I probably misunderstood your point.
    >>>>
    >>>> It appears from crossthread that the standard never defines
    >>>> 'subexpression', am I correct ?
    >>>
    >>> Yes. My C89 draft uses the word twice, and doesn't define it on
    >>> either occasion; n1256 mentions it slightly more often, but again
    >>> doesn't define it at any point.

    >>
    >> Probably the authors felt it wasn't necessary to define it. In
    >> general, a subfoo is a foo that's part of another foo.
    >>
    >> That doesn't always apply (subhuman, submarine, substandard), but IMHO
    >> it's clear enough from the context.

    >
    > sub-par. What do you do with that word?
    >
    > I love golf. I understand my entire world by getting in a good drive, a
    > reasoned-approach, and probablt a two-putt. When golfers play well, they
    > end up at minus ten.
    >
    > "Well that's another subpar performance for Tiger Woods."
    > --
    > Frank
    >
    > I'm going to die homeless, penniless, and 30 pounds overweight.
    > ~~ Al Franken


    Why not 60 pounds overweight? Why do you choose to underestimate your
    potential in life?
    Chris M. Thomasson, May 17, 2009
    #19
  20. Keith Thompson

    Guest

    On 18 May, 08:53, Franken Sense <> wrote:
    > In Dread Ink, the Grave Hand of Ben Bacarisse Did Inscribe:


    > >>> ...     The thing that precedes a comma operator is always an
    > >>> expression. Therefore, the comma operator always terminates an
    > >>> expression - as do all binary operators. Even = terminates an
    > >>> expression; in x = y, = terminates the expression x. (This isn't
    > >>> true for unary operators, however. In x = -y; the unary minus
    > >>> operator doesn't terminate an expression. Neither does & in p =
    > >>> &obj. But I think it's true for all binary operators.)

    >
    > >> If we have
    > >> (t=3, t+2)
    > >> then t = 5.

    >
    > > Do you mean t will be 5, i.e. in C notation t == 5?  If so, no.  The
    > > expression has value 5 and t will be 3 afterwards (in fact it will be
    > > 3 before the expression finishes).

    >
    > I see.
    >
    > Can you point to a C idiom where this is useful?


    The comma operator is rarely useful!

    for loops with >1 control variable
    for (i = 0, p = head; i < 100, p = p != NULL; i++, p = p->next)

    Various tricks with macros
    assert (("invalid handler type", 0));

    --
    Nick Keighley

    "I have always wished that my computer would be as easy to use as my
    telephone. My wish has come true. I no longer know how to use my
    telephone."
    - Bjarne Stroustrup
    , May 18, 2009
    #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. Paul Davis
    Replies:
    7
    Views:
    597
    Paul Davis
    Jul 17, 2003
  2. Derek
    Replies:
    6
    Views:
    375
    Old Wolf
    Apr 13, 2004
  3. Koster

    Yet another comma operator question

    Koster, Jan 16, 2004, in forum: C Programming
    Replies:
    7
    Views:
    382
    Old Wolf
    Jan 18, 2004
  4. Michael Scarlett

    the whys of the comma operator

    Michael Scarlett, Jun 10, 2004, in forum: C Programming
    Replies:
    8
    Views:
    315
    Michael Scarlett
    Jun 19, 2004
  5. G Patel

    comma operator and assignment operator

    G Patel, Feb 7, 2005, in forum: C Programming
    Replies:
    4
    Views:
    489
    Barry Schwarz
    Feb 8, 2005
Loading...

Share This Page