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-14-2008

[snip]

"Eligiusz Narutowicz" <(E-Mail Removed)> wrote in message
news:g0f69i$8u4$(E-Mail Removed)...

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


Brackets and scope? Yes I understand that. What does that have to do with
anything? It's "implmentations" and "portability" that was the question. And
I never expected this thread to become this large. I thought 2-4 responses
would be made. I am grateful to those who have answered my question(s).

[snip]



 
Reply With Quote
 
 
 
 
Bart
Guest
Posts: n/a
 
      05-14-2008
On May 14, 10:03*pm, "Bill Cunningham" <(E-Mail Removed)> wrote:
> "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?

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


This my approach to return in main():

(1) Completely ignore it
(2) If you /have/ to return a value, return anything (really, mostly
it doesn't matter)
(3) If someone/something complains, or it /does/ matter, return one of
EXIT_SUCCESS or EXIT_FAILURE, but those stick out like a sore thumb,
ruining the lines of my code (I don't like capitals much)

Your tutorial is taking the attitude that it's of little practical
importance to someone learning the language, which sounds right. There
are many more interesting things to worry about!

--
Bartc
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      05-14-2008
"Bill Cunningham" <(E-Mail Removed)> writes:
> "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.

[...]

I think your question was already answered, but I'll summarize.

A program can return a status code (of type int) to the host
environment (as it terminates) either by calling exit() with the
desired status as an argument, or by executing a return statement
within main(), returning the desired status. These two are
essentially equivalent. exit() can be called from anywhere, not just
from main().

The only portable status codes are 0, EXIT_SUCCESS, and EXIT_FAILURE.
Other values can be useful, but only in code intended for a particular
system.

Results returned by functions other than main() generally have
*nothing* to do with program status codes, so the advice to use 0,
EXIT_SUCCESS, or EXIT_FAILURE does not apply. The value you should
return from main() (or, equivalently, the value you should pass to
exit() is constrained by the host environment. The value you should
return from any other function is constrained by the requirements for
that function, something internal to your program.

Please feel free to ignore the following paragraph.

Returning a value from main and calling exit() with that same value
are *almost* equivalent. They can differ in an obscure and unlikely
scenario involving the atexit() function. They can also differ if you
call main() recursively, something that there's practically never a
good reason to do.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-14-2008
Bart <(E-Mail Removed)> writes:
[...]
> This my approach to return in main():
>
> (1) Completely ignore it
> (2) If you /have/ to return a value, return anything (really, mostly
> it doesn't matter)
> (3) If someone/something complains, or it /does/ matter, return one of
> EXIT_SUCCESS or EXIT_FAILURE, but those stick out like a sore thumb,
> ruining the lines of my code (I don't like capitals much)


Here's a better approach.

(1) Add ``return 0;'' immediately before the closing brace of your
main() function. Just think of it as part of the syntax.
(2) If you need to indicate failure, use EXIT_FAILURE. That's what
it's for. If you don't like capital letters, get over it.
(3) Consider using other values only if you need system-specific
status information.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      05-14-2008
Bart wrote:
> (3) If someone/something complains, or it /does/ matter, return one of
> EXIT_SUCCESS or EXIT_FAILURE, but those stick out like a sore thumb,
> ruining the lines of my code (I don't like capitals much)


#ifdef bart_source
#define exit_failure EXIT_FAILURE
#define exit_success EXIT_SUCCESS
#endif



--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      05-14-2008

"jacob navia" <(E-Mail Removed)> wrote in message
news:482b727a$0$932$(E-Mail Removed)...
> Bart wrote:
>> (3) If someone/something complains, or it /does/ matter, return one of
>> EXIT_SUCCESS or EXIT_FAILURE, but those stick out like a sore thumb,
>> ruining the lines of my code (I don't like capitals much)

>
> #ifdef bart_source
> #define exit_failure EXIT_FAILURE
> #define exit_success EXIT_SUCCESS
> #endif


I don't like caps either. I think I would use the preprocessor
directives like this.

#define FIL exit(EXIT_FAILURE);

On that exit macro.

Bill


 
Reply With Quote
 
Bart
Guest
Posts: n/a
 
      05-14-2008
On May 15, 12:15*am, jacob navia <(E-Mail Removed)> wrote:
> Bart wrote:
> > (3) If someone/something complains, or it /does/ matter, return one of
> > EXIT_SUCCESS or EXIT_FAILURE, but those stick out like a sore thumb,
> > ruining the lines of my code (I don't like capitals much)

>
> #ifdef bart_source
> #define exit_failure EXIT_FAILURE
> #define exit_success EXIT_SUCCESS
> #endif


Yes, I know, there's all sorts of little things I can add to enhance
my C programming experience.

But in the end this is just creating my own slightly different (or
possibly very different) version of the language. Which is great,
until I have to post code to this group for example.

--
Bartc
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-15-2008
"Bill Cunningham" <(E-Mail Removed)> writes:
> "jacob navia" <(E-Mail Removed)> wrote in message
> news:482b727a$0$932$(E-Mail Removed)...
>> Bart wrote:
>>> (3) If someone/something complains, or it /does/ matter, return one of
>>> EXIT_SUCCESS or EXIT_FAILURE, but those stick out like a sore thumb,
>>> ruining the lines of my code (I don't like capitals much)

>>
>> #ifdef bart_source
>> #define exit_failure EXIT_FAILURE
>> #define exit_success EXIT_SUCCESS
>> #endif

>
> I don't like caps either. I think I would use the preprocessor
> directives like this.
>
> #define FIL exit(EXIT_FAILURE);
>
> On that exit macro.


That's not nearly as good an idea as it might aeppar to be.

Everybody knows what ``exit(EXIT_FAILURE);'' means. Nobody but you
knows what ``FIL'' means. Think about the people who are going to be
reading your code.

To quote Henry Spencer's Ten Commandments for C Programmers, "... thy
creativity is better used in solving problems than in creating
beautiful new impediments to understanding".

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      05-15-2008
On 14 May, 20:07, "(E-Mail Removed)" <(E-Mail Removed)> wrote:

<snip>

>
> Maybe one should create a new group called
> comp.moderated.only.and.just.only.std.and.wiz.c


why don't you create it if you think there is a demand?
Presumably you'd be willing to moderate it.

--
Nick Keighley
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      05-15-2008
On 15 May, 00:57, Bart <(E-Mail Removed)> wrote:
> On May 15, 12:15*am, jacob navia <(E-Mail Removed)> wrote:
>
> > Bart wrote:
> > > (3) If someone/something complains, or it /does/ matter, return one of
> > > EXIT_SUCCESS or EXIT_FAILURE, but those stick out like a sore thumb,
> > > ruining the lines of my code (I don't like capitals much)

>
> > #ifdef bart_source
> > #define exit_failure EXIT_FAILURE
> > #define exit_success EXIT_SUCCESS
> > #endif

>
> Yes, I know, there's all sorts of little things I can add to enhance
> my C programming experience.
>
> But in the end this is just creating my own slightly different (or
> possibly very different) version of the language. Which is great,
> until I have to post code to this group for example.


so why recomend non-standard programming practices like
"return anything you like it doesn't matter"?

--
Nick Keighley

 
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