0

Possible Duplicate:
What is the correct answer for cout << c++ << c;?

this is my piece of code:

#include <iostream>
using namespace std;

int main(){
int val = 10;
cout << "\n" << val++<< " "<< ++val << " " << val;
cin.get();
return 0;
}

The output returns

11 12 12

But shouldn't it be

10 12 12

?

I read somewhere that in case of cascading multiple values, the calculations go from right to left but the printing takes place from left to right as usual. Is that true ?

Community
  • 1
  • 1
zerovar
  • 23
  • 1
  • 4
  • Or this one: http://stackoverflow.com/q/949433/10077 – Fred Larson Sep 23 '12 at 03:46
  • 1
    Although the functions are called in a left-to-right sequence, the arguments are not necessarily evaluated left-to-right. You are invoking undefined behaviour, so the compiler's output is correct regardless of what it gives. – Jonathan Leffler Sep 23 '12 at 03:49
  • 3
    Welcome to Stack Overflow. Your question was closed rather quickly because it was the same question as one that someone else had asked. They got a decent answer to the question, too. Please don't hesitate to ask more questions, but do try to find out whether the question has been asked before. – Jonathan Leffler Sep 23 '12 at 03:51
  • I think post increment and pre-increment value result different when you assign it to another vatiable but both increment increase its value.For eg `int p=val++;' and `int q=++val;` increase the value of val but the assignment to p and q is before increment and after increment... – nKandel Sep 23 '12 at 03:53
  • @NarayanKandel: That's true, but that fact has no bearing on the problem presented in this question. – Benjamin Lindley Sep 23 '12 at 05:12

0 Answers0