[A83] Re: C(++) programming
[Prev][Next][Index][Thread]
[A83] Re: C(++) programming
It is legal in ANSI C to call main. It is not legal in ANSI C++ to call
main. g++ allows it, however, and the same code demonstrating this will
work with either C or C++ using the GNU compilers:
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("%i\n", argc);
if (argc > 0)
main(argc - 1, argv);
return 0;
}
Yes, I know that I don't have to explicitly state "> 0", but it makes for a
cleaner example, and FreeBSD style(9) dictates writing tests that way for
types which are not boolean, which is what I generally adhere to when
writing C code. If you want to see another example of a program that calls
main, then I suggest checking out some entries for an obfuscated C contest.
Looking at those types of programs will show you some things about the C
language that you probably didn't know. For example, how many people know
about comma expressions, and could state the rules for them off the top of
their heads?
> you can never call main as far as i know! so a = main(); would be
impossible
> because main is not a standard function.
References: