
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are Postfix Operators in C++
In C++, operators are special symbols that are designed to perform various Operations on variables and values, like arithmetic, comparison, or logical operations.
A Postfix Operator is a type of operator that is used to increment or decrement a value by 1(unless overloaded). It is a unary operator, which works only on a single variable.
There are two types of postfix operators in C++:
Post Increment Operator (++)
The post-increment operator increments the value of a given variable by 1, but only after its current value is used in the expression.
Therefore, the expression will first use the current value provided in the code, and then increment its value by 1.
Syntax
Here is the following syntax for the post-increment operator.
variableName++;
Post decrement Operator (--)
Similarly, the post-decrement operator decrements the value of a given variable by 1, but only after its current value is used in the expression.
Therefore, the expression will first use the current value provided in the code, and then decrement its value by 1.
Syntax
Here is the following syntax for the post-decrement operator.
variableName --;
Example Code
#include <iostream> using namespace std; int main() { int num1 = 5; int incremented_num1 = num1++; // post-increment int num2 = 10; int decremented_num2 = num2--; // post-decrement cout << "num1 = " << num1 << ", a = " << incremented_num1 << endl; cout << "num2 = " << num2 << ", b = " << decremented_num2 << endl; return 0; }
Output
num1 = 6, a = 5 num2 = 9, b = 10
Explanation
- The variables num1 and num2 are declared and initialized with the values 5 and 10, respectively.
-
int a = num1++; in this first, the value of num1(5) is assigned to a and then num1 is incremented by 1.
So the a has a value 5, and then after incrementation value of num1 becomes 6. - Similarly, int b = num2--; first, the value of num2(10) is assigned to b, and then num2 is decremented by 1.
So the b has a value 10, and then after decrement value of num2 becomes 9.