top

Search

C Tutorial

.

UpGrad

C Tutorial

Pre-increment And Post-increment

Introduction

Increment operators (++) in C are primarily used to increase a variable’s value by 1. They provide a convenient way to control loops, manipulate pointers and even simplify code, among various other functionalities. There are mainly two types of increment operators in C, namely,

  • Pre-increment Operator (++var) - ‘var’ refers to the name of the variable.

  • Post-increment Operator (var++)

While both pre-increment and post-increment in C/C++ are responsible for enhancing the overall readability and efficiency of the code, there are a few differences when it comes to how each of them operates. 

On that note, let’s take a closer look at pre-increment and post-increment in C with examples. Additionally, you will also get to know some of the most commonly asked pre-increment and post-increment questions. 

What is Pre-increment in C?

Pre-increment in C programming language refers to a specific type of operator that is used to enhance a variable’s value by one before the expression is valued. It is typically denoted by using the ‘++ symbol, which is placed before the variable. 

Pre-increment in C is mostly used in loops and other similar situations wherein you have to increment a variable before using its updated value in an expression. It provides a convenient way to modify a variable’s value while simultaneously evaluating an expression. 

The syntax for pre-increment in C includes,

++variable

Let’s explore a few small pre-increment in C examples to help you get a better understanding of the same,

#include <stdio.h>
int main() {
    int num = 5;
    printf("Original value of num: %d\n", num);
    printf("After pre-increment: %d\n", ++num);
    printf("Updated value of num: %d\n", num);
    return 0;
}

Here, we have initially assigned value 5 to the ‘num’ variable. Following this, the pre-increment operator, ‘++num’, is used to add to the value of ‘num’ by one before the expression is evaluated. This will generate the following output.

Original value of num: 5
After pre-increment: 6
Updated value of num: 6

Pre-increment in An Assignment Statement

#include <stdio.h>
int main() {
    int num = 5;
    int result = ++num;
    printf("num: %d\n", num);
    printf("result: %d\n", result);
    return 0;
}

Here, the ‘++num’ pre-increment operator is used in the assignment statement, ‘int result = ++num’. The value of ‘num’ is incremented by one before being assigned to ‘result’.

Output

num: 6
result: 6

Pre-increment In An Expression

#include <stdio.h>
int main() {
    int x = 5;
    int y = 2;
    int result = 3 * (++x + y);
    printf("result: %d\n", result);
    return 0;
}

Here, the expression ‘3 * (++x + y)’ uses the increment operator ‘++x’ to increment the value of ‘x’ by one before evaluating the expression.

Output

result: 24

Pre-increment In A Conditional Statement

#include <stdio.h>
int main() {
    int count = 0;
    
    while (++count <= 5) {
        printf("Iteration %d\n", count);
    }
    
    return 0;
}

The ‘++count’ operator is used in the conditional statement of the ‘while’ loop, which then increments the value of ‘count’ before each iteration. 

Output

Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5

Pre-increment in A Function Argument

#include <stdio.h>
void printAndIncrement(int *num) {
    printf("Value before increment: %d\n", *num);
    ++(*num);
    printf("Value after increment: %d\n", *num);
}
int main() {
    int num = 10;
    printAndIncrement(&num);
    
    return 0;
}

Here, the increment operator, ‘++(*num), is leveraged to increase the value pointed to by the ‘num’ pointer within the function.

Output

Value before increment: 10
Value after increment: 11

Special Case of Pre-Increment Operator

Although the pre-increment operator in C is pretty simple to use, a special case is involved when it comes to large expressions. This means that when you use a pre-increment operator within a larger expression, the result of that expression will directly be influenced by the original value of the variable being incremented. Let’s take a closer look at this through the small example mentioned below. 

int num = 5;
int result = ++num + num;

Here, the variable, ‘num’, is being incremented using the pre-increment operator within the ‘++num + num’ expression. The goal is to increment ‘num’ and then add it to the incremented value. However, according to the C programming language, if you modify the value of a variable and use the modified value within the same expression, it might generate undefined behaviour. 

Therefore, in this case, we might not actually be able to achieve our goal since the specific outcome of this expression is not guaranteed. 

To prevent the same from happening, it is always recommended that when using a pre-increment operator, you must do the same in isolation or separate it from the expression that relies on the original value. Therefore, instead of the above-mentioned code, you must apply the following algorithm,

int num = 5;
++num;
int result = num + num;

As clearly visible, here we have created a well-defined distinction between the pre-increment operator and the expression. This not only ensures well-defined behaviour but also keeps any kind of potential issues that might threaten the order of evaluation at bay. 

What is Post-increment in C?

Contrary to the pre-increment operator in C, the post-increment operator is used to grow the value of any variable by one after the expression is evaluated. It, too, is denoted by the ‘++’ symbol, the only difference being, it is always placed after the variable. The syntax for the same goes as follows,

variable++

The post-increment operator in C is primarily used when you wish to use the current value of a variable within an expression and increment it afterwards.

