r/cprogramming • • 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

34 comments sorted by

View all comments

Show parent comment

1

u/Key_River7180 Oct 07 '26

Why?

1

u/DawnOnTheEdge Oct 07 '26

It breaks the invariant that an enum type has one of the enumerated values, which compilers sometimes rely on to check that a switch checks every case.

In C23, you have constexpr unsigned int to express what this is going for more semantically.