Open In App

Increment and Decrement Operators in C

Last Updated : 28 Aug, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The increment ( ++ ) and decrement ( — ) operators in C are unary operators for incrementing and decrementing the numeric values by 1 respectively. The incrementation and decrementation are one of the most frequently used operations in programming for looping, array traversal, pointer arithmetic, and many more.

In this article, we will discuss the increment operator and decrement operator, both their prefix and postfix applications, and the difference between them.

Increment Operator in C

The increment operator ( ++ ) is used to increment the value of a variable in an expression by 1. It can be used on variables of the numeric type such as integer, float, character, pointers, etc.

Syntax of Increment Operator

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

// AS PREFIX
++m
// AS POSTFIX
m++

where m is variable.

How to use the increment operator?

Both pre-increment and post-increment increase the value of the variable but there is a little difference in how they work.

1. Pre-Increment

In pre-increment, the increment operator is used as the prefix. Also known as prefix increment, the value is incremented first according to the precedence and then the less priority operations are done.

Example

result = ++var1;

The above expression can be expanded as

var = var + 1;
result = var;

2. Post-Increment

In post-increment, the increment operator is used as the suffix of the operand. The increment operation is performed after all the other operations are done. It is also known as postfix increment.

Example

result = var1++;

The above expression is equivalent

result = var;
var = var + 1;

Example of Increment Operator

C




// C Program to illustrate the increment of both type
#include <stdio.h>
 
void increment()
{
    int a = 5;
    int b = 5;
 
    // PREFIX
    int prefix = ++a;
    printf("Prefix Increment: %d\n", prefix);
 
    // POSTFIX
    int postfix = b++;
    printf("Postfix Increment: %d", postfix);
}
 
// Driver code
int main()
{
    increment();
 
    return 0;
}


Output

Prefix Increment: 6
Postfix Increment: 5

As we can see in postfix, the value is incremented after the assignment operator is done.

Note: The post-increment have higher precedence that pre-increment as it is postfix operator while pre-increment comes in unary operator category.

Decrement Operator in C

The decrement operator is used to decrement the value of a variable in an expression. In the Pre-Decrement, the value is first decremented and then used inside the expression. Whereas in the Post-Decrement, the value is first used inside the expression and then decremented.

Syntax

Just like the increment operator, the decrement operator can also be used in two ways:

// AS PREFIX
--m
// AS POSTFIX
m--

where m is variable.

1. Pre-Decrement Operator

The pre-decrement operator decreases the value of the variable immediately when encountered. It is also known as prefix decrement as the decrement operator is used as the prefix of the operand.

Example

result = --m;

which can be expanded to

m = m - 1;
result = m;

2. Post-Decrement Operator

The post-decrement happens when the decrement operator is used as the suffix of the variable. In this case, the decrement operation is performed after all the other operators are evaluated.

Example

result = m--;

The above expression can be expanded as

result = m;
m = m-1;

Example of Decrement Operator

C




// C program to illustrate the decrement operator of both
// types
#include <stdio.h>
 
void decrement()
{
    int a = 5;
    int b = 5;
 
    // PREFIX
    int prefix = --a;
    printf("Prefix = %d\n", prefix);
 
    // POSTFIX
    int postfix = b--;
    printf("Postfix = %d", postfix);
}
 
// Driver code
int main()
{
    decrement();
 
    return 0;
}


Output

Prefix = 4
Postfix = 5

Differences between Increment And Decrement Operators

The following table list the difference between the increment and decrement operators:

Increment Operator

Decrement Operator

Increment Operator adds 1 to the operand. Decrement Operator subtracts 1 from the operand.
The Postfix increment operator means the expression is evaluated first using the original value of the variable and then the variable is incremented(increased).The  The Postfix decrement operator means the expression is evaluated first using the original value of the variable and then the variable is decremented(decreased).
Prefix increment operator means the variable is incremented first and then the expression is evaluated using the new value of the variable. Prefix decrement operator means the variable is decremented first and then the expression is evaluated using the new value of the variable.
Generally, we use this in decision-making and looping. This is also used in decision-making and looping.


Next Article

Similar Reads

three90RightbarBannerImg
  翻译: