News for hackers. Stuff that matters.
Re:Main is usually a function, so when is it not? (2015) by anyfoo on Monday June 14, 2021 @04:54PM (#27508173)
Not just in Visual Studio. main is usually (always?) not the entry point on unixoid systems either, that's much more likely to be _start, which calls main() down the line.

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().

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.