Back to C Programming
2026-01-195 min read

Pre (Prefix) Increment Operator

Learn Pre (Prefix) Increment Operator step by step with clear examples and exercises.

Why This Matters

The pre (prefix) increment operator is a powerful tool in C programming that allows for efficient manipulation of variables. By understanding this operator, you can write cleaner, more readable code and optimize your programs for better performance. This lesson will delve into the intricacies of the pre increment operator, providing examples, best practices, and common pitfalls to help you become proficient in its use.

Prerequisites

Before diving into the pre increment operator, it is essential to have a solid foundation in C programming basics:

  1. Familiarity with variables, data types, and operators
  2. Understanding of basic control structures such as loops and conditional statements
  3. Knowledge of memory allocation and variable storage in C
  4. Comprehension of postfix and prefix notation

Core Concept

The pre increment operator is denoted by the ++ symbol before a variable. When used, it increments the value of the variable by 1 and then returns the new value. This behavior can be seen in the following example:

int x = 5;
x++; // Increment x and assign the new value to x
printf("%d\n", x); // Output: 6

However, when used before a variable in an expression, the pre increment operator first increments the variable and then uses the new value in the expression. This is demonstrated below:

int x = 5;
int y = ++x; // Increment x and assign the new value to both x and y
printf("x = %d, y = %d\n", x, y); // Output: x = 6, y = 6

Pre-increment with Arrays

It is also possible to use the pre increment operator with arrays to increment the index. This can be useful when iterating through arrays in a loop, as shown below:

int arr[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; ++i) {
printf("%d ", arr[i]); // Output: 1 2 3 4 5
}

Worked Example

Let's consider a more complex example that demonstrates the pre increment operator in action:

#include <stdio.h>

int main() {
int i = 0;
printf("Initial value of i: %d\n", i); // Output: Initial value of i: 0

while (i < 10) {
printf("Value of i before increment: %d\n", i);
printf("Value of i after increment: %d\n", ++i);
printf("\n");
}

return 0;
}

Output:

Initial value of i: 0
Value of i before increment: 0
Value of i after increment: 1

Value of i before increment: 1
Value of i after increment: 2

Value of i before increment: 2
Value of i after increment: 3

Value of i before increment: 3
Value of i after increment: 4

Value of i before increment: 4
Value of i after increment: 5

Value of i before increment: 5
Value of i after increment: 6

Value of i before increment: 6
Value of i after increment: 7

Value of i before increment: 7
Value of i after increment: 8

Value of i before increment: 8
Value of i after increment: 9

Value of i before increment: 9
Value of i after increment: 10

In this example, we initialize i to 0 and print its initial value. We then use a while loop to iterate through values from 0 to 9, demonstrating the pre increment operator's behavior both before and after using it in an expression.

Common Mistakes

  1. Forgetting that the pre increment operator returns the new value of the variable:
int x = 5;
int y = x++; // y is assigned the old value of x, not the new one
printf("x = %d, y = %d\n", x, y); // Output: x = 6, y = 5
  1. Using the pre increment operator in an expression where it doesn't make sense or leads to unexpected results:
int x = 5;
int y = ++x + 3; // Incrementing x before adding 3 might not always be what you want
printf("y = %d\n", y); // Output: y = 9, but it could have been 10 or 8 depending on the compiler's optimization

Common Mistakes (Additional)

  1. Incorrectly assuming that pre-increment with arrays increments the array elements instead of the index:
int arr[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; ++arr) { // Incorrect usage!
printf("%d ", arr[i]); // Output: Segmentation fault or undefined behavior
}

Practice Questions

  1. Write a program that uses the pre increment operator to print the numbers from 1 to 20 using a for loop.
#include <stdio.h>

int main() {
for (int i = 1; i <= 20; ++i) {
printf("%d ", i);
}
return 0;
}
  1. Given the following code snippet, what is the output?
int x = 5;
int y = ++x + 3;
printf("y = %d\n", y); // Output: y = 9

FAQ

What's the difference between the pre increment operator and the post increment operator in C?

The main difference is that the pre increment operator increments the variable before using it in an expression, while the post increment operator increments the variable after using it in an expression. This can lead to differences in behavior when used within expressions, as demonstrated below:

int x = 5;
int y = ++x + 3; // Pre-increment: x is incremented before adding 3
printf("y = %d\n", y); // Output: y = 9

int z = x++ + 3; // Post-increment: x is incremented after adding 3
printf("z = %d\n", z); // Output: z = 8, but the value of x is now 6

Can I use the pre increment operator with arrays in C?

Yes, you can use the pre increment operator with arrays to increment the index. However, be aware that array indices are zero-based, so incrementing the index past the last element of the array will lead to undefined behavior.

Is it a good practice to always use the pre increment operator instead of the post increment operator?

It's not necessarily a good practice to always use one over the other. The choice depends on the specific situation and your personal preference, as both have their uses. However, using the pre increment operator in expressions can sometimes lead to more readable code and better performance due to its behavior of returning the new value of the variable.

What happens if I use the pre increment operator on a constant or non-modifiable variable?

Attempting to use the pre increment operator on a constant or non-modifiable variable will result in a compile error, as these variables cannot be modified. For example:

const int x = 5; // Compile error: cannot modify const variable 'x'
int y = ++x; // Error: assignment makes integer from pointer without a cast