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, the 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 to prefix and postfix 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 in C programming language to get a better idea!

#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 the X value was incremented by 1.

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

C Prefix and Postfix

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

Let’s explore the C prefix and postfix

  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.

C Prefix and Postfix Example

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

#include<stdio.h> 

int main()
{

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

 printf("---- PRE INCREMENT 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 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 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 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;
 }

Output

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

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