Details the use of the increment operator ++ and the decrement operator in C++

  • 2020-05-07 20:12:11
  • OfStack

prefix increment and decrement operators: ++ and --
 
grammar

+ + unary expression � � unary - expression

note
The prefix increment operator (++) adds 1 to its operands; This increment is the result of the expression. The operand must be an lvalue whose type is not const. The result is an lvalue of the same type as the operand.
The prefix decrement operator is similar to the prefix increment operator, except that the operands are reduced by 1 and the result is a decrement value.
Prefix and suffix increment and decrement operators affect their operands. The main difference between them is the order in which the increment or decrement occurs in the evaluation of the expression. In the prefix form, the value is incremented or decrement before it is used in the expression evaluation, so the value of the expression is different from that of the operand. In the postfix form, the value is incremented or decrement after it is used in the expression evaluation, so that the value of the expression is the same as that of the operand. For example, the following program will print "++i = 6" :


// expre_Increment_and_Decrement_Operators.cpp
// compile with: /EHsc
#include <iostream>

using namespace std;

int main() {
  int i = 5;
  cout << "++i = " << ++i << endl;
}

The operands of an integer or floating type increase or decrease by integer value 1. The result type is the same as the operand type. The operands of a pointer type increase or decrease by the size of the object it addresses. An increasing pointer points to the next object. The decrementing pointer will point up 1 object.
Because of the side effects of increment and decrement operators, using expressions with increment or decrement operators in preprocessor macros produces unexpected results. See the following example:


// expre_Increment_and_Decrement_Operators2.cpp
#define max(a,b) ((a)<(b))?(b):(a)

int main()
{
  int i = 0, j = 0, k;
  k = max( ++i, j );
}

The macro will be extended to:


k = ((++i)<(j))?(j):(++i);

If i is greater than or equal to j or one less than j, it will be increments twice.
System_CAPS_note note
Because C++ inline functions eliminate side effects (as described here) and allow the language to perform more comprehensive type checking, in many cases C++ inline functions are preferable to macros.

suffix increment and decrement operators: ++ and --
 
grammar

          postfix-expression
          ++
postfix - expression � �
note
C++ provides prefix and suffix increment and decrement operators; The difference is that in the postfix notation, the operator appears after postfix-expression, while 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 to increment the value of the operand by 1 unit of the appropriate type. Similarly, the effect of applying the suffix decrement operator is to reduce the value of the operand by one cell of the appropriate type.
It is worth noting that postfix increment or decrement expressions evaluate to the value of the expression before the respective operators are applied. An increment or decrement operation occurs after the calculation of the operands. This problem occurs only when a postfix increment or decrement occurs in the context of a larger expression.
When the suffix operator is applied to a function parameter, there is no guarantee that the value of the parameter is increasing or decreasing until the value is passed to the function.
Applying the postfix increment operator to a pointer to an array of objects of type long actually increases the internal representation of the pointer by 4. This behavior causes the pointer to the n element of the previous reference array to refer to the n+1 element.
The operands of the postfix increment and postfix decrement operators must be modifiable (non-const) lvalues of arithmetic or pointer types. The result is of the same type as postfix-expression, but is no longer an lvalue.
The operand of the postfix increment operator can also be of type bool, in which case the operand is evaluated and then set to true). The postfix decrement operator cannot have an operand of type bool.
The following code demonstrates 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;
}

Post-increment and post-decrement operations for enumerated types are not supported:


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


Related articles: