HP C A.06.05 Reference Manual

Expressions and Operators
Increment and Decrement Operators (++, --)
Chapter 5 109
Increment and Decrement Operators
(++, --)
Syntax
lvalue
++ Increments the current value of
lvalue
after
lvalue
has been referenced.
lvalue
-- Decrements the current value of
lvalue
after
lvalue
has been referenced.
++
lvalue
Increments the current value of
lvalue
before
lvalue
is referenced.
--
lvalue
Decrements the current value of
lvalue
before
lvalue
has been referenced.
Arguments
lvalue
Any previously declared integer or pointer lvalue. Although
lvalue
can be a
pointer variable, it cannot be a pointer to a function.
Description
The increment operator (++) adds 1 to its operand. The decrement operator (--) subtracts 1
from its operand.
The increment and decrement operators are unary. The operand must be a scalar lvalue — it
is illegal to increment or decrement a constant, structure, or union. It is legal to increment or
decrement pointer variables, but the meaning of adding 1 to a pointer is different from adding
1 to an arithmetic value. This is described in “Pointer Operators (*, ->, &)” on page 118.
Postfix and Prefix Forms
There are two forms for each of the operators: postfix and prefix. Both forms increment or
decrement the appropriate variable, but they do so at different times. The statement ++i
(prefix form) increments i before using its value, while i++ (postfix form) increments it after
its value has been used. This difference can be important to your program.
The postfix increment and decrement operators fetch the current value of the variable and
store a copy of it in a temporary location. The compiler then increments or decrements the
variable. The temporary copy, which has the variable's value before it was modified, is used in
the expression.
In many cases, you are interested only in the side effect, not in the result of the expression. In
these instances, it doesn't matter whether you use postfix or prefix.