Increment and Decrement Operators in C
Adding 1 to a variable is so common that C has a special operator for it: ++. There is also -- to subtract 1.
The tricky part, and a favourite interview question, is the difference between i++ and ++i. In this lesson you will understand it with simple step-by-step tables.
What You Will Learn
- What
++and--do. - Pre-increment (
++i) vs post-increment (i++). - Pre-decrement and post-decrement.
- When the difference matters and when it doesn't.
- Why expressions like
i++ + ++imust be avoided.
Increment and Decrement Operators Explained
Exam definition: The increment operator (++) increases a variable's value by 1, and the decrement operator (--) decreases it by 1. Both are unary operators and have two forms: prefix (++x) and postfix (x++).
| Form | Name | What happens | If x = 5: y = ... | Then x |
|---|---|---|---|---|
++x | Pre-increment | First increase, then use | y = ++x → y = 6 | 6 |
x++ | Post-increment | First use, then increase | y = x++ → y = 5 | 6 |
--x | Pre-decrement | First decrease, then use | y = --x → y = 4 | 4 |
x-- | Post-decrement | First use, then decrease | y = x-- → y = 5 | 4 |
Memory trick: Look at where the ++ is. ++x: the ++ comes first, so add first, then use. x++: x comes first, so use first, then add. In both cases, x ends up 1 bigger.
When does the difference matter?
- On a line by itself (
i++;or++i;), there is no difference: both just add 1. - The difference matters only when the value is used in the same expression, like
y = x++orprintf("%d", x++).
Syntax of ++ and --
++variable; /* pre-increment */
variable++; /* post-increment */
--variable; /* pre-decrement */
variable--; /* post-decrement */
They work only on variables: 5++ or (a + b)++ are errors.
Example 1: Pre and Post Increment Step by Step
#include <stdio.h>
int main(void)
{
int x = 5, y;
y = x++; /* use 5, then x becomes 6 */
printf("After y = x++ : y = %d, x = %d\n", y, x);
y = ++x; /* x becomes 7, then use 7 */
printf("After y = ++x : y = %d, x = %d\n", y, x);
y = x--; /* use 7, then x becomes 6 */
printf("After y = x-- : y = %d, x = %d\n", y, x);
y = --x; /* x becomes 5, then use 5 */
printf("After y = --x : y = %d, x = %d\n", y, x);
return 0;
}
Output
After y = x++ : y = 5, x = 6 After y = ++x : y = 7, x = 7 After y = x-- : y = 7, x = 6 After y = --x : y = 5, x = 5
Code Explanation
y = x++;: y gets the old value 5, then x becomes 6.y = ++x;: x becomes 7 first, then y gets 7.y = x--;: y gets the old value 7, then x becomes 6.y = --x;: x becomes 5 first, then y gets 5.
How ++ Works Internally
x++is like:temp = x; x = x + 1;and the expression's value istemp(the old value).++xis like:x = x + 1;and the expression's value is the new x.- Modern compilers produce the same fast code for both when the value is not used, so on its own line, choose either.
Avoid changing a variable twice in one expression
Expressions like i++ + ++i, i = i++ or printf("%d %d", i++, i++) change the same variable more than once without a clear order. This is undefined behaviour: different compilers give different answers, and none of them is "the right one". Never write such code, and if an exam asks for its output, the correct answer is "undefined".
Example 2: Using ++ and -- in a Real Program
#include <stdio.h>
int main(void)
{
int tickets = 3;
int sold = 0;
printf("Tickets left: %d\n", tickets);
tickets--; sold++;
printf("Sold 1. Left: %d, Sold: %d\n", tickets, sold);
tickets--; sold++;
printf("Sold 1. Left: %d, Sold: %d\n", tickets, sold);
printf("Next ticket number: %d\n", ++sold);
printf("Sold count is now: %d\n", sold);
return 0;
}
Output
Tickets left: 3 Sold 1. Left: 2, Sold: 1 Sold 1. Left: 1, Sold: 2 Next ticket number: 3 Sold count is now: 3
On their own lines, tickets-- and sold++ simply subtract and add 1. In the last printf, ++sold adds 1 before printing, so it prints 3.
Important Points to Remember
++adds 1,--subtracts 1. Both are unary.++x(pre): change first, then use.x++(post): use first, then change.- On a line by itself,
x++and++xdo the same thing. - Changing a variable twice in one expression is undefined behaviour.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
5++ or (a+b)++ | Error: lvalue required | Use on a variable only |
i = i++; | Undefined behaviour | Just write i++; |
printf("%d %d", x++, x); | Undefined behaviour | Increment on a separate line |
x+ + with a space | Syntax error | No space: x++ |
Increment and Decrement Practice Questions
- What is the difference between
x++and++x? - If
a = 10, what are a and b afterb = a++;? - If
a = 10, what are a and b afterb = --a;? - Is there any difference between
i++;and++i;on their own lines? - What is the output of
i = 5; i = i++ + ++i;?
Show answers
x++uses the old value then adds 1;++xadds 1 first then uses the new value.- b = 10, a = 11.
- a = 9, b = 9.
- No, both add 1.
- Undefined behaviour: i is changed twice in one expression, so there is no correct fixed answer.
Coding Exercise
Start with int count = 10;. Print the result of count++, then count, then ++count, each on a separate printf line, and explain the three numbers.
Show solution
#include <stdio.h>
int main(void)
{
int count = 10;
printf("count++ gives %d\n", count++);
printf("count is now %d\n", count);
printf("++count gives %d\n", ++count);
return 0;
}
count++ gives 10 count is now 11 ++count gives 12
count++ prints the old value 10 and then makes it 11. ++count makes it 12 and then prints 12.
Quick Quiz: i++ vs ++i (MCQ)
-
If x = 3, what is y after
y = x++;?A) 3 B) 4 C) 2 D) 5
-
If x = 3, what is y after
y = ++x;?A) 3 B) 4 C) 2 D) 5
-
Increment and decrement operators are:
A) Unary B) Binary C) Ternary D) Logical
-
Which is invalid?
A)
a++B)--aC)10++D)++a -
Output of
int a = 5; printf("%d", a--);A) 4 B) 5 C) 6 D) Undefined
Show answers
- A: 3 (x becomes 4 after).
- B: 4.
- A: unary.
- C:
10++ - B: 5 (a becomes 4 after printing).
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
++ / -- | Add 1 / subtract 1 (unary) |
|---|---|
++x (pre) | Change first, then use |
x++ (post) | Use first, then change |
x = 5, y = x++ | y = 5, x = 6 |
x = 5, y = ++x | y = 6, x = 6 |
| On its own line | No difference |
i++ + ++i | Undefined behaviour |
Frequently asked questions
- What is the difference between i++ and ++i?
i++returns the old value then adds 1;++iadds 1 first and returns the new value.- Which is faster, i++ or ++i?
- For simple variables, the same: compilers produce identical code when the value isn't used.
- What is the output of i = i++?
- Undefined behaviour: i is modified twice without a defined order.
- Can ++ be used on a constant?
- No. It needs a variable (lvalue);
5++is an error.
Remember
- ++ first = add first; x first = use first.
- Never change the same variable twice in one expression.
Next Topic
Next lesson: Bitwise Operators in C. You will learn operators that work on the individual bits of a number.