portability and return

Discussion in 'C Programming' started by Bill Cunningham, May 13, 2008.

  1. 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_FAILURE)
    exit (EXIT_SUCCESS)

    Bill
     
    Bill Cunningham, May 13, 2008
    #1
    1. Advertising

  2. In article <2YnWj.20544$5b3.14006@trnddc05>,
    Bill Cunningham <> wrote:
    > 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_FAILURE)
    >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
     
    Walter Roberson, May 13, 2008
    #2
    1. Advertising

  3. 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_FAILURE)
    > exit (EXIT_SUCCESS)


    Those are good too.

    --
    Peter
     
    Peter Nilsson, May 13, 2008
    #3
  4. "Peter Nilsson" <> wrote in message
    news:...
    > 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
     
    Bill Cunningham, May 13, 2008
    #4
  5. On 13 May, 22:57, "Bill Cunningham" <> wrote:
    > "Peter Nilsson" <> wrote in message
    >
    > news:...
    >
    > > 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.
     
    Spiros Bousbouras, May 13, 2008
    #5
  6. 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?
     
    Harald van Dijk, May 13, 2008
    #6
  7. In article <79oWj.7860$Uz2.2677@trnddc06>,
    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.


    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
     
    Walter Roberson, May 13, 2008
    #7
  8. "Harald van D?k" <> wrote 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_FAILURE);
    }


    Bill
     
    Bill Cunningham, May 13, 2008
    #8
  9. Bill Cunningham

    jacob navia Guest

    Bill Cunningham wrote:
    > "Peter Nilsson" <> wrote in message
    > news:...
    >> 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
    http://www.cs.virginia.edu/~lcc-win32
     
    jacob navia, May 13, 2008
    #9
  10. In article <79oWj.7860$Uz2.2677@trnddc06>,
    Bill Cunningham <> wrote:

    > 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
     
    Richard Tobin, May 13, 2008
    #10
  11. "Richard Tobin" <> wrote in message
    news:g0d3t6$26ui$...
    > 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
     
    Bill Cunningham, May 13, 2008
    #11
  12. jacob navia <> wrote:
    > Bill Cunningham wrote:
    > > "Peter Nilsson" <> wrote in message
    > > news:...
    > >> 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 ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, May 13, 2008
    #12
  13. "Jens Thoms Toerring" <> wrote in message
    news:-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
     
    Bill Cunningham, May 13, 2008
    #13
  14. Mark McIntyre <> writes:

    > Eligiusz Narutowicz wrote:
    >> "Bill Cunningham" <> writes:
    >>> (stufff)

    >
    >
    >> You are for sure trolling. Please stop.

    >
    > No, in fact Bill has some learning difficulty as far as we can
    > discern.


    This is impossible. How can anyone at this stage ask if a normal "int"
    function can return -1 and then guess that it can not?
     
    Eligiusz Narutowicz, May 14, 2008
    #14
  15. Mark McIntyre <> writes:

    > Eligiusz Narutowicz wrote:
    >> Mark McIntyre <> writes:
    >>
    >>> Eligiusz Narutowicz wrote:
    >>>> "Bill Cunningham" <> writes:
    >>>>> (stufff)
    >>>
    >>>> You are for sure trolling. Please stop.
    >>> No, in fact Bill has some learning difficulty as far as we can
    >>> discern.

    >>
    >> This is impossible. How can anyone at this stage ask if a normal "int"
    >> function can return -1 and then guess that it can not?

    >
    > Suggest you find out what "learning difficulties" means.


    I know what learning difficulties means. Please do not be so rude. But I
    refuse to believe that someone who can get on usenet and post such
    questions does not know what a signed int is for returning from a
    function. It seems incredible. If he really is slow then he needs to be
    elsewhere than this group and perform a proper tutorial or course.
     
    Eligiusz Narutowicz, May 14, 2008
    #15
  16. In article <g0f0fq$69o$>,
    Eligiusz Narutowicz <> wrote:

    >This is impossible. How can anyone at this stage ask if a normal "int"
    >function can return -1 and then guess that it can not?


    If you look at the rest of the thread you will see that this is not
    what he meant: he just expressed himself badly. He really wanted to
    know if calling exit(-1) from functions other than main was just
    as unportable as returning -1 from main.

    -- Richard



    --
    :wq
     
    Richard Tobin, May 14, 2008
    #16
  17. Mark McIntyre <> writes:

    > Eligiusz Narutowicz wrote:
    >> Mark McIntyre <> writes:
    >>
    >>> Eligiusz Narutowicz wrote:
    >>>> Mark McIntyre <> writes:
    >>>>
    >>>>> No, in fact Bill has some learning difficulty as far as we can
    >>>>> discern.

    > ...
    >>>> This is impossible.

    > ...
    >>> Suggest you find out what "learning difficulties" means.

    >>
    >> I know what learning difficulties means.

    >
    > Good. Then perhaps you could consider how on earth you can possibly
    > know what is possible and impossible for someone you've never met?
    >
    >> Please do not be so rude.

    >
    > *shrug*. It wasn't rude, but if you want to consider it so that's your
    > prerogative.
    >
    >> But I
    >> refuse to believe that someone who can get on usenet

    >
    > I can't help what you believe. I'm posting the facts as we know them
    > and have observed from Bill's posts here over the last several years.
    >
    > Enough, no more - tis not so sweet now as it was before.


    Sigh ok. But considering how you answer some not so slow people I am
    astonished who think he is retarded as opposed to a troll. Someone who
    is capable of posting to usnet surely understands brackets and scope in
    C by now? I find it troubling that he can not. If he really is so slow
    then maybe there are better places for him to learning C.
     
    Eligiusz Narutowicz, May 14, 2008
    #17
  18. Bill Cunningham

    Guest

    Last time I get into this thread, Bill say that he know enough about
    the subject(by May 13, 6:47 pm). Something like "Thanks very much.
    That answers my question completely".

    Maybe one should create a new group called
    comp.moderated.only.and.just.only.std.and.wiz.c

    Regards
    Rafael
     
    , May 14, 2008
    #18
  19. Bill Cunningham

    santosh Guest

    Bill Cunningham wrote:

    >
    > "Richard Tobin" <> wrote in message
    > news:g0d3t6$26ui$...
    >> 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.


    In this case your best method (for now) is to use EXIT_SUCCESS and
    EXIT_FAILURE as appropriate. This will work on all conforming
    implementations. The only disadvantage is that there is only a single
    abnormal termination status value. You probably won't need multiple
    values for most programs, but if you do need them, then you may have to
    place system specific code surrounded by #ifdefs.
     
    santosh, May 14, 2008
    #19
  20. "Eligiusz Narutowicz" <> wrote in message
    news:g0f0fq$69o$...
    > This is impossible. How can anyone at this stage ask if a normal "int"
    > function can return -1 and then guess that it can not?


    I don't believe that is what I asked. My post reads "portability". Not
    can an int type return an int. This is not so much about the return types of
    C but portability of ending functions with success or failure on different
    implementations. I have been told return -1 was "wrong" (not portable). I
    wanted the voice of experience.

    No tutorial has ever spoke to me about the reactions of different
    implementations or this post would never have been made. I wanted to get
    straight ending with success or failure with a function. I only post to clc
    after I have
    1) read a topic and am confused
    2) and tried by trial and error and can't determine the straight answer by
    trial and error.

    I do have problems that are worsened by Klonopin. Read the side effects
    of that. Confusion. But

    I think you need to read the topic of a thread before guessing it's
    about return types as per values other than the topic like success or
    failure and its portability and the standard.
     
    Bill Cunningham, May 14, 2008
    #20
    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. Nikhil
    Replies:
    2
    Views:
    410
    Richard Tobin
    May 4, 2005
  2. Gianni Mariani

    Re: class memory layout and portability

    Gianni Mariani, Jul 7, 2003, in forum: C++
    Replies:
    2
    Views:
    502
    Gianni Mariani
    Jul 7, 2003
  3. Shane Beasley
    Replies:
    0
    Views:
    459
    Shane Beasley
    Jul 7, 2003
  4. Greenhorn
    Replies:
    15
    Views:
    849
    Keith Thompson
    Mar 6, 2005
  5. clayne
    Replies:
    4
    Views:
    263
    Jack Klein
    Jan 28, 2006
Loading...

Share This Page