Solved

Comma operator and order or precedence

Posted on 2013-12-11
5
284 Views
Last Modified: 2013-12-11
Ah hello.

I am sure I am having a silly moment here, but I am trying to understand the comma operator in C++ and "operator precedence" rules (which I believe are a general maths thing, hence me posting this in the Maths TA too:)).

From what I have read about the comma operator, we have two facts

1) The result of it is the right hand value, so for example a,b would yield b.
2) It has the lowest operator precedence.  I read this means "it's always the last one to bind to an expression", but I don't quite get that bit.

So, if we have

a = (b , c)

the evaulation sequence would first be (b,c) which would yield c, hence a would be assigned c.  But I am struggling with

a = b , c

This is apparently equivalent to

(a = b) , c

The result of this expression is b.  

Now, I know we have to evaluate the brackets first, which results in b, so we then have

b, c

Q1) From rule 1 above, why is the result hence not c?  Clearly something is incorrect in what I deduced above, but I cannot see it...

Q2) Can someone clarify fact 2) above and what "it's always the last one to bind to an expression" means?

TIA
0
Comment
Question by:mrwad99
  • 3
  • 2
5 Comments
 
LVL 31

Expert Comment

by:Zoppo
ID: 39710942
Hi mrwad99,

1. This is because the comma seperates the two statements, so the expression a = b , c is equivalent to ( a = b ) , c

For some further info about comma-operator you can start reading http://en.wikipedia.org/wiki/Comma_operator

One maybe important fact: In C++ it's possible to override comma-operators which may lead to confusion since if it is overridden it starts acting like a function call which means there's no order defined in which left- and rightside expressions are executed. For this you can find a sample at i.e. http://madebyevan.com/obscure-cpp-features/

Hope that helps,

ZOPPO
0
 
LVL 19

Author Comment

by:mrwad99
ID: 39710950
Thanks ZOPPO, but what you say I have already observed.  The result of

( a = b ) , c

is (and this is where I might be wrong!)

b, c

which is why I cannot see why the overall answer is not c!
0
 
LVL 31

Accepted Solution

by:
Zoppo earned 200 total points
ID: 39710959
Yes, that's correct, but the c is never assigned to anything. You have two different statements, "a = b" and "c", so a is equal to b afterward, no matter in which order the statements are evaluated.

Maybe it's clear when you check this:

   x = ( a = b, c );

This results in 'a = b' and 'x = c'.
0
 
LVL 19

Author Closing Comment

by:mrwad99
ID: 39710972
Ah-ha!  Your statement

"but the c is never assigned to anything"

plus the lovely little example perfectly explain this.

Thank you!
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 39710979
You're welcome, I'm glad I could help :o)

Have a nice day,

best regards,

ZOPPO
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Revenue table 8 87
computer science syllabus 3 69
Energy conservation - Edward Leedskalnin 20 93
deducing meaning of a word from its prefix-question 7 69
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now