C language side effects (side effect): Refers to the modification of data objects or files. For example, the statement var = 99; The side effect is to change the value of Var to 99. Evaluating an expression can also have side effects, such as seeking SE = 100 for an expression;
The side effect of the evaluation is that the value of SE is modified to 100.
Sequence points (sequence point): Refers to a special point in time at which the program runs, all side effects before that point have ended, and the subsequent side effects have not yet occurred. The C statement end flag-a semicolon (;) is a sequence point. The standard specifies that, between two sequence points, the value saved by an object can be modified at most.
Side effects caused by assignment, self-increment, or self-subtraction in a C statement must end before the semicolon. We'll talk about some operators that contain sequence points later. The point at which the end of any full expression operation ends is also the sequence point. The so-called complete expression, that is, the expression is not a sub-expression. The so-called sub-expression refers to an expression in an expression. For example: F = ++e% 3 This entire expression is a complete expression. ++e, 3, and ++e% 3 in this expression are all its sub-expressions.
With the concept of sequence points, let's analyze a very common error below:
int x = 1, y;y = x + + x + +;
Here y = x + + x + + is the complete expression, and X + + is its subexpression. The point at which the complete expression operation ends is a sequence dot, int x = 1, y; In the; is also a sequence point. In other words, X + + x + + is located between two sequence points. The standard specifies that, between two sequence points, the value saved by an object can be modified at most. But we can clearly see that in this example, the value of x has been modified two times between two sequence points. This is obviously wrong! Compiling this code on a different compiler may cause the value of Y to be different. The more common result is that the value of Y is finally modified to 2 or 3. Here, I do not intend to do a more in-depth analysis of this problem, you just have to remember that this is wrong, do not use it. If you are interested, take a look at the relevant information listed below.
c language standard defines side effects and sequence points as follows:
accessing a volatile object, modifying an object, modifying a file, or calling a function this does any of those operatio NS is all side effects, which is changes in the state of the execution environment. Evaluation of an expression may produce side effects. At certain specified points in the execution sequence called sequence points, all side effects of previous evaluations Sha ll be complete and no side effects of subsequent evaluations shall has taken place.
translate as follows:
Accessing a volatile object, modifying an object or file, or invoking a function that contains these operations are all side effects, and they all change the state of the execution environment. Evaluating an expression can also cause side effects. Some specific points in the execution sequence are called sequence points. At the sequence point, the side effects of all operations before that point should end, and the side effects of subsequent operations do not occur.
The Order points are:
1. The point of calling a function, after evaluating its arguments.
2. The end of the first operand of the && operator.
3. The end of the first operand of the | | operator.
4. The end of the first operand of the?: Conditional operator.
5. The end of the each operand of the comma operator.
6. Completing the evaluation of a full expression. They is the following:
7. Evaluating the initializer of an auto object.
8. The expression in a ' ordinary ' statement-an expression followed by semicolon.
9. The controlling expressions in does, while, if, switch or for statements.
Ten. The other and the expressions in a for statement.
One. The expression in a return statement.
The compiler can add "-wsequence-point" to the compiler to help us check possible errors on the checkpoint.
* TEST_SEQUENCE_POINT.C * gcc-wsequence-point test_sequence_point.c */#i nclude int main () {int i = 12 ; i = i--; printf ("The I is%d/n", i); return 0; }
Gcc-wsequence-point test_sequence_point.c test_sequence_point.c:in function ' main ': test_sequence_point.c:10: Warning:operation on ' I ' could be undefined
Reproduced in: http://blog.csdn.net/bnufq/article/details/8953465
C language side effect and sequence point