Question

Difference Between ++i and i++ in C

cfor-looppost-incrementpre-increment

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.

  • ++i is called pre-increment.
    • It increments i first.
    • Then the expression evaluates to the new value.
  • i++ is called post-increment.
    • It evaluates to the current value first.
    • Then i is 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

In the increment section of a for loop, such as:

for (int i = 0; i < 10; i++)

or

for (int i = 0; i < 10; ++i)

both forms usually do the same thing, because the value of the expression is ignored. The loop only needs i to increase.

So in plain C:

  • i++ and ++i are both correct in the increment part of a for loop
  • there is usually no practical difference there
  • many C programmers choose whichever style they find more readable or consistent

Mental Model

Think of i as a number written on a whiteboard.

  • ++i means: erase the number, write the bigger number, then show it
  • i++ means: show the current number, then erase it and write the bigger number

Example with i = 3:

  • ++i gives you 4, and i is now 4
  • i++ gives you 3, but i becomes 4 immediately 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.

Take Quiz

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 = %d, a = %d\n", i, a); // i = 6, a = 5

i++ returns the old value first, so a gets 5, then i becomes 6.

Example 3: in a for loop

for (int i = 0; i < 3; i++) {
    printf("%d\n", i);
}

This prints:

0
1
2

Using ++i gives the same result:

for (int i = 0; i < 3; ++i) {
    printf("%d\n", i);
}

Because the value of the increment expression is not used, both loops behave the same.

Step by Step Execution

Consider this code:

int i = 2;
int x = i++;
int y = ++i;

Step by step:

  1. i starts as 2.
  2. int x = i++;
    • x gets the current value of i, which is 2
    • then i is incremented to 3
  3. int y = ++i;
    • i is incremented first, from 3 to 4
    • then y gets that new value, which is 4

Final values:

i == 4
x == 2
y == 4

Now compare with a for loop:

for (int i = 0; i < 3; i++) {
    printf("%d\n", i);
}

Execution order for each iteration:

  1. Initialize i = 0
  2. Check i < 3
  3. Run loop body
  4. Execute i++
  5. Repeat

If you changed i++ to ++i, step 4 still increases i by 1, and the produced value is ignored. So the loop runs the same way.

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 p to the next position

That is different from:

char c = *++p;

which moves first, then reads.

Counting events

errors++;

When you only want to update a counter, either form works if used alone, but errors++ is commonly seen because it reads naturally as "increment errors."

Take Quiz

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.

4. Validation and counting

if (is_valid(value)) {
    valid_count++;
} else {
    invalid_count++;
}

This is a straightforward and readable use of post-increment.

Practical style guidance

  • Use i++ or ++i in for loop update expressions consistently with your team style.
  • Prefer simple expressions over clever ones.
  • If the returned value matters, choose the form that matches the intended behavior.
  • Avoid writing expressions that modify the same variable multiple times in one statement.

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.

Do not optimize based on guesswork. Prefer clarity.

4. Misreading pointer expressions

These are different:

*p++
*++p
  • *p++ means *(p++)
  • *++p means *(++p)

One reads then moves; the other moves then reads.

5. Using increment where a clearer statement is better

This is legal:

array[i++] = value;

But for beginners, this may be harder to read than:

array[i] = value;
i++;

If clarity matters more than compactness, split it into separate steps.

Comparisons

ExpressionWhat happens firstExpression resultFinal effect on variable
++iIncrementNew valuei increases by 1
i++Use current valueOld valuei increases by 1

++i vs i++ in common situations

SituationBetter choiceWhy
Standalone increment statementEitherBoth just add 1
for loop increment sectionEitherResult is ignored
Assignment where new value is needed++iExpression evaluates to incremented value
Assignment where old value is needed firsti++Expression evaluates to original value
Complex expressionsNeither if avoidableSimpler code is safer

Compared with i += 1

FormMeaningNotes
i++Add 1Returns old value
++iAdd 1Returns new value
i += 1Add 1Result can be used, but usually written for clarity

For simple standalone updates, these are often interchangeable in intent:

i++;
++i;
i += 1;

But only the first two are increment operators with pre/post behavior.

Take Quiz

Cheat Sheet

Quick rules

  • ++i = increment first, then use the value
  • i++ = use the value first, then increment
  • If used alone on a line, both usually have the same effect
  • In a for loop 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

*p++   // use value at p, then move p
*++p   // move p, then use value

Best beginner takeaway

If the expression value matters, choose carefully. If the expression value does not matter, such as in most loop updates, either form is fine.

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.

Can I use increment operators inside complex expressions?

You can, but it is often error-prone and harder to read. In C, modifying the same variable multiple times in one expression can even cause undefined behavior.

Take Quiz

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 ++i in an assignment
  • Create one example using i++ in an assignment
  • Add one for loop using i++
  • Add another for loop using ++i
  • Print enough output to compare the results clearly
Take Quiz

Keep learning

Related questions

View more