472,805 Members | 3,073 Online

hi,

suppose I have the following expression (all variables are of integer
type)

result = (first = 2, second = first + 1, third = second + 1);

what is the value of result?

1) result is 4. because the expressions are evaluated from left to right;

2) result is unpredictable. Because the comma operator returns the value
of he last expression. for the presivous expressions, there is no
guarantee of theorder of evaluation. Therefore, we could evaluate in the
following order:

second = first + 1;
first = 2;
third = second + 1;

therefore, result is unpredictable.

Which one is correct?

Many thanks...
Jul 22 '05 #1
8 1347
Bo Sun wrote:
suppose I have the following expression (all variables are of integer
type)

result = (first = 2, second = first + 1, third = second + 1);

what is the value of result?

1) result is 4. because the expressions are evaluated from left to right;
Yes.
2) result is unpredictable. Because the comma operator returns the value
of he last expression. for the presivous expressions, there is no
guarantee of theorder of evaluation. Therefore, we could evaluate in the
following order:

second = first + 1;
first = 2;
third = second + 1;

therefore, result is unpredictable.

Which one is correct?

The first. The order is well-defined and it's left-to-right.

V
Jul 22 '05 #2
The first. The order is well-defined and it's left-to-right.

Also, even if you have dozens of commas, it's always to the last
expression, eg.

int k = blah, blah2, blah3, blah4(), (blah5 ? 2 : 3), blah 7,
final_blah;
In the above, k's value becomes that of the expression "final_blah".
-JKop
Jul 22 '05 #3
Where is comma operator used in practice ? I think one example is in for loops
increment. Is there any other place where it is used commonly ?

-Arijit
Jul 22 '05 #4
Bo Sun <b0*****@cs.tamu.edu> wrote in message news:<Pi******************************@unix.cs.tam u.edu>...
suppose I have the following expression (all variables are of integer
type)

result = (first = 2, second = first + 1, third = second + 1);

what is the value of result?

1) result is 4. because the expressions are evaluated from left to right;

2) result is unpredictable. Because the comma operator returns the value
of he last expression. for the presivous expressions, there is no
guarantee of theorder of evaluation. Therefore, we could evaluate in the
following order:

One thing to keep in mind is the difference between the comma operator and
the commas used to seperate function arguments.

The comma operator guarantees the order of evaluation (left or right).

But the commas that separate the function arguments have no such guarantee.

So, in your example, after your statement, first will equal 2, second will
equal three, and both third and result will equal 4.

But if you change that around to this:
void some_func(int, int, int);
some_func(first = 2, second = first + 1, third = second + 1);

In this case, the value of the second and third arguments to some_func() and
the values of second and third after the call to some_func() have no guaranteed
value.

samuel
Jul 22 '05 #5
Arijit wrote:
Where is comma operator used in practice ? I think one example is in for loops
increment. Is there any other place where it is used commonly ?

I rarely use it even there. Of course, you still can call it "commonly"
if you consider where else it's used in my code (hint: basically nowhere).

V
Jul 22 '05 #6
Arijit posted:
Where is comma operator used in practice ? I think one example is in
for loops increment. Is there any other place where it is used commonly
?

-Arijit

I always use it when I'm only allowed give one statement, but I want to give
more. For example, the most basic loop:
int main()
{
int i = 2;
int s = 7;

Loop_Body:
{
if ( i == 4 ) s = 2;
}

Loop_Performed_after_each_iteration:
{
i += 2;
s -= 3;

//Here we've got two statements after each
//iteration.
}

goto Loop_begin;
Loop_Ended:
}
In the above, the usual "break" statement would become "goto Loop_ended;".

First_expression_we_want_evaluated;
break;

everywhere where we would just want "break". This can be achieved like so:

#include <fstream>

int main()
{

char p;

do
{
//processing
}
while( blah >> k, !blah.eof() );
}
Now here, we can just call "break" whenever we want and we know that a new
char will be pulled in...
-JKop

Jul 22 '05 #7
goto Loop_begin;

goto Loop_Body;
-JKop
Jul 22 '05 #8
pa*****@yahoo.co.in (Arijit) wrote in message news:<df*************************@posting.google.c om>...
Where is comma operator used in practice ? I think one example is in for loops
increment. Is there any other place where it is used commonly ?

Function-like macros are another common place to see it used. Of
course, these are far more common in C than C++...

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.