one question about comma | | |
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... | | | | re: one question about comma
Bo Sun wrote:[color=blue]
> 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;[/color]
Yes.
[color=blue]
> 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?[/color]
The first. The order is well-defined and it's left-to-right.
V | | | | re: one question about comma
[color=blue]
> The first. The order is well-defined and it's left-to-right.[/color]
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 | | | | re: one question about comma
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 | | | | re: one question about comma
Bo Sun <b0s6067@cs.tamu.edu> wrote in message news:<Pine.GSO.4.58.0410281414300.6641@unix.cs.tam u.edu>...[color=blue]
> 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:[/color]
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 | | | | re: one question about comma
Arijit wrote:[color=blue]
> 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 ?[/color]
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 | | | | re: one question about comma
Arijit posted:
[color=blue]
> 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[/color]
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 | | | | re: one question about comma
[color=blue]
> goto Loop_begin;[/color]
goto Loop_Body;
-JKop | | | | re: one question about comma pal_ari@yahoo.co.in (Arijit) wrote in message news:<dff492d.0410291045.7422ac65@posting.google.c om>...[color=blue]
> 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 ?[/color]
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. |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,510 network members.
|