Also talk about (++ I) + (++ I)

Source: Internet
Author: User

Today, someone posted a problem in the Forum:

Int I = 1, d; d = (++ I) + (++ I); printf ("% d \ n", d );


What is the output result above?



If not. You can first think about the result? Then perform programming verification again. If you cannot figure it out, check the cause again.


















In fact, this is related to the processing of the compiler. The output result here is actually 10. What I originally thought was 9.

Cause analysis:

+ Operators are combined from left to right. Therefore, (++ I) + (++ I) calculates the first two ++ I (the size of I has changed to 3) then calculate the first + operation, so the result of the preceding (++ I) + (++ I) is 6 (do not forget that the result of ++ I is the left value I, instead of the right value 3 ). Then, calculate the last ++ I. Since I is 3 before it, the value of I after this operation is 4, so the final result is 6 + 4 = 10.

This question is an understanding of the operator's priority order and return value.

However, if such code is written in actual programming, it will be scolded by others.


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.