Path: utzoo!attcan!uunet!snorkelwacker!apple!agate!darkstar!terra!daniel From: daniel@terra.ucsc.edu (Daniel Edelson) Newsgroups: comp.lang.c++ Subject: Re: Can you prototype main() ????? Message-ID: <6932@darkstar.ucsc.edu> Date: 16 Sep 90 23:04:18 GMT References: <14227.26dc5b2f@max.u.washington.edu> <24447@uflorida.cis.ufl.EDU> <59517@bbn.BBN.COM> Sender: usenet@darkstar.ucsc.edu Reply-To: daniel@cis.ucsc.edu (Daniel Edelson) Organization: University of California, Santa Cruz Lines: 69 In article <59517@bbn.BBN.COM> saustin@bbn.com (Steve Austin) writes: >bp@swamp.cis.ufl.edu (Brian Pane) writes: > >>|> I tried to compile one of his example programs with Turbo C++ I got a >>|> "cannot overload 'main'" error message. It seems the compiler choked on >>|> the author prototyping main (with the line: int main(); >>Actually, main is "int main (int, char**)." .... From the 2.1 reference manual, section 3.4: ``The function main() may be defined as: int main() { /* ... */ } or int main(int argc, char * argv[]) { /* ... */ }'' From these two possible definitions it follows that main may have either or two prototypes: int main(); or int main(int argc, char * argv[]); Of course, if you prototyped main() to have no arguments and then defined it with two you must get an error. In C++ if your prototype and definition differ in a way that is significant to the overloading mechanism then you are declaring an overloaded function. (There may be exceptions to this rule, I'm probably just over-generalizing.) >> If you declare main without >>argc or argv, the compiler realizes that you must be talking about some >>*other* main. Perhaps the author was using a machine without an operating >>system? :-) You can certainly define main as ``int main() { /* ... */ }'' regardless of the operating system you're using. You simply can't access the invocation arguments. >I don't think it make much sense at all to prototype main. You prototype a >function in order to call it - I don't know what you would get if you tried to >call main, Again from section 3.4, ``The function main() may not be called from within a program.'' > but I bet it wouldn't be good. Also, if you do manage to prototye >main, you will be asking for its name to be mangled, which will not please >your linker. Not a problem. The compiler (to be useful) must be able to set the program's entry point. >Other things wrong with the above definition "int main(int, char **)" are that >main does not return a value (again, I'm not sure what would happen if you >tried to return someting from main, but I bet it wouldn't be very meaningful). Again, from section 3.4, ``A return statement in main() has the effect of calling exit() with the return value as the argument.'' >Also, main can have a third argument (at least on some systems) which is a >"char **" and conatins the environment. This is nonstandard, though prevalent. According to ANSI C freestanding systems need not support the environment. > Steve Austin Daniel Edelson | C++ virtual garbage recycler: daniel@cis.ucsc.edu, or | ``Recycle your garbage. Please ....!sun!practic!peren!daniel | don't make me clean up after you.''