[comp.lang.c++] Can you prototype main

8324155@max.u.washington.edu (08/30/90)

 
 
       I'm hoping someone can help me with this (hopefully) easy question
 about C++, and specifically Turbo C++.  I have been learning C++ and using
 the book "C++ Techniques and Applications" by Scott Robert Ladd, and when
 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();
 Is this a problem with Turbo C++, or is it not allowed in C++ and the
 compiler that the author used just accepted it ?  It works fine in Turbo
 if I remove the prototype.
 
    Thanks ahead of time,    Mark Frey

bp@swamp.cis.ufl.edu (Brian Pane) (09/17/90)

In article <14227.26dc5b2f@max.u.washington.edu>,
8324155@max.u.washington.edu writes:
|>
|> 
|> 
|>       I'm hoping someone can help me with this (hopefully) easy question
|> about C++, and specifically Turbo C++.  I have been learning C++ and using
|> the book "C++ Techniques and Applications" by Scott Robert Ladd, and when
|> 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();
|> Is this a problem with Turbo C++, or is it not allowed in C++ and the
|> compiler that the author used just accepted it ?  It works fine in Turbo
|> if I remove the prototype.
|> 
|>    Thanks ahead of time,    Mark Frey
              
Actually, main is "int main (int, char**)."  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? :-)

-Brian Pane
-------------------------------------------------------------------------
Brian Pane	University of Florida Department of Computer Science
bp@swamp.cis.ufl.edu		Class of 1991

"If you can keep your expectations       |#ifdef OFFENDED_ANYONE
 tiny, you'll go through life            |#  include "disclaimer.h"
 without being so whiny" - Matt Groening |#endif
-------------------------------------------------------------------------

saustin@bbn.com (Steve Austin) (09/17/90)

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();
>|> Is this a problem with Turbo C++, or is it not allowed in C++ and the
>|> compiler that the author used just accepted it ?  It works fine in Turbo
>|> if I remove the prototype.
>|> 
>              
>Actually, main is "int main (int, char**)."  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? :-)

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, 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.

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).
Also, main can have a third argument (at least on some systems) which is a
"char **" and conatins the environment.

	Steve Austin

daniel@terra.ucsc.edu (Daniel Edelson) (09/17/90)

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.'' 

leo@atcmp.nl (Leo Willems) (09/17/90)

From article <14227.26dc5b2f@max.u.washington.edu>, by 8324155@max.u.washington.edu:
> 
>  
>  
>        I'm hoping someone can help me with this (hopefully) easy question
>  about C++, and specifically Turbo C++.  I have been learning C++ and using
>  the book "C++ Techniques and Applications" by Scott Robert Ladd, and when
>  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();
>  Is this a problem with Turbo C++, or is it not allowed in C++ and the
>  compiler that the author used just accepted it ?  It works fine in Turbo
>  if I remove the prototype.
>  
>     Thanks ahead of time,    Mark Frey

The annotated C++ ref man (E&S) is very explicit about main, amongst
others it states: main() can not be overloaded and its calling conventions
are implementation dependent, but two forms are recommended:
	main()   and  main(int, *char[])

So, however not stated explicitlly, as I see it you are not allowed to
give a prototype for main.

Hope this helps.

Leo

enag@ifi.uio.no (Erik Naggum) (09/18/90)

Sorry for barging in -- I just started reading this newsgroup.

In article <659@atcmpe.atcmp.nl> leo@atcmp.nl (Leo  Willems) writes:

   The annotated C++ ref man (E&S) is very explicit about main, amongst
   others it states: main() can not be overloaded and its calling conventions
   are implementation dependent, but two forms are recommended:

	   main()   and  main(int, *char[])

Isn't also a third parameter, char *envp[], supported, so that the
list should read

	main()
	main(int, *char[])
	main(int, *char[], *char[])
?

--
[Erik Naggum]		Naggum Software; Gaustadalleen 21; 0371 OSLO; NORWAY
	I disclaim,	<erik@naggum.uu.no>, <enag@ifi.uio.no>
  therefore I post.	+47-295-8622, +47-256-7822, (fax) +47-260-4427

william@syacus.acus.oz (William Mason) (09/19/90)

saustin@bbn.com (Steve Austin) writes:

>bp@swamp.cis.ufl.edu (Brian Pane) writes:

>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, 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.

While agreeing with the practical sentiment of this statement, I would like
to suggest that there is a place for "prototyped main()-s" out there some
where.

