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

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


 
Reply With Quote
 
 
 
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      05-13-2008
jacob navia <(E-Mail Removed)> wrote:
> 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, 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://www.velocityreviews.com/forums/(E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
 
 
 
Bill Cunningham
Guest
Posts: n/a
 
      05-13-2008

"Jens Thoms Toerring" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)-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


 
Reply With Quote
 
Eligiusz Narutowicz
Guest
Posts: n/a
 
      05-14-2008
Mark McIntyre <(E-Mail Removed)> writes:

> Eligiusz Narutowicz wrote:
>> "Bill Cunningham" <(E-Mail Removed)> 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?
 
Reply With Quote
 
Eligiusz Narutowicz
Guest
Posts: n/a
 
      05-14-2008
Mark McIntyre <(E-Mail Removed)> writes:

> Eligiusz Narutowicz wrote:
>> Mark McIntyre <(E-Mail Removed)> writes:
>>
>>> Eligiusz Narutowicz wrote:
>>>> "Bill Cunningham" <(E-Mail Removed)> 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.

 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      05-14-2008
In article <g0f0fq$69o$(E-Mail Removed)>,
Eligiusz Narutowicz <(E-Mail Removed)> 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
 
Reply With Quote
 
Eligiusz Narutowicz
Guest
Posts: n/a
 
      05-14-2008
Mark McIntyre <(E-Mail Removed)> writes:

> Eligiusz Narutowicz wrote:
>> Mark McIntyre <(E-Mail Removed)> writes:
>>
>>> Eligiusz Narutowicz wrote:
>>>> Mark McIntyre <(E-Mail Removed)> 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.
 
Reply With Quote
 
soscpd@terra.com.br
Guest
Posts: n/a
 
      05-14-2008


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
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      05-14-2008
Bill Cunningham wrote:

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

 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      05-14-2008

"Eligiusz Narutowicz" <(E-Mail Removed)> wrote in message
news:g0f0fq$69o$(E-Mail Removed)...
> 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.


 
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