home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / c / 13418 < prev    next >
Encoding:
Text File  |  1992-09-09  |  4.4 KB  |  117 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!uunet.ca!wildcan!sq!msb
  3. From: msb@sq.sq.com (Mark Brader)
  4. Subject: Re: main()
  5. Message-ID: <1992Sep10.003907.15391@sq.sq.com>
  6. Organization: SoftQuad Inc., Toronto, Canada
  7. References: <cee1.715416182@Isis.MsState.Edu> <1992Sep4.012725.24310@wyvern.twuug.com> <1992Sep4.191347.27577@klaava.Helsinki.FI>
  8. Date: Thu, 10 Sep 92 00:39:07 GMT
  9. Lines: 106
  10.  
  11. > main() is a function.  It returns an int to its caller.  You may
  12. > write it "main() {}" or "int main() {}" as you like.
  13.  
  14. Right.
  15.  
  16. > I suppose that "void foo() {}" does not affect the compiler at all.
  17.  
  18. You may suppose it, but that does not make it right.  First, it is wrong
  19. according to the standard.  Second, the compiler is at liberty to use a
  20. different calling sequence for a function according to the type that
  21. it returns.
  22.  
  23. It does seem to be true that there are no common implementations which
  24. take advantage of this liberty, and that misdeclaring main() as returning
  25. void will work on all common implementations, but it's still wrong and
  26. should be avoided.
  27.  
  28. > The void type is simply a clue to 'lint' or whatever that you will
  29. > not use the return value and not to bug you about its non-use.
  30.  
  31. Wrong.  See above.  And in addition to those points, it's a request to
  32. the compiler to reject a return statement with a value.  (Again, actual
  33. compilers may not actually do so.)
  34.  
  35. > "void main()" would declare that /bin/sh will not use the return value
  36. > from main.  A very bad assumption.
  37.  
  38. The notion that something called /bin/sh is involved is a bad assumption
  39. in the first place.
  40.  
  41. | > No operating system I have ever encountered will get confused by a
  42. | > program that returns an arbitrary value.
  43. | VAX/VMS comes to mind: for some exit codes it prints (confusing,
  44. | erroneous, and misleading) error messages.  For UNIX and MS-DOS, the OS
  45. | itself doesn't get particularly confused, but the user of the program
  46. | often does.
  47.  
  48. Or the calling program; for example, on UNIX make(1) will abort if it
  49. invokes a program that returns nonzero, unless it's explicitly told
  50. to ignore the returned status.
  51.  
  52. | > > Of course, if you terminate your program with exit or abort, it will
  53. | > > never get to the end of main, so in that case there is no need to
  54. | > > include a return statement there.
  55. | > 
  56. | > Personally, I put a ``return'' statement in regardless, with a 
  57. | > ``/* NOTREACHED */'' if appropriate.
  58. | A stylistic issue, of course, but I use simply:
  59. |     int main(void) {
  60. |         ...
  61. |         exit(0);
  62. |     }
  63. | I do not think that there is anything to be won by putting extra stuff
  64. | after the exit.  (I know it may silence lint or the compiler, but I
  65. | prefer to fix them instead. ...)
  66.  
  67. And I prefer to use return at the end of main(), reserving exit() for
  68. direct exits from called functions.  My reason is that it makes it simple
  69. to convert main() to a called function itself, in case I ever want to do
  70. something like this:
  71.  
  72.     main (int argc, char **argv)
  73.     {
  74.         int new_argc;
  75.         char **new_argv;
  76.  
  77.         massage_args (argc, argv, &new_argc, &new_argv);
  78.  
  79.         return original_main (new_argc, new_argv);
  80.     }
  81.  
  82. | Style wars are not usually fruitful in newsgroups.
  83.  
  84. Right.  I just put this forth by way of balance; I have no *complaint*
  85. against the use of exit().  In fact, on the machine where I'm typing this,
  86. I *can't* use my preferred style, because the compiler treats *any* return
  87. from the initial call of main() as an exit(0) call!  (This is a Sun-3 running
  88. SunOS 3.5, pre-ANSI by some years.)
  89.  
  90. | > You should be using ``exit( EXIT_FAILURE )'' or ``exit( EXIT_SUCCESS )''.
  91. | exit(0) and exit(EXIT_SUCCESS) are defined to be exactly equivalent, at
  92. | least according to Plauger & Brodie's "Standard C" (I do not, alas, have
  93. | access to a copy of the standard).
  94.  
  95. Correct according to the standard (section 4.10.4.3 in ANSI numbering,
  96. 7.10.4.3 in ISO).  The choice of 0 or EXIT_SUCCESS is entirely stylistic.
  97. Even on VMS, if they now have an ANSI C compiler there.  (It's been years
  98. since I used VMS.)
  99.  
  100. | [I thought] exit(EXIT_SUCCESS) [sic] and exit(1) were similar ...
  101.  
  102. No.  exit(1) returns an implementation-defined status.  The only way to
  103. portably return a failure status in ANSI C is exit(EXIT_FAILURE), or
  104. of course the analogous return from main().
  105.  
  106. -- 
  107. Mark Brader            "It's simply a matter of style, and while there
  108. SoftQuad Inc., Toronto         are many wrong styles, there really isn't any
  109. utzoo!sq!msb, msb@sq.com     one right style."    -- Ray Butterworth
  110.  
  111. This article is in the public domain.
  112.