The Wayback Machine - https://web.archive.org/web/20240717024735/https://www.geeksforgeeks.org/cpp-increment-and-decrement-operators/
Open In App

C++ Increment and Decrement Operators

Last Updated : 27 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: Operators in C++

What is a C++ increment Operator?

 The C++ increment operator is a unary operator. The symbol used to represent the increment operator is (++). The increment operator increases the value stored by the variable by 1. This operator is used for Numeric values only.

There are two types of C++ increment Operator: 

  • Pre-Increment
  • Post-Increment

1. Post-Increment operator (a++)

The postfix operator says that first use the value and then increment it. This means the value is first used up for the operation then the value is updated by 1.

Example:

C++




// C++ Program to implement
// Post-Increment
#include <iostream>
  
using namespace std;
  
int main()
{
  
    int x = 5;
    cout << "Value before using post increment operator is "
            ": "
         << x << endl;
    int temp = x++;
    cout << "The value stored by temp is : " << temp
         << endl;
    cout << "After using the post increment operator is : "
         << x << endl;
    return 0;
}


Output

Value before using post increment operator is : 5
The value stored by temp is : 5
After using the post increment operator is : 6

2. Pre-increment operator(++a)

The postfix operator says that first increment the value then use it. This means the value is increased by 1 for the operation then the value is used by the variable

Example: 

C++




// C++ Program to implement
// Pre-Increment
#include <iostream>
  
using namespace std;
  
int main()
{
  
    int x = 5;
    cout
        << "Value before using pre increment operator is : "
        << x << endl;
    int temp = ++x;
    cout << "The value stored by temp is : " << temp
         << endl;
    cout << "After using the post increment operator is : "
         << x << endl;
    return 0;
}


Output

Value before using pre increment operator is : 5
The value stored by temp is : 6
After using the post increment operator is : 6

What is a C++ decrement Operator?

The C++ decrement operator is a unary operator. The symbol used to represent the increment operator is (–). The decrement operator decreases the value stored by the variable by 1. This operator is used for Numeric values only.

There are two types of C++ decrement Operator:

  • Post-decrement operator
  • Pre-decrement operator

1. Post-decrement operator (a–): 

The postfix operator says that first use the value and then decrease it. This means the value is first used up for the operation then the value is decreased by 1.

Example: 

C++




// C++ Program to implement
// Post-Decrement
#include <iostream>
  
using namespace std;
  
int main()
{
  
    int x = 5;
    cout << "Value before using post decrement operator is "
            ": "
         << x << endl;
    int temp = x--;
    cout << "The value stored by temp is : " << temp
         << endl;
    cout << "After using the post decrement operator is : "
         << x << endl;
    return 0;
}


Output

Value before using post decrement operator is : 5
The value stored by temp is : 5
After using the post decrement operator is : 4

2. Pre-decrement operator (–a)

The postfix operator says that first decrease the value and then use it. This means the value is decreased by 1 for the operation then the value is used by the variable

Example:

C++




// C++ Program to implement
// Pre-Decrement
#include <iostream>
  
using namespace std;
  
int main()
{
  
    int x = 5;
    cout
        << "Value before using pre-decrement operator is : "
        << x << endl;
    int temp = --x;
    cout << "The value stored by temp is : " << temp
         << endl;
    cout << "After using the pre-decrement operator is : "
         << x << endl;
    return 0;
}


Output

Value before using pre-decrement operator is : 5
The value stored by temp is : 4
After using the pre-decrement operator is : 4


Similar Reads

Increment (Decrement) operators require L-value Expression
What will be the output of the following program? #include&lt;stdio.h&gt; int main() { int i = 10; printf(&quot;%d&quot;, ++(-i)); return 0; } A) 11 B) 10 C) -9 D) None Answer: D, None - Compilation Error. Explanation: In C/C++ the pre-increment (decrement) and the post-increment (decrement) operators require an L-value expression as operand. Provi
1 min read
Increment (++) and Decrement (--) Operator Overloading in C++
Operator overloading is a feature in object-oriented programming which allows a programmer to redefine a built-in operator to work with user-defined data types. Why Operator Overloading? Let's say we have defined a class Integer for handling operations on integers. We can have functions add(), subtract(), multiply() and divide() for handling the re
4 min read
Pre-increment (or pre-decrement) With Reference to L-value in C++
Prerequisite: Pre-increment and post-increment in C/C++ In C++, pre-increment (or pre-decrement) can be used as l-value, but post-increment (or post-decrement) can not be used as l-value. For example, following program prints a = 20 (++a is used as l-value) l-value is simply nothing but the memory location, which has an address. C/C++ Code // CPP p
2 min read
Pre-increment and Post-increment in C/C++
In C/C++, Increment operators are used to increase the value of a variable by 1. This operator is represented by the ++ symbol. The increment operator can either increase the value of the variable by 1 before assigning it to the variable or can increase the value of the variable by 1 after assigning the variable. Thus it can be classified into two
3 min read
Increment Operator Behavior When Passed as Function Parameters in C++
In C++, an increment operator is used to increment the value of the variable by 1. The symbol ++ is used to represent the increment operator. There are two types of increment operators: Pre-Increment Operator: This form of increment operator increases the value of the variable by 1 before assigning it to the variable.Post-Increment Operator: This f
3 min read
What are the differences between bitwise and logical AND operators in C/C++?
A Bitwise And operator is represented as '&amp;' and a logical operator is represented as '&amp;&amp;'. The following are some basic differences between the two operators. a) The logical and operator '&amp;&amp;' expects its operands to be boolean expressions (either 1 or 0) and returns a boolean value. The bitwise and operator '&amp;' work on Inte
4 min read
What are the Operators that Can be and Cannot be Overloaded in C++?
There are various ways to overload Operators in C++ by implementing any of the following types of functions: 1) Member Function 2) Non-Member Function 3) Friend Function List of operators that can be overloaded are: + - * ? % ? &amp; | ~ ! = &lt; &gt; += -= *= ?= %= ?= &amp;= |= &lt;&lt; &gt;&gt; &lt;&lt;= &gt;&gt;= == != &lt;= &gt;= &amp;&amp; ||
4 min read
Conditionally assign a value without using conditional and arithmetic operators
Given 4 integers a, b, y, and x, where x can assume the values of either 0 or 1 only. The following question is asked: If 'x' is 0, Assign value 'a' to variable 'y' Else (If 'x' is 1) Assign value 'b' to variable 'y'. Note: - You are not allowed to use any conditional operator (including the ternary operator) or any arithmetic operator (+, -, *, /)
6 min read
new and delete Operators in C++ For Dynamic Memory
Dynamic memory allocation in C/C++ refers to performing memory allocation manually by a programmer. Dynamically allocated memory is allocated on Heap, and non-static and local variables get memory allocated on Stack (Refer to Memory Layout C Programs for details). What are applications?  One use of dynamically allocated memory is to allocate memory
6 min read
Left Shift and Right Shift Operators in C/C++
In C/C++, left shift (&lt;&lt;) and right shift (&gt;&gt;) operators are binary bitwise operators that are used to shift the bits either left or right of the first operand by the number of positions specified by the second operand allowing efficient data manipulation. In this article, we will learn about the left shift and right shift operators. Le
6 min read
three90RightbarBannerImg