Path: utzoo!mnetor!uunet!husc6!cmcl2!brl-adm!umd5!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.unix.questions Subject: Re: exit(main(argc,argv,env)); Message-ID: <9875@mimsy.UUCP> Date: 18 Dec 87 21:59:09 GMT References: <10875@brl-adm.ARPA> <1451@houdi.UUCP> <2058@bloom-beacon.MIT.EDU> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 52 As various people have pointed out, crt0.c (the file in which the exit(main(argc, argv, env)) call was found) is a system dependent way of getting C code started. (When the operating system is asked to run a program, it jumps to some starting location with registers and/or stack set up in some particular way. The code at this start location has to turn this into a normal C stack frame and call main. In most Unixes, this is done by a routine in crt0.s or crt0.c.) Nonetheless, exit(main(argc, argv, env)) is a perfectly legal thing to write. Here is an example with the env parameter deleted: int main(argc, argv) int argc; char **argv; { int n; /* * Compute and print the factorial of all arguments. */ if (argc < 2) return (0); /* no arguments */ /* do the first argument */ n = atoi(argv[1]); printf("%d! = %d\n", n, fac(n)); /* do the rest of the arguments */ exit(main(argc - 1, argv + 1)); } int fac(n) int n; { if (n < 2) return (1); return (n * fac(n - 1)); } The *only* thing that is special about `main' is that the runtime system somehow calls it to start the program, and that when it returns to the runtime system (if ever), the `int' value it returns is given back to the system as a status value. -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690) Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris