Stat Mania
  • C Programming
  • All Courses
  1. Operators and Input
  2. Pre-Increment vs Post-Increment in C: ++i vs i++
  • Course Home
  • Getting Started
    • C's "Hello, World!" Explained Line by Line (for R & Python Users)
    • How to Test a Small C Snippet Quickly
  • Variables and Data Types
    • Declaring Multiple Variables in One Line in C
    • float vs double in C: Precision, Memory, and When to Use Which
  • Operators and Input
    • Pre-Increment vs Post-Increment in C: ++i vs i++
    • Checking User Input in C: Is It Really a Number?
  • Making Decisions
    • Nested if in C, Explained with Real-Life Examples
    • Shortcut Ways to Write if in C: the Ternary Operator and More
    • The switch Statement in C: Syntax, break, Fall-Through and Examples
  • Loops
    • The do-while Loop in C: Run First, Check After
    • Why Use break in a while Loop If the Condition Already Stops It?
    • Printing a Comma-Separated List in C (No Trailing Comma, Clean Line Breaks)
    • 5 while Loop Practice Problems in C (with Hints and Solutions)
    • 5 for Loop Practice Problems in C (with Hints and Solutions)
    • 5 Nested for Loop Practice Problems in C (with Hints and Solutions)
  • Arrays and Structs
    • How to Save Loop Results in an Array in C
    • Structs in C: Grouping Related Data (with Python and R Comparisons)
  • Random Numbers
    • srand(time(NULL)) in C: Why Random Numbers Repeat and How to Seed Them
    • Tossing a Die in C Until We Get a 6

On this page

  • Direct comparison
  • 1. In assignments
  • 2. Inside printf
  • 3. As a standalone statement
  • Performance note
  • Summary
  • References
  1. Operators and Input
  2. Pre-Increment vs Post-Increment in C: ++i vs i++

Pre-Increment vs Post-Increment in C: ++i vs i++

The difference between ++i and i++ in C: when the value changes, what the expression returns, with compiled examples, and why it only matters inside larger expressions.
Reading 5 min

By the end of this lesson you will

  • Tell ++i from i++ and say what each expression returns
  • Know when the difference matters

Resources

  • Increment and decrement
  • Operator precedence

Two columns. Pre-increment b = ++a: a goes from 5 to 6, then b is 6. Post-increment y = x++: y gets the old value 5, then x goes from 5 to 6.

With a = 5, ++a updates a first, so b gets 6. With x = 5, x++ hands over the old value first, so y gets 5.

Both ++i and i++ add 1 to i. The difference is the value the expression gives back, which matters when we use it inside a larger expression:

  • Pre-increment (++i) changes the value first, then returns the new value.
  • Post-increment (i++) returns the original value first, then changes the value.

Direct comparison

Feature Pre-increment (++i) Post-increment (i++)
Syntax ++variable; variable++;
Order Increments, then evaluates Evaluates, then increments
Value returned The updated value The original value
Precedence Lower (prefix) Higher (postfix)

1. In assignments

When we assign the result to another variable, the order decides the final values:

int a = 5;
int b = ++a;   // 'a' becomes 6, then 6 is assigned to 'b'
printf("a = %d, b = %d\n", a, b);

int x = 5;
int y = x++;   // 5 is assigned to 'y', then 'x' becomes 6
printf("x = %d, y = %d\n", x, y);

Output:

a = 6, b = 6
x = 6, y = 5

2. Inside printf

The timing changes what appears on the screen:

int i = 10;
printf("%d\n", ++i);   // increments first, prints 11

int j = 10;
printf("%d\n", j++);   // prints the old value, 10
printf("%d\n", j);     // now j is 11

Output:

11
10
11

3. As a standalone statement

When the result is not used, there is no difference. i ends up as i + 1 either way. This is why a for loop works the same with i++ or ++i:

i++;   // i becomes i + 1
++i;   // i becomes i + 1
WarningDon’t change a variable twice in one expression

Statements such as i = i++; or a[i] = i++; modify and read i without a defined order, so their behaviour is undefined in C. We keep ++ and -- to one use per variable in each expression.

Performance note

In C, a compiler produces the same code for a standalone ++i and i++. In C++, pre-increment is usually preferred for iterators and other objects, because post-increment must make a temporary copy of the old value. So ++i is a good habit to build early.

Summary

  • ++i: update, then use the new value.
  • i++: use the old value, then update.
  • On their own they are identical; the difference shows up only when the result is used.

References

  • UpGrad: pre-increment and post-increment in C
  • Scaler: pre-increment and post-increment in C
  • Reddit r/C_Programming: pre vs post increment
  • DEV: unmasking the difference between ++i and i++
  • ++i changes i, then returns the new value.
  • i++ returns the old value, then changes i.
  • On its own line they do the same thing.

Side effect. A change to a variable made while an expression is evaluated, such as i++.

Back to top
Stat Mania

Free, hands-on statistics and programming courses that run right in your browser.

Courses

All courses R Programming Statistics Probability

Resources

Practice quizzes Tools & games Quarto docs Course kit on GitHub

Company

Home About Contact Privacy policy Terms of use
© Stat Mania. All rights reserved.
Operators and Input
Checking User Input in C: Is It Really a Number?