Increment (++) and Decrement (--) Operator as Prefix and Postfix
In this tutorial, we will learn about the increment operator ++ and the decrement operator -- in detail with the help of examples..
Increment and Decrement Operator
In programming (C, C++ etc. ), the increment operator ++
increases the value of a variable by 1. Similarly, the decrement operator --
decreases the value of a variable by 1.
For Example:- let's understand increment and decrement as prefix and postfix.
a = 2 ++a; // a becomes 3 a++; // a becomes 4 --a; // a becomes 3 a--; // a becomes 2
Simple enough till now. However, there is a slight but important difference you should know when these two operators are used as prefix and postfix.
++ and -- operator as prefix and postfix
- If you use the
++
operator as "prefix" like:++var
. The value of var is incremented by 1 then, it returns the value. - If you use the
++
operator as "postfix" like:var++
. The original value of var is returned first then, var is incremented by 1. - The
--
operator works in a similar way like the++
operator except it decreases the value by 1.
Let's see the use of ++
as prefix and postfix in C, and C++
Example 1: C Programming increment operator as prefix and postfix
// C programming example to understand the increment operator as prefix and postfix.
#include <stdio.h>
int main() {
int var1 = 2, var2 = 2;
// var1 is displayed
// Then, var1 is increased to 3.
printf("var1=%d\n", var1++);
// var2 is increased to 3
// Then, it is displayed.
printf("var2=%d\n", ++var2);
return 0;
}
Output
var1=2 var2=3
Example 2: C programming Decrement operator as prefix and postfix.
We can convert a char
array to double
by using the std::atof()
function.
For Example :-
// C programming example to understand the decrement operator as prefix and postfix.
#include <stdio.h>
int main() {
int var1 = 2, var2 = 2;
// var1 is displayed
// Then, var1 is decreased to 1.
printf("var1=%d\n", var1--);
// var2 is decreased to 1
// Then, it is displayed.
printf("var2=%d\n", --var2);
return 0;
}
Output
var1=2 var2=1
Example 3: C++ Increment operator as prefix and postfix.
This function accepts a number (can be any data type) and returns the number in the desired string.
// C++ programming example to understand the increment operator as prefix and postfix.
#include <iostream>
using namespace std;
int main() {
int var1 = 2, var2 = 2;
// var1 is displayed
// Then, var1 is increased to 3.
cout << "var1=" << var1++ << endl;
// var2 is increased to 3
// Then, it is displayed.
cout << "var2=" << ++var2 << endl;
return 0;
}
Output
var1=2 var2=3
Example 4: C++ decrement operator as prefix and postfix.
// C++ programming example to understand the decrement operator as prefix and postfix.
#include <iostream>
using namespace std;
int main() {
int var1 = 2, var2 = 2;
// var1 is displayed
// Then, var1 is decrease to 1.
cout << "var1=" << var1-- << endl;
// var2 is increased to 1
// Then, it is displayed.
cout << "var2=" << --var2 << endl;
return 0;
}
Output
var1=2 var2=1
We hope that this tutorial helped you develop better understanding of the concept of Increment-Decrement-operator-difference-prefix-postfix in C++.
Keep Learning : )