one question about comma

B

Bo Sun

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...
 
V

Victor Bazarov

Bo said:
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
 
J

JKOp

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
 
A

Arijit

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
 
R

Ron Samuel Klatchko

Bo Sun said:
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
 
V

Victor Bazarov

Arijit said:
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
 
J

JKop

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;".
The nice thing about this is that we don't have:

First_expression_we_want_evaluated;
break;

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

#include <fstream>

int main()
{
ifstream blah("readme.txt");

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
 
J

Jerry Coffin

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++...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,968
Members
47,518
Latest member
TobiasAxf

Latest Threads

Top