Prefix Increment and Decrement Operators: ++, --
unary-expression :
++ unary-expression
--
unary-expression
The unary operators (++ and
--)
are called “prefix” increment or decrement operators when the increment or
decrement operators appear before the operand. Postfix increment and
decrement has higher precedence than prefix increment and decrement
operators. The operand must have integral, floating, or pointer type and
must be a modifiable l-value expression (an expression without the const
attribute). The result is not an l-value.
When the operator
appears before its operand, the operand is incremented or decremented and
its new value is the result of the expression.
Example
In the following example, first
the variable
nNum2
is incremented. Then the sum of
nNum1
and nNum2 is placed into
nTotal.
// Example of the prefix increment operator
int nNum1=1, nNum2=2, nTotal;
nTotal = (nNum1) + (++nNum2); // nTotal now is 4
|