What are Pre-increment and Post-increment in C?
In the C/C++ programming language, there exists a operator that is used to increase the value of a variable by 1. The operator is denoted by the ++ symbol. When we increase the value of a variable before assigning it to another variable then it is known as Pre-Increment. When the value of a variable is incremented after assigning it to a variable, it is known as Post-Increment.
So, we can increment the value of a variable in the following two ways -
- Pre-Increment
- Post-Increment
Properties of the Increment Operator
Following are the main properties of the preincrement and postincrement in C -
- It increases the value of a variable by 1.
- It can only be used with variables.
- It is represented by a double plus ++ symbol.
Pre-Increment Operator in C
As the name suggests, the pre-increment operator alters the value of the variable before using it in any expression. Therefore, we can say that the pre-increment operator increases the value of the variable first and then use it in the expression.
Syntax:
For example, if the initial value of a were 5, then the value 6 would be assigned to b.
Code:
Output:
Post-Increment Operator in C
The post-increment operator is used when it is required to increment the value of the variable after evaluating the expression. Therefore, in post-increment value is first used in the expression, and then it is incremented.
Syntax:
For example, assume the initial value of a to be 5. Then after executing the above statement the final value of b will be 5 as the value of a will be incremented after performing the expression.
Code:
Output:
Special Case of Post Increment Operator
What will happen if we use the post-increment operator on a variable and assign it to the same variable? In this case, the value will not be incremented it will be exactly equal to its initial value.
For example, let's say we execute the following statement -
In this case, the value of x will not get changed no matter what was the initial value of x.
Code
Output:
Evaluating Post and Pre-Increment Operators in C
In Operators Precedence & Associativity Table we have seen that the precedence of the post-increment is higher than that of pre-increment. Also, their associativity is as follows -
- The pre-increment is right to left associative.
- The post-increment is left to right associative.
Examples
Exmaple 1
Find the output of the following code -
Output:
Explanation:
The initial values of x and y were 5 and 6 respectively. In the expression z = x++ + y++, we are performing post-increment with x and pre-increment with y. So, the value of x will be incremented but the initial value of x will be used to evaluate the expression, while the value y will be incremented and the incremented value will be used to evaluate the expression.
Example 2
Find the output of the following code -
Output:
Explanation:
The initial values of x and y were 5 and 6 respectively. In the expression z = x++ + y++, we are performing post-increment with x and pre-increment with y. So, the increased values of x and y will be used to evaluate the answer, and then it is assigned to z.
Example 3
Find the output of the following code -
Output:
Explanation:
The initial values of x and y were 'a' and 'b' respectively.
Firstly, we are using post-incrementing with the variable x which will change its value to 'b'. Then, we are using the pre-increment operator on x and assigning the result to z, due to which we will have x = 'c' and z = 'c'. At last, we are performing a special case of post-increment operator on y that will not change the value of y.
Example 4
Find the output of the following code -
Output:
Explanation:
Inside the for loop we have initialized the values of i and j to 1 and 120 respectively. After which we are iterating till the condition i < j holds true. Also, after each iteration, we are multiplying i by 2, and performing post-increment on j. So in each iteration, we will have the following values of i and j.
Iteration No. | ||
---|---|---|
1 | 1 | 120 |
2 | 2 | 121 |
3 | 4 | 122 |
4 | 8 | 123 |
5 | 16 | 124 |
6 | 32 | 125 |
7 | 64 | 126 |
8 | 128 | 127 |
So, after the iteration, the condition i < j fails and the final values of i and j get printed.
Conclusion
- Preincrement and Postincrement in C are the two ways to use the increment operator.
- In Pre-Increment, the operator sign (++) comes before the variable. It increments the value of a variable before assigning it to another variable.
- In Post-Increment, the operator sign (++) comes after the variable. It assigns the value of a variable to another variable and then increments its value.
- Preincrement and Postincrement in C are very useful and handy operators, especially when it is used inside the loops.