Advertisements

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

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

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 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.

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).

"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.

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.

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.

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.

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.

"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.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!