Search code examples
cundefined-referencesequence-points

Is there a sequence point between these assignments?


Is there a sequence point between the two assignments in the following code:

f(f(x=1,1),x=2);

Solution

  • The relevant quote from the (draft) standard [6.5.2.2, 10] is:

    The order of evaluation of the function designator, the actual arguments, and subexpressions within the actual arguments is unspecified, but there is a sequence point before the actual call.

    So for your expression, the first argument (in particular the call to f) could be evaluated before the second argument; e.g.:

    (x = 1, 1), f <sp> call, (x = 2), f <sp> call
    

    Or, it could be evaluated after the second argument; e.g.:

    (x = 2), (x = 1, 1), f <sp> call, f <sp> call
    

    [The function call itself can (and most probably will) contain more sequence points (in particular if it contains a return statement).]

    Depending on that, there is a sequence point between the assignments or not. It is up to the platform ("unspecified").

    Since in the 2nd case, you are assigning to x twice between two sequence points, you have undefined behaviour on such a platform.