[TUHS] where did "main" come from?

Michael Kjörling michael at kjorling.se
Sat May 23 06:01:49 AEST 2020


On 22 May 2020 14:43 -0400, from stewart at serissa.com (Lawrence Stewart):
> C main programs define “main”.

I don't have a ready answer to your question where that name came
from, but it's worth remembering (and easy to forget) that main()
isn't the actual starting point of execution of a C program. Rather,
the starting point is a function within the C library, which does some
early setup work and then ultimately calls main() and takes care of
passing the return value from main() back to the operating system (see
[1] for Linux, for example).

This is perhaps most obvious in C programs for Microsoft Windows,
which don't have the traditional main() but do have a WinMain() in its
place.

It looks like at least glibc uses _start as the actual entry point
[2]. In turn, on x86-64 (and very likely also on other architectures),
that calls __libc_start_main(), which in turn calls main() via a
function pointer passed to it.

 [1]: https://refspecs.linuxbase.org/LSB_3.1.0/LSB-generic/LSB-generic/baselib---libc-start-main-.html
 [2]: https://blogs.oracle.com/linux/hello-from-a-libc-free-world-part-1-v2

-- 
Michael Kjörling • https://michael.kjorling.semichael at kjorling.se
 “Remember when, on the Internet, nobody cared that you were a dog?”



More information about the TUHS mailing list