What are postfix operators in C++?


Postfix operators are unary operators that work on a single variable which can be used to increment or decrement a value by 1(unless overloaded). There are 2 postfix operators in C++, ++ and --.

In the postfix notation (i.e., i++), the value of i is incremented, but the value of the expression is the original value of i. So basically it first assigns a value to expression and then increments the variable. For example,

Example

#include<iostream>
using namespace std;

int main() {
   int j = 0, i = 10;

   // If we assign j to be i++, j will take i's current
   // value and i's value will be increatemnted by 1.
   j = i++;
   cout << j << ", " << i << "\n";
   return 0;
}

Output

This will give the output −

10, 11

Updated on: 11-Feb-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements