Why do libraries such as GLFW define constants with hexadecimal values instead of using an enum? For example:
#define GLFW_NO_ERROR 0
#define GLFW_NOT_INITIALIZED 0x00010001
#define GLFW_NO_CURRENT_CONTEXT 0x00010002
#define GLFW_INVALID_ENUM 0x00010003
The constants are used like enum values when checking errors, such as `if (code == GLFW_NOT_INITIALIZED)`. Is hexadecimal being used for a specific technical reason, or is it mainly a convention? Would an enum provide any advantages here?
4 Answers
If the values are simply distinct labels, decimal, hexadecimal, and an enum would all work in comparisons. Hex becomes especially useful when the value is intended as a bit flag or bit field. So for these GLFW errors, the exact reason is likely API design and stable numeric assignments, while the hexadecimal format also makes the deliberate bit layout easier to inspect.
The hexadecimal notation itself does not change the value; it is still just an integer. Hex is convenient because each digit corresponds to four binary bits, making bit patterns, masks, and grouped fields easier to recognize. In this example, the shared `0x0001` prefix may be intentional structure, such as reserving bits for an error category or leaving room for future values.
The numeric values may also be part of an external or long-lived API contract. Once applications and language bindings depend on particular numbers, changing them can cause compatibility problems. Named constants let users write readable code while preserving stable values for error handling and interoperability.
A C enum could represent fixed values like these, but it has some limitations. C enums are ultimately integer values, and the language gives implementations flexibility over their underlying integer type. A library may prefer preprocessor constants so its public API has predictable integer values and works consistently across C, C++, bindings, headers, and other languages.

Related Questions
How To: Running Codex CLI on Windows with Azure OpenAI
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically