recursive main() implicit return
Tags: #quiz(3) #is it ub(1) #C(1)#include <stdio.h>
static int first_call = 1;
int main() {
if (first_call) {
first_call = 0;
int x = main();
if (x == 0) {
printf("hi!\n");
}
}
// implicitly return
}
I’m working off N3096, the latest available C2X draft here. Below is the relevant section, with not relevant parts trimmed.
5.1.2.2.3 Program termination
If the return type of the
mainfunction is a type compatible withint, a return from the initial call to themainfunction is equivalent to calling theexitfunction with the value returned by themainfunction as its argument; reaching the } that terminates themainfunction returns a value of 0.
The key point is if “reaching the } that terminates the main function returns a value of 0” only
applies to “the initial call to the main function”.
The answer is probably not, so the answer is “no, this is not UB”. But also the spec is hard to read here.