Visualise an object oriented operating system ... (pretend it looks like a
Mac or OS/2-PM, i.e. WIMPS-ish).  Now in this environment I have a program
"display.c"  with overloaded and proto-typed main()-s.  Each main() represents
a different input for the display tool.

For example:
	int main( postscript ){...}
	int main( troff ) {... }
	int main( GIFF ){...}

Now, the actual implementation need not be one executable with multiple entry
points (shades of FORTRAN).  I would expect the compiler or pre-processor to
generate a normal argc and argv style for the real main() and inside this the 
appropriate overloaded main() could be called.

What is happening is that the (possibly GUI) shell is co-operating with the
compiler to supply the correct switches so the real main() gets the correct 
information.  The supposed shell could be primitive and only look at file type
or it could use some resource file, etc. to effect the desired behaviour
from "display".

The important thing is that a lot of operating system interface messyness
is done away with.  Extensions to the model would allow environmental 
variables as main() parameters or defaults.  E.g.:

	int main( Postscript_File pf, Device_type dt = $(TERM), ){ ... }

Some of this functionality was part of the Pascal language, but only file
parameters were allowed.  The Microsoft Pascal extended this other types
(it does prompt but I can't remember if it takes 'things' from an MS-DOS
command line).

Thanks for your time,

William Mason
ACUS Sydney.

rfg@NCD.COM (Ron Guilmette) (09/21/90)

In article <6932@darkstar.ucsc.edu> daniel@cis.ucsc.edu (Daniel Edelson) writes:
>In article <59517@bbn.BBN.COM> saustin@bbn.com (Steve Austin) writes:
>>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.''

Hummm... This looks like yet another one of these little C incompatabilities
which didn't quite make it into the list of the incompatabilities in
chapter 18.

My favorite one is illustrated by the following example:

	void foobar (int irritating)
	{
		int irritating;
	}

Put that in your C++ compiler!  Then look for this in chapter 18.  It ain't
there.
-- 

// Ron Guilmette  -  C++ Entomologist
// Internet: rfg@ncd.com      uucp: ...uunet!lupine!rfg
// Motto:  If it sticks, force it.  If it breaks, it needed replacing anyway.

steve@taumet.com (Stephen Clamage) (09/21/90)

rfg@NCD.COM (Ron Guilmette) writes:

>	void foobar (int irritating)
>	{
>		int irritating;
>	}

>Put that in your C++ compiler!  Then look for this in chapter 18.  It ain't
>there.

Try putting it in your ANSI C compiler.  It won't work there either.
"irritating" is defined twice in the same scope, which is illegal.
If your C compiler allows this, report it to the vendor as a bug!
-- 

Steve Clamage, TauMetric Corp, steve@taumet.com

hardin@hpindda.cup.hp.com (John Hardin) (09/22/90)

> Hummm... This looks like yet another one of these little C incompatabilities
> which didn't quite make it into the list of the incompatabilities in
> chapter 18.
> 
> My favorite one is illustrated by the following example:
> 
> 	void foobar (int irritating)
> 	{
> 		int irritating;
> 	}
> 
> Put that in your C++ compiler!  Then look for this in chapter 18.  It ain't
> there.
> 
> // Ron Guilmette  -  C++ Entomologist
----------

Sorry, perhaps I'm just being dense today, but I don't get your point.
When I put the above code through an ANSI C compiler I get

   "x.c", line 3: warning: redeclaration of 'irritating' hides formal parameter

while I get the following when I put it through my C++ compiler:

   CC: "x.C", line 3: error 1031: two declarations of irritating

Could you please clarify?

Thanks.

John Hardin
hardin@hpindgh.hp.com
-----------

rfg@NCD.COM (Ron Guilmette) (09/22/90)

In article <461@taumet.com> steve@taumet.com (Stephen Clamage) writes:
<rfg@NCD.COM (Ron Guilmette) writes:
<
<>	void foobar (int irritating)
<>	{
<>		int irritating;
<>	}
<
<>Put that in your C++ compiler!  Then look for this in chapter 18.  It ain't
<>there.
<
<Try putting it in your ANSI C compiler.  It won't work there either.
<"irritating" is defined twice in the same scope, which is illegal.

ACK!  I stand corrected.  I though that was legal ANSI C.  Apparently I
was wrong.  Sorry.

<If your C compiler allows this, report it to the vendor as a bug!

I just did.  Thanks Steve.

-- 

// Ron Guilmette  -  C++ Entomologist
// Internet: rfg@ncd.com      uucp: ...uunet!lupine!rfg
// Motto:  If it sticks, force it.  If it breaks, it needed replacing anyway.