cconventions

C Return Value Conventions (Bool or Err Code)


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?


Solution

  • 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: