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; }
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
- ++i (Pre-increment): It will increment the value of i even before assigning it to the variable i.
- i++ (Post-increment): The operator will return the variable value first (i.e, i value) then only i value is incremented by 1.
- –i (Pre decrement): It will decrement the value of i even before assigning it to the variable i.
- 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; }
Although increment and decrement operators used in For Loop, While 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