Increment and Decrement Operators in C
In this tutorial, we will focus on the two most important and slightly difficult to understand operators, which are called increment and decrement operators.
In this tutorial, we will focus on the two most important and slightly difficult to understand operators, which are called increment and decrement operators.
Increment and decrement operators are shortcuts to increase or decrease the value of a variable by 1.
Instead of writing i = i + 1
to increase i by 1, we can simply write i++
. Similarly, to decrease i by 1, instead of writing i = i - 1
, we can use i--
.
Increment and decrement operators can be categorized as follows:
These operators are used to change the value of a variable, but it’s essential to understand the difference between pre and post versions of the operators, as they behave slightly differently.
Both the pre-increment operator (++i) and the post-increment operator (i++) are used to increase the value of a variable, but they work differently.
The pre-increment operator (++i) increments the value of a variable first, and then returns its value. On the other hand, post-increment (i++) returns the value of a variable first, then it increments its value.
The following programs shows how the pre-increment works:
#include <stdio.h>
int main()
{
int i = 1;
printf("Value of i is %d\n", ++i);
printf("Value of i is %d", i);
return 0;
}
Output:
Value of i is 2
Value of i is 2
First, the value of i is increased by 1, and then the new value is returned. This means that the first printf function prints 2. Since the new value of i is 2, the second printf function also prints 2.
Now, consider the following program:
#include <stdio.h>
int main()
{
int i = 1;
printf("Value of i is %d\n", i++);
printf("Value of i is %d", i);
return 0;
}
Output:
Value of i is 1
Value of i is 2
Observe the distinction. The post-increment operator returns the original value of i first, which, in this case, is 1. That’s why the first printf function displays 1 on the screen. Afterward, it increments the value of i, so the next time we use i, we see the updated value, which is now 2. This is why the second printf function displays the value 2.
In simple words,
But how much later? The C standard does not provide any specific time for when the value will be updated. However, we can safely assume that the variable will be incremented before the next statement is executed.
The difference is the same as with pre and post-increment operators. In place of increment, decrement will be used. The following programs are enough to demonstrate the difference between the two.
#include <stdio.h>
int main()
{
int i = 1;
printf("Value of i is %d\n", --i);
printf("Value of i is %d\n", i);
return 0;
}
Output:
Value of i is 0
Value of i is 0
#include <stdio.h>
int main()
{
int i = 1;
printf("Value of i is %d\n", i--);
printf("Value of i is %d", i);
return 0;
}
Output:
Value of i is 1
Value of i is 0
The precedence of increment and decrement operators is higher than the rest of the operators except parentheses. Also, the precedence of the postfix operators are greater than the prefix operators. The following table shows the same:
Precedence | Operators | Associativity |
---|---|---|
1st | () | Left to right |
2nd | Postfix increment and decrement (++, –) | Left to right |
3rd | Unary + and -, Logical negation (!), Prefix increment and decrement (++, –) | Right to left |
4th | *, /, % | Left to right |
5th | +, – | Left to right |
6th | <, <=, >, >= | Left to right |
7th | ==, != | Left to right |
8th | && | Left to right |
9th | || | Left to right |
10th | =, +=, -=, *=, /=, %=, <<=, >>=, &=, |=, ^= | Right to left |
Notice that the associativity of postfix increment and decrement operators is left to right and that of the prefix increment and decrement operators is right to left.
Can you determine the output of the following program?
#include <stdio.h>
int main()
{
int i = 1, j = 2;
printf("%d", ++i + j++);
return 0;
}
As a bonus, determine the value of i and j after the execution of the printf function.
The output of the above program is 4. The evaluation of the expression ++i + j++
is as follows:
++i + j++
++i + 2 (j++ means use the value of j first, then increment)
2 + 2 (i++ means increment the value of i immediately)
4
After the execution of printf, the value of i is 2 and the value of j is 3. This can be verified through the following program:
#include <stdio.h>
int main()
{
int i = 1, j = 2;
printf("%d\n", ++i + j++);
printf("%d %d", i, j);
return 0;
}
Output:
4
2 3
Leave a comment