Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #3186

Re: Return value for error

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Return value for error
Date 2011-05-03 12:03 -0700
Organization None to speak of
Message-ID <lnaaf3vc77.fsf@nuthaus.mib.org> (permalink)
References <e63ceea9-301d-4b90-9495-4e2a8085fae5@x1g2000yqb.googlegroups.com> <ipol8c$ohd$1@dont-email.me>

Show all headers | View raw


James Kuyper <jameskuyper@verizon.net> writes:
> On 05/03/2011 04:31 AM, pozz wrote:
>> A very simple question. What do you use as the return value of a
>> function for error/ok results?
>> I can't decide between 0 for error and !=0 for ok, or 0 for ok and !=0
>> for errors.
>> 
>> If !=0 is ok, I can write:
>> 
>>   if (myfunction()) printf("OK\n");
>
> This is a perfectly reasonable approach, but when you use it, one of the
> most important things is to make sure to name the function so that it
> can be read as a question, and give it a non-zero value if the answer to
> that question is "yes". Example from the C standard library: isalpha().

Right.  The important distinction here is that the result of isalpha()
is purely a yes/no result, *not* a success/failure result.  If isalpha()
could tell you either
    Yes, that's an alphabetic character
or
    No, that's not an alphabetic character
or
    Oops, I can't tell what that is
the it would need a different interface.  In fact, none of the is*()
functions have any way to indicate failure.

> However, there's also lots of functions in the C standard library which
> violate this rule; for instance, fseek().

I wouldn't say fseek() violates the rule, it just follows a different
one.  It attempts to perform an action (seeking to the specified
position) and returns a result indicating either that it succeeded, or
that it failed.  The common convention is to use 0 for success, non-0
for failure.  (It doesn't use different non-0 values to denote different
kinds of failure.)

> If you can't think of a name for the function which would make this
> work, then using this approach is probably not appropriate.

Agreed.  But even if the function answers a yes/no question, you still
have to think about cases where the question can't be answered.  Most
yes/no functions in the standard library don't have ways to indicate
errors (see is*(), feof(), ferror()).

[...]

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <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"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Return value for error pozz <pozzugno@gmail.com> - 2011-05-03 01:31 -0700
  Re: Return value for error jacob navia <jacob@spamsink.net> - 2011-05-03 10:46 +0200
  Re: Return value for error Ike Naar <ike@otaku.freeshell.org> - 2011-05-03 09:04 +0000
    Re: Return value for error Ian Collins <ian-news@hotmail.com> - 2011-05-03 21:19 +1200
  Re: Return value for error James Kuyper <jameskuyper@verizon.net> - 2011-05-03 06:26 -0400
    Re: Return value for error Keith Thompson <kst-u@mib.org> - 2011-05-03 12:03 -0700
      Re: Return value for error "Kleuskes & Moos" <kleuske@xs4all.nl> - 2011-05-04 05:15 -0700
        Re: Return value for error Keith Thompson <kst-u@mib.org> - 2011-05-04 08:26 -0700
          Re: Return value for error "Kleuskes & Moos" <kleuske@xs4all.nl> - 2011-05-04 17:08 -0700
            Re: Return value for error Keith Thompson <kst-u@mib.org> - 2011-05-04 17:54 -0700
              Re: Return value for error "Kleuskes & Moos" <kleuske@xs4all.nl> - 2011-05-05 12:03 -0700
  Re: Return value for error James Dow Allen <jdallen2000@yahoo.com> - 2011-05-03 03:45 -0700
    Re: Return value for error Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-05-03 20:50 +0000
  Re: Return value for error pete <pfiland@mindspring.com> - 2011-05-03 07:50 -0400
    Re: Return value for error Keith Thompson <kst-u@mib.org> - 2011-05-03 12:04 -0700
  Re: Return value for error Martin Ambuhl <mambuhl@earthlink.net> - 2011-05-03 07:59 -0400
  Re: Return value for error Mark Storkamp <mstorkamp@yahoo.com> - 2011-05-03 08:46 -0500
  Re: Return value for error Stephen Sprunk <stephen@sprunk.org> - 2011-05-03 10:13 -0500
    Re: Return value for error William Ahern <william@wilbur.25thandClement.com> - 2011-05-03 13:08 -0700
  Re: Return value for error Kenneth Brody <kenbrody@spamcop.net> - 2011-05-03 13:23 -0400
    Re: Return value for error Peter Nilsson <airia@acay.com.au> - 2011-05-03 16:01 -0700
    Re: Return value for error Nobody <nobody@nowhere.com> - 2011-05-05 07:21 +0100
      Re: Return value for error Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-05-05 15:13 +0100
        Re: Return value for error Nobody <nobody@nowhere.com> - 2011-05-06 03:51 +0100
          Re: Return value for error James Kuyper <jameskuyper@verizon.net> - 2011-05-05 23:38 -0400
      Re: Return value for error Keith Thompson <kst-u@mib.org> - 2011-05-05 08:44 -0700
        Re: Return value for error Seebs <usenet-nospam@seebs.net> - 2011-05-05 20:49 +0000
          Re: Return value for error Keith Thompson <kst-u@mib.org> - 2011-05-05 14:10 -0700
            Re: Return value for error Seebs <usenet-nospam@seebs.net> - 2011-05-06 00:26 +0000
  Re: Return value for error Keith Thompson <kst-u@mib.org> - 2011-05-03 12:09 -0700

csiph-web