Adding one to a counter is the most frequent single operation in programming. C provides a special shorthand for it, and another for subtracting one. They are short and convenient, but their two forms behave differently and must be understood exactly.
The Operators
Definition: Increment operator. The operator ++, which increases the value of a variable by one.
Definition: Decrement operator. The operator --, which decreases the value of a variable by one.
The statement i++; has the same effect as i = i + 1;, and i--; has the same effect as i = i - 1;. These operators can be applied only to variables. An expression such as 5++ or (a + b)++ is not allowed, because there is no variable in which to store the result.
Prefix and Postfix Forms
Each operator can be written before or after the variable.
- Prefix (
++i): the variable is changed first, and the new value is used in the expression. - Postfix (
i++): the present value is used in the expression, and the variable is changed afterwards.
Table 3.9: The four forms, starting each time with i = 4
| Statement | Name | Value of j | Value of i afterwards |
|---|---|---|---|
j = ++i; |
Prefix increment | 5 | 5 |
j = i++; |
Postfix increment | 4 | 5 |
j = --i; |
Prefix decrement | 3 | 3 |
j = i--; |
Postfix decrement | 4 | 3 |
In every case the variable i itself changes by one. The two forms differ only in the value that the expression passes on.
When the Form Does Not Matter
When the operator is used as a statement on its own, as in i++; or ++i;, nothing uses the value of the expression, and the two forms have exactly the same effect. This is the commonest use. In for (i = 1; i <= n; i++), either form could be written.
Writing Clear Code
The difference between the forms matters only when the operator appears inside a larger expression. Such expressions are compact but easy to misread. Two rules keep programs safe and clear.
- Prefer to write the increment or decrement as a separate statement.
- Never change the same variable more than once in a single expression. An expression such as
i = i++ + ++ihas no defined result in C; different compilers may give different answers.
Related Shorthand
C also provides compound assignment operators. sum += x means sum = sum + x, and similarly for -=, *=, /= and %=. They change a variable by any amount, whereas ++ and -- change it by exactly one.
Example 3.9: Prefix and postfix in action
Problem: Show the effect of the prefix and postfix forms of the increment and decrement operators.
Solution:
- Start with i = 4 before each statement.
- Assign the result of each form to j and display both variables.
#include <stdio.h>
int main(void)
{
int i, j;
i = 4;
j = ++i;
printf("j = ++i gives i = %d, j = %d\n", i, j);
i = 4;
j = i++;
printf("j = i++ gives i = %d, j = %d\n", i, j);
i = 4;
j = --i;
printf("j = --i gives i = %d, j = %d\n", i, j);
i = 4;
j = i--;
printf("j = i-- gives i = %d, j = %d\n", i, j);
return 0;
}
Output:
j = ++i gives i = 5, j = 5
j = i++ gives i = 5, j = 4
j = --i gives i = 3, j = 3
j = i-- gives i = 3, j = 4
Result: The output agrees with Table 3.9. In the prefix forms j receives the changed value; in the postfix forms j receives the value that i had before the change.
Common mistakes
- Assuming that
j = i++gives j the increased value. - Changing a variable twice in one expression, such as
i = i++ + ++i. - Applying
++to a constant or to an expression instead of a variable.
Key takeaways
++adds one to a variable and--subtracts one.- Prefix changes the variable first and then uses the new value.
- Postfix uses the old value first and then changes the variable.
- As a separate statement, both forms behave identically.
Practice questions
- If a is 10, what are the values of a and b after b = a++; ?
- If a is 10, what are the values of a and b after b = --a; ?
- Is the expression (x + y)++ valid in C?
Answers to the practice questions
- a is 11 and b is 10.
- a is 9 and b is 9.
- No. The operator can be applied only to a variable.
Sources
- Official BCA syllabus (NEP 2025 scheme), Problem Solving Techniques, course code 25BCA401SEC01, Unit III
