want to know

Karl Heuer karl at haddock.ima.isc.com
Sat Aug 12 04:27:46 AEST 1989


In article <664 at laic.UUCP> darin at nova.UUCP (Darin Johnson) writes:
>[Name deleted on purpose] writes:
>> I'm also a University's student in need of help. I always see that people
>> define the function 'main' in C-programs. Why is that? What should it
>> return?  Can't I use another name?

Come on, folks, this was almost certainly a JOKE.  It was posted in response
to another "dumb question".

>It is a UNIX-ism.  The UNIX linker uses that function name as the name of
>the first routine to jump to.

And the answer is somewhere between misleading and wrong.  First of all,
main() is special according to everything that resembles a standard: (K&R,
H&S, D&M, pANS); this is a property of C, not of UNIX.  Second, it is not
special-cased by the linker in any UNIX implementation I know of.  Rather,
there is a pre-main routine that calls main() just like a normal function.
Execution begins at this pre-main routine, not because of a linker hack, but
because when the `cc' command invokes `ld', it tells it the name of the
entry-point label.  The magic is in the driver program `cc'.

>Also, for you confusion, most UNIX kernels do not start at main() or even
>have a main()...

Which is, btw, also consistent with the pANS, which states: "In a freestanding
environment ... the name and type of the function called at program startup
are implementation-defined." [2.1.2.1]  For all practical purposes (unless
you're compiling C code to run on microwave ovens), "freestanding" means
you're compiling the UNIX kernel.

Karl W. Z. Heuer (ima!haddock!karl or karl at haddock.isc.com), The Walking Lint



More information about the Comp.lang.c mailing list