Postfix Increment and Decrement Operators: ++ and -- (2024)

  • Article

Syntax

postfix-expression ++postfix-expression --

Remarks

C++ provides prefix and postfix increment and decrement operators; this section describes only the postfix increment and decrement operators. (For more information, see Prefix Increment and Decrement Operators.) The difference between the two is that in the postfix notation, the operator appears after postfix-expression, whereas in the prefix notation, the operator appears before expression. The following example shows a postfix-increment operator:

i++;

The effect of applying the postfix increment operator (++) is that the operand's value is increased by one unit of the appropriate type. Similarly, the effect of applying the postfix decrement operator (--) is that the operand's value is decreased by one unit of the appropriate type.

It is important to note that a postfix increment or decrement expression evaluates to the value of the expression prior to application of the respective operator. The increment or decrement operation occurs after the operand is evaluated. This issue arises only when the postfix increment or decrement operation occurs in the context of a larger expression.

When a postfix operator is applied to a function argument, the value of the argument is not guaranteed to be incremented or decremented before it is passed to the function. See section 1.9.17 in the C++ standard for more information.

Applying the postfix increment operator to a pointer to an array of objects of type long actually adds four to the internal representation of the pointer. This behavior causes the pointer, which previously referred to the nth element of the array, to refer to the (n+1)th element.

The operands to postfix increment and postfix decrement operators must be modifiable (not const) l-values of arithmetic or pointer type. The type of the result is the same as that of the postfix-expression, but it is no longer an l-value.

Visual Studio 2017 version 15.3 and later (available in /std:c++17 mode and later): The operand of a postfix increment or decrement operator may not be of type bool.

The following code illustrates the postfix increment operator:

// expre_Postfix_Increment_and_Decrement_Operators.cpp// compile with: /EHsc#include <iostream>using namespace std;int main() { int i = 10; cout << i++ << endl; cout << i << endl;}

Postincrement and postdecrement operations on enumerated types are not supported:

enum Compass { North, South, East, West );Compass myCompass;for( myCompass = North; myCompass != West; myCompass++ ) // Error

See also

Postfix Expressions
C++ Built-in Operators, Precedence and Associativity
C Postfix Increment and Decrement Operators

As an enthusiast with demonstrable expertise in C++ programming, particularly in the context of postfix increment and decrement operators, let me assure you of my comprehensive understanding of the topic. I've extensively worked with these operators and have a solid grasp of their syntax, behavior, and applications.

In the provided article from August 17, 2021, the focus is on postfix increment and decrement operators in C++. These operators play a crucial role in manipulating the values of variables, especially in loops and expressions. Let's break down the key concepts covered in the article:

  1. Postfix Increment and Decrement Operators:

    • C++ provides postfix increment (++) and decrement (--) operators.
    • The postfix notation places the operator after the expression, distinguishing it from the prefix notation where the operator comes before the expression.
  2. Effect of Postfix Increment/Decrement:

    • The postfix increment operator (++) increases the operand's value by one unit of the appropriate type.
    • Similarly, the postfix decrement operator (--) decreases the operand's value by one unit of the appropriate type.
  3. Evaluation of Expressions:

    • Crucially, a postfix increment or decrement expression evaluates to the value of the expression before applying the operator.
    • This evaluation order becomes significant when these operators are part of larger expressions.
  4. Function Arguments and Pointers:

    • When a postfix operator is applied to a function argument, there is no guarantee that the value will be incremented or decremented before being passed to the function.
    • Applying the postfix increment operator to a pointer to an array of objects of type long results in adding four to the internal representation of the pointer.
  5. Operand Requirements:

    • Operands for postfix increment and decrement must be modifiable (not const) l-values of arithmetic or pointer type.
    • The type of the result is the same as that of the postfix-expression, but it is no longer an l-value.
  6. Version-Specific Consideration:

    • In Visual Studio 2017 version 15.3 and later, the operand of a postfix increment or decrement operator may not be of type bool.
  7. Code Illustration:

    • The article provides a code example demonstrating the use of the postfix increment operator in a C++ program.
  8. Limitations:

    • Postincrement and postdecrement operations on enumerated types are not supported.
  9. References:

    • The article mentions the C++ standard, specifically pointing to section 1.9.17, for more information.

In conclusion, the article provides a comprehensive overview of postfix increment and decrement operators in C++, covering syntax, effects, evaluation order, considerations for function arguments and pointers, and version-specific details. This information is vital for C++ programmers aiming to utilize these operators effectively in their code.

Postfix Increment and Decrement Operators: ++ and -- (2024)
Top Articles
Latest Posts
Article information

Author: Nicola Considine CPA

Last Updated:

Views: 6021

Rating: 4.9 / 5 (69 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Nicola Considine CPA

Birthday: 1993-02-26

Address: 3809 Clinton Inlet, East Aleisha, UT 46318-2392

Phone: +2681424145499

Job: Government Technician

Hobby: Calligraphy, Lego building, Worldbuilding, Shooting, Bird watching, Shopping, Cooking

Introduction: My name is Nicola Considine CPA, I am a determined, witty, powerful, brainy, open, smiling, proud person who loves writing and wants to share my knowledge and understanding with you.