In C, it is best to use enumerations for actual enumerations: when a variable can contain one of several values ββto which names can be assigned. One of the advantages of enums is that the compiler can perform some checks, in addition to what the language requires, for example, that the switch statement for an enum type does not skip one of these cases. Enumeration identifiers also apply to debug information. In the debugger, you can see the identifier name as the value of the enum variable, and not just a numerical value.
Enumerations can only be used as a side effect of creating symbolic constants of an integral type. For example:
enum { buffer_size = 4096 };
this practice is not so widespread. First, buffer_size will be used as an integer, not an enumerated type. The debugger will not map 4096 to buffer_size because this value will not be represented as an enumerated type. If you declare multiple char array[max_buffer_size]; , then the sizeof array will not display as buffer_size . In this situation, the enumeration constant disappears at compile time, so it can also be a macro. And there are flaws, such as the inability to control its exact type. (In some situations, there may be some slight advantage when the output of the translation preprocessing stages is fixed as text. The macro will turn into 4096, while buffer_size will remain as buffer_size ).
The preprocessor symbol allows us to do this:
#define buffer_size 0L
Note that various values ββfrom C <limits.h> , such as UINT_MAX , are preprocessor characters, not enumerated characters, which is a good reason for this, since these identifiers must be of a precisely defined type. Another advantage of the preprocessor symbol is that we can check its presence or even make decisions based on its value:
#if ULONG_MAX > UINT_MAX #endif
Of course, we can also check the constants listed, but not so that we can modify global declarations based on the result.
Enumerations are also poorly suited for bitmaxes:
enum modem_control { mc_dsr = 0x1, mc_dtr = 0x2, mc_rts = 0x4, ... }
it just doesn't make sense, because when values ββare combined with a bitwise OR, they produce a value that is outside the type. Such code also causes a headache if it is ported to C ++, which has (somewhat more) type numbering.