Increment and Decrement Operators in C

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Learn via video course

C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
By Prateek Narang
Free
star5
Enrolled: 1000
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
Prateek Narang
Free
5
icon_usercirclecheck-01Enrolled: 1000
Start Learning

Overview

The increment(++) and decrement operators(--) are important unary operators in C. Unary operators are those which are applied on a single operand. The increment operator increases the value of the variable by one and the decrement operator decreases the value of the variable by one.

Scope

  • This article explains increment and decrement operators in C.
  • It covers the postfix and prefix expression of these operators.
  • It also explains the precedence of these operators with the help of some examples.

Types of Increment and Decrement Operators in C

  1. Prefix Increment Operator: When we use this operator, the value of the variable first increases by 1 and then the variable is used inside the expression.
  2. Prefix Decrement Operator: When we use this operator, the value of the variable first decreases by 1 and then the variable is used inside the expression.
  3. Postfix Increment Operator: When we use this operator, the variable is used inside the expression with its original value and then its value is increased by 1.
  4. Postfix Decrement Operator: When we use this operator, the variable is used inside the expression with its original value and then its value is decreased by 1.

Increment Operators

This operator increases the value of the variable by 1. The above expression is same as m = m + 1 or m += 1.

Prefix Increment Operator

When we use this operator, the value of the variable first increases by 1 and then the variable is used inside the expression.

Syntax Of Prefix Increment operator

Example Of Prefix Increment operator

Output

Postfix Increment Operator

When we use this operator, the variable is used inside the expression with its original value and then its value is increased by 1.

Syntax Of Postfix Increment operator

Example Of Postfix Increment operator

Output

Decrement Operators

The operator decreases the value of the variable by 1. The above expression is the same as m = m - 1 or m -= 1.

Prefix Decrement Operator

When we use this operator, the value of the variable first decreases by 1 and then the variable is used inside the expression.

Syntax Of Prefix Decrement Operator

Example Of Prefix Decrement Operator

Output

Postfix Decrement Operator

When we use this operator, the variable is used inside the expression with its original value and then its value is decreased by 1.

Syntax Of Postfix Decrement Operator

Example Of Postfix Decrement Operator

Output

Precedence in Increment and Decrement Operators in C

The increment and decrement operators have higher precedence than other operators except for parentheses. This means when an expression is evaluated the increment/decrement operations are performed before other operations. The postfix increment/decrement operators have higher precedence than prefix increment/decrement operators.

The associativity of increment/decrement operators is from left to right in an expression.

Examples

Output

Explanation

First bb-- is calculated since the postfix operator has higher precedence. So first b is used in the expression and then its value is decremented by 1. Then a--a is calculated. Since this is a prefix expression, the value of a decreases by 1(now a = 4) and then this new value is used to calculate c.

c=a+bc = --a + b-- c=a+7c = --a + 7
(b now becomes 6) c=4+7c = 4 + 7 c=11c = 11

Output

Explanation

First b++b++ is calculated. b=9 is used in the expression and then b is incremented by 1. Then (a+b)(--a + b--) is calculated. a is decremented by 1 so a becomes 3 and then used in the expression. b-- is calculated. b=10 is used in the expression and then b is decremented by 1.

c=(a+b++)+(a+b)c = (a + b++) + (--a + b--) c=(4+9)+(a+b)c = (4 + 9) + (--a + b--)
(b now becomes 10) c=13+(3+10)c = 13 + (3 + 10)
(a = 3 and b becomes 9) c=26c = 26

Differences between Increment and Decrement Operators

Increment OperatorsDecrement Operators
Increment operator increases the value of the variable by 1.Decrement operator decreases the value of the variable by 1.
When postfix increment operator is used, the variable is used inside the expression with its original value and then its value is increased by 1.When postfix decrement operator is used, the variable is used inside the expression with its original value and then its value is decreased by 1.
When prefix increment operator is used, first the value of the variable increases by 1 and then the new value is used for evaluation in the expression.When prefix decrement operator is used, first the value of the variable decreases by 1 and then the new value is used for evaluation in the expression.

Some interesting facts about increment and decrement operators in C

  • Nesting of the increment(++++) and decrement(--) operators in C are not allowed.

The above code will result in a compilation error as the nesting of these operators is not allowed.

  • They can also be applied to boolean variables.

Output

Explanation The value of false is 0(therefore a=0), so after incrementing a, it becomes 1.

  • They cannot be applied on constants.

The above code will result in a compilation error because these operators cannot be applied to constants.

Conclusion

  • Increment operators increase the value of the variable by 1.
  • Decrement operators decrease the value of the variable by 1.
  • There are prefix/postfix increment and decrement operators in C.
  • They have higher precedence than other operators except for parentheses.
  • Postfix operators have higher precedence than prefix operators.