Increment and Decrement Operators in C
Learn via video course

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
- 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.
- 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.
- 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.
- 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 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 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.
(b now becomes 6)
Output
Explanation
First is calculated. b=9 is used in the expression and then b is incremented by 1. Then 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.
(b now becomes 10)
(a = 3 and b becomes 9)
Differences between Increment and Decrement Operators
Increment Operators | Decrement 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.