Back to C Programming
2026-03-135 min read

Precedence of Post / Prefix Increment / Decrement Operators

Learn Precedence of Post / Prefix Increment / Decrement Operators step by step with clear examples and exercises.

Why This Matters

In this comprehensive lesson, we delve into the intricacies of increment and decrement operators in C programming, explaining their precedence, usage, and common mistakes to help you master efficient and error-free coding. Understanding these operators is essential for writing clean, maintainable code and excelling in both coding interviews and real-world programming tasks.

Why This Matters

The increment (++) and decrement (--) operators play a significant role in C programming by allowing you to modify the value of a variable by 1. These operators come in two forms: postfix (++) and prefix (--). Understanding their differences, usage, and precedence is crucial for writing efficient and error-free code, especially when used within complex expressions or loops.

Prerequisites

To fully grasp this lesson, you should be familiar with:

  1. Basic C syntax and control structures (loops, conditionals)
  2. Variables and data types in C programming
  3. Operators and their precedence rules
  4. Understanding of function calls and return values
  5. Familiarity with the concept of side effects and their implications

Core Concept

Increment and decrement operators are used to modify the value of a variable by 1. These operators come in two forms: postfix (++) and prefix (--). The main difference between them lies in their evaluation order, which we will discuss below.

Postfix Increment/Decrement Operator (++, --)

The postfix increment/decrement operator is placed after the variable it modifies. It first evaluates the expression and then increments or decrements the variable. The value of the expression before the modification is used in the current context. For example:

int i = 5;
printf("%d", i++); // Output: 5 (The value of i remains 5 until the printf function finishes executing)
printf("%d", i); // Output: 6

In this code snippet, i is first used in the printf function, and then its value is incremented. The side effect of the increment operation does not affect the result printed by printf.

Prefix Increment/Decrement Operator (++, --)

In contrast, the prefix increment/decrement operator is placed before the variable it modifies. It first increments or decrements the variable and then evaluates the expression. The value of the modified variable is used in the current context. For example:

int i = 5;
printf("%d", ++i); // Output: 6 (The value of i is incremented before being used by printf)
printf("%d", i); // Output: 6

In this code snippet, i is incremented before it is used in the printf function. The side effect of the increment operation affects the result printed by printf.

Worked Example

Consider the following code snippet, which demonstrates the difference between postfix and prefix increment operators in a loop:

#include <stdio.h>

int main() {
int i = 0;
for(; i < 5; ++i) {
printf("Inside loop, i is %d\n", i);
}
printf("\nOutside loop, i is %d\n", i);
return 0;
}

Output:

Inside loop, i is 0
Inside loop, i is 1
Inside loop, i is 2
Inside loop, i is 3
Inside loop, i is 4
Outside loop, i is 5

Here, the postfix increment operator (++i) inside the loop increments i after each iteration, while the prefix increment operator (++i) in the main function increments i before the loop starts. The difference in evaluation order can significantly impact the behavior of your code, especially within loops and complex expressions.

Common Mistakes

  1. Forgetting the order of precedence: When using multiple operators in an expression, remember that postfix increment/decrement operators have lower precedence than other arithmetic operators (e.g., addition, subtraction). For example:
int a = 5, b = 3;
int c = a + ++b; // Correct: c is assigned the value of a + (b incremented before being used)
int d = a + b++; // Incorrect: d is assigned the value of a + b (before b is incremented)

In this example, c is correctly assigned the result of a + (b incremented before being used), while d is incorrectly assigned the result of a + b (before b is incremented).

  1. Inconsistent usage: Using both postfix and prefix operators in the same expression can lead to unexpected results, as their evaluation order differs. For example:
int i = 5;
printf("%d", ++i--); // Output: 6 (The value of i remains 6 until printf finishes executing)

In this code snippet, the postfix decrement operator is applied after the prefix increment operator, resulting in a side effect that does not affect the result printed by printf.

  1. Misunderstanding side effects: Postfix and prefix operators can have different side effects on the variables they modify, which can lead to unexpected behavior if not properly understood. For example:
int i = 5;
int j = i++; // j is assigned the value of i before it is incremented
printf("%d %d\n", i, j); // Output: 6 5

In this code snippet, i is incremented after its value is assigned to j, resulting in a side effect that affects the behavior of the program.

Practice Questions

  1. Write a C program that prints the numbers from 1 to 20 using both postfix and prefix increment operators in separate loops.
  2. Given the following code snippet:
int a = 5, b = 3;
int c = a + ++b;
printf("%d\n", c);

What is the value of c? Explain why.

  1. Write a C program that demonstrates the difference between postfix and prefix increment operators in a loop, printing the values of both the loop variable and an additional variable modified using each operator.

FAQ

  1. Why does the order of increment operators matter in an expression?

The order matters because postfix increment/decrement operators have lower precedence than other arithmetic operators, which can lead to unexpected results if not used correctly.

  1. Can I use both postfix and prefix increment operators within a single expression?

It is generally recommended to avoid mixing postfix and prefix increment operators in the same expression due to potential confusion about their evaluation order and side effects.

  1. What are some common pitfalls when using increment/decrement operators in C programming?

Some common mistakes include forgetting the order of precedence, inconsistent usage of postfix and prefix operators, misunderstanding the difference between pre- and post-increment/decrement behavior, and failing to account for side effects on variables.