954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

m i right to think that the output is inverted?

Hi, there is a simple code,but i cant understand the output:

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x=10;
cout<<++x<<"  "<<++x<<"  "<<++x<<endl;
x=10;
cout<<x++<<"  "<<x++<<"  "<<x++<<endl;
getch();
}

output:
13 12 11
12 11 10

nida afaq
Newbie Poster
8 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

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.

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

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)<

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<

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.

sankalp_999
Newbie Poster
6 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

can u plz explain that when is the order of precedense will be right to left?

nida afaq
Newbie Poster
8 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

>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_faq2.html#evaluation-order ) and also on Marshal Cline's FAQs( http://www.parashift.com/c++-faq-lite/misc-technical-issues.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/

siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
 

Ok,Thanks a lot...

nida afaq
Newbie Poster
8 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

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.

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: