I am not able to understand prefix operator behaviour in scenario when it is used multiple times in a statement .
Here is an example code to illustrate my problem
#include<iostream>
using namespace std;
int main()
{ int a=2,adup=2;
int b = (++a) * (++a);
cout<<endl<<"square of "<<a<<" is "<<b<<endl;//i get 16
int c = (++adup) * (++adup) * (++adup);
cout<<endl<<"adup is "<<adup<<" and c is "<<c<<endl;// i get 80
return 0;
}
Save above file as code.cpp and run 'gcc code.cpp -lstdc++' to verify.
It seems that in first case 2 was incremented twice then b = 4x4
But in second case I was hoping 5x5x5 on the lines of above but we ge 4x4x5 . How can that be explained ?
Thanks.
BTW , I found that postfix operator seems logical . it simply updates value and uses it next time variable is used even when in same statement .
After reading answers my conclusion is - Can it be said that using pre or post operation on same variable more than once in a statement will be undefined behaviour regardless of anything on LHS of expression ?