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?
Return a non-zero value to indicate failure. This way you can write you functions calls as so:
This convention will allow you to use any
!0value to indicate a specific error, and this will allow you to use one variable in a uniform fashion. So the body of theifshown 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.