C Increment and Decrement

Beginner
6 min read
Updated: Nov 2026
1 example

What Is Increment and Decrement?

++ adds one and -- subtracts one. In prefix form (++i) the variable changes first and the new value is used. In postfix form (i++) the old value is used and then the variable changes.

Remember
i++    /* use i, then add 1 */
++i    /* add 1, then use i */
i--    /* use i, then subtract 1 */
--i    /* subtract 1, then use i */

Cheat Sheet

Keep these facts handy while you work with Increment and Decrement in C.

TopicDetail
PostfixExpression value is the old value
PrefixExpression value is the new value
In loopsfor (i = 0; i < n; i++)
Pointersp++ moves by sizeof *p bytes

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Bitwise Operators.

Example 1 — Prefix versus postfix

The same variable gives different results depending on the form.

C
#include <stdio.h>

int main(void) {
    int i = 5;

    int a = i++;
    printf("a=%d i=%d\n", a, i);

    int b = ++i;
    printf("b=%d i=%d\n", b, i);

    int c = i--;
    printf("c=%d i=%d\n", c, i);

    int d = --i;
    printf("d=%d i=%d\n", d, i);
    return 0;
}

How It Works

  1. i++ stores 5 in a, then i becomes 6.
  2. ++i makes i 7 first, so b is 7.
  3. i-- stores 7 in c, then i becomes 6.
  4. --i makes i 5 first, so d is 5.

Edge Cases & Pitfalls

Check these before relying on Increment and Decrement in real programs.

UB

Modifying twice

i = i++; and a[i] = i++; are undefined or unspecified. Change a variable once per expression.

loop

Prefix vs postfix in loops

For plain ints in a loop header both work. For iterator-like objects prefix can be cheaper, but in C it rarely matters.

Key Takeaways

  • Postfix: old value, then change.
  • Prefix: change, then new value.
  • Rule: one modification per expression.

One line: ++ adds one and -- subtracts one.

Frequently Asked Questions

Both add one to i. i++ evaluates to the old value; ++i evaluates to the new value.
No. Compilers generate the same code for statements like i++; and i += 1;, so choose the clearest form.
Continue with "C Bitwise Operators" next. Work on individual bits with & | ^ ~ << >>. The C Operators hub lists every topic in order.

Next: Bitwise Operators

Work on individual bits with & | ^ ~ << >>.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful