472,805 Members | 3,073 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

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...
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;".
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

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.

Similar topics

4
by: Arne | last post by:
From: "Arne de Booij" <a_de_booij@hotmail.com> Subject: Comma delimited array into DB problems Date: 9. februar 2004 10:39 Hi, I have an asp page that takes input from a form on the previous...
5
by: Derek | last post by:
I came upon the idea of writting a logging class that uses a Python-ish syntax that's easy on the eyes (IMO): int x = 1; double y = 2.5; std::string z = "result"; debug = "Results:", x, y,...
2
by: benben | last post by:
I am looking for a good example of overloading operator , (operator comma) Any suggestions? Ben
3
by: SteelDetailer | last post by:
Thnaks in advance for considering this post. It's probably very simple, but..... I have an old VB6 application that allows me to create, save and edit a "project information file" that is a...
4
by: G Patel | last post by:
Hi, I've read a book on C, and I understand how comma operators work, but my book didn't say that the comma operators between function arguments were not really comma operators (even though it...
21
by: siliconwafer | last post by:
Hi, In case of following expression: c = a && --b; if a is 0,b is not evaluated and c directly becomes 0. Does this mean that && operator is given a higher precedence over '--'operator? as...
9
by: Wayne | last post by:
I have the following string: "smith", "Joe", "West Palm Beach, Fl." I need to split this string based on the commas, but as you see the city state contains a comma. String.split will spilt the...
15
by: Lighter | last post by:
In 5.3.3.4 of the standard, the standard provides that "The lvalue-to- rvalue(4.1), array-to-pointer(4.2),and function-to-pointer(4.3) standard conversions are not applied to the operand of...
6
by: lazukars | last post by:
Hi, I am attempting an ajax request. I am using PHP serverside. Below is what the response text should be. ajaxRequest.responseText ="{username_is_taken : "This username has been taken",}" ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.