want to know

Badger BA 64810 bbadger at x102c.harris-atd.com
Sat Aug 19 06:51:17 AEST 1989


In article <10770 at smoke.BRL.MIL> gwyn at brl.arpa (Doug Gwyn) writes:
>In article <2549 at trantor.harris-atd.com> bbadger at x102c.harris-atd.com (Badger BA 64810) writes:
>>You use significant names for the files of your programs, why shouldn't you 
>>use significant names for the main routines?
>
>main() certainly is a descriptive name for the role of that pseudo-function
>within a C application.  The application within its invocation context has
>yet another, hopefully descriptive, name ("grep"?) or icon (smiling frog?)
>or whatever.
Descriptive of what?  I guess if you do something often enough it begins 
to seem natural.  Why should there even _be_ a distinguished main program?
Consider the compress/uncompress program, which changes its function based 
on the argv[0] string.  To me, this is a clever work-around to a problem 
which has no _need_ to exist -- just let the program have multiple entry
points.  I know, you have to get the shell to invoke the program at a
particular entry point, but this can be worked out.

>
>>There really wouldn't be any much change to C.  Even now, main() isn't
>>treated specially by the C compiler.
>
>Speak for yourself.  The C compiler I use at home processes the main()
>pseudo-function differently from other functions, and in general any
>Standard-conforming implementation is going to have to give some degree
>of special treatment to main(), since it doesn't follow quite the same
>rules as normal C functions.
OK, but the only thing special about main() that I found in the gcc 
compiler source code was to prevent main() from being inline-d.  
Exactly what does your C compiler _do_ differently for main?  I don't 
see why a compiler would care that crt0.o happens to call main() eventually.
What's so different about main()?  That is, what rules are you inferring?

>
>The question has come up, can the main() function be invoked as a normal
>C function (i.e. recursively by the application).  I think it is not
>guaranteed by the Standard; others think it is.  This would be a good
>thing for X3J11 to address in the "interpretations" phase.

On my system (HCX-9 running HCX/UX 3.0), you can call main() from main():
--- begin cut here ---
/* Can main () call main() ?
 * Print out arguments in reverse order.
 */
#include <stdio.h>
int main(argc, argv )
     int argc;
     char *argv[];
{
  if (argc > 0) {
    main(argc - 1, &argv[1]);
    if (argc > 1) putchar(' ');
    printf("%s",argv[0]);
  }
  return 0;
}
--- end cut here ---

But why not allow this as a main program?:
--- begin cut here ---
/* 
 * Print out arguments in reverse order.
 */
#include <stdio.h>
int reverse(argc, argv )
     int argc;
     char *argv[];
{
  if (argc > 0) {
    reverse(argc - 1, &argv[1]);
    if (argc > 1) putchar(' ');
    printf("%s",argv[0]);
  }
  return 0;
}
--- end cut here ---



Bernard A. Badger Jr.	407/984-6385          |``Use the Source, Luke!''
Secure Computer Products                      |``Get a LIFE!''  -- J.H. Conway
Harris GISD, Melbourne, FL  32902             |Buddy, can you paradigm?
Internet: bbadger%x102c at trantor.harris-atd.com|'s/./&&/g' Tom sed expansively.



More information about the Comp.lang.c mailing list