portability and return

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

    portability and return

    I have heard that return -1 is not portable. So what would be the answer
    to portability in this case? What about exit(0) and exit (-1) or exit (1)?
    Or would it be best to stick with C's macros, hence:

    exit(EXIT_FAILU RE)
    exit (EXIT_SUCCESS)

    Bill


  • Walter Roberson

    #2
    Re: portability and return

    In article <2YnWj.20544$5b 3.14006@trnddc0 5>,
    Bill Cunningham <nospam@nspam.c omwrote:
    I have heard that return -1 is not portable. So what would be the answer
    >to portability in this case? What about exit(0) and exit (-1) or exit (1)?
    >Or would it be best to stick with C's macros, hence:
    >exit(EXIT_FAIL URE)
    >exit (EXIT_SUCCESS)
    It would be best to stick with C's macros. The results are implementation
    defined if you use any value other than 0 or one of the macros.
    In particular, exit(1) is *not* something that C assigns any meaning to.
    --
    "What is important, then, is not that the critic should possess a
    correct abstract definition of beauty for the intellect, but a
    certain kind of temperament, the power of being deeply moved by
    the presence of beautiful objects." -- Walter Pater

    Comment

    • Peter Nilsson

      #3
      Re: portability and return

      Bill Cunningham wrote:
      I have heard that return -1 is not portable.
      Presumably you're talking about the return value from main()?
      So what would be the answer to portability in this case?
      What about exit(0) and exit (-1) or exit (1)?
      exit(0) is fine.
      Or would it be best to stick with C's macros, hence:
      >
      exit(EXIT_FAILU RE)
      exit (EXIT_SUCCESS)
      Those are good too.

      --
      Peter

      Comment

      • Bill Cunningham

        #4
        Re: portability and return


        "Peter Nilsson" <airia@acay.com .auwrote in message
        news:3222fc05-7aee-4eb3-b874-eb55981751dd@v2 6g2000prm.googl egroups.com...
        Bill Cunningham wrote:
        >I have heard that return -1 is not portable.
        >
        Presumably you're talking about the return value from main()?
        Yes. Would return -1 from a function other than main be ok? I'm guessing
        no but I thought I'd ask.

        Bill


        Comment

        • Spiros Bousbouras

          #5
          Re: portability and return

          On 13 May, 22:57, "Bill Cunningham" <nos...@nspam.c omwrote:
          "Peter Nilsson" <ai...@acay.com .auwrote in message
          >
          news:3222fc05-7aee-4eb3-b874-eb55981751dd@v2 6g2000prm.googl egroups.com...
          >
          Bill Cunningham wrote:
          I have heard that return -1 is not portable.
          >
          Presumably you're talking about the return value from main()?
          >
          Yes. Would return -1 from a function other than main be ok? I'm guessing
          no but I thought I'd ask.
          exit(-1) is not portable no matter which function
          calls it.


          Comment

          • Harald van =?UTF-8?b?RMSzaw==?=

            #6
            Re: portability and return

            On Tue, 13 May 2008 21:57:23 +0000, Bill Cunningham wrote:
            Yes. Would return -1 from a function other than main be ok? I'm
            guessing
            no but I thought I'd ask.
            Are you asking if it's okay to return -1 in such cases as calling
            add(2, -3), defined as

            int add(int a, int b) {
            return a + b;
            }

            ? What else would you want to make this function return?

            Comment

            • Walter Roberson

              #7
              Re: portability and return

              In article <79oWj.7860$Uz2 .2677@trnddc06> ,
              Bill Cunningham <nospam@nspam.c omwrote:
              Yes. Would return -1 from a function other than main be ok? I'm guessing
              >no but I thought I'd ask.
              For any function other than main, it is fine to return to its
              caller any value that lies within the range of the type of the return
              value.

              But, just in case you meant something different: if you are calling
              exit() from *anywhere* in the program, you should restrict the
              exit value to 0 or one of the two status macros.
              --
              "Whenever there is a hard job to be done I assign it to a lazy
              man; he is sure to find an easy way of doing it."
              -- Walter Chrysler

              Comment

              • Bill Cunningham

                #8
                Re: portability and return




                "Harald van D?k" <truedfx@gmail. comwrote in Are you asking if it's okay
                to return -1 in such cases as calling
                add(2, -3), defined as
                >
                int add(int a, int b) {
                return a + b;
                }
                >
                ? What else would you want to make this function return?
                No no. Not in this case. Of course return as you specified is correct I
                understand. I mean to simply exit with say and error.

                if ( argc != 5)
                { fprintf(stderr, "error\n");
                return -1; /* exit(-1) is wrong I guess the proper return would be */
                exit(EXIT_FAILU RE);
                }


                Bill


                Comment

                • jacob navia

                  #9
                  Re: portability and return

                  Bill Cunningham wrote:
                  "Peter Nilsson" <airia@acay.com .auwrote in message
                  news:3222fc05-7aee-4eb3-b874-eb55981751dd@v2 6g2000prm.googl egroups.com...
                  >Bill Cunningham wrote:
                  >>I have heard that return -1 is not portable.
                  >Presumably you're talking about the return value from main()?
                  >
                  Yes. Would return -1 from a function other than main be ok? I'm guessing
                  no but I thought I'd ask.
                  >
                  Bill
                  >
                  >
                  Obviously returning -1 from ANY function (including main)
                  is portable and well defined by the language.

                  Now, returning -1 from main could be interpreted by the OS
                  (when there is one) in different ways and could be non
                  portable in the sense that it could mean different things in
                  different OSes.

                  But this is after main returns, so it is no longer a C problem.

                  By the way, I find all this discussion completely stupid sorry.

                  What do you have against -1???

                  It is just as good a number as 1, or 4477665 for that matter.



                  --
                  jacob navia
                  jacob at jacob point remcomp point fr
                  logiciels/informatique

                  Comment

                  • Richard Tobin

                    #10
                    Re: portability and return

                    In article <79oWj.7860$Uz2 .2677@trnddc06> ,
                    Bill Cunningham <nospam@nspam.c omwrote:
                    Yes. Would return -1 from a function other than main be ok?
                    Yes of course. You can return any int from a function declared as
                    returning an int.

                    There's no problem returning -1 from main() either, as far as the C
                    program is concerned. All that's undefined is how the "host
                    environment" - the shell or operating system or parent program -
                    interprets it. 0 (or EXIT_SUCCESS) indicates success. EXIT_FAILURE
                    indicates failure. What anything else means depends on your system.

                    Most of my programs return 0, 1, or sometimes a larger value, which
                    can be interpreted reasonably in the unix environments I intend them
                    for. But you can perfectly well run them in other environments; you
                    just have to ensure that you run them in such a way that the return
                    value is not misinterpreted. I suppose it's possible that somewhere
                    there's an operating system that always shuts down completely if any
                    C program calls exit(1), but the solution to that is to get a different
                    operating system.

                    -- Richard
                    --
                    :wq

                    Comment

                    • Bill Cunningham

                      #11
                      Re: portability and return


                      "Richard Tobin" <richard@cogsci .ed.ac.ukwrote in message
                      news:g0d3t6$26u i$1@pc-news.cogsci.ed. ac.uk...
                      There's no problem returning -1 from main() either, as far as the C
                      program is concerned. All that's undefined is how the "host
                      environment" - the shell or operating system or parent program -
                      interprets it. 0 (or EXIT_SUCCESS) indicates success. EXIT_FAILURE
                      indicates failure. What anything else means depends on your system.
                      [snip]

                      That's what I am concerned with. How the host system will react. What I
                      am really concerned with is what the standard says would run best on any
                      environment. I write small C programs in linux and I want to be able to take
                      the same source code to my windows system and compile it there and get the
                      same results. I am trying to get into the habit of treating all code as
                      development code so it would work in all host environments.

                      Bill


                      Comment

                      • pete

                        #12
                        Re: portability and return

                        Bill Cunningham wrote:
                        "Peter Nilsson" <airia@acay.com .auwrote in message
                        news:3222fc05-7aee-4eb3-b874-eb55981751dd@v2 6g2000prm.googl egroups.com...
                        >Bill Cunningham wrote:
                        >>I have heard that return -1 is not portable.
                        >Presumably you're talking about the return value from main()?
                        >
                        Yes. Would return -1 from a function other than main be ok?
                        I'm guessing no but I thought I'd ask.
                        You can write another function that returns -1 if you want to.
                        There's no problem with that.
                        A lot of functions return EOF.
                        Any negative int, is an allowable value
                        for an implementation to use to define EOF.

                        The 3 portable return values from main are:
                        0
                        EXIT_SUCCESS
                        EXIT_FAILURE

                        --
                        pete

                        Comment

                        • Jens Thoms Toerring

                          #13
                          Re: portability and return

                          jacob navia <jacob@nospam.o rgwrote:
                          Bill Cunningham wrote:
                          "Peter Nilsson" <airia@acay.com .auwrote in message
                          news:3222fc05-7aee-4eb3-b874-eb55981751dd@v2 6g2000prm.googl egroups.com...
                          Bill Cunningham wrote:
                          >I have heard that return -1 is not portable.
                          Presumably you're talking about the return value from main()?
                          Yes. Would return -1 from a function other than main be ok? I'm guessing
                          no but I thought I'd ask.

                          Bill
                          Obviously returning -1 from ANY function (including main)
                          is portable and well defined by the language.
                          Now, returning -1 from main could be interpreted by the OS
                          (when there is one) in different ways and could be non
                          portable in the sense that it could mean different things in
                          different OSes.
                          But this is after main returns, so it is no longer a C problem.
                          By the way, I find all this discussion completely stupid sorry.
                          What do you have against -1???
                          It is just as good a number as 1, or 4477665 for that matter.
                          Jacob, I am rather sure you know better, so just for the OP. If
                          you want to return a value from main() that indicates success
                          to the system that invoked the program (and that holds for all
                          systems) then return either 0 or EXIT_SUCCESS. If you want to
                          return a value that indicates failure return EXIT_FAILURE.
                          That's what the C standard requires. It even might mean that
                          when you return 0 (or EXIT_SUCCESS) the calling system will
                          receive a value of 42 if it expects 42 to mean success (and
                          -53 if you return EXIT_FAILURE and on that system -53 indi-
                          cates failure).

                          With everything else you don't have such a guarantee. That
                          makes it not wrong per se to return something else but it
                          restricts the usability of the return value to those systems
                          that you know what return value they expect (and e.g. 4477665
                          won't be anything useful for UNIX systems where the return
                          value has to fit into 8 bits).

                          Regards, Jens
                          --
                          \ Jens Thoms Toerring ___ jt@toerring.de
                          \______________ ____________ http://toerring.de

                          Comment

                          • Eric Sosman

                            #14
                            Re: portability and return

                            jacob navia wrote:
                            Bill Cunningham wrote:
                            >"Peter Nilsson" <airia@acay.com .auwrote in message
                            >news:3222fc0 5-7aee-4eb3-b874-eb55981751dd@v2 6g2000prm.googl egroups.com...
                            >>Bill Cunningham wrote:
                            >>>I have heard that return -1 is not portable.
                            >>Presumably you're talking about the return value from main()?
                            >>
                            > Yes. Would return -1 from a function other than main be ok? I'm
                            >guessing no but I thought I'd ask.
                            >>
                            >Bill
                            >>
                            >>
                            >
                            Obviously returning -1 from ANY function (including main)
                            is portable and well defined by the language.
                            >
                            Now, returning -1 from main could be interpreted by the OS
                            (when there is one) in different ways and could be non
                            portable in the sense that it could mean different things in
                            different OSes.
                            >
                            But this is after main returns, so it is no longer a C problem.
                            >
                            By the way, I find all this discussion completely stupid sorry.
                            >
                            What do you have against -1???
                            >
                            It is just as good a number as 1, or 4477665 for that matter.
                            A few years ago somebody asked about systems where a
                            termination status of -1 would survive its journey to the
                            environment and be meaningful when it got there. As far
                            as I can recall, nobody could name such a system. The
                            somewhat bemused question, of course, was: If there aren't
                            any systems where -1 makes sense (or if they're so rare
                            that nobody can think of any), why is -1 so often used
                            as a termination status?

                            (Open)VMS was a near-miss, though, since the value would
                            make it unscathed all the way out to the invoker. However,
                            VMS' "condition codes" contain various sub-fields in their
                            32 bits, and the value -1 would set the "severity" sub-field
                            to a reserved-to-HP value, would set a bit that inhibits the
                            display of a termination message, and would set three "must
                            be zero" bits non-zero. So: survivable, but not meaningful.

                            --
                            Eric.Sosman@sun .com

                            Comment

                            • Bill Cunningham

                              #15
                              Re: portability and return


                              "Jens Thoms Toerring" <jt@toerring.de wrote in message
                              news:68uj7rF2ua tl5U1@mid.uni-berlin.de...
                              Jacob, I am rather sure you know better, so just for the OP. If
                              you want to return a value from main() that indicates success
                              to the system that invoked the program (and that holds for all
                              systems) then return either 0 or EXIT_SUCCESS. If you want to
                              return a value that indicates failure return EXIT_FAILURE.
                              That's what the C standard requires. It even might mean that
                              when you return 0 (or EXIT_SUCCESS) the calling system will
                              receive a value of 42 if it expects 42 to mean success (and
                              -53 if you return EXIT_FAILURE and on that system -53 indi-
                              cates failure).
                              >
                              With everything else you don't have such a guarantee. That
                              makes it not wrong per se to return something else but it
                              restricts the usability of the return value to those systems
                              that you know what return value they expect (and e.g. 4477665
                              won't be anything useful for UNIX systems where the return
                              value has to fit into 8 bits).
                              >
                              Regards, Jens
                              Thanks very much. That answers my question completely.

                              Bill


                              Comment

                              Working...