HP C A.06.05 Reference Manual
Expressions and Operators
Increment and Decrement Operators (++, --)
Chapter 5110
You need to be careful, however, when you use the increment and decrement operators within
an expression.
Standalone Increment Decrement Expressions
For example, as a stand-alone assignment or as the third expression in a for loop, the side
effect is the same whether you use the prefix or postfix versions. The statement
x++;
is equivalent to
++x;
Similarly, the statement
for (j = 0; j <= 10; j++)
is equivalent to
for (j = 0; j <= 10; ++j)
Using Increment and Decrement within Expressions Consider the following function
that inserts newlines into a text string at regular intervals.
#include <stdio.h>
void break_line(int interval)
{
int c, j=0;
while ((c = getchar()) != '\n') {
if ((j++ % interval) == 0)
printf("\n");
putchar(c);
}
}
This works because the postfix increment operator is used. If you use the prefix increment
operator, the function breaks the first line one character early.
Side Effects of the Increment and Decrement Operators The increment and
decrement operators and the assignment operators cause side effects. That is, they not only
result in a value, but they change the value of a variable as well. A problem with side effect
operators is that it is not always possible to predict the order in which the side effects occur.
Consider the following statement:
x = j * j++;