1.
What is Increment Operator in C?
1.1.
Syntax of Increment Operator C
2.
Types of Increment Operators in C
2.1.
1. Prefix Increment Operator
2.2.
C
2.3.
2. Postfix Increment Operator
2.4.
C
3.
What is Decrement Operators in C?
3.1.
Syntax of Increment Operator C
4.
Types of Decrement Operators in C
4.1.
1. Prefix Decrement Operator
4.2.
C
4.3.
2. Postfix Decrement Operator
4.4.
C
5.
Precedence in Increment and Decrement Operators in C
5.1.
C
6.
Properties of Increment and Decrement Operators in C
6.1.
C
7.
Difference between Increment and Decrement Operators
8.
Some interesting facts about increment and decrement operators in C
9.
9.1.
What is the difference between Postfix and Prefix increment operators?
9.2.
Which operators have higher precedence- Prefix or Postfix?
9.3.
Can increment/decrement operators be used with constant values?
10.
Conclusion
Last Updated: Jul 9, 2024
Easy

# Increment and Decrement Operators in C

Gaurav Gandhi
0 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

The increment operator increases/increments the value of the variable by one, while the Decrement operator decreases/decrements the value of the variable by one?

In this article, we will learn about increment and decrement operators and their different types in C. We will also talk about properties and precedence of increment and decrement operators in C.

Also read - Bit stuffing program in c

## What is Increment Operator in C?

Increment operators increase the value of the variable by one. The increment operator is used to increment the current value of a variable by adding an integer 1. Increment operators can be applied to only variables. The increment operator is denoted by ++.

### Syntax of Increment Operator C

``````variable++;
++variable;``````

Both variable++ and ++variable increment the value of the variable by 1. The difference lies in their behavior regarding the value used in expressions and assignment operations.

For example,

``c = c+1;``

Or

``c+=1;``

The above operation increments the value of c by one.

## Types of Increment Operators in C

Below are the two types of increment operators.

1. Prefix Increment Operator
2. Postfix Increment Operator

### 1. Prefix Increment Operator

This operator increases the variable's value by one immediately, and then this incremented value is used in the expression. It is represented as ++c (here, c is the variable name).

Syntax

``````// prefix increment
++ variable;``````

Example

• C

### C

``#include <stdio.h>int main(){    int c=5;    int n=++c;        // The value of c gets incremented by one and then used in the expression    printf("Prefix Increment Operator:\n");    printf("Value of variable c: %d\n", c);    printf("Value of variable n: %d\n", n);        return 0;}``

Output

Explanation

The value of c gets incremented first, i.e., the value of c is increased by one, and then this incremented value is assigned to the variable n. Hence, both variables contain the value 6.

### 2. Postfix Increment Operator

This operator allows us to use the current value of the variable in the expression, and then it increments the variable's value by one. It is represented as c++ (here, c is the variable name).

Syntax

``````// postfix increment
variable++;``````

Example

• C

### C

``#include <stdio.h>int main(){    int c=5;    int n=c++;        // The current value of c gets assigned to n first and then its value gets incremented by one    printf("Postfix Increment Operator:\n");    printf("Value of variable c: %d\n", c);    printf("Value of variable n: %d\n", n);        return 0;}``

Output

Explanation

The current value of c gets assigned to n first, and then its value gets incremented by one. Hence, variable n stores the value five, and variable c stores the incremented value, which is 6.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

## What is Decrement Operators in C?

The decrement operators in C provide a convenient way to decrease the value of a variable by 1. They are unary operators, meaning they operate on a single operand.

### Syntax of Increment Operator C

``````variable--;
--variable;
``````

Both variable-- and --variable decrement the value of the variable by 1. The difference lies in their behavior regarding the value used in expressions and assignment operations.

For example,

``c = c-1;``

Or

``c-=1;``

The above operation decrements the value of c by one.

## Types of Decrement Operators in C

Below are the two types of decrement operators.

1. Prefix Decrement Operator
2. Postfix Decrement Operator

### 1. Prefix Decrement Operator

This operator immediately decreases the variable's value by one, and then this decremented value is used in the expression. It is represented as --c (here, c is the variable name).

Syntax

``````// prefix decrement
-- variable;``````

Example

• C

### C

``#include <stdio.h>int main(){    int c=5;    int n=--c;        // The value of c gets decremented by one and then used in the expression    printf("Prefix Decrement Operator:\n");    printf("Value of variable c: %d\n", c);    printf("Value of variable n: %d\n", n);        return 0;}``

