indication

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nomadcontauroi@hotmail.com

    #1

    indication

    #include <stdio.h>
    #include <math.h>

    int main(void)
    {
    int number = -1234;

    printf("number: %d absolute value: %d\n", number, abs(number));
    return 0;
    }





    what does the "return 0" indicate in the above program?
    thanks.

  • Vladimir S. Oka

    #2
    Re: indication

    nomadcontauroi@ hotmail.com wrote:[color=blue]
    > #include <stdio.h>
    > #include <math.h>
    >
    > int main(void)
    > {
    > int number = -1234;
    >
    > printf("number: %d absolute value: %d\n", number, abs(number));
    > return 0;
    > }
    >
    >
    >
    >
    >
    > what does the "return 0" indicate in the above program?
    > thanks.
    >[/color]

    It indicates a success, i.e. the program finished with no errors.

    This value is intended to be checked by the host environment (e.g. a
    shell script) to determine the outcome of the program run. Standard C
    also defines it as EXIT_SUCCESS macro. There's also EXIT_FAILURE
    (defined as 1, with obvious meaning), and the two are the only values
    standard knows about, i.e. the only portable values to return to the
    environment. To use either, you have to #include <stdlib.h>.

    Cheers

    Vladimir


    --
    My e-mail address is real, and I read it.

    Comment

    • Lew Pitcher

      #3
      Re: indication

      -----BEGIN PGP SIGNED MESSAGE-----
      Hash: SHA1

      nomadcontauroi@ hotmail.com wrote:[color=blue]
      > #include <stdio.h>
      > #include <math.h>
      >
      > int main(void)
      > {
      > int number = -1234;
      >
      > printf("number: %d absolute value: %d\n", number, abs(number));
      > return 0;
      > }
      >
      >
      >
      >
      >
      > what does the "return 0" indicate in the above program?
      > thanks.[/color]

      It indicates that the main() function explicitly returns a value of zero to it's
      caller. What the caller does with this return value depends on the nature of the
      caller.


      - --
      Lew Pitcher
      IT Specialist, Enterprise Data Systems,
      Enterprise Technology Solutions, TD Bank Financial Group

      (Opinions expressed are my own, not my employers')
      -----BEGIN PGP SIGNATURE-----
      Version: GnuPG v1.2.4 (MingW32)

      iD8DBQFDz5SdagV FX4UWr64RArWNAJ 92HjC3hkFkFb12S XsygeXJ7YNh6ACg 5bKA
      OBEbSUzKAmEeywr j2UbE51U=
      =hyvF
      -----END PGP SIGNATURE-----

      Comment

      • Flash Gordon

        #4
        Re: indication

        Vladimir S. Oka wrote:[color=blue]
        > nomadcontauroi@ hotmail.com wrote:[color=green]
        >> #include <stdio.h>
        >> #include <math.h>
        >>
        >> int main(void)
        >> {
        >> int number = -1234;
        >>
        >> printf("number: %d absolute value: %d\n", number, abs(number));
        >> return 0;
        >> }
        >>
        >> what does the "return 0" indicate in the above program?
        >> thanks.[/color]
        >
        > It indicates a success, i.e. the program finished with no errors.[/color]

        Yes.
        [color=blue]
        > This value is intended to be checked by the host environment (e.g. a
        > shell script) to determine the outcome of the program run. Standard C
        > also defines it as EXIT_SUCCESS macro. There's also EXIT_FAILURE
        > (defined as 1, with obvious meaning),[/color]

        No. EXIT_FAILURE is defined with some value, not necessarily 1. On at
        least some systems returning 1 will indicate *success*, and on such a
        system it is highly doubtful that EXIT_FAILURE would be defined as 1.
        [color=blue]
        > and the two are the only values
        > standard knows about, i.e. the only portable values to return to the
        > environment. To use either, you have to #include <stdlib.h>.[/color]

        To be clear, the portable values a 0, EXIT_SUCCESS and EXIT_FAILURE,
        with 0 meaning success and the other two are obvious. A return value of
        1 is most definitely *not* portable.
        --
        Flash Gordon
        Living in interesting times.
        Although my email address says spam, it is real and I read it.

        Comment

        • Eric Sosman

          #5
          Re: indication

          Vladimir S. Oka wrote:
          [color=blue]
          > nomadcontauroi@ hotmail.com wrote:[color=green]
          >>[...]
          >> what does the "return 0" indicate in the above program?
          >> thanks.
          >>[/color]
          >
          > It indicates a success, i.e. the program finished with no errors.
          >
          > This value is intended to be checked by the host environment (e.g. a
          > shell script) to determine the outcome of the program run. Standard C
          > also defines it as EXIT_SUCCESS macro. There's also EXIT_FAILURE
          > (defined as 1, with obvious meaning), and the two are the only values
          > standard knows about, i.e. the only portable values to return to the
          > environment. To use either, you have to #include <stdlib.h>.[/color]

          Vladimir is partly correct, but not entirely.
          EXIT_SUCCESS is not necessarily zero and EXIT_FAILURE
          is not necessarily one. (If they were always zero and
          one, the macros would be fairly useless ...).

          On a host system that has no notion of the success
          or failure of a program, it is even conceivable that
          EXIT_FAILURE == EXIT_SUCCESS. (Note that I wrote
          "conceivabl e," not "likely.")

          Summary: zero means "success," EXIT_SUCCESS means
          "success" (possibly a different flavor), and EXIT_FAILURE
          means "failure" -- and these three (or two, or one) values
          are the only termination codes blessed by the Standard.

          --
          Eric Sosman
          esosman@acm-dot-org.invalid

          Comment

          • Vladimir S. Oka

            #6
            Re: indication

            Eric Sosman wrote:[color=blue]
            >
            > Vladimir is partly correct, but not entirely.
            > EXIT_SUCCESS is not necessarily zero and EXIT_FAILURE
            > is not necessarily one. (If they were always zero and
            > one, the macros would be fairly useless ...).
            >
            > On a host system that has no notion of the success
            > or failure of a program, it is even conceivable that
            > EXIT_FAILURE == EXIT_SUCCESS. (Note that I wrote
            > "conceivabl e," not "likely.")
            >
            > Summary: zero means "success," EXIT_SUCCESS means
            > "success" (possibly a different flavor), and EXIT_FAILURE
            > means "failure" -- and these three (or two, or one) values
            > are the only termination codes blessed by the Standard.
            >[/color]

            Thanks Eric, you're right. I stand corrected.

            Cheers

            Vladimir

            --
            My e-mail address is real, and I read it.

            Comment

            • Keith Thompson

              #7
              Re: indication

              Flash Gordon <spam@flash-gordon.me.uk> writes:
              [...][color=blue]
              > To be clear, the portable values a 0, EXIT_SUCCESS and EXIT_FAILURE,
              > with 0 meaning success and the other two are obvious. A return value
              > of 1 is most definitely *not* portable.[/color]

              And since 0 is required to indicate success, there's probably no good
              reason for an implementation to define EXIT_SUCCESS as anything other
              than 0 -- but it could, and you shouldn't assume that EXIT_SUCCESS==0 .
              (There's little reason to make such an assumption anyway.)

              IMHO, the standard would have been clearer and more consistent if it
              had *not* required 0 to indicate success, just defining EXIT_SUCCESS
              and EXIT_FAILURE with implementation-specific values. The requirement
              causes some problems for VMS^H^H^H OpenVMS, where odd values denote
              success and even values denote errors; the C runtime system has to
              make special allowances for "return 0;".

              But I suppose "return 0;" or "exit(0);" was too firmly entrenched --
              and it would have required nearly *every* program to have a
              "#include <stdlib.h>".

              --
              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
              San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
              We must do something. This is something. Therefore, we must do this.

              Comment

              • Eric Sosman

                #8
                Re: indication



                Keith Thompson wrote On 01/19/06 16:14,:[color=blue]
                > Flash Gordon <spam@flash-gordon.me.uk> writes:
                > [...]
                >[color=green]
                >>To be clear, the portable values a 0, EXIT_SUCCESS and EXIT_FAILURE,
                >>with 0 meaning success and the other two are obvious. A return value
                >>of 1 is most definitely *not* portable.[/color]
                >
                >
                > And since 0 is required to indicate success, there's probably no good
                > reason for an implementation to define EXIT_SUCCESS as anything other
                > than 0 -- but it could, and you shouldn't assume that EXIT_SUCCESS==0 .
                > (There's little reason to make such an assumption anyway.)[/color]

                The very O/S you proceed to cite offers a good reason
                for EXIT_SUCCESS != 0 ...
                [color=blue]
                > IMHO, the standard would have been clearer and more consistent if it
                > had *not* required 0 to indicate success, just defining EXIT_SUCCESS
                > and EXIT_FAILURE with implementation-specific values. The requirement
                > causes some problems for VMS^H^H^H OpenVMS, where odd values denote
                > success and even values denote errors; the C runtime system has to
                > make special allowances for "return 0;".[/color]

                In the Very Old Days, a C program's exit status was
                passed back verbatim as a VMS condition code, so exit(0)
                meant "failure" ("warning," actually -- there were five
                severity levels) and exit(1) meant "success." At some
                point DEC decided to bow to prevailing custom, and put
                in a hack that mapped 0->1 and 1->0 but left other values
                untouched. That way, exit(0) and exit(1) had the meanings
                on VMS that were not too different from what people were
                accustomed to on Unix, while VMS-aware programs could still
                exit with more informative VMS codes.

                Then the Standard came along, and we had EXIT_SUCCESS
                and EXIT_FAILURE. If I recall correctly (it's been a few
                years), DEC didn't define EXIT_SUCCESS as 0 and let the
                library change it to 1, but defined it as a large odd
                number, turning "generic success" into "success of C program."
                Similarly, EXIT_FAILURE wasn't defined as 1 to be mapped to
                0, but as a large even value meaning "failure of C program."
                Sounds like a silly distinction when viewed in isolation,
                but remember: VMS condition codes were structured, with
                various fields conveying different pieces of information.
                By picking apart the fields you could determine not only
                success/failure, but a severity level (five were defined),
                a specific condition (a program could succeed in many ways,
                or fail in many ways), and the system component where the
                condition arose. A bare zero or one omitted most of this
                information, whereas EXIT_xxxx supplied it.

                --
                Eric.Sosman@sun .com

                Comment

                • Keith Thompson

                  #9
                  Re: indication

                  Eric Sosman <eric.sosman@su n.com> writes:[color=blue]
                  > Keith Thompson wrote On 01/19/06 16:14,:[color=green]
                  >> Flash Gordon <spam@flash-gordon.me.uk> writes:
                  >> [...]
                  >>[color=darkred]
                  >>>To be clear, the portable values a 0, EXIT_SUCCESS and EXIT_FAILURE,
                  >>>with 0 meaning success and the other two are obvious. A return value
                  >>>of 1 is most definitely *not* portable.[/color]
                  >>
                  >> And since 0 is required to indicate success, there's probably no good
                  >> reason for an implementation to define EXIT_SUCCESS as anything other
                  >> than 0 -- but it could, and you shouldn't assume that EXIT_SUCCESS==0 .
                  >> (There's little reason to make such an assumption anyway.)[/color]
                  >
                  > The very O/S you proceed to cite offers a good reason
                  > for EXIT_SUCCESS != 0 ...[/color]

                  Given that exit(0) is required to denote success, defining
                  EXIT_SUCCESS as 0 would be perfectly sensible even under VMS.
                  Defining it as some arbitary odd value could have some advantages,
                  though. For one thing, it might be somehow useful for exit(0) and
                  exit(EXIT_SUCCE SS) to behave in some subtly different manner.

                  In fact, I just remembered I have an account on a VAX running OpenVMS
                  6.2. A quick test shows that EXIT_SUCCESS is defined as 0, and
                  EXIT_FAILURE is defined as 268435458 (0x10000002).
                  [color=blue][color=green]
                  >> IMHO, the standard would have been clearer and more consistent if it
                  >> had *not* required 0 to indicate success, just defining EXIT_SUCCESS
                  >> and EXIT_FAILURE with implementation-specific values. The requirement
                  >> causes some problems for VMS^H^H^H OpenVMS, where odd values denote
                  >> success and even values denote errors; the C runtime system has to
                  >> make special allowances for "return 0;".[/color]
                  >
                  > In the Very Old Days, a C program's exit status was
                  > passed back verbatim as a VMS condition code, so exit(0)
                  > meant "failure" ("warning," actually -- there were five
                  > severity levels) and exit(1) meant "success." At some
                  > point DEC decided to bow to prevailing custom, and put
                  > in a hack that mapped 0->1 and 1->0 but left other values
                  > untouched. That way, exit(0) and exit(1) had the meanings
                  > on VMS that were not too different from what people were
                  > accustomed to on Unix, while VMS-aware programs could still
                  > exit with more informative VMS codes.[/color]

                  Actually, if I recall correctly, it maps exit(0) to an odd value, but
                  it doesn't map exit(1). Back when I worked on VMS systems, I saw
                  programs that used exit(1) with the intent of indicating failure; they
                  didn't.

                  --
                  Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                  San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                  We must do something. This is something. Therefore, we must do this.

                  Comment

                  • Xiaodong Xu

                    #10
                    Re: indication

                    nomadcontauroi@ hotmail.com writes:

                    each function has a return value, so does the 'main'
                    function.

                    you'd better have each function with a return value
                    so that the caller could learn the result of the function
                    called.

                    in common case, the return value '0' means the function
                    is executed suceessfully. of course you may define other
                    error codes for it if anything abnormal occurs.
                    [color=blue]
                    > #include <stdio.h>
                    > #include <math.h>
                    >
                    > int main(void)
                    > {
                    > int number = -1234;
                    >
                    > printf("number: %d absolute value: %d\n", number, abs(number));
                    > return 0;
                    > }
                    >
                    >
                    >
                    >
                    >
                    > what does the "return 0" indicate in the above program?
                    > thanks.[/color]

                    Comment

                    Working...