473,395 Members | 1,756 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,395 software developers and data experts.

comma operator

int i,j=10,k=11;
i=j,k;//here i=j
i=(j,k);//here i=k
Dec 7 '09 #1

✓ answered by Banfa

This is related to both operator precedence and the value returned by the comma operator.

You could easily look both of these up in which case you would be able to tell what the value of

l = (i=j,k);

l (assuming it is another int) is in this expression.

What most people forget is how the comma operator works, it has the syntax

<expression1>,<expression2>

but it returns the value <expression2>. Now at this point you are probably about to ask if it returns the value <expression2> then how can i = j in the statement

i=j,k;

surely i should equal k if the comma operator is returning <expression2>. However both = and , are operators so the order they get evaluated in in C is dependent on their relative precedence. Looking at an operator precedence table you will quickly see that the comma operator has the lowest precedence of any operator, that is everything takes precedence over it which means that

i=j,k;

is evaluated as

(i=j),k;

which is why i = j following the statement. So coming back to my original question what is the value of l following

l = (i=j,k);

We know that i = j because of the relative operator precedence of = and , I have surrounded the expression in ( ) to force it to evaluate first (i.e. break the precedence order which is what ( and ) are for) l takes on the value of the expression

i=j,k

which we know is the second expression so l = k.

To be honest I have never come across what I would call a good use for the comma operator except in code obfuscation contests.

7 1709
Markus
6,050 Expert 4TB
Do you have a question?
Dec 7 '09 #2
actually i forgot to type my query.
ya my question is regarding assignment in the two expression.
in the second expression why is i=k. why cant it be like this i=j.
thanx
Dec 9 '09 #3
RedSon
5,000 Expert 4TB
Have you tried testing these out in code to see what happens?
Dec 9 '09 #4
ya i tried both code. i needed the reason for two assignment.
Dec 9 '09 #5
RedSon
5,000 Expert 4TB
Have you tried Google?

I suggest you search for "c++ assignment operator" and read the articles.
Dec 9 '09 #6
Banfa
9,065 Expert Mod 8TB
This is related to both operator precedence and the value returned by the comma operator.

You could easily look both of these up in which case you would be able to tell what the value of

l = (i=j,k);

l (assuming it is another int) is in this expression.

What most people forget is how the comma operator works, it has the syntax

<expression1>,<expression2>

but it returns the value <expression2>. Now at this point you are probably about to ask if it returns the value <expression2> then how can i = j in the statement

i=j,k;

surely i should equal k if the comma operator is returning <expression2>. However both = and , are operators so the order they get evaluated in in C is dependent on their relative precedence. Looking at an operator precedence table you will quickly see that the comma operator has the lowest precedence of any operator, that is everything takes precedence over it which means that

i=j,k;

is evaluated as

(i=j),k;

which is why i = j following the statement. So coming back to my original question what is the value of l following

l = (i=j,k);

We know that i = j because of the relative operator precedence of = and , I have surrounded the expression in ( ) to force it to evaluate first (i.e. break the precedence order which is what ( and ) are for) l takes on the value of the expression

i=j,k

which we know is the second expression so l = k.

To be honest I have never come across what I would call a good use for the comma operator except in code obfuscation contests.
Dec 10 '09 #7
RedSon
5,000 Expert 4TB
Shoot!

I had a big counter argument for why Banfa was wrong complete with research and links and whatnot and then I re-read the post and my argument evaporated. I was going to say: Oh yea, well then how do you explain the right-to-left associativity of the assignment operator in the expression
a = b = c = d = 1; since it is interpreted as (a = (b = (c = (d = 1))));
But then I noticed that you are talking about the comma between the right hand operands as in i = j,k;
I'm such a dunce some times :(
Dec 10 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so...
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
7
by: Koster | last post by:
Hi folks, As I understand it, amongst other things, the comma operator may be used to cause any number of expressions to be evaluated (who's results are thrown away except the last) where only...
11
by: Shawn Odekirk | last post by:
Some code I have inherited contains a macro like the following: #define setState(state, newstate) \ (state >= newstate) ? \ (fprintf(stderr, "Illegal...
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...
2
by: grid | last post by:
Hi, I need some clarifications on how the comma operator is used to return values from function like macros.I saw a typical implementation as : #define sigfillset(ptr) ( *(ptr) &= ~(sigset_t)0 ,...
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...
1
by: CJK | last post by:
this is what i have so far: #include <iostream> #include <string> #include <vector> #include <fstream> using namespace std; class Person {
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...

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.