-3

Possible Duplicate:
Behavior of post increment in cout

//Increment Decrement
#include <iostream>
using namespace std;


int main()
{
int a=5,b=6;

cout<<++a<<a++<<++a<<++a<<++a;

return 0;

}

My expected output should be 108876 but g++ compiler shows the output as 108101010

Community
  • 1
  • 1
mabus44
  • 141
  • 1
  • 4
  • 12

2 Answers2

2

It is an Undefined Behavior. So we cannot define that behavior for you.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • "Unspecified behavior", right? – John Jul 12 '11 at 18:18
  • But there should be some rules on which the post increement and preincrement runs.... Are you implying that it has not defined in g++ compiler? – mabus44 Jul 12 '11 at 20:45
  • @user278092: This is undefined behavior because an object is getting modified more than once without an intervening sequence point. – Alok Save Jul 13 '11 at 04:15
  • @John: An Specifed behavior leads to an Undefined Behavior. – Alok Save Jul 13 '11 at 04:15
  • @Als: Not really. The order of argument evaluation for a function call is unspecified, but it doesn't lead to undefined behavior. Undefined behavior is specifically called out in the standard as "all bets are off, even for before the undefined behavior occurs." Unspecified behavior adds indeterminism for a limited scope. – John Jul 13 '11 at 13:22
2

As others will doubtlessly point out, you have undefined behavior, so you really shouldn't expect anything. But I'm curious where you derived your "expected behavior" from. If the behavior were determined (e.g. as it is in Java), I would expect something like 668910.

James Kanze
  • 150,581
  • 18
  • 184
  • 329