home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!utcsri!utgpu!attcan!telly!druid!pseudo!mjn
- From: mjn@pseudo.uucp (Murray Nesbitt)
- Newsgroups: comp.lang.c
- Subject: Re: main()
- Message-ID: <MJN.92Sep4010149@pseudo.uucp>
- Date: 4 Sep 92 09:01:49 GMT
- References: <cee1.715416182@Isis.MsState.Edu> <1992Sep2.083929.7518@klaava.Helsinki.FI>
- Sender: mjn@pseudo.uucp (Murray Nesbitt)
- Organization: Private system in Toronto, ON
- Lines: 67
- In-Reply-To: wirzeniu@klaava.Helsinki.FI's message of 2 Sep 92 08:39:29 GMT
-
-
- wirzeniu@klaava.Helsinki.FI (Lars Wirzenius) writes:
-
- >cee1@ra.msstate.edu (Charles Evans) writes:
- >>BC++ books say int main(void) but what is the benefit of 'return 0' at the
- >>end.. seems like wasted space.
- >
- >It it NOT wasted space. Each and every function (that returns at all)
- >needs to explicitly return some value, otherwise it will return garbage
- >value and cause your hair turn into worms.
-
- Functions do not have to explicitly return values. What's the return
- value of ``free()''? Of ``srand()''? Of ``rewind()''? Are you
- telling me I must rewrite my ``void spinthewheel( wheel *w )''
- function?
-
- > And if you return random
- >values from main, the operating system (that called main to run your
- >program) is going to be very confused.
-
- No operating system I have ever encountered will get confused by a
- program that returns an arbitrary value. See if you can confuse your
- operating system with the following:
-
- #include <stdlib.h>
- #include <time.h>
-
- int main( void )
- {
- srand( (unsigned)time( (time_t *)NULL ) );
- return rand();
- }
-
- >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.
-
- > In fact, as a stylistic issue, I
- >prefer to use ``exit(0)'' instead of ``return 0'' in my programs. That
- >is purely personal opinion, of course.
-
- You should be using ``exit( EXIT_FAILURE )'' or ``exit( EXIT_SUCCESS )''.
- What does exit(0) mean to VMS?
-
- >C books that have ``void main'' are broken and are probably only useful
- >for heating purposes.
-
- I hope this doesn't include my copy of K&R, in which most example
- programs have no explicit return value from main(). This is
- functionally equivalent to ``void main()''.
-
- >>so what should i use
- >
- >Use: int main(void)
- >
- >I have ignored the possibility to use command line arguments to main.
- >If you want those, you use the declaration:
- >
- > int main(int argc, char **argv)
-
- This much I can agree with.
-
- --
- Murray
-