home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!uunet.ca!wildcan!sq!msb
- From: msb@sq.sq.com (Mark Brader)
- Subject: Re: main()
- Message-ID: <1992Sep10.003907.15391@sq.sq.com>
- Organization: SoftQuad Inc., Toronto, Canada
- References: <cee1.715416182@Isis.MsState.Edu> <1992Sep4.012725.24310@wyvern.twuug.com> <1992Sep4.191347.27577@klaava.Helsinki.FI>
- Date: Thu, 10 Sep 92 00:39:07 GMT
- Lines: 106
-
- > main() is a function. It returns an int to its caller. You may
- > write it "main() {}" or "int main() {}" as you like.
-
- Right.
-
- > I suppose that "void foo() {}" does not affect the compiler at all.
-
- You may suppose it, but that does not make it right. First, it is wrong
- according to the standard. Second, the compiler is at liberty to use a
- different calling sequence for a function according to the type that
- it returns.
-
- It does seem to be true that there are no common implementations which
- take advantage of this liberty, and that misdeclaring main() as returning
- void will work on all common implementations, but it's still wrong and
- should be avoided.
-
- > The void type is simply a clue to 'lint' or whatever that you will
- > not use the return value and not to bug you about its non-use.
-
- Wrong. See above. And in addition to those points, it's a request to
- the compiler to reject a return statement with a value. (Again, actual
- compilers may not actually do so.)
-
- > "void main()" would declare that /bin/sh will not use the return value
- > from main. A very bad assumption.
-
- The notion that something called /bin/sh is involved is a bad assumption
- in the first place.
-
- | > No operating system I have ever encountered will get confused by a
- | > program that returns an arbitrary value.
- |
- | VAX/VMS comes to mind: for some exit codes it prints (confusing,
- | erroneous, and misleading) error messages. For UNIX and MS-DOS, the OS
- | itself doesn't get particularly confused, but the user of the program
- | often does.
-
- Or the calling program; for example, on UNIX make(1) will abort if it
- invokes a program that returns nonzero, unless it's explicitly told
- to ignore the returned status.
-
- | > > Of course, if you terminate your program with exit or abort, it will
- | > > never get to the end of main, so in that case there is no need to
- | > > include a return statement there.
- | >
- | > Personally, I put a ``return'' statement in regardless, with a
- | > ``/* NOTREACHED */'' if appropriate.
- |
- | A stylistic issue, of course, but I use simply:
- |
- | int main(void) {
- | ...
- | exit(0);
- | }
- |
- | I do not think that there is anything to be won by putting extra stuff
- | after the exit. (I know it may silence lint or the compiler, but I
- | prefer to fix them instead. ...)
-
- And I prefer to use return at the end of main(), reserving exit() for
- direct exits from called functions. My reason is that it makes it simple
- to convert main() to a called function itself, in case I ever want to do
- something like this:
-
- main (int argc, char **argv)
- {
- int new_argc;
- char **new_argv;
-
- massage_args (argc, argv, &new_argc, &new_argv);
-
- return original_main (new_argc, new_argv);
- }
-
- | Style wars are not usually fruitful in newsgroups.
-
- Right. I just put this forth by way of balance; I have no *complaint*
- against the use of exit(). In fact, on the machine where I'm typing this,
- I *can't* use my preferred style, because the compiler treats *any* return
- from the initial call of main() as an exit(0) call! (This is a Sun-3 running
- SunOS 3.5, pre-ANSI by some years.)
-
- | > You should be using ``exit( EXIT_FAILURE )'' or ``exit( EXIT_SUCCESS )''.
- |
- | exit(0) and exit(EXIT_SUCCESS) are defined to be exactly equivalent, at
- | least according to Plauger & Brodie's "Standard C" (I do not, alas, have
- | access to a copy of the standard).
-
- Correct according to the standard (section 4.10.4.3 in ANSI numbering,
- 7.10.4.3 in ISO). The choice of 0 or EXIT_SUCCESS is entirely stylistic.
- Even on VMS, if they now have an ANSI C compiler there. (It's been years
- since I used VMS.)
-
- | [I thought] exit(EXIT_SUCCESS) [sic] and exit(1) were similar ...
-
- No. exit(1) returns an implementation-defined status. The only way to
- portably return a failure status in ANSI C is exit(EXIT_FAILURE), or
- of course the analogous return from main().
-
- --
- Mark Brader "It's simply a matter of style, and while there
- SoftQuad Inc., Toronto are many wrong styles, there really isn't any
- utzoo!sq!msb, msb@sq.com one right style." -- Ray Butterworth
-
- This article is in the public domain.
-