4 ms·
> ꙮ I always use enum instead of #define for numeric constants. I'm new to C (and very much a self-taught amateur), but what's the advantage of this? (No snark
by telemachos 14y ago
> ꙮ I always use enum instead of #define for numeric constants.
I'm new to C (and very much a self-taught amateur), but what's the advantage of this? (No snark - I just don't see it.)
I have a file at hand with this:
#define MAXLINE 512
How would that be improved by putting it in an enum instead?
- kragen 14y agoThe advantage is smallest for things like that, which you'd put in a nameless enum. To my eyes, there's still a better signal-to-noise ratio in max_line = 512, than in #define MAXLINE 512 but I agree that the difference is small. In some cases, your code is more readable if you can define more than one constant per line, and the difference becomes larger: min_x = 20, min_y = 20, max_x = 8.5*72 - 20, max_y = 11*72 - 20, Beyond that, non-anonymous enums have the additional advantages that you can define them closer to where they're used, and the debugger knows how to print them.
- gosu 14y agothe debugger knows how to print them. I just want to emphasize this point, as this is the main advantage in my eyes.
- kragen 14y agoIt's a pretty big advantage when it applies, but it doesn't apply to the MAXLINE case, and it only applies when you're using a debugger. Which, for me, is very rarely.