help with expression

eilon's Avatar, Join Date: Aug 2014
Newbie Member
i have this code

int i=3;
int j=(++x)*(++x);

now j become 25, why?
cant explain it, why not 4*5=20?
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Because value is incremented and then applied to the expression.

It also depends on parser to parser but most of them would result in 25
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
The behaviour is undefined. All the standard says about preincrement is that the variable is incremented before it is used; it does not say WHEN it is incremented. So it could be equivalent to 4*5 as you expect, but it could equally be equivalent to ++x; ++x; int j=x*x; which gives 25.
shabbir like this
0
eilon's Avatar, Join Date: Aug 2014
Newbie Member
thanks