Beni said:
I have been programming in C for about a year now. It sounds silly,
but I never took the time to question why a C(or C++ or Java) program
execution begins only at the main(). Is it a convention or is there
some deeper underlying reason?
I love reading all of the wanking on this thread regarding "hosted
implementations" and the CRT.
An executable image gets loaded by the OS and somewhere in the image, there
has to be a starting point, usually denoted in the header. The OS jumps to
the starting points address after the image is loaded into memory (today,
this also means dynamically linking libraries too and a lot of other
garbage.) But in C terms, the starting point is an address (pointer to fcn
returning int). return (*entry_point)(). BFD.
This starting point gets set by the linker (loader or linking loader in
years gone by). The C compiler frontend adds the symbol name of that
starting point to the linker command line when the objects are linked into
an executable. Now, pick a name for that starting point -- "main" sounded
like a good one and it stuck. Worse yet, the CRT is generally now the entry
point and the CRT expects to call main as a function.
Why is the user's entry point called "main"? It could have been something
else entirely, but at the time of PDP-11s and early Unix/C, the "main entry
point of the program" was a phrase in common usage.