C89/C90: Returning 0 from main()

Discussion in 'C Programming' started by Michal Nazarewicz, Feb 13, 2007.

  1. Hi,

    What does returning 0 from main() mean according to C89/C90 standard?
    I've found that in C99 it means successful termination (7.20.4.3p5)
    however as I'm editing book on C at Polish Wikibooks I'd like to know
    what C89/C90 says about it - quotation from C89 or C90 would be nice.

    Moreover, in C99 main() function is somewhat special because lack of
    return statement is equivalent with returning zero (5.1.2.2.3p1). Is
    it also the case in C89/C90?

    BTW. Is there any publicly available (draft of) C89/C90 standard?
     
    Michal Nazarewicz, Feb 13, 2007
    #1
    1. Advertisements

  2. Michal Nazarewicz

    matevzb Guest

    First, the main() quote:
    "A return from the initial call to the main function is equivalent to
    calling the exit function with the value returned by the main function
    as its argument. If the main function executes a return that specifies
    no value, the termination status returned to the host environment is
    undefined."
    Return statement:
    "Reaching the } that terminates a function is equivalent to executing
    a return statement without an expression."
    exit() function:
    "Finally, control is returned to the host environment. If the value of
    status is zero or EXIT_SUCCESS , an implementation-defined form of the
    status successful termination is returned. If the value of status is
    EXIT_FAILURE , an implementation-defined form of the status
    unsuccessful termination is returned. Otherwise the status returned is
    implementation-defined."
    It doesn't seem to be with regards to the quoted material, but someone
    else should confirm this.
    It's available at http://web.archive.org/web/20050207005628/http://
    dev.unicals.com/papers/c89-draft.html.
     
    matevzb, Feb 13, 2007
    #2
    1. Advertisements

  3. From n869 [1]:

    5.1.2.2.3: "If the return type of the main function is a type compatible
    with int, a return from the initial call to the main function is
    equivalent to calling the exit function with the value returned by the
    main function as its argument;"

    7.20.4.3 (The exit function), subparagraph 5: "If the value of status
    is zero or EXIT_SUCCESS, an implementation-defined form of the status
    successful termination is returned."
    No; in C89, omission of the return statement causes an unspecified
    termination status to be returned from the host environment (I will
    leave quoting the relevant portion of the standard to someone with
    more time to look it up).
    [1] I apparently have been laboring under the delusion that n869 was a
    draft of the C89 Standard; a look at the foreward of that document
    reveals that it is not so. I also thought the FAQ addressed this
    question, but I don't see that either. I've started my morning 0/2
    and mostly failed to be helpful; sorry about that.
     
    Christopher Benson-Manica, Feb 13, 2007
    #3
  4. D'oh - of course, that should be "TO the host environment". Perhaps I
    should forego attempting to work today, or at least head back to
    Starbucks.
     
    Christopher Benson-Manica, Feb 13, 2007
    #4
  5. The same verbiage you referred to is found in C89 at 7.10.4.3p5.
    No, the value returned in this case is undefined.
    I'm sure there is, I don't know where one would find it though.

    Robert Gamble
     
    Robert Gamble, Feb 13, 2007
    #5
  6. Thanks, that answers all my questions.
     
    Michal Nazarewicz, Feb 13, 2007
    #6
  7. C89 draft:

    "Program termination"

    A return from the initial call to the main function is
    equivalent to calling the exit function with the value returned
    by the main function as its argument. If the main function
    executes a return that specifies no value, the termination
    status returned to the host environment is undefined.
    It does so for the _initial_ call to main. Recursive calls to main
    are allowed, and it's possible to perform such calls legitimately
    without main ever returning a value, so long as the calling function
    doesn't attempt to use the return value.
    Strictly speaking it says an _undefined_ status will be returned to
    the host. Sloppy wording perhaps, but it's probably meant to convey
    that the C standard doesn't limit the termination status to something
    as simple as an integer value or C like object. For instance, an
    unsuccessful status might be to ask the host to ring the lab klaxons!
    N869 is the last public draft prior to the issuance of C99. N1124 is
    a more recent draft that includes the TCs.
     
    Peter Nilsson, Feb 13, 2007
    #7
  8. However, N869 describes a language that has much more in common with
    C99 than C89, correct? That's the fact that I've previously missed.
     
    Christopher Benson-Manica, Feb 14, 2007
    #8
  9. It's a draft of C99 yes, but in terms of being a reference for
    C89 it's quite useful. N1124 is better still. C99 merely clarified
    and extended C89 for the most part. The changes are summarised
    in the Forward.

    There are differences of course, but C99 reflects the modern
    spirit of C better than the crusty C89 standard.
     
    Peter Nilsson, Feb 14, 2007
    #9
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.