I\'m coming back to some C development after working in C++ for a while. I\'ve gotten it into my head that macros should be avoided when not necessary in favor of making the com
Nowadays, in C++
there is no real good reason to use #define
for compile-time constants. On the other hand, there are good reasons to use enum
s or enum class
es instead. First and most important - they are much more readable during debugging.
In C
you may want to explicitly choose underlying type, which is impossible with enum
s. That might be a reason to use define
s or const
s. But enums should be strongly prefered.
Runtime overhead is not a problem - in modern compilers there won't be any difference in generated machine code (as long as sizeof(the_enum)=sizeof(the_type_used_by_define_based_values)
).