06. Increment & Decrement Operators (Prefix vs Postfix)

🟢 Difficulty: Beginner | ++/-- Unary Operators, Evaluation Timing & Sequence Points

📖 Definition

The increment operator (++) adds 1 to a modifiable integer or pointer variable, and the decrement operator (--) subtracts 1. Both operators can be used in prefix notation (++x, --x) or postfix notation (x++, x--), which dictates whether the increment/decrement takes effect before or after evaluating the expression value.

🎯 Why It Is Used

  • Loop Control Counters: Used extensively in for loop increment steps (for (int i=0; i < n; i++)).
  • Pointer Traversal: Advancing pointer addresses through arrays (*ptr++).

⚙️ How It Works: Prefix vs Postfix Evaluation Order

  • Prefix Mode (++x / --x): Increments/decrements the variable first, then returns the newly updated value to the expression.
  • Postfix Mode (x++ / x--): Returns the original value to the current expression first, then increments/decrements the variable in memory before the next sequence point.

📝 Syntax Comparison Table

Syntax Type Value Returned to Expression Variable Memory Change
++xPrefix IncrementNew incremented value (x + 1)Increments immediately
x++Postfix IncrementOriginal value (x)Increments after evaluation
--xPrefix DecrementNew decremented value (x - 1)Decrements immediately
x--Postfix DecrementOriginal value (x)Decrements after evaluation

🔀 Undefined Behavior Warning: Multiple Modifications

In ISO C, modifying the same scalar variable more than once between consecutive sequence points (e.g. x = x++ or i = ++i + i++) is Undefined Behavior (UB)! Different compilers or optimization levels will yield different, unportable results. Never use multiple increment/decrement operators on the same variable in a single expression.

🌱 Beginner Example

#include <stdio.h>

int main(void) {
    int a = 5;
    int b = 5;

    int res1 = ++a; // Prefix: 'a' becomes 6, res1 receives 6
    int res2 = b++; // Postfix: res2 receives 5, 'b' becomes 6

    printf("Prefix ++a : res1 = %d, a = %d\n", res1, a);
    printf("Postfix b++: res2 = %d, b = %d\n", res2, b);
    return 0;
}

🌼 Intermediate Example: Pointer Incrementing & Idiomatic String Copy

#include <stdio.h>

void custom_strcpy(char *dest, const char *src) {
    // Idiomatic C loop using postfix pointer increment
    while ((*dest++ = *src++) != '\0') {
        // Copies char, then increments both pointers!
    }
}

int main(void) {
    char source[] = "C Language";
    char destination[20];

    custom_strcpy(destination, source);
    printf("Copied String: %s\n", destination);
    return 0;
}

🌲 Advanced Example: Sequence Point Rules & Unsafe Code Demonstration

#include <stdio.h>

int main(void) {
    int i = 1;

    // SAFE & PORTABLE CODE:
    i++;
    int a = i;
    printf("Safe i = %d, a = %d\n", i, a);

    // UNSAFE UNDEFINED BEHAVIOR CODE (DO NOT DO THIS!):
    // i = i++; // Undefined Behavior in C!
    // printf("%d %d %d\n", i++, i++, i++); // Undefined evaluation order!

    return 0;
}

🔍 Line-by-Line Explanation

  • int res1 = ++a;: Increments a from 5 to 6, then assigns 6 to res1.
  • int res2 = b++;: Assigns current value 5 to res2, then increments b from 5 to 6.
  • while ((*dest++ = *src++) != '\0'): Dereferences src, copies char to dest, then increments both pointers to point to the next character in RAM.

💻 Expected Terminal Output

Prefix ++a : res1 = 6, a = 6
Postfix b++: res2 = 5, b = 6
Copied String: C Language
Safe i = 2, a = 2

⚠️ Common Mistakes & Pitfalls

  • Writing x = x++: Modifying x twice without an intervening sequence point triggers Undefined Behavior.
  • Confusing Prefix and Postfix in Conditions: Using if (count++ < 5) versus if (++count < 5) causes off-by-one loop errors.

📌 Important Rules & Limitations

  • Increment/decrement operators require a modifiable lvalue (e.g. ++5 is illegal).
  • Never use ++ or -- more than once on the same variable in a single statement.

🌐 Multilingual Explanation: English

Prefix (++x) increments before returning value; postfix (x++) returns original value before incrementing. Modifying a variable multiple times in one statement (like x = x++) causes Undefined Behavior.

🌐 Multilingual Explanation: Hindi (Roman Script)

Prefix ++x pehle value badhata hai fir use karta hai. Postfix x++ pehle purani value use karta hai fir 1 badhata hai. Ek hi statement mein ek variable ko do baar i++ mat karein kyunki ye Undefined Behavior hai.

🌐 Multilingual Explanation: Marathi (Roman Script)

Prefix ++x aadhi value 1 ne vadhavto. Postfix x++ aadhi juni value vaparto aani nantar 1 ne vadhavto. Ekach line madhye x = x++ lihine chuki che aahe.

❓ Practice Questions

  1. What is the value of y after int x = 10; int y = x++;?
  2. Why is i = i++ classified as Undefined Behavior in ISO C?
  3. What does *ptr++ do when traversing an array in C?

🚀 Practical Coding Challenge

Write a program named countdown.c that initializes a counter to 10 and uses a while (count-- > 0) loop to print a rocket launch countdown down to 0, followed by "Liftoff!".

🧭 Navigation & Breadcrumbs

← Previous: 05. Assignment Operators | Module 05 Guide | Next: 07. Special C Operators →