where does /return value/ go?

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

    #1

    where does /return value/ go?

    1.) we usually say /return 0/ indicates success e.g

    #include <iostream>

    int main() {
    int i=3;

    std::cout << i << std::endl;

    return 0;
    }


    i ran the programme & i see only /3/ as output. i dont see any /0/ on
    my terminal. where does it go? & who reads /0/ to indicate success?

  • Steve Pope

    #2
    Re: where does /return value/ go?

    arnuld <arnuld3@gmail. comwrote:
    >1.) we usually say /return 0/ indicates success e.g
    >
    >#include <iostream>
    >
    >int main() {
    int i=3;
    >
    std::cout << i << std::endl;
    >
    return 0;
    >}
    >
    >
    >i ran the programme & i see only /3/ as output. i dont see any /0/ on
    >my terminal. where does it go? & who reads /0/ to indicate success?
    Programs such as shells and make utilities can see and use the return
    value of a program. A user executing the program from the command
    line does not usually see it.

    You'll have to look at your operating system documentation to
    learn more.

    Steve

    Comment

    • Gianni Mariani

      #3
      Re: where does /return value/ go?

      arnuld wrote:
      1.) we usually say /return 0/ indicates success e.g
      >
      #include <iostream>
      >
      int main() {
      int i=3;
      >
      std::cout << i << std::endl;
      >
      return 0;
      }
      >
      >
      i ran the programme & i see only /3/ as output. i dont see any /0/ on
      my terminal. where does it go? & who reads /0/ to indicate success?
      If you executed your program from the std::system function call then the
      value is partly returned by the system call. If the return value from
      system() is negative then there was an error, otherwise the lower 8 bits
      are the lower 8 bits returned from main often termed the "status".

      Other facilities like "pclose", createprocess, waitpid, etc also enable
      getting at the "status" in the same way.

      The convention is to return 0 if the program was "successful " and non
      zero otherwise. I a compiler finds an error, it is expected that the
      return status is "1". For example, in the "sendmail" the mail system,
      if the program has a "temporary" error, the return status should be "75"
      or sometimes termed EX_TEMPFAIL.

      see:


      You almost never have to worry about anything other than "0" - success,
      or "1" failure.

      Comment

      • Jacek Dziedzic

        #4
        Re: where does /return value/ go?

        arnuld wrote:
        1.) we usually say /return 0/ indicates success e.g
        >
        #include <iostream>
        >
        int main() {
        int i=3;
        >
        std::cout << i << std::endl;
        >
        return 0;
        }
        >
        >
        i ran the programme & i see only /3/ as output. i dont see any /0/ on
        my terminal. where does it go? & who reads /0/ to indicate success?
        <OT>
        Your command shell took it. Usually you'll have an OS-specific
        means to check the return value, e.g. in a bash shell under
        linux you may try, assuming your program executable is named a.out

        #!/bin/bash
        ../a.out
        echo "The return value is $?"

        </OT>

        HTH,
        - J.

        Comment

        • Default User

          #5
          Re: where does /return value/ go?

          Gianni Mariani wrote:

          If you executed your program from the std::system function call then
          the value is partly returned by the system call. If the return value
          from system() is negative then there was an error, otherwise the
          lower 8 bits are the lower 8 bits returned from main often termed the
          "status".
          There is no such requirement for the system() function. From the C99
          draft standard:

          7.20.4.5 The system function

          Synopsis

          [#1]

          #include <stdlib.h>
          int system(const char *string);

          Description

          [#2] If string is a null pointer, the system function
          determines whether the host environment has a command
          processor. If string is not a null pointer, the system
          function passes the string pointed to by string to that
          command processor to be executed in a manner which the
          implementation shall document; this might then cause the
          program calling system to behave in a non-conforming manner
          or to terminate.

          Returns

          [#3] If the argument is a null pointer, the system function
          returns nonzero only if a command processor is available.
          If the argument is not a null pointer, and the system
          function does return, it returns an implementation-defined
          value.





          Brian

          Comment

          Working...