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

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.

Comments
Well said.

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.

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

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

Comments
Great explanation.

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.

Edited 7 Years Ago by __avd: n/a

This article has been dead for over six months. Start a new discussion instead.