Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!ncar!ico!ism780c!haddock!karl
From: karl@haddock.ima.isc.com (Karl Heuer)
Newsgroups: comp.lang.c
Subject: Re: want to know
Message-ID: <14269@haddock.ima.isc.com>
Date: 11 Aug 89 18:27:46 GMT
References: <8487@bsu-cs.bsu.edu> <2980@solo9.cs.vu.nl> <182@sunquest.UUCP> <664@laic.UUCP>
Reply-To: karl@haddock.ima.isc.com (Karl Heuer)
Organization: Interactive Systems, Boston
Lines: 31

In article <664@laic.UUCP> darin@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@haddock.isc.com), The Walking Lint