[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Return value for error

pozz

5/3/2011 8:31:00 AM

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");

but I have only one error code (0), while, in some cases, it could be
useful to have more informations about the type of error.

If !=0 is error I can have several error codes, but I have to write

if (!myfuction()) printf("OK\n");

that apparently seems myfunction has not terminated with success.

Another strategy is to define custom error code:

#define ERROR_OK 0
#define ERROR_UNDEFINED 1
#define ERROR_WRONGVALUE 2

and so on, and always write:

if (myfuction() == ERROR_OK) printf("OK\n");
30 Answers

jacob navia

5/3/2011 8:47:00 AM

0

Le 03/05/11 10:31, pozz a écrit :
> 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");
>
> but I have only one error code (0), while, in some cases, it could be
> useful to have more informations about the type of error.

Yes... That is the reason that option is not really useful.

>
> If !=0 is error I can have several error codes, but I have to write
>
> if (!myfuction()) printf("OK\n");
>
> that apparently seems myfunction has not terminated with success.
>

I would recommend
if (myfunction() == SUCCESS)

where SUCCESS is zero

> Another strategy is to define custom error code:
>
> #define ERROR_OK 0
> #define ERROR_UNDEFINED 1
> #define ERROR_WRONGVALUE 2
>
> and so on, and always write:
>
> if (myfuction() == ERROR_OK) printf("OK\n");

The problem with "ERROR_OK" is that it is contradictory: An "OK error" ?

In the C containers library I decided (after much pondering) that:

Positive value: No error

Negative value: error code

Zero: Warning but OK.

For instance a function like POP(container) will return

< 0 if container == NULL. This is a hard error.
Zero: container was empty. Maybe this isn't an error.

>0 one element was popped. This means everything is OK.

if (Pop(container) < 0) {
// error handling
}

Note that you can code other useful information in positive values.
You could return the number of elements still remaining in the stack
for instance.

Ike Naar

5/3/2011 9:04:00 AM

0

On 2011-05-03, pozz <pozzugno@gmail.com> 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");
>
> but I have only one error code (0), while, in some cases, it could be
> useful to have more informations about the type of error.
>
> If !=0 is error I can have several error codes, but I have to write
>
> if (!myfuction()) printf("OK\n");
>
> that apparently seems myfunction has not terminated with success.
>
> Another strategy is to define custom error code:
>
> #define ERROR_OK 0
> #define ERROR_UNDEFINED 1
> #define ERROR_WRONGVALUE 2
>
> and so on, and always write:
>
> if (myfuction() == ERROR_OK) printf("OK\n");

Another improvement would be to drop the ``ERROR'' prefix from
the constants (``ERROR_OK'' is a contradictio in terminis, isn't it?)
and replace it with a prefix that indicates that the constant has to do
with a particular function.
This helps if your program has several functions that follow the same
strategy.

#define FROBOZZ_OK 0
#define FROBOZZ_UNDEFINED 1
#define FROBOZZ_WRONGVALUE 2

#define WHACK_OK 0
#define WHACK_UNWHACKABLE 1
#define WHACK_ALREADYWHACKED 2

if (frobozz() == FROBOZZ_OK) printf("OK\n");
if (whack() == WHACK_OK) printf("OK\n");

Ian Collins

5/3/2011 9:19:00 AM

0

On 05/ 3/11 09:04 PM, Ike Naar wrote:
> On 2011-05-03, pozz<pozzugno@gmail.com> 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");
>>
>> but I have only one error code (0), while, in some cases, it could be
>> useful to have more informations about the type of error.
>>
>> If !=0 is error I can have several error codes, but I have to write
>>
>> if (!myfuction()) printf("OK\n");
>>
>> that apparently seems myfunction has not terminated with success.
>>
>> Another strategy is to define custom error code:
>>
>> #define ERROR_OK 0
>> #define ERROR_UNDEFINED 1
>> #define ERROR_WRONGVALUE 2
>>
>> and so on, and always write:
>>
>> if (myfuction() == ERROR_OK) printf("OK\n");
>
> Another improvement would be to drop the ``ERROR'' prefix from
> the constants (``ERROR_OK'' is a contradictio in terminis, isn't it?)
> and replace it with a prefix that indicates that the constant has to do
> with a particular function.
> This helps if your program has several functions that follow the same
> strategy.
>
> #define FROBOZZ_OK 0
> #define FROBOZZ_UNDEFINED 1
> #define FROBOZZ_WRONGVALUE 2

I prefer an enum to #defines. It's more debugger friendly, less typing
and gives you a type to return.

--
Ian Collins

James Kuyper

5/3/2011 10:26:00 AM

0

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().
However, there's also lots of functions in the C standard library which
violate this rule; for instance, fseek().

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

....
> Another strategy is to define custom error code:
>
> #define ERROR_OK 0
> #define ERROR_UNDEFINED 1
> #define ERROR_WRONGVALUE 2
>
> and so on, and always write:
>
> if (myfuction() == ERROR_OK) printf("OK\n");

If your code returns multiple different error codes, then somewhere you
should have a call to the function that actually does different things,
depending upon which error code is returned; a switch() is usually the
best way to do this. You can also have calls that don't make a
distinction; but if there aren't any calls to the function which do make
the distinction, then you don't really need to distinguish different
error conditions.

I'm guilty of violating that rule in my own code; but my intention is
that test drivers for the called routine will check which error code is
returned, even if the actual delivered program does not.
--
James Kuyper

James Dow Allen

5/3/2011 10:46:00 AM

0

As James Kuyper says, best is to have the return value
implicit in the function name:
is_kosher(this.soup) && do_serve(&this);
or
dad = find_father();
if (dad == NULL) {
/* process orphan */
}

Letting zero represent incorrect/error is often good enough:
many simple programs will handle all error returns from a
simple function the same, so a single error "reason" is enough.

James

pete

5/3/2011 11:50:00 AM

0

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.

When there may be several various error return codes,
then use 0 for success.

When there are *not* several various error return codes,
such as in the isspace function
and the the other <ctype.h> functions,
then use 0 for failure.

--
pete

Martin Ambuhl

5/3/2011 11:59:00 AM

0

On 5/3/2011 4:31 AM, pozz wrote:

> Another strategy is to define custom error code:
>
> #define ERROR_OK 0
> #define ERROR_UNDEFINED 1
> #define ERROR_WRONGVALUE 2

But not with those names. These names are reserved for the implementation.

ram

5/3/2011 1:16:00 PM

0

pozz <pozzugno@gmail.com> writes:
>I can't decide between 0 for error and !=0 for ok, or 0 for ok and !=0
>for errors.

0 means »no error«, since there is only one way to
succeed, but many ways to fail.

>if (!myfuction()) printf("OK\n");

Yes, the »!« looks ugly there, but often one wants to handle
the error in some way:

if( int const code = alpha() )error( code ); else printf ...

(I know that one cannot define variables in the
if expression, but it would be nice.)

Mark Storkamp

5/3/2011 1:46:00 PM

0

In article <error-codes-20110503151314@ram.dialup.fu-berlin.de>,
ram@zedat.fu-berlin.de (Stefan Ram) wrote:

> pozz <pozzugno@gmail.com> writes:
> >I can't decide between 0 for error and !=0 for ok, or 0 for ok and !=0
> >for errors.
>
> 0 means »no error«, since there is only one way to
> succeed, but many ways to fail.
>
> >if (!myfuction()) printf("OK\n");
>
> Yes, the »!« looks ugly there, but often one wants to handle
> the error in some way:

To steal a page from BCPL you can always do something like this:

#define unless(c) if(!(c))

Then you have

unless (myfunction()) printf("OK\n");

>
> if( int const code = alpha() )error( code ); else printf ...
>
> (I know that one cannot define variables in the
> if expression, but it would be nice.)

Stephen Sprunk

5/3/2011 3:14:00 PM

0

On 03-May-11 03:31, 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.

General rules:

1. If one success state and many error states, let 0 indicate success.

2. If one error state and many success states, let 0 indicate error.

3. If many success states and many error states, let negative indicate
error and non-negative indicate success.

> If !=0 is ok, I can write:
>
> if (myfunction()) printf("OK\n");
>
> but I have only one error code (0), while, in some cases, it could be
> useful to have more informations about the type of error.

This is usually the case.

> If !=0 is error I can have several error codes, but I have to write
>
> if (!myfuction()) printf("OK\n");
>
> that apparently seems myfunction has not terminated with success.

You could consider rewriting that line as:

if (myfuction()==0) printf("OK\n");

That form has the same meaning to the compiler, but some folks feel it
conveys a different/better meaning to humans. This frequently comes up
in debates about strcmp() et al, for example.

S

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking