r/cprogramming • u/Upper-Tomatillo7454 • Oct 03 '26
Enums vs #define for constants
Hi 👋 guys, I've just started learning C after I was first introduced to it while I was in my first year of computer science but I'll be graduating by the end of this year and I've been primarily using Python since then but now I want to really learn C on the side, and I want to start with "craftinginterpreters" and I want to start small so I was wondering whether I should use C enums or #define to define a set of tokens.
17
Upvotes
23
u/pjl1967 Oct 03 '26
If you want a set of related constants, use
enum. If you're using C23, useconstexpr. Otherwise#define.Details here.
19
u/hascalsavagejr Oct 03 '26
defines never reach the compiler; they are replaced in-line and care must be taken to be sure they work properly. Enums are properly integrated into the language... any modern enough one, anyway
6
u/logic_circuit Oct 04 '26
I use precompiler #define always. Mater of habit after 30 yrs. They may be problem with type conversions but i like the elegance of working with them.
5
u/lfdfq Oct 03 '26
Probably, enum.
Sometimes you really care about the numeric value of the constant, e.g. if it has some particular meaning. In those cases enum has some disadvantages, namely that the value must be a signed int, and so you'll find people use #define for those.
4
u/Ander292 Oct 03 '26
I myself would advise using enums because they might be cleaner to use but its the same thing.
4
u/theNbomr Oct 03 '26
For me, it's mostly about the value of using the ordered nature of the symbols and the simpler maintenance if you do make use of the order of the keys. If you don't, then preprocessor macros are probably better.
4
u/Chippors Oct 04 '26
I use enums, at least used to. The reason is they produce debug information, so the debugger recognizes them. Although constexpr is better IMO.
3
u/Eastern_Type6942 Oct 03 '26
enums, one you're defining 'a set of tokens' - enums lend themselves well to this. Two, when debugging you will have the symbol for the enum in use - same cannot be said for using a macro.
3
u/duane11583 Oct 03 '26
I prefer defines
If I use enums I put a value with every enum I never agree that the next one is plus1 reasonÂ
explaining to a hardware person what the value is not fun so defines always have the value and by my rules enums do too
 using enums for register bits is dumb I have seen it done too many timesÂ
I also often need to enforce an enums to be 32 bits using a standard uint32_t forces that
3
u/kishaloy Oct 04 '26
Nominal typing vs Structural typing.
First one is a contract, second one a suggestion though both may compile away to a machine int.
1
u/Sorry_Difficulty_250 Oct 04 '26
OK, here's the problem I've started running into: enums cannot be declared with forward declarations. That means if you have one header that can't include the one where the enum is defined, it can't define anything with the enum either because you can't re-declare enums in C.
To solve this, I've stared doing this:
typedef uint8_t MyEnumType;And then I define the values as things like
#define MY_ENUM_VALUE ((MyEnumType) 1)That way, in other headers that need the enum but can't include the header, I can do the uint8_t typedef again and keep things sane. The #define with the type cast allows me to keep my sanity with type checking at the compiler level.
Just my $0.02 for the problem I've faced lately. Your mileage may vary...
1
u/Key_River7180 Oct 07 '26
For tokens enum, for spare constants use #define or const
1
u/actguru Oct 07 '26
I prefer enums because you can generate them with macros.
0
u/Abrissbirne66 Oct 04 '26
I think define existed first and enums were added later and are probably the better option for these use cases.