To share this page click on the buttons below;
Increment and decrement operators
This chapter deals with an argument apparently really simple: the increment and decrement of a variable.
There is an obvious way to increment and decrement a variable: if we suppose that a
is an integer we can increment that variable by one with the statement a = a + 1;
and we can decrement it by one with the statement a = a - 1;
(or we can even take it shorter and simply write a += 1
and a -= 1
). In this case however we are not using the increment / decrement operators but we are using the mathematical operators plus and minus. The previous syntax takes the value hold by the variable, sum or subtract 1 to that value and then assign the result back to the same variable.
Increment and decrement a variable by one however is so common that C introduced specific operators to perform such operations.
In particular C introduced 2 different notations to perform such operations: the postfix and the prefix notation.
The postfix notation
Suppose that you have an integer variable a
.
You can increment by one that variable with the postfix notation by writing a++;
and you can decrement the variable by one using a--;
.
The prefix notation
Suppose that you have again your a
integer variable.
You can increment by one that variable with the prefix notation by writing ++a;
and you can decrement the variable by one using --a;
.
What is the matter?
The difference between the postfix and the prefix notation is not the value of the incremented or decremented variable, but in the value that other variables will assume while the two different increment, decrement operations are used within other expressions. In the following I will use just the increment operator, but you can easily infer by yourself the behavior for the decrement operator.
Suppose that you have two integer variables a
and b
, suppose a = 5
and b = 0
.
Now suppose that you write b = ++a;
. At the end of this operation both variables will contain the value of 6. That is due to the fact that the prefix operator first increment and only after that the assignment will take place.
But if you now write b = a++;
something different will happen. At the end of this operation b
will remain equal to 6 (from the previous step), but a will be equal to 7. That is due to the fact that the postfix operator will be applied to a
after
the assignment.
The following example shows exactly this:
#include <stdio.h>
/* program to show postfix and prefix increment
decrement notation */
void main(void) {
int b = 0;
int a = 5;
printf(" (1) b = %d, a = %d\n", b, a);
b = ++a;
printf(" (2) b = %d, a = %d\n", b, a);
b = a++;
printf(" (3) b = %d, a = %d\n", b, a);
}
There are some extremely infrequent situations (really really rare) in which you can take advantage from this different behaviors. In those situations please comment the code "heavily". In all other cases be aware of this postfix / prefix caveat in order to avoid unexpected behavior and in any case use parenthesis to proper specify the order of the operations.
To share this page click on the buttons below;