Increment and Decrement Operators in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Increment and decrement operators in C play a pivotal role in adjusting variable values by precisely one unit. These operators, categorized into pre-increment (or prefix increment) and post-increment (or postfix increment), alongside their decrement counterparts, provide nuanced control over the modification of variables. Grasping the distinctions between pre increment and post increment, as well as their decrement equivalents, is essential for mastering control flow and data manipulation in C programming.

Increment Operators

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

Syntax of C Increment Operator

Increment Operator Syntax can be written in two ways which are as follows:

1: Prefix Increment Operator

2: Postfix Increment Operator

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

Example

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

Example

Output

Example of Increment Operator in C

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.

Syntax of C Decrement Operator

Decrement Operator Syntax can be written in two ways which are as follows:

1: Prefix Decrement Operator

2: Postfix Decrement Operator

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

Example

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

Example

Output

Example of Decrement Operator in C

Output:

Difference between Increment and Decrement Operators

FeatureIncrement Operators (++)Decrement Operators (--)
OperationIncreases the value of its operand by 1.Decreases the value of its operand by 1.
Use CaseOften used in loops to increment index or counter variables.Often used to decrement index or counter variables in loops.
Effect on VariablesMakes the variable's value larger by one unit.Makes the variable's value smaller by one unit.
Prefix Operation (++var / --var)Increases the variable's value before the variable is used in the expression.Decreases the variable's value before the variable is used in the expression.
Postfix Operation (var++ / var--)The variable's current value is used in the expression, and then its value is increased by 1.The variable's current value is used in the expression, and then its value is decreased by 1.
Impact in ExpressionsCan change the outcome of an expression by modifying the variable's value before or after its use.Similarly alters the result of an expression depending on whether the variable is used before or after decrementing.

FAQs

Q. What is the main difference between prefix and postfix forms of increment operators in C?

A. The prefix form (++var) increases the variable's value before using it in an expression, whereas the postfix form (var++) uses the variable's current value in the expression and then increases it.

Q. Can increment and decrement operators be used with constants in C?

A. No, increment (++) and decrement (--) operators cannot be applied to constants in C. Attempting to do so will result in a compilation error.

Q. How do increment and decrement operators affect boolean variables in C?

A. Increment and decrement operators can be applied to boolean variables in C. Incrementing a false (which is equivalent to 0) changes its value to true (1), showcasing their versatility.

Q. Are nesting increment or decrement operators allowed in C?

A. Nesting increment (++) or decrement (--) operators are not permitted in C. For example, ++(++a); will cause a compilation error due to the syntax not being supported in the language.

Conclusion

  • Increment and decrement operators in C are essential for modifying variable values by exactly one unit.
  • These operators come in two varieties: pre increment (prefix) and post increment (postfix), along with their decrement equivalents.
  • Pre increment and post increment operations allow for precise control over the timing of value adjustments in variables.
  • The understanding and application of these operators are crucial for effective programming in C, impacting loops, conditionals, and data manipulation tasks.