943,619 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 740
  • C++ RSS
Sep 23rd, 2009
0

m i right to think that the output is inverted?

Expand Post »
Hi, there is a simple code,but i cant understand the output:
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nida afaq is offline Offline
8 posts
since Aug 2008
Sep 23rd, 2009
1

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

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.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Sep 23rd, 2009
0

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

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sankalp_999 is offline Offline
6 posts
since Sep 2009
Sep 23rd, 2009
0

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

can u plz explain that when is the order of precedense will be right to left?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nida afaq is offline Offline
8 posts
since Aug 2008
Sep 23rd, 2009
1

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

>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/
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007
Sep 24th, 2009
0

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

Ok,Thanks a lot...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nida afaq is offline Offline
8 posts
since Aug 2008
Sep 24th, 2009
0

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

Take a look at this thread - Everything you wanted to know about undefined behaviour (but were afraid to ask).

Quote ...
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.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Download certificate and install
Next Thread in C++ Forum Timeline: Binary Search Tree, Array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC