Have not done the compiler is to be deceived, from a sick i++ problem guessing compiler behavior (expression from left to right scan, the same variable affect each other)

Source: Internet
Author: User

First of all, do not be blindfolded, if so, basically compiled but:

int I=1;int b=i+++++i;printf ("%d%d\n", b, i);

MinGW Error: Error:lvalue required as increment operand (as if to say, + + missing left value, where the + + is the third fourth Plus on the title)

VC2010 Error: Error C2105: ' + + ' needs L-value

--------------------------------------------------------------------------

The correct face question should be this:

int I=1;int b=i++ + ++i;printf ("%d%d\n", b, i);

I did not do the compiler, can only learn from the previous knowledge to guess:

As a precondition, there are three expressions in this statement, namely i++ (EXP1), ++i (EXP3), Exp+exp3 = Exp2

1. Remember that before the IF statement has more than one condition, turn on the optimization switch, once the first condition is established, the following conditions may not be evaluated, which means that the compiler parses the expression from left to right.

2. EXP1 = i++ to execute after the current entire statement has been executed. If the sentence has more than one function or contains multiple expressions, you should wait for all of the expressions to be evaluated before executing the + + operation.

3. The expression i++ (EXP1) in this line, and ++i (EXP3), use the same I, so if one of the expressions evaluates to the value of another expression before the end of the entire statement.

Synthesis above analysis: first executes ++i (EXP3), executes after i=2; Then execute B = exp1 + Exp3 = i + i = 2 + 2 = 4; Last execution exp1 = i++ = 2+1 = 3

Results: The results of MinGW and VC2010 were 4, 3, and I was all right. Remember two: the expression is executed from left to right, and multiple expressions share a variable interaction ( rather than a closed calculation after each expression is split). There is always only one I in memory, and the compiler should not take the liberty of adding locks, or more copies of memory, if the intended effect, should be the programmer to do this work, the compiler should not someone else).

Have not done the compiler is to be deceived, from a sick i++ problem guessing compiler behavior (expression from left to right scan, the same variable affect each other)

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.