Question
In C, what is the difference between ++i and i++, and which form should be used in the increment section of a for loop?
Short Answer
By the end of this page, you will understand how pre-increment (++i) and post-increment (i++) work in C, when they produce different results, and why both are usually equivalent in the increment part of a for loop.
Concept
In C, both ++i and i++ increase the value of i by 1, but they differ in when the old or new value is produced as the result of the expression.
++iis called pre-increment.- It increments
ifirst. - Then the expression evaluates to the new value.
- It increments
i++is called post-increment.- It evaluates to the current value first.
- Then
iis incremented.
Why this matters
This difference only matters when the increment expression is used as part of a larger expression.
For example:
int i = 5;
int a = ++i; // i becomes 6, a gets 6
int i = 5;
int a = i++; // a gets 5, then i becomes 6
In real C programs, understanding this helps you:
- avoid logic bugs
- read loop code correctly
- avoid undefined or confusing expressions
- write clearer intent when updating counters
In a for loop
Mental Model
Think of i as a number written on a whiteboard.
++imeans: erase the number, write the bigger number, then show iti++means: show the current number, then erase it and write the bigger number
Example with i = 3:
++igives you4, andiis now4i++gives you3, butibecomes4immediately after
In a for loop update section, nobody is looking at the returned value. They only care that the whiteboard number increases. That is why both forms behave the same there.
Syntax and Examples
Core syntax
++i; // pre-increment
i++; // post-increment
Both increase i by 1.
Example 1: used alone
int i = 0;
++i;
printf("%d\n", i); // 1
i++;
printf("%d\n", i); // 2
When used by themselves on a line, both simply increment the variable.
Example 2: used in an assignment
int i = 5;
int a = ++i;
printf("i = %d, a = %d\n", i, a); // i = 6, a = 6
++i increments first, so both i and a become 6.
int i = 5;
int a = i++;
printf(, i, a);
Step by Step Execution
Consider this code:
int i = 2;
int x = i++;
int y = ++i;
Step by step:
istarts as2.int x = i++;xgets the current value ofi, which is2- then
iis incremented to3
int y = ++i;iis incremented first, from3to4- then
ygets that new value, which is4
Final values:
i == 4
x == 2
y == 4
Real World Use Cases
Loop counters
The most common use is counting iterations:
for (int i = 0; i < count; i++) {
// process item i
}
Array traversal
Increment operators are often used while walking through arrays:
for (int i = 0; i < size; i++) {
sum += numbers[i];
}
Pointer movement
In C, incrementing pointers is very common:
char *p = buffer;
p++; // move to next character
When expression value matters, choosing pre- or post-increment becomes important.
Reading tokens or characters
char c = *p++;
This means:
- read the character currently pointed to by
p - then move
pto the next position
That is different from:
char c = *++p;
Real Codebase Usage
In real C codebases, developers use ++ in a few common ways.
1. Loop updates
for (int i = 0; i < n; i++)
or
for (int i = 0; i < n; ++i)
In C, both are fine in loop updates because the result is discarded.
2. Guarded processing loops
for (int i = 0; i < count; i++) {
if (items[i] == NULL) {
continue;
}
process(items[i]);
}
The increment is separate from the logic, which keeps the loop readable.
3. Pointer-based parsing
while (*p != '\0') {
putchar(*p);
p++;
}
Or more compactly:
while (*p) {
putchar(*p++);
}
This pattern is common, but beginners should be careful because expression order matters more.
Common Mistakes
1. Thinking they are always identical
They are not always identical.
int i = 5;
int a = ++i; // a = 6
int i = 5;
int a = i++; // a = 5
If the value is used, the difference matters.
2. Writing overly complex expressions
Broken or confusing example:
int i = 1;
int x = i++ + ++i;
This is hard to reason about and may lead to undefined behavior in C because i is modified more than once without a proper sequencing relationship.
Avoid this completely.
Safer version:
int i = 1;
int x = i;
i++;
++i;
x = x + i;
Or better, rewrite the logic clearly.
3. Assuming ++i is always faster in C
In C, especially for built-in integer types, modern compilers usually generate equally efficient code for loop counters.
Comparisons
| Expression | What happens first | Expression result | Final effect on variable |
|---|---|---|---|
++i | Increment | New value | i increases by 1 |
i++ | Use current value | Old value | i increases by 1 |
++i vs i++ in common situations
| Situation | Better choice | Why |
|---|---|---|
| Standalone increment statement | Either | Both just add 1 |
Cheat Sheet
Quick rules
++i= increment first, then use the valuei++= use the value first, then increment- If used alone on a line, both usually have the same effect
- In a
forloop update section, both are usually equivalent in C
Examples
int i = 3;
int a = ++i; // i = 4, a = 4
int i = 3;
int a = i++; // a = 3, i = 4
Loop usage
for (int i = 0; i < 10; i++)
for (int i = 0; i < 10; ++i)
Both are acceptable in C.
Safe advice
- Use whichever is clearer in loop updates
- Do not modify the same variable multiple times in one expression
- Keep increment expressions simple
Pointer reminder
FAQ
Is ++i faster than i++ in C?
Usually not in any meaningful way for basic integer variables. Modern C compilers typically optimize both forms well.
Which should I use in a for loop in C?
Either i++ or ++i is fine in the increment section of a for loop, because the expression result is not used.
Do ++i and i++ always give the same result?
No. They both increment the variable, but the value produced by the expression is different.
When does the difference actually matter?
It matters when the increment expression is part of a larger expression, such as an assignment, function argument, or pointer expression.
Is i = i + 1 the same as i++?
As an update, yes, both increase i by 1. But i++ also has a specific expression result: it returns the old value before incrementing.
Why do some programmers prefer ++i in loops?
Sometimes it is a style habit, and in some other languages or types it may matter more. In plain C integer loops, there is usually no practical difference.
Mini Project
Description
Build a small C program that demonstrates the difference between pre-increment and post-increment in assignments and shows that both forms behave the same in a simple for loop. This helps turn the concept into something you can run and verify yourself.
Goal
Create a program that prints the results of ++i, i++, and two equivalent for loops so you can compare their behavior directly.
Requirements
- Create one example using
++iin an assignment - Create one example using
i++in an assignment - Add one
forloop usingi++ - Add another
forloop using++i - Print enough output to compare the results clearly
Keep learning
Related questions
Building More Fault-Tolerant Embedded C++ Applications for Radiation-Prone ARM Systems
Learn practical C++ and compile-time techniques to reduce soft-error damage in embedded ARM systems exposed to radiation.
Definition vs Declaration in C and C++: What’s the Difference?
Learn the difference between declarations and definitions in C and C++ with simple examples, common mistakes, and practical usage.
Difference Between #include <...> and #include "..." in C and C++
Learn the difference between #include with angle brackets and quotes in C and C++, including search paths, examples, and common mistakes.