#include <stdio.h>
static int foo(){}
int main(){
if (foo()) {
printf("OK\n");
}
return 0;
}
I am not very sure abort that, and I didn't find any explain in ISO/IEC 9899:1999 (E) yet. Maybe I missed it.
So, if(foo()) is UB in this case?
I test with gcc7.3 aarch64-linux.
-O0: prints OK
-O1 to -O3: prints nothing
C17 6.9.1 §12
Unless otherwise specified, if the
}that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.
The caller is using the function's value as part of the if evaluation, so your code invokes undefined behavior. This was always the case since the very first C standard.
(The text "Unless otherwise specified" likely refers to main() which has a special exception, allowing one to omit the return statement, which is then equivalent to return 0, but only in main()!)
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