m i right to think that the output is inverted?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2008
Posts: 8
Reputation: nida afaq is an unknown quantity at this point 
Solved Threads: 0
nida afaq nida afaq is offline Offline
Newbie Poster

m i right to think that the output is inverted?

 
0
  #1
Sep 23rd, 2009
Hi, there is a simple code,but i cant understand the output:
  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. int x=10;
  7. cout<<++x<<" "<<++x<<" "<<++x<<endl;
  8. x=10;
  9. cout<<x++<<" "<<x++<<" "<<x++<<endl;
  10. getch();
  11. }
output:
13 12 11
12 11 10
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,983
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: m i right to think that the output is inverted?

 
1
  #2
Sep 23rd, 2009
If your teacher taught you this, (s)he should be fired.

void main
clrscr
And use cin.get() instead of getch(); It's a non-standard function.
Also update your Turbo compiler to something from the last 10 years by downloading code::blocks (free)

Now for your question: There's no way to know what the ouput it. It is completely undefined behaviour as you've wrote it. My rule of thumb: Never use more then 1 increment operator in a statement.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 1
Reputation: sankalp_999 is an unknown quantity at this point 
Solved Threads: 0
sankalp_999 sankalp_999 is offline Offline
Newbie Poster

Re: m i right to think that the output is inverted?

 
0
  #3
Sep 23rd, 2009
see the precedence of the operator "<<" is from right 2 left whereas the output is delivered from left 2 right.
take it this way -

int x=10;
cout<<++x(3)<<" "<<++x(2)<<" "<<++x(1)<<endl;


here all increment operators are pre-increment and since precedence of the operator "<<" is from right 2 left
=> (1) makes the value of x as 11 and keeps it there (at that position)
=> (2) makes the value of x as 12 and keeps it there
=> (3) makes the value of x as 13 and keeps it there
...... after all this the output is delivered from left 2 right i.e as (3),(2) and (1) i.e 13 12 11

NOW Case 2 -

x=10;
cout<<x++(c)<<" "<<x++(b)<<" "<<x++(a)<<endl;


here again precedence is in the same order right 2 left ( for processing) and left 2 right (for output).

all the increment operators here are post-increment
=> (a) makes the value of x as 11 and but keeps the value 10 *
=> (b) makes the value of x as 12 and but keeps the value 11
=> (c) makes the value of x as 13 and but keeps the value 12
...... after all this the output is delivered from left 2 right i.e as (c),(b) and (a) i.e 12 11 10.

*note u need 2 know i thing abt post increment operators that they print the value first and then increment and pass it on.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 8
Reputation: nida afaq is an unknown quantity at this point 
Solved Threads: 0
nida afaq nida afaq is offline Offline
Newbie Poster

Re: m i right to think that the output is inverted?

 
0
  #4
Sep 23rd, 2009
can u plz explain that when is the order of precedense will be right to left?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 794
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: m i right to think that the output is inverted?

 
1
  #5
Sep 23rd, 2009
>can u plz explain that when is the order of precedense will be right to left?
Are you deaf?
Look at the post #2 and try to accept that what ever niek has said is actually true.
Your program is suffering from undefined behaviour. All that means to you is that 'anything' can happen when you run the program.
There is a FAQ on Bjarne Stroustrup's FAQs(http://www.research.att.com/~bs/bs_f...aluation-order) and also on Marshal Cline's FAQs(http://www.parashift.com/c++-faq-lit...html#faq-39.15)

Try to read and understand them.
In simple term:
1. Sequence point is a point of time while executing your code at which all the variables have defined values.
2. Any operation done which alter the value of a variable more than once in between only a pair of consecutive sequence point leads to undefined behaviour.

No doubt, there are teachers who put forward these kinds of question trying to show off how tricky their questions are . Actually, all they are doing is teaching their student to depend on studying implementation specific behaviour.

Regarding, the use of void main, conio.h and clrscr(), read http://siddhant3s.elementfx.com/
Siddhant Sanyam
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 8
Reputation: nida afaq is an unknown quantity at this point 
Solved Threads: 0
nida afaq nida afaq is offline Offline
Newbie Poster

Re: m i right to think that the output is inverted?

 
0
  #6
Sep 24th, 2009
Ok,Thanks a lot...
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,721
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 501
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: m i right to think that the output is inverted?

 
0
  #7
Sep 24th, 2009
Take a look at this thread - Everything you wanted to know about undefined behaviour (but were afraid to ask).

SUMMARY (posted by salem) :
This is a catalogue of some experiments on just two aspects of undefined behaviour.

This was inspired by yet another long bout of explanation in a recent thread, so I thought it was time to dust off a bunch of compilers, and compare.
Last edited by adatapost; Sep 24th, 2009 at 7:21 am.
Reply With Quote Quick reply to this message  
Reply


Message:




Views: 428 | Replies: 6
Thread Tools Search this Thread



Tag cloud for incrementoperators, pleasehelpmetolearn!!
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC