Auto-increment operator-add

Source: Internet
Author: User

1. What is the output of the following code?

Int main ()
{
Int A [2] = {0, 0 };
Int * P =;
* (P ++) = 1;
Cout <A [0] <A [1] <Endl;
System ("pause ");
}

The result is 10, that is, a [0] = 1, a [1] = 0. Although there are Parentheses, p is not first Auto-incrementing. In fact, the auto-incrementing step occurs after the expression where the symbol is located ends. That is, * (p ++) = 1; equivalent to * p = 1; p ++;

2. What is the output of the following code?

Int main ()
{
Int A = 1;
Cout <(A ++) * (a ++) <Endl;
A = 1;
Cout <(++ A) * (++ A) <Endl;

System ("pause ");
}

The former outputs 1, and the latter outputs 180.
For post-addition, (A ++) * (A ++) is equivalent to A * A; A ++; A ++; that is, 1*1*1*1 = 1.
For the prefix addition, if you take it for granted, add the prefix four times before multiplication. The result is 5*5*5*5 = 625, and the result is 180. In fact, the add-on is more complex:
· First, computing (++ A) * (++ A) is equivalent to ++ A; A *;
· Then, multiply by the third factor, which is equivalent to ++ A; the existing result is *;
· Finally, multiply by the fourth factor, which is equivalent to ++ A; the existing result is *.
· 3*3*4*5 = 180.
3. Summary

For the add-on operation, the add-on operation is performed after the expression.
For the add-on operation, the add-on operation is performed before the expression, but the combination sequence inside the expression is considered, that is, the subexpression problem is considered for gradual addition.

4. Questions

Although it has been verified by the program, some people on the Internet say this, but the combination of "Add before" and "Add after" inside the expression is different, it is not clear why subexpressions are considered for "Add after" and not for "Add before.
The compilation tool dev c ++ has also been verified in the reference articles for VC6.0. It cannot be guaranteed for other compilers.

5. Reference

C language auto-incrementing operator in-depth analysis http://www.bhcode.net/article/20110226/15159.html

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.