Output

Explanation

The value of c gets decremented first, i.e., the value of c is decreased by one, and then this decremented value is assigned to the variable n. Hence, both variables contain the value 4.

### 2. Postfix Decrement Operator

This operator allows us to use the current value of the variable in the expression, and then it decrements the variable's value by one. It is represented as c-- (here, c is the variable name).

Syntax

``````// postfix decrement
variable--;``````

Example

• C

### C

``#include <stdio.h>int main(){    int c=5;    int n=c--;        // The current value of c gets assigned to n first and then its value gets decremented by one    printf("Postfix Decrement Operator:\n");    printf("Value of variable c: %d\n", c);    printf("Value of variable n: %d\n", n);        return 0;}``

Output

Explanation

The current value of c gets assigned to n first, and then its value gets decremented by one. Hence, variable n stores the value five, and variable c stores the decremented value, which is 4.

## Precedence in Increment and Decrement Operators in C

The increment and decrement operators have higher precedence than any other C operators except for parentheses. Higher precedence means that when an expression is evaluated, the increment/decrement operations are performed before any other operations.

Postfix increment/decrement operators have higher precedence than Prefix increment/decrement operators.
Example

• C

### C

``#include <stdio.h>int main(){    int a=3;    int b=4;    int c=a++ + --b;       printf("Value of expression : %d\n", c);    printf("Value of variable a: %d\n", a);    printf("Value of variable b : %d\n", b);        return 0;}``

Output

Explanation

As postfix increment/decrement operators have higher precedence, so a++ is evaluated first. The current value of a, 3, is used in the expression, and then its value is incremented by one. So, the value of a becomes 4.

After this, --b (prefix operation) is evaluated. --b first decrements the value by one. The decremented value of b becomes 3, and this decremented value is used in the expression.

The addition operator (â€˜+â€™) adds the values of a and b, i.e., 3+3 is calculated. Hence, the value of the expression is 6.

## Properties of Increment and Decrement Operators in C

Below are the properties of increment and decrement operators in C.

1. The operators can be used with variables and not with constants.

``int c=++5;``

The above line throws a compilation error as the operator uses a constant.
2. Nesting of the increment and decrement operator gives a compilation error. Nesting means an operator inside an operator.

``````int c=3;
int n=--(++c);``````

The above lines give an error as nesting of decrement and increment is not allowed.
3. These operators can also be used with Boolean values.

Code

• C

### C

``#include <stdio.h>#include <stdbool.h>int main(){    bool c=true;    c--;    printf("Value: %d\n", c);    return 0;}``

Output

Explanation

As c is true, this means the value of c is 1. After decrementing, the value of c becomes 0.

## Difference between Increment and Decrement Operators

Below is the difference table between increment and decrement operators.

## Some interesting facts about increment and decrement operators in C

Increment and decrement operators in C, namely ++ and --, hold exciting characteristics. They increase or decrease a variable's value by one, but nuances exist.

• Postfix vs. Prefix: i++ and ++i behave differently. The former uses the current value, then increments, while the latter increments first, and then uses the value.
• Complex Expressions: In complex expressions, the order of evaluation matters. Incrementing within an expression can lead to different postfix or prefix usage results.
• Side Effects: Overusing increment/decrement can lead to code that's easier to read and maintain. Use them wisely for clarity.
• Performance: Some compilers may optimize prefix increments faster than postfix ones.

### What is the difference between Postfix and Prefix increment operators?

The Postfix operator allows us to use the current value of the variable in the expression. Then it increments the variable's value by one, while the Prefix operator increases the variable's value by one immediately, and then this incremented value is used in the expression.

### Which operators have higher precedence- Prefix or Postfix?

Postfix increment/decrement operators have higher precedence than Prefix increment/decrement operators. Higher precedence means that when an expression is evaluated, the postfix increment/decrement operations are performed before prefix increment/decrement operations.

### Can increment/decrement operators be used with constant values?

No, increment/decrement operators cannot be used with constant values. They are unary operators designed to modify the value of variables, not constants.

## Conclusion

In this article, we have discussed increment and decrement operators in C. These operators provide convenient ways to modify variable values. Understanding their syntax, behavior, and best practices empowers developers to write efficient and concise code.

We hope this article has helped you understand increment and decrement operators and their different types. If this article helped you in any way, then you can read more such articles on our platform, Code360. You will find articles on almost every topic on our platform. For interview preparations, you can read the Interview Experiences of popular companies.
Happy Coding !!

Live masterclass