[comp.std.c++] calling main in ANSI C

Leo Willems) (12/02/90)

The ARM (the annotated C++ reference manual), section 3.4 states:

	"The function main() may not be called from within a program."

I was looking for the same restriction in ANSI C, but could not find
any statement in the ANSI C standard (2.1.2.2.1).
Par. 2.1.2.2.3 (Program termination) states:

	"A return from the initial call to the main function...."

The word "initial" suggests (to me) that main() may be called from within the
program.

Is C++ different from ANSI C on this point?
If I didn't read the ANSI C standard careful enough, please mention
the section where I can find the information.

Thanks.

Leo
-- 
 Leo Willems			Internet: leo@atcmp.nl
 AT Computing			UUCP:     mcsun!hp4nl!kunivv1!atcmpe!leo
 P. O. Box 1428			6501 BK  Nijmegen, The Netherlands
 Phone:    +31-80-566880	Fax:	  +31-80-555887

steve@taumet.com (Stephen Clamage) (12/03/90)

leo@atcmp.nl (!Leo  Willems) writes:

|The ARM (the annotated C++ reference manual), section 3.4 states:
|	"The function main() may not be called from within a program."
|I was looking for the same restriction in ANSI C, but could not find
|any statement in the ANSI C standard (2.1.2.2.1).
|Is C++ different from ANSI C on this point?

Yes.  In ANSI C, main() may be called from anywhere in the program.
In C++, it may not be called, nor may its address be taken.  Some
reasons for this are given in the ARM.
-- 

Steve Clamage, TauMetric Corp, steve@taumet.com

gwyn@smoke.brl.mil (Doug Gwyn) (12/03/90)

In article <814@atcmpe.atcmp.nl> leo@atcmp.nl (!Leo  Willems) writes:
>The word "initial" suggests (to me) that main() may be called from within the
>program.

I asked X3J11 for an interpretation on this point, and they confirmed
that main() may be invoked "recursively" by a strictly conforming C
program.

jimp@cognos.UUCP (Jim Patterson) (12/03/90)

In article <814@atcmpe.atcmp.nl> leo@atcmp.nl (!Leo  Willems) writes:
>The ARM (the annotated C++ reference manual), section 3.4 states:
>
>	"The function main() may not be called from within a program."
>
>I was looking for the same restriction in ANSI C, but could not find
>any statement in the ANSI C standard (2.1.2.2.1).
>Par. 2.1.2.2.3 (Program termination) states:
>
>	"A return from the initial call to the main function...."
>
>The word "initial" suggests (to me) that main() may be called from within the
>program.

At least some implementations treat the function 'main' in a special
manner. Here's a snippet from a listing from VAX/VMS C V3.1-051 with
the "/machine_code" flag on:

  146           int main(void) {
                                          0000  main:
                                    0000  0000          .entry  main,^m<>
                              5E 08 C2    0002          subl2   #8,sp
                       00000000* EF 16    0005          jsb     C$MAIN_ARGS

The subroutine C$MAIN_ARGS is only called by main, and the call is
generated automatically by the compiler. I believe that it's purpose is
to arrange for the argc and argv parameters to be provided to the main
entry point. It's my guess that if you tried to call main() with different
arguments with this compiler it wouldn't work (main would get the command
line arguments no matter what you did, or something worse would happen).

I won't hazard a guess as to whether this is considered legitimate
behaviour according to the standard.
-- 
Jim Patterson                              Cognos Incorporated
UUCP:uunet!mitel!cunews!cognos!jimp        P.O. BOX 9707    
PHONE:(613)738-1440                        3755 Riverside Drive
NOT a Jays fan (not even a fan)            Ottawa, Ont  K1G 3Z4