home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / c / 16011 < prev    next >
Encoding:
Text File  |  1992-11-04  |  2.5 KB  |  65 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: How to use 'return' in c?
  5. Message-ID: <1992Nov5.054934.29397@sq.sq.com>
  6. Organization: SoftQuad Inc., Toronto, Canada
  7. References: <1992Oct29.025437.9289@nuscc.nus.sg> <1992Nov2.203701.162@ipact.com> <1992Nov4.231347.26999@u.washington.edu>
  8. Date: Thu, 5 Nov 92 05:49:34 GMT
  9. Lines: 54
  10.  
  11. > After several people wrote me on the use of EXIT_SUCCESS and
  12. > EXIT_FAILURE  and their inability to locate those files on their
  13. > systems I checked K&R 2ndEd for the definitive answer...
  14. > " void exit(int status)
  15. > ... How status is returned to the environment is
  16. > implementation-dependent, but zero is taken as successful
  17. > termination.  The values EXIT_SUCCESS and EXIT_FAILURE may also
  18. > be used."                                              ^^^
  19. > Because the word *may* is used I must assume that you either get
  20. > it or you don't depending on your compiler/OS. ...
  21.  
  22. No.  What's going on here is that K&R2 is about ANSI C (a.k.a. ISO C,
  23. or standard C), but the standard is a relatively new thing (less than
  24. 3 years) and there are still lots of older compilers around.
  25.  
  26. An ANSI C implementation *must* provide EXIT_SUCCESS and EXIT_FAILURE
  27. in the header <stdlib.h>, and you *may* use them to specify your status.
  28. Alternatively you may use 0, which is also specified as indicating
  29. success, or some other number, whose meaning will be specific to your
  30. implementation.
  31.  
  32. Older implementations generally did *not* provide EXIT_SUCCESS, EXIT_-
  33. FAILURE, or for that matter, <stdlib.h>.  On UNIX and I believe most
  34. other environments, 0 was specified as success and anything else
  35. indicated failure.  However, not all environments conformed to this;
  36. on VMS, for instance, success and failure were indicated by *odd* and
  37. *even* status returns, with specific values encoding specific types
  38. of success or failure.
  39.  
  40. For portability to ANSI C and to older systems using the UNIX convention,
  41. the following is one possibility:
  42.  
  43.     #if __STDC__
  44.     #include <stdlib.h>
  45.     #endif
  46.  
  47.     #ifndef EXIT_FAILURE
  48.     #define    EXIT_FAILURE 1
  49.     #endif
  50.  
  51.     #ifndef EXIT_SUCCESS
  52.     #define    EXIT_SUCCESS 0
  53.     #endif
  54.  
  55. Then use EXIT_SUCCESS or EXIT_FAILURE.
  56. -- 
  57. Mark Brader          "In the rough, roguish, early days, when men were
  58. SoftQuad Inc., Toronto       men, women were women, and pointers were ints,
  59. utzoo!sq!msb           Real C Programmers wouldn't even bother to invent
  60. msb@sq.com           tags for structs ..."    -- Steve Summit
  61.  
  62. This article is in the public domain.
  63.