Re: what's the meaning of the following expression?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: David Mair (mairdanot_at_nothotmail.com)
Date: 08/05/04


Date: Wed, 04 Aug 2004 19:58:41 -0600

Victor Bazarov wrote:

> "David Mair" <mairdanot@nothotmail.com> wrote...
>
>>sjtu_huang wrote:
>>
>>
>>>i=(bSuccess) ? a:(b,c);
>>>especially what's the meaning of "(b,c)"?
>>
>>Here's how I understand it. b and c are evaluated from left to right
>>and the value of the group is the value of the rightmost expression - c.
>> All side-effects take place for each expression before the next
>>expression is evaluated. For example:
>>
>>a = 1;
>>b = 2;
>>c = 3;
>>bSuccess = false;
>>
>>With the expression above i takes the value 3, the value of c. It's
>>utility probably makes more sense for an example like this:
>>
>>i = (bSuccess) ? a : (b++, c);
>>
>>b is evaluated (incremented for this example) before c is evaluated and
>>the result of the expression is c. Or:
>>
>>i = (bSuccess) ? a : (b++, c + b);
>>
>>This one is more interesting in that the value of the group is the value
>>of c plus the incremented value of b.
>>
>>You can use more than one comma to have more than two expressions but
>>the rightmost one is the value of the group.
>>
>>The comma operator is used for function arguments.
>
>
> Please don't spread confusion. The comma seen in the list of function
> arguments in a function call has NOTHING to do with the comma operator
> except the use of the same character. The most significant difference
> between the two is that the operands of the comma operator are always
> evaluated from left to right and there is a sequence point between them
> while the order of evaluation of function arguments is _unspecified_ in
> C++.

Sorry, mis-read the MSDN Library page, it couldn't be clearer really:

"Commas can be used as separators in some contexts, such as function
argument lists. Do not confuse the use of the comma as a separator with
its use as an operator; the two uses are completely different."

I read the first paragraph then the examples, the paragraph above is in
between.



Relevant Pages

  • Re: whats wrong with this code from a book?
    ... you need to enclose them in curly braces. ... > What you've done here is use the comma operator to combine the ... > There are rules about the value yielded by an expression with a comma ... exchange of elements in 'reverse', where the exchange can be thought of ...
    (comp.lang.c)
  • Re: Order of evaluation of function arguments
    ... > Combine this with the common misconception that the commas separating ... > arguments are comma operators, ... Note that, although the comma operator is a sequence point, the ... Of course, to confuse things, one could combine the comma operator ...
    (comp.lang.c)
  • Re: printf output
    ... The comma operator does guarantee left-to-right evaluation, ...
    (comp.lang.c)
  • Re: Order of evaluation.
    ... >> The comma in a parameter list is not the comma operator. ... but I'd presume that commas in argument lists ... Comeau C/C++ with Dinkumware's Libraries... ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Separators(like comma) not allowed in lookup of text custom fi
    ... Project 2007 as any of the value lists with commas included will not ... in not only the field values but in the lookup tables as well. ... comma already exist and it works perfectly fine there.However, ... cannot include the list separator ...
    (microsoft.public.project)