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 Operators

Syntax

  1. Prefix Increment Operator:
  1. Postfix Increment Operator:

Example

  1. Prefix Increment Operator:

Output:

  1. Postfix Increment Operator:

Output:

Decrement Operators

Syntax

  1. Prefix Decrement Operator:
  1. Postfix Decrement Operator:

Example

  1. Prefix Decrement Operator:

Output:

  1. Postfix Decrement Operator:

Output:

Types of Increment and Decrement Operators in C

1. Prefix Increment/Decrement Operator

The prefix increment/decrement operator first updates the value of the variable and then returns the updated value for any operation.

Example of Prefix Increment Operator:

Output:

Example of Prefix Decrement Operator:

Output:

2. Postfix Increment/Decrement Operator

The postfix increment/decrement operator first returns the current value of the variable for any operation, and then updates the variable's value.

Example of Postfix Increment Operator:

Output:

Example of Postfix Decrement Operator:

Output:

Types of Increment and Decrement Operators in C

1. Prefix Increment Operator

When the prefix increment operator is used, the value of the variable first increases by 1, and then the updated value is used in the expression or operation.

Syntax:

2. Prefix Decrement Operator

With the prefix decrement operator, the value of the variable first decreases by 1, and then the updated value is used in the expression or operation.

Syntax:

3. Postfix Increment Operator

When using the postfix increment operator, the current value of the variable is used in the expression or operation first, and only then is the variable's value increased by 1.

Syntax:

4. Postfix Decrement Operator

For the postfix decrement operator, the current value of the variable is used in the expression or operation first, and after that, the variable's value decreases by 1.

Syntax:

Increment Operators

1. Prefix Increment Operator

The prefix increment operator first increases the value of the variable by 1, and then the updated value is used in the expression or operation.

Syntax Of Prefix Increment Operator:

Example Of Prefix Increment Operator:

Output:

2. Postfix Increment Operator

When using the postfix increment operator, the current value of the variable is used in the expression or operation first, and only then is the variable's value increased by 1.

Syntax Of Postfix Increment Operator:

Example Of Postfix Increment Operator:

Output:

General Syntax of Increment Operators:

Increment operators can be represented in two general forms:

  1. ++variable_name (Prefix)
  2. variable_name++ (Postfix)

General Example of Increment Operators:

Output:

Decrement Operators

1. Prefix Decrement Operator

The prefix decrement operator first decreases the value of the variable by 1, and then the updated value is used in the expression or operation.

Syntax Of Prefix Decrement Operator:

Example Of Prefix Decrement Operator:

Output:

2. Postfix Decrement Operator

When using the postfix decrement operator, the current value of the variable is used in the expression or operation first, and only then is the variable's value decreased by 1.

Syntax Of Postfix Decrement Operator:

Example Of Postfix Decrement Operator:

Output:

General Syntax of Decrement Operators:

Decrement operators can be represented in two general forms:

  1. --variable_name (Prefix)
  2. variable_name-- (Postfix)

General Example of Decrement Operators:

Output:

Precedence in Increment and Decrement Operators in C

Understanding the precedence and associativity of operators is crucial when working with multiple operators in a single expression. In C, the increment (++) and decrement (--) operators are unary operators, and they have high precedence.

Precedence:

  1. Increment and Decrement Operators: These operators have a higher precedence than most other operators in C, excluding parentheses (()). This means that when evaluating an expression containing increment or decrement operators alongside other operators, the increment and decrement operations are performed first.

  2. Postfix vs Prefix: Postfix increment (a++) and decrement (a--) operators have higher precedence than prefix increment (++a) and decrement (--a) operators.

Associativity:

For increment and decrement operators, the associativity is from left to right. This means if you have multiple increment or decrement operators in a single expression, the leftmost operator will be executed first, moving rightward.

Examples:

  1. Precedence between Postfix and Prefix:

    In this example, a++ is executed first due to postfix having a higher precedence. Thus, b will be 5 + 7, resulting in b = 12.

  2. Combined with other operators:

    Here, the increment of x (prefix) is performed before multiplication, and the current value of y is used in multiplication before it's decremented. The result is z = 6 * 7, so z = 42.

Differences between Increment and Decrement Operators

When working with C, it's essential to understand the fundamental differences between increment and decrement operators. Here's a comparison in a tabular format to highlight the primary distinctions:

Feature/OperationIncrement Operators (++)Decrement Operators (--)
PurposeIncreases the value of the variable by 1.Decreases the value of the variable by 1.
Prefix OperationThe value of the variable is increased before its evaluation in an expression.The value of the variable is decreased before its evaluation in an expression.
Postfix OperationThe variable is evaluated in an expression before its value is increased.The variable is evaluated in an expression before its value is decreased.
Example (Prefix)++a where if a=5 initially, it becomes 6 after operation.--a where if a=5 initially, it becomes 4 after operation.
Example (Postfix)a++ where if a=5 initially, it remains 5 for current operation but becomes 6 later.a-- where if a=5 initially, it remains 5 for current operation but becomes 4 later.
Combined with Assignmentb = a++ assigns the original value of a to b, then increases a.b = a-- assigns the original value of a to b, then decreases a.
Typical Use CaseOften used in loop counters to increment the loop variable.Frequently used in loop counters to decrement towards a termination condition.

Note: It's crucial to be aware of the behavior differences between prefix and postfix versions of these operators, as it can lead to unexpected results if misunderstood.

FAQs

Q. What would be the output if we use the ++a++ in a code?

A. The expression ++a++ is syntactically incorrect in C. The compiler will throw an error if you try to use this expression. The reason is that you can't apply two unary operators (++) to a single variable at the same time in this manner.

Q. What would be the output if we use the ++(a*b+1) in a code?

A. This expression is also syntactically incorrect in C. The prefix increment operator (++) is applied directly to a variable, not to an entire arithmetic expression like (a*b+1). Thus, trying to compile a code with this expression will result in a compilation error.

Q. Can we use prefix and postfix operators in the same code with both – increment and decrement operators?

A. Yes, you can use both prefix (++a or --a) and postfix (a++ or a--) increment and decrement operators in the same code. However, it's essential to ensure that they are used in the correct context and sequence to avoid unexpected behavior. For instance:

In the above code, ++a increments a to 6 first. The value of a (now 6) is then added to a--, which takes the current value of a (still 6) and then decrements it. The sum is 12, which is assigned to b.

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.