Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,427 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,260 people online right now. Registration is fast and FREE... Join Now!




Operator precedence C++

 
Reply to this topicStart new topic

Operator precedence C++

amitdhar
4 Feb, 2008 - 01:19 PM
Post #1

New D.I.C Head
*

Joined: 1 Feb, 2008
Posts: 33



Thanked: 1 times
My Contributions
int a=10,c;
c= a++ + ++a + a-- - --a;
cout<<c;

The above code is giving output 20.
What is the sequence of operations in the above expression? Becasue according to operator precedence post increment/decrement has got higher precedence than pre increment/decrement.
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Operator Precedence C++
4 Feb, 2008 - 01:24 PM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
i won't give you the answer exactly as wanted, but i will help you out with an example:
CODE

if a=0;
cout<<a; //is 0
cout<<a++; //is stil 0, as a post increment the value of a is used first
//now a is 1
cout<<++a;// the pre increment will result in the addition before the cout, thus 2 is displayed


The only trick to your above example is that all operations take place on a single line, just do all pre order elements, and since nothing other than assignment is applied to them after a post operations, you can effectively remove them from the equation.
User is offlineProfile CardPM
+Quote Post

Bench
RE: Operator Precedence C++
4 Feb, 2008 - 03:38 PM
Post #3

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 683



Thanked: 24 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
QUOTE(amitdhar @ 4 Feb, 2008 - 09:19 PM) *

int a=10,c;
c= a++ + ++a + a-- - --a;
cout<<c;

The above code is giving output 20.
What is the sequence of operations in the above expression? Becasue according to operator precedence post increment/decrement has got higher precedence than pre increment/decrement.

Whatever you think the operator precedence might be is irrelevant. There's no correct answer, its undefined behaviour according to the language standard.

http://www.research.att.com/~bs/bs_faq2.ht...valuation-order
http://www.c-faq.com/expr/ieqiplusplus.html
User is online!Profile CardPM
+Quote Post

KYA
RE: Operator Precedence C++
4 Feb, 2008 - 04:33 PM
Post #4

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,850



Thanked: 159 times
Dream Kudos: 1375
My Contributions
I find it more helpful to have a goal when solving a problem like this. What is the point of the code segment?

--KYA
User is online!Profile CardPM
+Quote Post

Bench
RE: Operator Precedence C++
5 Feb, 2008 - 02:33 AM
Post #5

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 683



Thanked: 24 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
QUOTE(KYA @ 5 Feb, 2008 - 12:33 AM) *

I find it more helpful to have a goal when solving a problem like this. What is the point of the code segment?

Usually, I believe such expressions are used as an experiment of evaluation order. Unfortunately, it doesn't work for anything beyond compiler-specific behaviour, because compilers are free to evaluate each component in any order which they see fit, hence why the OP may not have gotten the output which they were expecting
User is online!Profile CardPM
+Quote Post

amitdhar
RE: Operator Precedence C++
5 Feb, 2008 - 06:36 AM
Post #6

New D.I.C Head
*

Joined: 1 Feb, 2008
Posts: 33



Thanked: 1 times
My Contributions
QUOTE(William_Wilson @ 4 Feb, 2008 - 02:24 PM) *

i won't give you the answer exactly as wanted, but i will help you out with an example:
CODE

if a=0;
cout<<a; //is 0
cout<<a++; //is stil 0, as a post increment the value of a is used first
//now a is 1
cout<<++a;// the pre increment will result in the addition before the cout, thus 2 is displayed


The only trick to your above example is that all operations take place on a single line, just do all pre order elements, and since nothing other than assignment is applied to them after a post operations, you can effectively remove them from the equation.



That is not clear to me.. even after all the pre order elements executed and post one ignored.. the given output of 20 is not coming.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 11:25AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month