A deep understanding of binary operators and auto-incrementing characters. Be careful with multiple ++ traps.

Source: Internet
Author: User

Recently, I saw a C language question:

Int x, y, I = 3;
X = (I ++) + (I ++ );
I = 3;
Y = (++ I) + (++ I); What are the values of x and y? Answer: It is easy to see the value of x, which is x = 3 + 3 = 6 and I = 5 at the same time.

What about y? After two ++ I operations in the same expression, is it 4 + 5 or 5 + 5? The correct answer is y = 5 + 5 = 10, I = 5. Here, both sides of the addition operator are variable x, so 5 + 5.

 

This question seems very simple. In fact, it can be associated. If the expression of y is:

I = 3;
Y = (++ I) + (++ I); what is the value of y? Y = 7 + 7 + 7 + 7 = 28?

The correct answer is y = 23, I = 7. Why is this problem?

If you do not understand this problem, you have not fully understood the operating rules of the C language operators.

The actual calculation rules are as follows:

Because the addition operator "+" is a binary operator, each operation requires only two operands, and the results are saved in a cache variable ans.

Therefore, the first operation (++ I) + (++ I) will result in ans = 5 + 5 = 10, I = 5;

Then, calculate ans + (++ I), and the result is ans = 10 + 6 = 16, I = 6;

Then, calculate ans + (++ I), and the result is ans = 16 + 7 = 23, I = 7;

Then assign the value y = ans = 23;

I can see it here. In fact, it is easy to understand this problem by combining the stack solution of the inverse polish expression.

Do not mistakenly think that y = (++ I) + (++ I) is simply after 4 Self-addition operations, the addition between I and I.

 

Think the same way:

I = 3;
Y = (I ++) + (I ++)

Is it ans = 3 + 3 = 6; ans = 6 + 5 = 11; ans = 11 + 6 = 17; y = 17 ;?

You can verify that y = 3 + 3 + 3 + 3 = 12;

This indicates that the four I ++ mentioned here are self-added after the end of y's job search.

How can this problem be solved? Is there a conflict with our previous conclusions?

In fact, there is no conflict. The real reason is that you need to understand the accurate conditions for I ++ and I:

For ++ I, you can replace it with "(I = + 1)", that is, in the case of ++ I, you must first perform self-Addition before completing other operations.

For I ++, the auto-increment here, to be precise, is the operation that needs to be automatically added only after all the operands in the stack of the reverse polish expression are cleared. To put it simply, ignore all I ++ operators, perform operations first, and perform auto-Addition on the corresponding variables only after the value of the expression is solved.

 

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.