Question
In C, is there any performance difference between i++ and ++i when the resulting value is not used?
For example, if I write code like this:
int i = 0;
i++;
++i;
and I do not use the value produced by either expression, will one form be faster than the other, or do they behave the same in practice?
Short Answer
By the end of this page, you will understand the difference between postfix increment (i++) and prefix increment (++i) in C, how their values differ, and why there is usually no performance difference when the result is not used. You will also learn when the distinction matters, common mistakes involving increment operators, and how compilers typically optimize these expressions.
Concept
In C, both i++ and ++i increase i by 1, but they differ in the value of the expression:
++iis prefix increment: increment first, then produce the new value.i++is postfix increment: produce the old value, then increment.
Core idea
If you only care that i becomes larger by 1, both forms achieve the same final change:
int i = 5;
++i; // i becomes 6
int i = 5;
i++; // i becomes 6
The important difference appears only when the expression's value is used:
int i = 5;
int a = ++i; // i = 6, a = 6
int i = 5;
int a = i++; // i = 6, a = 5
Mental Model
Think of i as a counter on a handheld clicker.
++imeans: click first, then show the number.i++means: show the current number, then click.
If nobody looks at the number being shown, both actions just end with the counter increased by 1.
So if the displayed value is ignored, the only thing that matters is that the counter moved forward once.
Syntax and Examples
Basic syntax
++i; // prefix increment
i++; // postfix increment
Example: result not used
#include <stdio.h>
int main(void) {
int i = 0;
i++;
++i;
printf("%d\n", i);
return 0;
}
Output:
2
Here, both lines simply increase i by 1.
Example: result used
#include <stdio.h>
int main(void) {
int i = 5;
int a = i++;
int b = ++i;
(, i, a, b);
;
}
Step by Step Execution
Consider this example:
#include <stdio.h>
int main(void) {
int i = 3;
int x = i++;
int y = ++i;
printf("i=%d x=%d y=%d\n", i, x, y);
return 0;
}
Step-by-step trace
1. Initialize i
int i = 3;
Now:
i = 3
2. Evaluate x = i++
int x = i++;
Postfix increment does two things:
- Uses the current value of
i - Increments
i
So:
Real World Use Cases
Loop counters
Increment operators are most commonly used in loops:
for (int i = 0; i < 10; i++) {
printf("%d\n", i);
}
Here, i++ is traditional and very common.
Array traversal
for (int i = 0; i < size; ++i) {
total += numbers[i];
}
This also works exactly as expected. In C, i++ and ++i are both common in loop headers.
Token or character scanning
while (*p != '\0') {
putchar(*p);
p++;
}
This advances a pointer through a string.
Consuming input one item at a time
while (index < count) {
process(items[index]);
index++;
}
The increment is used for progression, not for its returned value.
Building compact expressions
Real Codebase Usage
In real C codebases, developers usually focus more on clarity and correctness than on any imagined micro-optimization between i++ and ++i.
Common patterns
Simple loop progression
for (size_t i = 0; i < count; i++) {
handle(items[i]);
}
This is idiomatic and very common.
Pointer advancement
while (*src) {
*dst++ = *src++;
}
Here the postfix form is useful because the old pointer value is used for dereferencing before advancing.
Guarded processing
if (index >= limit) {
return;
}
index++;
The increment is a standalone side effect, so either form works.
State machines and parsers
char c = input[pos++];
This reads the current character and then advances the position. The expression value matters, so postfix is chosen intentionally.
Style in real teams
Many teams use:
i++in loop updates because it is conventional
Common Mistakes
1. Assuming prefix is always faster in C
A common belief is that ++i is always faster than i++.
For standalone increments in C, this is usually not true.
i++;
++i;
When the value is ignored, both are typically compiled the same way.
2. Using them interchangeably when the value matters
These are not equivalent:
int i = 5;
int a = i++;
int b = ++i;
a and b will get different values.
3. Writing confusing expressions with multiple side effects
Broken or unsafe style:
int i = 1;
int x = i++ + ++i;
This is problematic because modifying the same variable multiple times in one expression leads to undefined behavior in C.
Avoid code like this. Split it into separate statements.
Safer version:
int i = ;
left = i;
i++;
++i;
x = left + i;
Comparisons
| Concept | Meaning | Expression value | Final effect on variable | Typical use |
|---|---|---|---|---|
++i | Prefix increment | New value | Increments by 1 | When you need increment-before-use |
i++ | Postfix increment | Old value | Increments by 1 | When you need use-before-increment |
Standalone statement comparison
++i;
i++;
When written as standalone statements, both usually have the same practical effect:
iincreases by 1- result value is ignored
- performance is generally the same in optimized C code
Compared with i += 1
Cheat Sheet
Quick rules
++i= increment first, then use the new valuei++= use the old value, then increment- If the expression value is ignored, they usually perform the same in C
- Do not choose one for speed unless profiling proves a difference
Examples
int i = 5;
int a = ++i; // i=6, a=6
int i = 5;
int a = i++; // i=6, a=5
int i = 5;
i++;
++i;
// final i is 7
Safe usage
Good:
for (int i = 0; i < 10; i++) { }
for (int i = 0; i < 10; ++i) { }
Good:
buffer[pos++] = ch;
Avoid:
FAQ
Is ++i faster than i++ in C?
Usually no, not when the result is not used. Modern C compilers typically generate the same or equivalent code.
Do i++ and ++i always do the same thing?
No. They both increment the variable, but the expression value differs. i++ returns the old value, while ++i returns the new value.
Which one should I use in a for loop in C?
Either is fine for basic integer loop counters in C. i++ is very common by convention.
When does the difference actually matter?
It matters when the expression's value is used, such as in assignments, array indexing, pointer arithmetic, or function arguments.
Is i += 1 the same as i++?
They all increment by 1, but the expression semantics are not identical. In simple standalone statements, they usually have the same practical effect.
Can I use multiple increments in one expression?
You should avoid it. Expressions like i++ + ++i are undefined behavior in C and can produce unpredictable results.
Does this change for pointers?
Mini Project
Description
Build a small C program that demonstrates the difference between prefix and postfix increment in a way you can observe directly. This helps reinforce that both forms increment the variable, but they produce different expression values when used in assignments or output.
Goal
Create a program that shows the value of a variable before and after using i++ and ++i, and confirms that standalone increments both simply add 1.
Requirements
- Declare an integer variable and initialize it with a starting value.
- Show the result of assigning
i++to another variable. - Reset the value and show the result of assigning
++ito another variable. - Include a standalone
i++;and++i;example and print the final value. - Print clear labels so the output is easy to understand.
Keep learning
Related questions
Array-to-Pointer Conversion in C and C++ Explained
Learn what array-to-pointer conversion means in C and C++, how array decay works, and how it differs from a pointer to an array.
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.
C Pointer to Array vs Array of Pointers: How to Read Complex Declarations
Learn the difference between pointer-to-array and array-of-pointers in C, plus a simple rule for reading complex declarations correctly.