Related question: Any good reason why assignment operator isn't a sequence point?
From the comp.lang.c FAQ I would infer that the program below is undefined. Strangely, it only mentions the call to f
as a sequence point, between the computation of the arguments and the transfer of control to f
. The transfer of control from f
back to the calling expression is not listed as a sequence point.
int f(void) { i++; return 42; }
i = f();
Is it really undefined?
As an end-note that I add to many of my questions, I am interested in this in the context of static analysis. I am not writing this myself, I just want to know if I should warn about it in programs written by others.
A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function. The C standard library provides numerous built-in functions that your program can call.
In C++, a function contained within a class is called a Member function. Member functions are operators and functions that are declared as members of a class. Member functions do not include operators and functions declared with the friend specifier.
A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses (). Function names can contain letters, digits, underscores, and dollar signs (same rules as variables). The parentheses may include parameter names separated by commas: (parameter1, parameter2, ...)
The transfer of control from f back to the calling expression is not listed as a sequence point.
Yes it is.
at the end of the evaluation of a full expression
The complete expression that forms an expression statement, or one of the controlling expressions of an if, switch, while, for, or do/while statement, or the expression in an initializer or a return statement.
You have a return statement, therefore, you have a sequence point.
It doesn't even appear that
int f(void) { return i++; } // sequence point here, so I guess we're good
i = f();
is undefined. (Which to me is kind of weird.)
That's not undefined at all. One of the sequence points listed in Appendix C of C99 is the end of a full expression, of which one is the expression in a return statement.
Since you're returning 42, there's a sequence point immediately following that return
statement.
For completeness, the C99 sequence points are listed here, with the relevant one bolded:
The following are the sequence points described in 5.1.2.3:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With