In article <ct7d2e$l1t$>
Seong-Kook Shin <> wrote:
>1. Does ANSI standard say that these two forms of
> termination of program might be different? I have C99 copy, and
> it says..
>
> 5.1.2.2.3 If the return type of the `main' function is a type compatible
> with int, a return from the initial call to the `main' function is
> equivalent to calling the `exit' function with the value returned by
> `main' function returns a value of 0.
>
> If these two forms are equivalent, from when are they equivalent?
> ANSI? or C98? or C99?
There is something garbled in your quote here.
C89 and C99 differ a bit. If you are an implementor (the guy
writing the C compiler) and are implementing C89, you can implement
the initial call to main as, e.g.:
.global __libc_start
.entry __libc_start
__libc_start:
[ whatever special tricks are required to find argc and argv ]
[ here I assume they wind up in registers v0 and v1, which
are also the return-value registers for integer-valued
functions ]
/* now we just do: exit(main(argc, argv)) */
push v1 /* argv */
push v0 /* argc */
call main /* v0 = main(v0, v1) */
add #8,sp /* undo arguments */
push v0 /* main's return value is the desired exit status */
call exit /* exit(status) */
/* NOTREACHED */
halt /* "just in case" */
.end __libc_start
(you would write this in some kind of assembler language, typically).
Note that this passes main() exactly two arguments, and assumes
that passing two arguments to "int main(void)" is harmless. The
latter is usually true on most machines -- if not, you will need
to have your compiler tell you whether to call the two-argument
main() or the zero-argument main().
In C99, your job as compiler-and-library-writer is harder, because
C99 allows programs to "fall off the end" of main without returning
a value, and have main() act as if it ended with "return 0;" in
that case. That is:
int main(void)
{
}
and:
int main(void)
{
return 0;
}
are 100% identical programs in C99, but very different programs
in C89. You have to invent some mechanism to make this work in
C99.
>2. Steve's mention to "recursive call" and C99's "initial call to the main".
> I think that it is possible to call main() recursively. But I want
>to make it
> sure. Is it legal to call main() recursively? And if yes, can
>anyone show me
> practical (or concise at least) example of it?
Yes, and maybe:
#include <stdio.h>
int main(int argc, char **argv) {
if (argc > 1) {
main(argc - 1, argv + 1);
puts(argv[1]);
}
return 0;
}
>3. Above C99 5.1.2.2.3, In "If the return type of the main is a type
>compatible
> ..." Euh? Is it possible that main()'s return type is not compatible
>with int?
>
> I thought main should be one of these two forms:
> int main(void) { ... }
> int main(int argc, char *argv[]) { ... }
>
>I'll thank you for your answers in advance.
It should indeed be, if you are writing a C program for any hosted
implementation.
Any implementation can, on the other hand, *allow* (but not require)
you to do something else to get some interesting and wanted (or
pointless, or unwanted, or even harmful) non-Standard behavior.
For instance, an implementor could decide that if you write:
double main(void) { }
and compile it, the resulting program will play Chess at grandmaster
level, but if the computer loses, make the computer burst into
flames.
Since Standard C cannot even draw graphics, much less let you play
chess with a display and mouse, this might be a big selling point.
Of course, this is a really bizarre way to do it; it would probably
be wiser, if you are the implementor, to provide non-Standard
<graphics.h>, <mouse.h>, and <chess.h> headers or some such. (The
auxiliary "sore loser" support might perhaps be in <badtemper.h>.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it
http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.