An interesting C language question: s = (++i) + (++i) + (++i)

Source: Internet
Author: User

Today, I saw someone ask such a question;

I=3,s=0
S= (++i) + (++i) + (++i)
Why is the result s=16,i=6?

First look, I think the result should not be 15 (4+5+6), is he mistaken it, insurance, I decided to run with the compiler, look at the results, firstly with VC is a bit, the result is true,

Will not be a compiler problem, no, I try another compiler, changed Codeblocks tried:

So devc++, I'll try it again:

The result is still so, OK, I give up, the result should be 16, then this is why?

I settled down, thought it over, thought for a moment, and thought it should be like this:

i = 3, after running ++i i = 4; After running ++i i = 5; after finding out the + required two operands, the calculation of the number of two operands and put into the register, the two operands are located in the position of the current I, so now and for 5+5=10, put into the register; now the last +, with the left operand, the right operand is not sure, First, the right operand ++i, after the operation i = 6; So the end result is 10+6=16.
In order to verify my own conjecture, I wrote a small program, asked for the value of S = (++i) + (++i), according to conjecture, this result should be 10, tested, is indeed

Just ready to put their ideas to others to listen to the time, have encountered a new problem, the students raised the question said, with VS2013 run a bit, the result is 18!!! At that time I was shocked, is there a problem with just understanding??? With a skeptical attitude, I ran with VS2013 and the results were as follows:


It is true that, seeing this result, I confused again ...

Just as I was helpless, it occurred to me that we were not learning computer principles and compilations (though we don't have much to say now), and a thought came to my mind, or look at the assembly code they generated after compiling, to see if they could see something (the assembly only touched a little). With a try mentality, I looked at the assembly code:

In this view, it seems that there is a point, through the assembly code, we can find that VC and VS are compiled in a different way:

VC is so processed, in the addition operation, the left and right values are obtained, then their sum, and then the next operation;
VS is handled in this way, when more than one formula is added, the values of each of the expressions are calculated, and finally the addition is performed (i.e., after executing the ++i,++i,++i, the sum of them is 18);
Here we should also pay attention to the point: when the operation of (++i) (assuming that the address p is stored in the value of i), we use the value of the address p, that is, I-related operation after the execution (to consider the priority) I value.
Told my classmates about my thoughts, which finally explained the problem, finally can sigh. That's an interesting question.

An interesting C language question: s = (++i) + (++i) + (++i)

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.