views:

146

answers:

4

Hi,

Currently I'm returning -1 in my custom functions in C if something wrong happens and 0 for success. For instance, working with a linked list and some function needs a non-empty list to work properly. If the list passed as argument is empty, I return -1 (error) and 0 if it's not empty and the function worked without a problem.

Should I, maybe, return 1 instead of -1?

Is this the standard way of doing things in C or do you recommend a different approach?

+7  A: 

Return a non-zero value to indicate failure. This way you can write you functions calls as so:

if(func_call())
{
    doErrorHandling();
}

This convention will allow you to use any !0 value to indicate a specific error, and this will allow you to use one variable in a uniform fashion. So the body of the if shown in the example above can then have a switch statement to process the specific errors.

You can do it differently -- but if you choose to do so stick to a convention -- the win32 API (and other API's I have used) mix and match conventions unfortunately.

Hassan Syed
If you don't have any specific error codes, you could always just return a bool.
Steve H.
+1 **Doesn't have to be positive**, though, 0 for success and !0 for error is all you need. In fact, in my experience it's more common for error codes to be negative.
T.J. Crowder
@Steve: In some languages/environments that would be appropriate, but not C. The 0 = success is so strongly ingrained in the C culture that you need to have a *really good* reason to do something else.
T.J. Crowder
@TJ.C I have updated the answer -- I was playing it safe :D
Hassan Syed
A: 

There are many schemes - but whatever you do, do it consistently!

If you don't have many failing conditions, just use 0 for error. That's because error tests are written in a simple way:

if (!list_insert(...)) { handle_error; }

Otherwise below-zero answers are good to use along with normal answers >= 0. You can use this for functions like list length, which in normal conditions will not be negative. Or if you want many error codes (-1 - nonexistant, -2 - not found, -3 ..., ...)

viraptor
Don't use 0 for error. C has a very, very strong history of 0 = success, !0 = failure. As Hassan Syed pointed out, the failure of the Windows API designers to recognize this fact has lead to no end of confusion.
T.J. Crowder
I don't see a big problem with either way. There are projects doing 0==failure and there are others doing 0==success. As long as it's documented and you keep to the same convention throughout the project - no way is "wrong". For me, it's easier to read - "if not list_insert", meaning list_insert failed. Unless you expect your functions to fail most of the time... Also Hassan pointed out that they mix and match different ways, (not that they use 0==error) and basically said the same thing I started with - whatever you do, stick to it.
viraptor
+2  A: 

That sounds fine. -1 is used in I/O function because a positive return value usually means success and is the number of bytes that were processed. If you have multiple ways a function can go wrong, then you can either return different integers or set a global error variable (errno is used by the standard library) to contain the error code.

In terms of style, I prefer not to return status codes as it means my functions can't (cleanly) return anything else. Instead I would check the input before calling the function. But this is subjective and depends on the context.

abc
A: 

I suggest that you find a way to format a fully-informative human-readable string at the point of the error, where all information is still available, and design a way to get it down thru the rest of the program, thru the user, his mobile phone, to your development team for the analysis.

This seems to me like an important feature of any design, if you want to produce better software faster. Killing error information on the way is a major crime, and C language is no excuse.

Pavel Radzivilovsky
Fully informative to whom - the end user, or the developer???And in what language???
Péter Török
Fully informative to the developer, of course. Read my answer again. Language: in English. Developers should understand english. Users/helpdesks should be able to try and forward it to the team.
Pavel Radzivilovsky