I am working on improving the code quality and portability of my C library, specifically a ring buffer implementation, that will be used in larger applications. I have encountered a dilemma regarding error code handling, and I would like to seek advice on industry best practices.
In some of my libraries, I currently use custom error codes defined as enums in the header files, like this:
typedef enum {
SUCCESS = 0,
INVALID_ARGUMENT = 1,
OUT_OF_MEMORY = 2,
BUFFER_OVERFLOW = 3,
UNINITIALIZED = 4,
} buffer_error_t;
On the other hand, I've noticed that other libraries strictly adhere to the error codes provided by errno.h and utilize functions like perror()
or strerror()
to obtain human-readable error messages.
Considering that the library is intended for use in larger applications, my goal is to maximize code readability, maintainability, and compatibility with existing practices.
What are the industry best practices or standards when it comes to error code handling in libraries? Are there any widely accepted guidelines or considerations that can help me make an informed decision between custom error codes and errno.h?