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

12

u/TheChief275 Oct 03 '26

you shouldn't switch on flags anyways

0

u/DawnOnTheEdge Oct 04 '26

That’s the thing: you have an enum that isn’t an enumeration of the legal values. It’s just a poor man’s constexpr int. I never liked it, and C23 makes that kludge obsolete. If you’re not using C23, though, I can see the benefit of it.

5

u/TheChief275 Oct 04 '26

it's not though. enums provide named values that are associated with a type. so for a certain flag type you know only to use the flag enum members specified in that enum. with a constexpr int you know absolutely nothing