Here are a few small examples highlighting how the post-increment operator works for varied scenarios in the C programming language. 

Post-increment Operator In A Loop

#include <stdio.h>
int main() {
    int i = 1;
    while (i <= 5) {
        printf("%d ", i++);
    }
    return 0;
}

Here, ‘i’ refers to the name of our variable, and the ‘i++’ post-increment operator is used to enhance the value of this variable after each iteration of the loop. When you run this program, it will generate the following outcome.

1 2 3 4 5

Post-increment With Multiple Variables

#include <stdio.h>
int main() {
    int x = 5;
    int y = 3;
    int result = x++ + y++;
    printf("result: %d\n", result);
    return 0;
}

Here, we have two variables, namely ‘x’ and ‘y’. The post-increment operators, ‘x++’ and ‘y++’, are used to increment the values of both these variables after evaluating the expression. 

Output

result: 8

Post-increment in An Expression

#include <stdio.h>
int main() {
    int x = 5;
    int y = 2;
    int result = 3 * (x++ + y);
    printf("result: %d\n", result);
    return 0;
}

In this example, the expression, ‘3 * (x++ + y), uses the post-increment operator, ‘x++’, to increment the value of variable ‘x’ after evaluation.

Output

result: 21

Post-increment In An Assigned Statement

#include <stdio.h>
int main() {
    int num = 5;
    int result = num++;
    printf("num: %d\n", num);
    printf("result: %d\n", result);
    return 0;
}

Here, the post-increment operator, ‘num++’, is used on the assigned statement, ‘int result = num++’. The value of ‘num’ is initially allotted to the ‘result’, following which ‘num’ is incremented. 

Output

num: 6
result: 5

Post-increment In A Conditional Statement

#include <stdio.h>
int main() {
    int count = 0;
    while (count++ < 5) {
        printf("Iteration %d\n", count);
    }
    return 0;
}

The post-increment operator, ‘count++’, is applied on the conditional statement of the ‘while’ loop. The value of ‘count’ is then incremented after each iteration. 

Special Case of Post-Increment Operator

Similar to the pre-increment operator in C, the post-increment operator, too, has a special case when used as a part of a larger expression. When you assign the post-increment operator on a variable and assign it to the same variable, then the value will not be incremented. Let’s take a look at this short example.

#include <stdio.h>
int main() {
    int x = 5;
    int y = x++;
    printf("x: %d\n", x);
    printf("y: %d\n", y);
    return 0;
}

Output

x: 6
y: 5

Here, we have the statement, ‘int y = x++’, where the post-increment operator is used to assign the value of two variables, namely, ‘x’ and ‘y’, and then increment the value of ‘x’. The special case of the post-increment operator states that the value used in the assignment is the original value of the variable before the increment takes place. Therefore, ‘y’ gets the original value of ‘x’ (5) before it is incremented to 6.

Evaluating Post and Pre-Increment Operators in C

There is another significant difference when it comes to pre-increment and post-increment in C/C++. It is related to their associativity. The associativity of an operator basically determines the order in which the operations will perform when multiple operators of the same precedence exist in an expression.

 By default, the pre-increment operator in C has a lower and right-to-left associativity, which means that the increment operation is performed before the surrounding expression is evaluated. 

The post-increment operator, on the other hand, has a higher precedence and left-to-right associativity, which means that the surrounding expression is evaluated first, followed by the increment. 

In order to accurately predict the behaviour of expressions and ensure the desired results in your code, having an in-depth understanding of the associativity of pre-increment and post-increment in C is, therefore, extremely crucial. 

Conclusion

Hopefully, with this, you have gained a clear perception of what is post-increment and what is pre-increment in C. Both these increment operators are used to add to a variable’s value by 1. They serve multiple purposes and provide convenience in various programming scenarios. Such include arithmetic operations, array traversal, loop iterations, and pointer manipulation, among others. The ultimate choice between whether you wish to go for post-increment or pre-increment ultimately boils down to whether you wish to use the original value before or after the increment in the expression. 

On that note, we recommend checking out upGrad’s Executive Post Graduate Program In Software Development, offered under the guidance of IIIT-B, which will help you to enhance your knowledge of C programming language. It comes alongside numerous benefits, including 1:1 high-performance coaching sessions and personalised industry sessions, among others. 

FAQs

1. Can you explain the precedence of pre-increment and post-increment in C?

The precedence of post-increment is significantly higher than that of pre-increment in C. Furthermore, when it comes to associativity, pre-increment has right-to-left associativity, whereas post-increment has left-to-right associativity. 

2. What is the syntax for increment in C programming language?

Increment operators, whether it is post-increment or pre-increment, are denoted by the ‘++’ symbol. Their ultimate purpose is to increase the numerical count of a variable by 1.

3. What do we mean by ++I and I++ for loop?

Both these are responsible for incrementing the number; however, the ways in which they operate are different. For example, ++I increments the number before the evaluation of the current expression, whereas I++ increments the number after the evaluation of the current expression. 

Leave a Reply

Your email address will not be published. Required fields are marked *