Increment and Decrement Operators in C

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.

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:

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:

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:

Expected Output:

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:

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

Similarly, in the statement:

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

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

Expected Output:

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:

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.

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

Step 3: Evaluate 6 + 8.

Example 2:

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:

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

Step 3: Evaluate 10 * 5.

Step 4: Evaluate 50 - 19.

Step 5: Evaluate +=.

1 thought on “Increment and Decrement Operators in C

Leave a Comment

%d bloggers like this: