I get this is probably a loaded question, but in C, is it better practice to return a int with error code, or a "success?" boolean. I notice that "0" for success seems convention in libc, but when working with windows.h, they tend to return "1" on success.
I prefer "0" on success because I can encode different errors, I know others who prefer "1" because of the boolean cast,
Is there any defined/prefered right way, or is it just random?
It all depends on if you are writing some low-level/generic stuff, in which case you can pretty much return anything including data, or if you are writing a high-level API to be used by others.
In case of the latter, there are indeed best design practices which are widely accepted, that go along the lines of this:
enum
or equivalent with a number of codes per library/API. The result type is typically declared along with the library header.bool
, 1
/0
or null pointers etc are not recommended for such high level code.errno
, GetLastError
or whatever it may be named is a big no-no. These are known to be very error prone, users forgetting to clear them etc. And they can potentially cause problems during multi-threading, even when access to the global resource is thread-safe in itself.