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.


Introduction

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:

  1. Pre-increment operator
  2. Post-increment operator
  3. Pre-decrement operator
  4. Post-decrement operator

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.


Difference between Pre-Increment and Post-Increment Operators

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,

  • Pre-increment operator immediately increments the value of a variable.
  • Post-increment operator says “use the original value of a variable for now, then later increment its value.”

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.


Difference between Pre-Decrement and Post-Decrement Operators

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.

Pre-Decrement

#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

Post-Decrement

#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

Precedence and Associativity of Increment and Decrement Operators

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:

PrecedenceOperatorsAssociativity
1st() Left to right
2ndPostfix increment and decrement (++, –)Left to right
3rdUnary + 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.


Try It Yourself

Try it Yourself

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.

Yes, I have determined the output of the program, and now I want to compare my solution with yours.
No. It’s too difficult for me. Show me your solution.
explanation

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

Leave a comment

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

Thank you for choosing to leave a comment. Please be aware that all comments are moderated in accordance with our policy. For more information, please review our comments policy. Rest assured that your email address will not be shared with anyone. Kindly refrain from using keywords in your comment. Let’s maintain a respectful atmosphere and engage in meaningful conversations.