18

So an enum works like this:

enum {
  false,
  true
}

which is equivalent to

int false = 0
int true = 1

Why wouldn't I substitute enum with #define?

#define FALSE 0
#define TRUE 1

To me, it seems like they are interchangeable. I'm aware that #define is able to handle arguments, hence operates in an entirely different way than enum. What exactly are the main uses of enum when we have #define in this case?

If I were to guess, as the #define is a preprocessor feature, enum would have some runtime advantages. How far off am I?

Thanks in advance.

userenum
  • 191
  • 1
  • 1
  • 4
  • 1
    see http://stackoverflow.com/questions/136946/difference-between-enum-and-define-statements – SirDarius Mar 09 '11 at 08:56
  • Odd, that one didn't appear within my search results. Thanks! – userenum Mar 09 '11 at 09:02
  • 1
    It is not equivalent to `int false = 0` nor to `int const false = 0` (which would be more appropriate). `int` variables would have an address, you could then do `&false`, which you can't for an `enum`. – Jens Gustedt Mar 09 '11 at 10:40
  • Note that [SO 136946](http://stackoverflow.com/q/136946/) is tagged with both C++ and C. Nominally, it is not as good a choice of duplicate as SO 1674032 which is for C only, but I could be persuaded otherwise. – Jonathan Leffler Nov 30 '15 at 23:47
  • 1
    It's funny that I found this question whilst wondering on exact the opposite: why are C programmers using `#defines`s instead of `enum`s. – Hi-Angel Nov 17 '18 at 09:08

4 Answers4

33

The advantages of enum show up when you have a long list of things you want to map into numbers, and you want to be able to insert something in the middle of that list. For example, you have:

pears 0
apples 1
oranges 2
grapes 3
peaches 4
apricots 5

Now you want to put tangerines after oranges. With #defines, you'd have to redefine the numbers of grapes, peaches, and apricots. Using enum, it would happen automatically. Yes, this is a contrived example, but hopefully it gives you the idea.

Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
jcomeau_ictx
  • 37,688
  • 6
  • 92
  • 107
  • 1
    Yes, I see the difference now. Thanks! – userenum Mar 09 '11 at 09:06
  • 5
    This is a good answer. **But assumes** the reader (who could be any NOOB) knows that she can declare an `enum` with `pears 0` and NOT number the rest. – gideon Nov 25 '14 at 03:04
18

I find it useful for debugging in an environment such as gdb since enum values are handled at compile time (where #define is a preprocessor macro) and thus available for introspection.

mduvall
  • 1,018
  • 1
  • 8
  • 15
5

Although your question is tagged as C, there is a big advantage when writing in C++, you can place enum:s inside classes or namespaces.

This way you could refer to your constants like SpaceshipClass::galaxy.

Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
Lindydancer
  • 25,428
  • 4
  • 49
  • 68
0

enum is an integer constant. so, there would be a type check during compilation process.