Strange lint mumblings
Mark Brader
msb at sq.uucp
Thu Jan 5 10:52:16 AEST 1989
[If you see two versions of this, it's not my fault, I cancelled the first.]
To a comment about:
> > ... easy clean up facility
> > that exit() provides. That is, flushing all I/O buffers and closing
> > them. return(status) works cleanly if the programmer takes care of
> > this.
Doug Gwyn replies:
> Oh, come on. Return from main() is ALSO supposed to do this; it's
> practically like a coroutine jump to exit() with the return value
> being passed to exit as its argument.
Personally I prefer to think of it as "It's as if the system started
the program by calling exit(main(...));", but it amounts to the same thing.
However, in a later article Doug says:
> exit(), which on systems conforming to the C
> standard will flush STDIO buffers and invoke any functions registered
> via atexit() ...
And this tips us to his unstated assumption. Doug is talking about ANSI C,
which doesn't quite exist yet. (Nearly -- the draft has been finalized and
submitted to X3, I hear tell -- but not yet.) In the meantime, we can't
strictly call an implementation wrong if it complies with the original
C Reference Manual -- K&R appendix A.
And that Appendix doesn't mention main() at all, let alone the return
from main(). For that matter, it doesn't mention exit() either. Status
returns through the argument of exit() *are* mentioned in the (tutorial)
body of the book, but the return from main() is not mentioned there either.
So systems where the return from main() is not equivalent to exit() are
not exactly wrong -- only old-fashioned, and soon to be nonstandard.
In the meantime, the cautious programmer will call exit().
Actually, in all of the foregoing I should speak of the return from the
*initial* call of main(). [Reference in draft standard: section 2.1.2.2]
The function could also be called recursively, and a return from an inner
call is in no way special.
Mark Brader "... one of the main causes of the fall of
SoftQuad Inc., Toronto the Roman Empire was that, lacking zero, they
utzoo!sq!msb had no way to indicate successful termination
msb at sq.com of their C programs." -- Robert Firth
More information about the Comp.lang.c
mailing list