portability and return

B

Bill Cunningham

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
 
W

Walter Roberson

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

Peter Nilsson

Bill said:
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.
 
B

Bill Cunningham

Peter Nilsson said:
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
 
S

Spiros Bousbouras

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

Harald van Dijk

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?
 
W

Walter Roberson

Bill Cunningham said:
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.
 
B

Bill Cunningham

Harald van D?k said:
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
 
J

jacob navia

Bill said:
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.
 
R

Richard Tobin

Bill Cunningham said:
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
 
B

Bill Cunningham

Richard Tobin said:
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
 
J

Jens Thoms Toerring

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
 
B

Bill Cunningham

Jens Thoms Toerring said:
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
 
E

Eligiusz Narutowicz

Mark McIntyre said:
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?
 
E

Eligiusz Narutowicz

Mark McIntyre said:
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.
 
R

Richard Tobin

Eligiusz Narutowicz said:
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
 
E

Eligiusz Narutowicz

Mark McIntyre said:
Good. Then perhaps you could consider how on earth you can possibly
know what is possible and impossible for someone you've never met?


*shrug*. It wasn't rude, but if you want to consider it so that's your
prerogative.


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

soscpd

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
 
S

santosh

Bill said:
Richard Tobin said:
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.
 
B

Bill Cunningham

Eligiusz Narutowicz said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top