Explain the usage of increment operator + + and decrement operator in C + + _c language

Source: Internet
Author: User

Prefix increment and decrement operators: + + and-

Grammar

+ + Unary-expression––unary-expression

Note
The prefix increment operator (+ +) adds 1 to its operand, which is the result of an expression. The operand must be a left value that is not a const type. The result is a left value of the same type as the operand.
The prefix decrement operator (-) is similar to the prefix increment operator, except that the operand is reduced by 1 and the result is a descending value.
Both the prefix and suffix increment and decrement operators affect their operands. The main difference between them is to increment or decrement the order that occurs in the calculation of expressions. In the prefix form, values are incremented or decremented before the value is used in the expression evaluation, so the value of the expression differs from the operand's value. In the suffix form, values are incremented or decremented after the value is used in the expression evaluation, so the value of the expression is the same as the value 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;
}

Operands of an integral or floating type are incremented or decremented by an integer value of 1. The type of the result is the same as the operand type. Operands of a pointer type are incremented or decremented by the size of the object to which they are addressed. The incremented pointer points to the next object, and the descending pointer points to the previous object.
Because the increment and decrement operators have side effects, the use of an expression with an increment or decrement operator in a preprocessor macro produces unexpected results. Take a look at 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 expands to:

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

If I is greater than or equal to J or 1 smaller than J, it will be incremented two times.
System_caps_note Note
Because the C + + inline function eliminates side effects (such as the side-effects described here) and allows the language to perform a more comprehensive type check, in many cases C + + inline functions are preferable to macros.

Suffix increment and decrement operators: + + and-

Grammar

      postfix-expression
      + +
postfix-expression– –
Memo
C + + provides a prefix and suffix increment and decrement operator; the difference is that in suffix notation, operators appear after postfix-expression, and in prefix notation, operators appear before expression. The following example shows a suffix increment operator:
i++;
The effect of applying the suffix increment operator (+ +) is to increase the value of the operand by an appropriate type of unit. Similarly, the effect of applying the suffix decrement operator (-) is to reduce the value of the operand to a unit of the appropriate type.
It is noteworthy that the suffix increment or decrement expression evaluates to the value of the expression before the respective operator is applied. The increment or decrement operation occurs after the operand is computed. This problem occurs only if a suffix increment or decrement operation occurs in the context of a larger expression.
When the suffix operator is applied to a function argument, there is no guarantee that the value is incremented or decremented until the value of the parameter is passed to the function. The
applying the suffix 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 a pointer to the nth element of the previously referenced array to refer to the n+1 element. The operands of the
Postfix increment operator and the suffix decrement operator must be a modifiable (not const) left value for arithmetic or pointer types. The type of the result is the same as the type of the postfix-expression, but is no longer the left value. The operands of the
Postfix increment operator can also be of type bool, in which case the operand is computed and then set to true. The operand of the postfix decrement operator cannot be a bool type. The
The following code demonstrates the suffix increment operator:

Expre_postfix_increment_and_decrement_operators.cpp
//compile with:/EHsc
#include <iostream>
using namespace std;

int main () {
  int i = ten;
  cout << i++ << Endl;
  cout << i << Endl;
}

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

Enum Compass {North, South, N, west);
Compass Mycompass;
for (Mycompass = North; Mycompass!= West; mycompass++)//Error

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.