Summary of C-difficult and miscellaneous _ Sequence points (2)

Source: Internet
Author: User

Summary of C-difficult and miscellaneous _ Sequence points (2)

Question: int I = 3; int B = I ++ * I ++; why is the final result B = 9?

Answer: (1) the standard stipulates that the program execution sequence can be arbitrary between two sequence points. If an object is modified multiple times, the final result will not be determined.Different compilers may run different results.

(2) An object can be modified only once between two adjacent sequence points.

(3) The End mark of the C statement, semicolon (;), is a type of sequence point. That is to say, the side effect caused by the value assignment, auto-increment, or auto-subtraction in the C statement must end before the semicolon.

(4) There is no guarantee that auto-increment or auto-increment will be performed immediately after the original value of the output variable and before the calculation of other parts of the expression. It cannot be guaranteed that the update of the variable will be performed at a certain time before the expression is completed (according to ansi c, before the next "sequence point. In this example, the compiler chooses to multiply the old value of the variable before auto-incrementing the two.Only after reaching a sequence point can the auto-increment operationMake sure that the task is actually executed (that is, you only need to ensure that the task is executed at any time between the previous sequence point and the sequence point ).


Program Analysis:

# Include
Using namespace std;

Int main (int argc, char * argv []) {
Int I [12] = {3, 3, 3, 3, 3, 3, 3, 3, 3 };
Int B [12];
B [0] = I [0] ++ * I [0] ++;
B [1] = ++ I [1] * ++ I [1];
B [2] = ++ I [2] * I [2] ++;
B [3] = I [3] ++ * ++ I [3];
B [4] = I [4] ++ * I [4] ++ * I [4] ++;
B [5] = I [5] ++ * I [5] ++ * ++ I [5];
B [6] = ++ I [6] * ++ I [6] * ++ I [6];
B [7] = ++ I [7] * ++ I [7] * I [7] ++;
B [8] = ++ I [8] * I [8] ++ * I [8] ++;
B [9] = ++ I [9] * I [9] ++ * ++ I [9];
B [10] = I [10] ++ * ++ I [10] ++ I [10];
B [11] = I [11] ++ * ++ I [11] * I [11] ++;
For (int j = 0; j <12; j ++ ){
Cout < }
Return 0;
}

The left side is the Codeblocks running result, and the right side is the VS2010 running result. You can compare the above concepts with the following running results for better understanding.


Ps: due to the limited technical skills of the author, if there are errors and inconveniences, please kindly advise me!

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.