My program is written in C for Linux and has many functions with different patterns for return values:
1) one or two returns n on success and -1 on error.
2) some return 0 on success and -1 on failure.
3) some return 1 on success and 0 on failure (I usually reject using the boolean type).
4) pointers return 0 on failure (I usually reject using NULL ).
My confusion arises from the first three functions that the pointers return, always return 0 on failure, this is easy.
The first option usually includes functions that return a length that can only be positive.
The second option is usually associated with command line processing functions, but I'm not sure if it has the correctness, maybe higher values ββwill be EXIT_SUCCESS and EXIT_FAILURE?
The third option is for functions that are convenient and natural to call in conditions, and I usually emulate a Boolean type here, using int values ββ1 and 0.
Despite everything that seems reasonably reasonable, I still find areas where it is not so clear or obvious which style to use when I create a function, or which style to use when I want to use it.
So, how can I add clarity to my approach when choosing return types here?
c function linux return-value
James morris
source share