HP C A.06.05 Reference Manual

Expressions and Operators
Increment and Decrement Operators (++, --)
Chapter 5 111
The C language does not specify which multiplication operand is to be evaluated first. One
compiler may evaluate the left operand first, while another evaluates the right operand first.
The results are different in the two cases. If j equals 5, and the left operand is evaluated first,
the expression will be interpreted as
x = 5 * 5; /* x is assigned 25 */
If the right operand is evaluated first, the expression becomes
x = 6 * 5; /* x is assigned 30 */
Statements such as this one are not portable and should be avoided. The side effect problem
also crops up in function calls because the C language does not guarantee the order in which
arguments are evaluated. For example, the function call
f(a, a++)
is not portable because compilers are free to evaluate the arguments in any order they choose.
To prevent side effect bugs, follow this rule: If you use a side effect operator in an expression,
do not use the affected variable anywhere else in the expression. The ambiguous expression
above, for instance, can be made unambiguous by breaking it into two assignments:
x = j * j;
++j;
Precedence of Increment and Decrement Operators
The increment and decrement operators have the same precedence, but bind from right to left.
So the expression
--j++
is evaluated as
--(j++)
This expression is illegal because j++ is not an lvalue as required by the operator. In general,
you should avoid using multiple increment or decrement operators together.
Examples
i=k--; /* Stores the value of k in i then decrements k. */
j=l++; /* Stores the value of l in j then increments l. */
i=--k; /* Decrements k then stores the new value of k in i. */
j=++l; /* Increments l then stores the new value of l in j. */
The following example uses both prefix and postfix increment and decrement operators: