Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > portability and return

Reply
Thread Tools

portability and return

 
 
Bill Cunningham
Guest
Posts: n/a
 
      05-13-2008
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


 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      05-13-2008
In article <2YnWj.20544$5b3.14006@trnddc05>,
Bill Cunningham <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Peter Nilsson
Guest
Posts: n/a
 
      05-13-2008
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
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      05-13-2008

"Peter Nilsson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
Spiros Bousbouras
Guest
Posts: n/a
 
      05-13-2008
On 13 May, 22:57, "Bill Cunningham" <(E-Mail Removed)> wrote:
> "Peter Nilsson" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed)...
>
> > 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.


 
Reply With Quote
 
Harald van Dijk
Guest
Posts: n/a
 
      05-13-2008
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?
 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      05-13-2008
In article <79oWj.7860$Uz2.2677@trnddc06>,
Bill Cunningham <(E-Mail Removed)> 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
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      05-13-2008



"Harald van D?k" <(E-Mail Removed)> 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


 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      05-13-2008
Bill Cunningham wrote:
> "Peter Nilsson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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
 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      05-13-2008
In article <79oWj.7860$Uz2.2677@trnddc06>,
Bill Cunningham <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Casting return value to fit within constraints of error values (portability) clayne C Programming 4 01-28-2006 10:15 PM
Vonage Number Portability and peering agreements? Paul Westwell UK VOIP 0 07-16-2005 07:18 AM
Speed and portability of XML parsers Nikhil XML 2 05-04-2005 08:31 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
Number Assignment and Number Portability Brian Young VOIP 2 05-09-2004 03:11 PM



Advertisments