c - Is calling a function with local side-effects twice in the same expression undefined behavior? -
int f() { static int i=0; return ++i; } int g() { return f() + f(); }
does g()
return 3
or result undefined
?
6.5.2.2 function calls
...
10 there sequence point after evaluations of function designator , actual arguments before actual call. every evaluation in calling function (including other function calls) not otherwise sequenced before or after execution of body of called function indeterminately sequenced respect execution of called function.94)
94) in other words, function executions not ‘‘interleave’’ each other
upshot there sequence point between each ++i
virtue of being part of function call. thus, behavior well-defined.
whether intend matter. note @ point risk signed overflow, undefined. , others have pointed out, f() - f()
may not give result expect (left right evaluation not guaranteed in case).
Comments
Post a Comment