when ++ and -- are used in an expression such as
will the value of j in (i * j) be that before or after the increment in (i * j++)?
In addition to evaluating the expression, the value of the variable in memory is altered (this is called a side effect). Do not use a variable more than once in an expression if one (or more) of the references has one of these operators attached to it. The standard does not specify the order in which the operands of an operator are evaluated and there is no guarantee when an affected variable will change its value. For example:
-
int i=2, j=3;
-
printf("%d \n", (i * j++) + (i * j));
-
on a Windows PC the gcc compiler prints 12 the Boralnd Turobo C V3 compiler prints 14.
for we look at your example
-
int i=4;
-
int x = (++i) + (++i);
-
cout << x; // prints 12
-
the gcc compiler prints 12 the Boralnd Turobo C V3 compiler prints 11.