Path: utzoo!attcan!utgpu!watmath!iuvax!purdue!mentor.cc.purdue.edu!l.cc.purdue.edu!cik From: cik@l.cc.purdue.edu (Herman Rubin) Newsgroups: comp.lang.c Subject: Re: want to know Summary: This is a property of the linker, not the language Message-ID: <1496@l.cc.purdue.edu> Date: 15 Aug 89 12:41:30 GMT References: <8487@bsu-cs.bsu.edu> <2980@solo9.cs.vu.nl> <182@sunquest.UUCP> <14269@haddock.ima.isc.com> Organization: Purdue University Statistics Department Lines: 45 In article <14269@haddock.ima.isc.com>, karl@haddock.ima.isc.com (Karl Heuer) writes: > 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'. No, the question is not a JOKE. It is a stupidity in UNIX which causes the problem. I have used other systems in which the main program could have any name whatever, and even in which the entry need not be to a main program, while a main program is present. I have even used it. Those who remember the old Fortran will know that one normally had a PROGRAM card, which was of the form PROGRAM NAME(.............) amd NAME was the name of the main entry point of the program. If you look at the documentation of ld, it states that one can specify the original entry point. This seems not to be implemented well in any version of UNIX with which I am familiar, and frankly, I miss it. It is true that one can get around this by rewriting crt0 and linking it, but THAT problem is the one which I believe the loader should handle, and does not. There is no even moderately fair reason why the user's program should start at main (or _main if from C, or _MAIN_ if from Fortran). -- Herman Rubin, Dept. of Statistics, Purdue Univ., West Lafayette IN47907 Phone: (317)494-6054 hrubin@l.cc.purdue.edu (Internet, bitnet, UUCP)