views:

114

answers:

1
+1  Q: 

Different outputs

Possible Duplicate: C++ pre/post increment expression evaluation

#include<iostream>
using namespace std;
int main()
{
     int a=5;
     b=a++*++a; //(A) gives 36
     cout<<(a++*++a); //(B) gives 35 
     return 0;
}

Why expressions marked as (A) and (B) gives different outputs?

+11  A: 

Both the expressions (A) and (B) invoke "Undefined Behavior" because the value of a is changing more than once between two Sequence Points.

The C-Standard says that :

1) Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression.

2) Furthermore, the prior value shall be accessed only to determine the value to be stored.

The first sentence rules out both your expressions.

Prasoon Saurav