Re:Main is usually a function, so when is it not? (2015)
by hermitdev on Monday June 14, 2021 @08:06PM
(#27509918)
Also, just as execution doesn't start in main, it also doesn't end with main, either. In C, you can register `atexit` handlers. You can do that in C++, too. In C++, you can also have "user" code executed before & after main by virtue of static initialization & destruction.
Nevertheless, main is treated specially by the compiler for the aforementioned historical reasons, for example to not warn/error out if it does not return a value despite the type clearly telling so.
Observe:
% echo 'int main(void) { }' > foo.c; clang -c foo.c
<no output>
% echo 'int foo(void) { }' > foo.c; clang -c foo.c
foo.c:1:17: warning: non-void function does not return a value [-Wreturn-type]
int foo(void) { } ^ 1 warning generated.
As you can see, clang is happy to ignore the missing return value for main(), but not for foo().