Search code examples
cc89post-incrementansi-c

Is post-increment operator guaranteed to run instantly?


Let's say I have the following code:

int i = 0;
func(i++, i++);

The increment is happening right after returning the value? Is it guaranteed that the first argument will be 0, and the second argument will be 1?


Solution

  • This code is broken for two reasons:

    • Accessing a variable twice between sequence points, for other purposes than to determine which value to store, is undefined behavior. There are no sequence points between the evaluation of function parameters. Meaning anything could happen, your program might crash & burn (or more likely display incorrect or garbage values).
    • The order of evaluation of function parameters is unspecified behavior, meaning you can't know which one that will be evaluated first.

    Undefined behavior and sequence points

    Why are these constructs (using ++) undefined behavior?