Return value of main

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • polas

    Return value of main

    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
  • Richard Bos

    #2
    Re: Return value of main

    polas <nick@helpforce .comwrote:
    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

    Comment

    • Morris Dovey

      #3
      Re: Return value of main

      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

      Comment

      • polas

        #4
        Re: Return value of main

        On 10 Mar, 12:40, Morris Dovey <mrdo...@iedu.c omwrote:
        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

        Comment

        • santosh

          #5
          Re: Return value of main

          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.

          Comment

          • polas

            #6
            Re: Return value of main

            On 10 Mar, 13:52, santosh <santosh....@gm ail.comwrote:
            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

            Comment

            • Richard Bos

              #7
              Re: Return value of main

              polas <nick@helpforce .comwrote:
              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

              Comment

              • Jean-Marc Bourguet

                #8
                Re: Return value of main

                rlb@hoekstra-uitgeverij.nl (Richard Bos) writes:
                polas <nick@helpforce .comwrote:
                >
                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

                Comment

                • Kenneth Brody

                  #9
                  Re: Return value of main

                  Richard Bos wrote:
                  >
                  polas <nick@helpforce .comwrote:
                  [...]
                  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:ThisIsA SpamTrap@gmail. com>


                  Comment

                  • Martin Ambuhl

                    #10
                    Re: Return value of main

                    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.

                    Comment

                    • pete

                      #11
                      Re: Return value of main

                      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

                      Comment

                      • santosh

                        #12
                        Re: Return value of main

                        user923005 wrote:
                        On Mar 10, 6:52 am, santosh <santosh....@gm ail.comwrote:
                        >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.

                        Comment

                        Working...