calling main in ANSI C

Jim Patterson jimp at cognos.UUCP
Tue Dec 4 01:59:43 AEST 1990


In article <814 at atcmpe.atcmp.nl> leo at 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



More information about the Comp.std.c mailing list