Subject:

recursive main() implicit return


Message-Id: https://www.5snb.club/w/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 main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument; reaching the } that terminates the main function 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.