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!