Return value of main

Discussion in 'C Programming' started by Morris Dovey, Mar 10, 2008.

  1. Morris Dovey

    Morris Dovey Guest

    polas wrote:

    > Is there any standard for operating systems to handle the C return
    > value in a particular way (I know some ignore it and some do not) ?
    > For instance, if OS A regards return value 0 as successes and OS B
    > regards return value 0 as failure then surely this would affect the
    > portability of the C code?


    If you include <stdlib.h>, the symbols EXIT_SUCCESS and
    EXIT_FAILURE will be defined appropriately.

    --
    Morris Dovey
    DeSoto Solar
    DeSoto, Iowa USA
    http://www.iedu.com/DeSoto
    Morris Dovey, Mar 10, 2008
    #1
    1. Advertising

  2. Morris Dovey

    polas Guest

    Good afternoon all,

    I appreciate that the standard requires that main should return an
    integer. What I was wondering though is there any accepted
    standardisation on exactly what value to return - do people tend to
    return 0 or 1 or anything else? In addition, if there has been some
    failure, do people tend to return another value (such as -1?)

    Is there any standard for operating systems to handle the C return
    value in a particular way (I know some ignore it and some do not) ?
    For instance, if OS A regards return value 0 as successes and OS B
    regards return value 0 as failure then surely this would affect the
    portability of the C code?

    Cheers,
    Nick
    polas, Mar 10, 2008
    #2
    1. Advertising

  3. Morris Dovey

    Richard Bos Guest

    polas <> wrote:

    > I appreciate that the standard requires that main should return an
    > integer. What I was wondering though is there any accepted
    > standardisation on exactly what value to return - do people tend to
    > return 0 or 1 or anything else?


    The Standard requires three values to return a definite status to the
    environment: 0 and EXIT_SUCCESS signal success, EXIT_FAILURE signals
    failure. EXIT_SUCCESS and EXIT_FAILURE are macros #defined in
    <stdlib.h>; what their actual values are is not defined by the Standard,
    and may vary from implementation to implementation. Of course it is
    highly common for EXIT_SUCCESS to be 0, and EXIT_FAILURE is often 1, but
    neither of those is guaranteed.
    If you return any other value from main() (or through exit()), this does
    not make the program exhibit undefined behaviour; the only result is
    that the meaning of the status returned to the environment is undefined
    by the Standard. So calling exit(2) does not make your program print
    weird things or write over memory it doesn't own; but it does mean that
    the C Standard itself does not guarantee that other programs will see
    this as your program having succeeded, failed, or otherwise.

    > Is there any standard for operating systems to handle the C return
    > value in a particular way (I know some ignore it and some do not) ?


    That depends entirely on the OS.

    > For instance, if OS A regards return value 0 as successes and OS B
    > regards return value 0 as failure then surely this would affect the
    > portability of the C code?


    It does; hence the existence of EXIT_SUCCESS and EXIT_FAILURE, which can
    be adapted to the OS' needs.

    Richard
    Richard Bos, Mar 10, 2008
    #3
  4. Morris Dovey

    polas Guest

    On 10 Mar, 12:40, Morris Dovey <> wrote:
    > polas wrote:
    > > Is there any standard for operating systems to handle the C return
    > > value in a particular way (I know some ignore it and some do not) ?
    > > For instance, if OS A regards return value 0 as successes and OS B
    > > regards return value 0 as failure then surely this would affect the
    > > portability of the C code?

    >
    > If you include <stdlib.h>, the symbols EXIT_SUCCESS and
    > EXIT_FAILURE will be defined appropriately.
    >
    > --
    > Morris Dovey
    > DeSoto Solar
    > DeSoto, Iowa USAhttp://www.iedu.com/DeSoto


    Thanks for the replies - that clears it up completely for me.

    Nick
    polas, Mar 10, 2008
    #4
  5. Morris Dovey

    santosh Guest

    polas wrote:

    > Good afternoon all,
    >
    > I appreciate that the standard requires that main should return an
    > integer. What I was wondering though is there any accepted
    > standardisation on exactly what value to return - do people tend to
    > return 0 or 1 or anything else? In addition, if there has been some
    > failure, do people tend to return another value (such as -1?)


    The only standard values for these are 0, EXIT_SUCCESS and EXIT_FAILURE.
    Zero is the same as EXIT_SUCCESS. For using any other values (like
    1, -1 etc.) you'll have to seek the guarantees of standards beyond ISO
    C.

    > Is there any standard for operating systems to handle the C return
    > value in a particular way (I know some ignore it and some do not) ?
    > For instance, if OS A regards return value 0 as successes and OS B
    > regards return value 0 as failure then surely this would affect the
    > portability of the C code?


    No such standard behaviour is specified by ISO C. Each system might have
    it's own standardised or semi-standard behaviour. The portability of C
    code in your example will not be affected because a return of zero
    always means successful termination and the C library is obliged to
    translate this to whatever code that the system uses for successful
    termination. Thus if for the underlying system 10 indicates successful
    termination and you return zero, the C library will actually return 10.
    It will do the same for a return of EXIT_SUCCESS too. A recompile for
    each platform will ensure correct behaviour.
    santosh, Mar 10, 2008
    #5
  6. Morris Dovey

    polas Guest

    On 10 Mar, 13:52, santosh <> wrote:
    > polas wrote:
    > > Good afternoon all,

    >
    > > I appreciate that the standard requires that main should return an
    > > integer. What I was wondering though is there any accepted
    > > standardisation on exactly what value to return - do people tend to
    > > return 0 or 1 or anything else? In addition, if there has been some
    > > failure, do people tend to return another value (such as -1?)

    >
    > The only standard values for these are 0, EXIT_SUCCESS and EXIT_FAILURE.
    > Zero is the same as EXIT_SUCCESS. For using any other values (like
    > 1, -1 etc.) you'll have to seek the guarantees of standards beyond ISO
    > C.
    >
    > > Is there any standard for operating systems to handle the C return
    > > value in a particular way (I know some ignore it and some do not) ?
    > > For instance, if OS A regards return value 0 as successes and OS B
    > > regards return value 0 as failure then surely this would affect the
    > > portability of the C code?

    >
    > No such standard behaviour is specified by ISO C. Each system might have
    > it's own standardised or semi-standard behaviour. The portability of C
    > code in your example will not be affected because a return of zero
    > always means successful termination and the C library is obliged to
    > translate this to whatever code that the system uses for successful
    > termination. Thus if for the underlying system 10 indicates successful
    > termination and you return zero, the C library will actually return 10.
    > It will do the same for a return of EXIT_SUCCESS too. A recompile for
    > each platform will ensure correct behaviour.


    Right - just to clear up a minor point, is 0 defined by the standard
    to be success (as well as EXIT_SUCCESS)?

    Nick
    polas, Mar 10, 2008
    #6
  7. Morris Dovey

    Richard Bos Guest

    polas <> wrote:

    > Right - just to clear up a minor point, is 0 defined by the standard
    > to be success (as well as EXIT_SUCCESS)?


    Yes. (As an aside, it's not _guaranteed_ that 0 and EXIT_SUCCESS are the
    same kind of success, but in practice, I don't think there are many
    ssytems which have more than one kind anyway.)

    Richard
    Richard Bos, Mar 10, 2008
    #7
  8. (Richard Bos) writes:

    > polas <> wrote:
    >
    > > Right - just to clear up a minor point, is 0 defined by the standard
    > > to be success (as well as EXIT_SUCCESS)?

    >
    > Yes. (As an aside, it's not _guaranteed_ that 0 and EXIT_SUCCESS are the
    > same kind of success, but in practice, I don't think there are many
    > ssytems which have more than one kind anyway.)


    IIRC, VMS has several kinds of success: every even result is a failure and
    every odd one a success; the runtime does something particular in the case
    of 0 so that it has its standard behaviour instead of the VMS one).

    Yours,

    --
    Jean-Marc
    Jean-Marc Bourguet, Mar 10, 2008
    #8
  9. Richard Bos wrote:
    >
    > polas <> wrote:

    [...]
    > The Standard requires three values to return a definite status to the
    > environment: 0 and EXIT_SUCCESS signal success, EXIT_FAILURE signals
    > failure. EXIT_SUCCESS and EXIT_FAILURE are macros #defined in
    > <stdlib.h>; what their actual values are is not defined by the Standard,
    > and may vary from implementation to implementation. Of course it is
    > highly common for EXIT_SUCCESS to be 0, and EXIT_FAILURE is often 1, but
    > neither of those is guaranteed.


    Well, it is guaranteed that EXIT_FAILURE cannot be defined as zero.

    > > For instance, if OS A regards return value 0 as successes and OS B
    > > regards return value 0 as failure then surely this would affect the
    > > portability of the C code?

    >
    > It does; hence the existence of EXIT_SUCCESS and EXIT_FAILURE, which can
    > be adapted to the OS' needs.


    Note that exit(0) will still signal "success", even in this case.

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
    Kenneth Brody, Mar 10, 2008
    #9
  10. polas wrote:
    > Good afternoon all,
    >
    > I appreciate that the standard requires that main should return an
    > integer. What I was wondering though is there any accepted
    > standardisation on exactly what value to return - do people tend to
    > return 0 or 1 or anything else? In addition, if there has been some
    > failure, do people tend to return another value (such as -1?)


    Without including <stdlib.h>, there is only one portable value, 0, which
    indicates successful completion.

    If you include <stdlib.h>, you have available the values EXIT_SUCCESS
    and EXIT_FAILURE.


    > Is there any standard for operating systems to handle the C return
    > value in a particular way (I know some ignore it and some do not) ?
    > For instance, if OS A regards return value 0 as successes and OS B
    > regards return value 0 as failure then surely this would affect the
    > portability of the C code?


    There were C implementations that when the C program returned 0 would
    indicate to the operating system that the program had failed. Such
    implementations, as far as I know, are no longer used and obsolete. If
    you want to indicate successful completion, return 0 or EXIT_SUCCESS.
    If you want to portably indicate failure, return EXIT_FAILURE. Your
    implementation and operating system might define meanings for other
    values, but they are not portable or defined by the C language.
    Martin Ambuhl, Mar 10, 2008
    #10
  11. In article <>,
    Martin Ambuhl <> wrote:

    >Without including <stdlib.h>, there is only one portable value, 0, which
    >indicates successful completion.


    And of course the C standard can't really guarantee anything much
    about this "success".

    For example, on Unix, the return code is not interpreted by the
    operating system as such, but by the parent program. Even if you run
    your C program from the shell, the exit value is only treated as
    "success" or "failure" in a very few circumstances. More often it is
    treated as a boolean value, and even more often it is not interpreted
    at all.

    -- Richard
    --
    :wq
    Richard Tobin, Mar 10, 2008
    #11
  12. Morris Dovey

    user923005 Guest

    On Mar 10, 6:52 am, santosh <> wrote:
    > polas wrote:
    > > Good afternoon all,

    >
    > > I appreciate that the standard requires that main should return an
    > > integer. What I was wondering though is there any accepted
    > > standardisation on exactly what value to return - do people tend to
    > > return 0 or 1 or anything else? In addition, if there has been some
    > > failure, do people tend to return another value (such as -1?)

    >
    > The only standard values for these are 0, EXIT_SUCCESS and EXIT_FAILURE.
    > Zero is the same as EXIT_SUCCESS. For using any other values (like
    > 1, -1 etc.) you'll have to seek the guarantees of standards beyond ISO
    > C.
    >
    > > Is there any standard for operating systems to handle the C return
    > > value in a particular way (I know some ignore it and some do not) ?
    > > For instance, if OS A regards return value 0 as successes and OS B
    > > regards return value 0 as failure then surely this would affect the
    > > portability of the C code?

    >
    > No such standard behaviour is specified by ISO C. Each system might have
    > it's own standardised or semi-standard behaviour. The portability of C
    > code in your example will not be affected because a return of zero
    > always means successful termination and the C library is obliged to
    > translate this to whatever code that the system uses for successful
    > termination. Thus if for the underlying system 10 indicates successful
    > termination and you return zero, the C library will actually return 10.
    > It will do the same for a return of EXIT_SUCCESS too. A recompile for
    > each platform will ensure correct behaviour.


    From the standard ISO/IEC 9899:1999 (E) we have this:
    "5.1.2.2.3 Program termination
    1 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;10) reaching the } that terminates the main
    function returns a value of 0. If the return type is not compatible
    with int, the termination status returned to the host environment is
    unspecified.
    Forward references: definition of terms (7.1.1), the exit function
    (7.20.4.3)."

    And this:
    "7.20.4.3 The exit function
    Synopsis
    1 #include <stdlib.h>
    void exit(int status);
    Description
    2 The exit function causes normal program termination to occur. If
    more than one call to the exit function is executed by a program, the
    behavior is undefined.
    3 First, all functions registered by the atexit function are called,
    in the reverse order of their registration,253) except that a function
    is called after any previously registered functions that had already
    been called at the time it was registered. If, during the call to any
    such function, a call to the longjmp function is made that would
    terminate the call to the registered function, the behavior is
    undefined.
    4 Next, all open streams with unwritten buffered data are flushed, all
    open streams are closed, and all files created by the tmpfile function
    are removed.
    5 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.
    Returns
    6 The exit function cannot return to its caller."

    So we can conclude that return 0 or return EXIT_SUCCESS will return
    successful status and that return EXIT_FAILURE will return
    unsuccessful status and that any other value must be implementation
    defined.
    user923005, Mar 10, 2008
    #12
  13. Morris Dovey

    pete Guest

    Kenneth Brody wrote:

    > Well, it is guaranteed that EXIT_FAILURE cannot be defined as zero.


    That's not guaranteed.

    Some systems don't make use of the return value of main,
    and on systems that don't,
    there is no reason to have any meaning at all
    encoded into the values that main can return.

    --
    pete
    pete, Mar 10, 2008
    #13
  14. >So we can conclude that return 0 or return EXIT_SUCCESS will return
    >successful status and that return EXIT_FAILURE will return
    >unsuccessful status and that any other value must be implementation
    >defined.


    Nothing in that statement prohibits different types of success or
    failure within the broad classes, such as:

    EXIT_SUCCESS_WITH_PROMOTION_AND_RAISE
    EXIT_SUCCESS_YOU_GET_TO_KEEP_YOUR_JOB
    EXIT_FAILURE_YOURE_FIRED
    EXIT_FAILURE_YOU_WILL_BE_SHOT

    Although it's not allowed for <stdlib.h> to define these, they could
    be defined in an implementation-specific header.
    Gordon Burditt, Mar 11, 2008
    #14
  15. Morris Dovey

    santosh Guest

    user923005 wrote:

    > On Mar 10, 6:52 am, santosh <> wrote:
    >> polas wrote:
    >> > Good afternoon all,

    >>
    >> > I appreciate that the standard requires that main should return an
    >> > integer. What I was wondering though is there any accepted
    >> > standardisation on exactly what value to return - do people tend to
    >> > return 0 or 1 or anything else? In addition, if there has been some
    >> > failure, do people tend to return another value (such as -1?)

    >>
    >> The only standard values for these are 0, EXIT_SUCCESS and
    >> EXIT_FAILURE. Zero is the same as EXIT_SUCCESS. For using any other
    >> values (like 1, -1 etc.) you'll have to seek the guarantees of
    >> standards beyond ISO C.


    <snip>

    > So we can conclude that return 0 or return EXIT_SUCCESS will return
    > successful status and that return EXIT_FAILURE will return
    > unsuccessful status and that any other value must be implementation
    > defined.


    That's what I believe I said too.
    santosh, Mar 12, 2008
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wl
    Replies:
    2
    Views:
    568
    Dimitri Maziuk
    Mar 5, 2004
  2. Sweety

    main return value stange question

    Sweety, Dec 23, 2003, in forum: C Programming
    Replies:
    11
    Views:
    604
    Christopher Benson-Manica
    Dec 30, 2003
  3. Greenhorn
    Replies:
    15
    Views:
    795
    Keith Thompson
    Mar 6, 2005
  4. Christian Christmann

    return value of main

    Christian Christmann, Aug 2, 2006, in forum: C Programming
    Replies:
    25
    Views:
    982
    Chris Torek
    Aug 7, 2006
  5. Army1987

    int main(void) { return main(); }

    Army1987, Mar 29, 2007, in forum: C Programming
    Replies:
    37
    Views:
    1,420
    Daniel Rudy
    Apr 3, 2007
Loading...

Share This Page