portability and return
  Home FAQ Contact Sign in
comp.lang.c only
 
Advanced search
POPULAR GROUPS

more...

comp.lang.c Profile…
 Up
portability and return         


Author: Bill Cunningham
Date: May 13, 2008 14:43

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
46 Comments
Re: portability and return         


Author: Walter Roberson
Date: May 13, 2008 14:47

In article <2YnWj.20544$5b3.14006@trnddc05>,
Bill Cunningham nspam.com> 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
no comments
Re: portability and return         


Author: Peter Nilsson
Date: May 13, 2008 14:49

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
no comments
Re: portability and return         


Author: Bill Cunningham
Date: May 13, 2008 14:57

"Peter Nilsson" acay.com.au> wrote in message
news:3222fc05-7aee-4eb3-b874-eb55981751dd@v26g2000prm.googlegroups.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
no comments
Re: portability and return         


Author: Spiros Bousbouras
Date: May 13, 2008 15:02

On 13 May, 22:57, "Bill Cunningham" nspam.com> wrote:
> "Peter Nilsson" acay.com.au> wrote in message
>
> news:3222fc05-7aee-4eb3-b874-eb55981751dd@v26g2000prm.googlegroups.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.
no comments
Re: portability and return         


Author: Harald van Dijk
Date: May 13, 2008 15:03

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?
no comments
Re: portability and return         


Author: Walter Roberson
Date: May 13, 2008 15:06

In article <79oWj.7860$Uz2.2677@trnddc06>,
Bill Cunningham nspam.com> 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
no comments
Re: portability and return         


Author: Bill Cunningham
Date: May 13, 2008 15:08

"Harald van D?k" gmail.com> 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
no comments
Re: portability and return         


Author: jacob navia
Date: May 13, 2008 15:09

Bill Cunningham wrote:
> "Peter Nilsson" acay.com.au> wrote in message
> news:3222fc05-7aee-4eb3-b874-eb55981751dd@v26g2000prm.googlegroups.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.
Show full article (1.07Kb)
no comments
Re: portability and return         


Author: Richard Tobin
Date: May 13, 2008 15:15

In article <79oWj.7860$Uz2.2677@trnddc06>,
Bill Cunningham nspam.com> 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.
Show full article (1.09Kb)
no comments

RELATED THREADS
SubjectArticles qty Group
Access 97 using ODBC returning SQL server ntext fields dont return the full datamicrosoft.public.sqlserver.odbc ·
1 2 3 4 5