C Notes
Master increment (++) and decrement (--) operators in C. Understand the difference between prefix and postfix, common pitfalls, undefined behavior, and practical usage in loops and expressions.
The increment (++) and decrement (--) operators are unary operators that add or subtract 1 from a variable. They're among the most frequently used operators in C — you'll see them in virtually every loop, counter, and pointer operation. But the distinction between prefix and postfix forms trips up many programmers, even experienced ones.
Basic Syntax
| Operator | Name | Effect |
|---|---|---|
++x | Pre-increment | Increment x, then use the new value |
x++ | Post-increment | Use the current value, then increment x |
--x | Pre-decrement | Decrement x, then use the new value |
x-- | Post-decrement | Use the current value, then decrement x |
Both forms ultimately change the variable by 1 — the difference lies in when the change takes effect in an expression.
Prefix vs Postfix: The Key Difference
Pre-increment: ++a = 6 a is now: 6 Post-increment: b++ = 5 b is now: 6
Think of it this way:
- Prefix (
++x): "Increment, then give me the value" — the operator comes first, so it acts first - Postfix (
x++): "Give me the value, then increment" — the operator comes after, so it acts after
Standalone Statements
When used as standalone statements (not inside larger expressions), prefix and postfix behave identically:
x = 10, y = 10 x = 10, y = 10
In Expressions: Where It Matters
The difference becomes critical when ++/-- is part of a larger expression:
a=6, b=5 a=6, b=6 result=13, a=4 result=14, a=4
Decrement Operators
Decrement operators work exactly the same way but subtract 1:
#include <stdio.h>
int main() {
int x = 10;
printf("x = %d\n", x);
printf("--x = %d\n", --x); // x becomes 9, prints 9
printf("x-- = %d\n", x--); // prints 9, then x becomes 8
printf("x = %d\n", x); // 8
// Countdown
int countdown = 5;
printf("\nCountdown: ");
while (countdown > 0) {
printf("%d ", countdown--);
}
printf("Blast off!\n");
return 0;
}x = 10 --x = 9 x-- = 9 x = 8 Countdown: 5 4 3 2 1 Blast off!
Usage in Loops
This is where you'll use increment/decrement operators most often:
Using i++: 0 1 2 3 4 Using ++i: 0 1 2 3 4 5! = 120 Elements: 10 20 30 40 50
Note: In a for loop's update expression (i++ vs ++i), both produce the same result because the expression value is discarded. However, for C++ iterators, ++i is preferred for efficiency.
With Pointers
Increment and decrement operators are extensively used with pointers:
*ptr = 100 *ptr++ = 100 *ptr = 200 *++ptr = 300 ++*ptr = 301
Understanding pointer operations with ++:
*ptr++→*(ptr++)— dereference, then advance pointer*++ptr→*(++ptr)— advance pointer, then dereference++*ptr→++(*ptr)— increment the pointed-to value(*ptr)++— use the pointed-to value, then increment it
Common Pitfalls and Undefined Behavior
Multiple modifications in one expression
Safe result: 12
Rule: Never modify a variable more than once between two sequence points. The C standard says this is undefined behavior, meaning the compiler can produce any result.
Confusing in function arguments
a=5, b=6
Practical Examples
Digit Counter
Number 123456 has 6 digits
String Length (Manual)
Length of "Hello, World!" = 13
Array Reversal
Reversed: 5 4 3 2 1
Comparison Table: Prefix vs Postfix
| Aspect | Prefix (++x) | Postfix (x++) |
|---|---|---|
| When increment happens | Before use in expression | After use in expression |
| Value of expression | New value (after increment) | Old value (before increment) |
| In standalone statement | Same effect | Same effect |
| In for loop update | Same effect | Same effect |
| Performance (in C) | Same | Same |
| Returns | lvalue (in C++) | rvalue |
Interview Questions
Q1: What is the output of int a = 5; printf("%d %d", a++, ++a);?
This is undefined behavior. The C standard does not specify the order in which function arguments are evaluated. The output is unpredictable and varies between compilers. Never write code like this.
Q2: In a for loop, is there any difference between i++ and ++i?
In C, there's no practical difference when used as a standalone statement in the loop's update expression — both increment i by 1 and the expression value is discarded. In C++ with iterators, ++i is preferred because i++ may create a temporary copy.
**Q3: What does *p++ do vs (*p)++?**
*p++ is *(p++) — it dereferences the pointer to get the current value, then advances the pointer to the next element. (*p)++ dereferences the pointer and increments the value at that memory location. The pointer itself doesn't move.
Q4: Why is i = i++ undefined behavior?
Because i is being modified twice in the same expression without a sequence point — once by the assignment i = ... and once by i++. The C standard says modifying a variable more than once between sequence points is undefined behavior.
Q5: Can you use ++ or -- with constants or expressions?
No. These operators require an lvalue — something with a memory location that can be modified. 5++ or (a+b)++ will result in a compilation error. Only variables (or dereferenced pointers) can be incremented/decremented.
Summary
++adds 1 and--subtracts 1 from a variable- Prefix (
++x) increments first, then returns the new value - Postfix (
x++) returns the current value, then increments - As standalone statements, prefix and postfix are identical
- Never modify the same variable twice in one expression — it's undefined behavior
- In for loops,
i++and++iproduce the same result - With pointers,
*p++and++*phave very different meanings - These operators only work on lvalues (variables), not constants or expressions
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Increment and Decrement Operators in C.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, operators, increment, decrement, increment and decrement operators in c
Related C Programming Topics