OverIQ.com

Increment and Decrement Operators in C

Last updated on July 27, 2020


C has two special unary operators called increment (++) and decrement (--) operators. These operators increment and decrement value of a variable by 1.

++x is same as x = x + 1 or x += 1
--x is same as x = x - 1 or x -= 1

Increment and decrement operators can be used only with variables. They can't be used with constants or expressions.

1
2
3
4
5
6
7
int x = 1, y = 1;

++x;     // valid

++5;      // invalid - increment operator operating on a constant value

++(x+y);  // invalid - increment operating on an expression

Increment/Decrement operators are of two types:

  1. Prefix increment/decrement operator.
  2. Postfix increment/decrement operator.

Let's start with the first one.

Prefix increment/decrement operator #

The prefix increment/decrement operator immediately increases or decreases the current value of the variable. This value is then used in the expression. Let's take an example:

y = ++x;

Here first, the current value of x is incremented by 1. The new value of x is then assigned to y. Similarly, in the statement:

y = --x;

the current value of x is decremented by 1. The new value of x is then assigned to y.

The following program demonstrates prefix increment/decrement operator in action:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<stdio.h>

int main()
{
    int x = 12, y = 1;

    printf("Initial value of x = %d\n", x); // print the initial value of x
    printf("Initial value of y = %d\n\n", y); // print the initial value of y

    y = ++x; // increment the value of x by 1 then assign this new value to y

    printf("After incrementing by 1: x = %d\n", x);
    printf("y = %d\n\n", y);

    y = --x; // decrement the value of x by 1 then assign this new value to y

    printf("After decrementing by 1: x = %d\n", x);
    printf("y = %d\n\n", y);

    // Signal to operating system everything works fine
    return 0;
}

Expected Output:

1
2
3
4
5
6
7
8
Initial value of x = 12
Initial value of y = 1

After incrementing by 1: x = 13
y = 13

After decrementing by 1: x = 12
y = 12

Postfix Increment/Decrement operator #

The postfix increment/decrement operator causes the current value of the variable to be used in the expression, then the value is incremented or decremented. For example:

y = x++;

Here first, the current value of x is assigned to y then x is incremented.

Similarly, in the statement:

y = x--;

the current value of x is assigned to y then x is decremented.

The following program demonstrates postfix increment/decrement operator in action:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<stdio.h>

int main()
{
    int x = 12, y = 1;

    printf("Initial value of x = %d\n", x); // print the initial value of x
    printf("Initial value of y = %d\n\n", y); // print the initial value of y

    y = x++; // use the current value of x then increment it by 1

    printf("After incrementing by 1: x = %d\n", x);
    printf("y = %d\n\n", y);

    y = x--; // use the current value of x then decrement it by 1

    printf("After decrementing by 1: x = %d\n", x);
    printf("y = %d\n\n", y);

    // Signal to operating system everything works fine
    return 0;
}

Expected Output:

1
2
3
4
5
6
7
8
Initial value of x = 12
Initial value of y = 1

After incrementing by 1: x = 13
y = 12

After decrementing by 1: x = 12
y = 13

Precedence #

The increment and decrement operators have higher precedence than the operators we have discussed so far (with the only exception being the parentheses). Further, Postfix increment/decrement operators have higher precedence than the prefix increment/decrement operators.

The following table lists the precedence and associativity of operators we have discussed so far:

Operators Description Associativity
() parentheses left to right
++, -- postfix increment operator, postfix decrement operator left to right
++, --, +, - prefix increment operator, prefix decrement operator, unary plus, unary minus right to left
*, /, % Multiplication, Division and Modulus left to right
+, - Addition and Subtraction left to right
=, +=, -=, *=, /=, %= Assignment Operator and Compound assignment operator right to left

Let's take some expression and solve them on the basis of operator precedence.

Example 1:

1
2
3
4
int x, y, z;
x = 5;
y = 8;
z = ++x + y++;

Solution:

Step 1: Evaluate y++. Since ++ is postfix, the current value of y will be used in the expression and then it will be incremented.

z = ++x + 8;

Step 2: Evaluate ++x. Since ++ is prefix, the value of x will be incremented immediately.

z = 6 + 8;

Step 3: Evaluate 6 + 8.

z = 14;

Example 2:

1
2
3
4
5
int a, b, c;
a = 10;
b = 20;
c = 1;
c += a++ * 5 - --b;

Solution:

Step 1: Evaluate a++. Since ++ is postfix, the current value of a will be used in the expression then it will be incremented. The expression now becomes:

c += 10 * 5 - --b;

Step 2: Evaluate --b. Since -- is prefix, the value of b will be decremented immediately. The expression now becomes:

c += 10 * 5 - 19;

Step 3: Evaluate 10 * 5.

c += 50 - 19;

Step 4: Evaluate 50 - 19.

c += 31;

Step 5: Evaluate +=.

c = 32;