Increment and Decrement Operators in C

The Increment and Decrement Operators in C are some of the Operators, which are used to increase or decrease the value by 1. For instance, Incremental operator ++ is used to increase the existing variable value by 1 (x = x + 1). And decrement operator – – is used to decrease or subtract the existing value by 1 (x = x – 1).

The syntax of the increment and decrement operators in C Programming is:

  • Increment Operator : ++x or x++
  • Decrement Operator: –x or x–

Increment and decrement operators in C Example

Let’s see one example of increment and decrement operators to get a better idea!

/* Increment and Decrement Operators in C Example */

#include <stdio.h> 

int main()
{
 int x = 10,y = 20;

 printf("----INCREMENT OPERATOR EXAMPLE---- \n");
 printf("Value of x : %d \n", x); //Original Value
 printf("Value of x : %d \n", x++); // Using increment Operator
 printf("Value of x : %d \n", x); //Incremented value
 
 printf("----DECREMENT OPERATOR EXAMPLE---- \n");
 printf("Value of y : %d \n", y); //Original Value
 printf("Value of y : %d \n", y--); // using decrement Operator
 printf("Value of y : %d \n", y); //decremented value

 return 0;
}
Increment and decrement operators in c

At Line 7, we used the increment operator. So the value of the X was returned first (i.e., 10), and then X value is incremented by 1.

Line 8: We called the X value again; it displays 11 because the value has already updated. Same with the decrement operator.

Prefix and Postfix in C

If you observe the above syntax, we can assign the C increment and decrement operators either before operand or after the operand. When ++ or — is used before operand like: ++x, –x then we call it as prefix, if ++ or — is used after the operand like: x++ or x– then we called it as postfix.

Let’s explore the prefix and postfix of increment and decrement operators in C

  1. ++i (Pre-increment): It will increment the value of i even before assigning it to the variable i.
  2. i++ (Post-increment): The operator will return the variable value first (i.e, i value) then only i value is incremented by 1.
  3. –i (Pre decrement): It will decrement the value of i even before assigning it to the variable i.
  4. i– (Post decrement): The operator returns the variable value first (i.e., i value), then only i value decremented by 1.

Prefix and Postfix in C Example

This Program shows how to use C Increment and Decrement Operators as a Prefix and Postfix in C.

/* Increment and Decrement Operators in C as Prefix and Postfix */

#include<stdio.h> 

int main()
{

 int x = 10,y = 20, a = 5, b= 4;

 printf("---- PRE INCREMENT OPERATOR EXAMPLE---- \n");
 printf("Value of x : %d \n", x); //Original Value
 printf("Value of x : %d \n", ++x); // using Pre increment Operator
 printf("Value of x Incremented : %d \n", x); //Incremented value

 printf("----POST INCREMENT OPERATOR EXAMPLE---- \n");
 printf("Value of y : %d \n", y); //Original Value
 printf("Value of y : %d \n", y++); // using Post increment Operator
 printf("Value of Incremented y : %d \n", y); //Incremented value

 printf("----PRE DECREMENT OPERATOR EXAMPLE---- \n");
 printf("Value of a : %d \n", a); //Original Value
 printf("Value of a : %d \n", --a); // using Pre decrement Operator
 printf("Value of decremented y : %d \n", a); //decremented value

 printf("----POST DECREMENT OPERATOR EXAMPLE---- \n");
 printf("Value of b : %d \n", b); //Original Value
 printf("Value of b : %d \n", b--); // using Post decrement Operator
 printf("Value of decremented b : %d \n", b); //decremented value

 return 0;
 }

C Prefix and Postfix output

---- PRE INCREMENT OPERATOR EXAMPLE---- 
Value of x : 10 
Value of x : 11 
Value of x Incremented : 11 
----POST INCREMENT OPERATOR EXAMPLE---- 
Value of y : 20 
Value of y : 20 
Value of Incremented y : 21 
----PRE DECREMENT OPERATOR EXAMPLE---- 
Value of a : 5 
Value of a : 4 
Value of decremented y : 4 
----POST DECREMENT OPERATOR EXAMPLE---- 
Value of b : 4 
Value of b : 4 
Value of decremented b : 3 

Although increment and decrement operators used in For LoopWhile and Do-While loops, we are not given an example. Because it is too early to understand the loops concept. Try to understand the concept of the prefix and postfix in C. This Operator helps you to understand if statement, for loop, while loop and do-while loop syntax in the C Programming easily