473,402 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 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 1368
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",}" ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.