views:

3392

answers:

15

What's the difference between using a define statement and an enum statement in C/C++? (and is there any difference when using them with either C or C++?)

For example, when should one use

enum {BUFFER = 1234}; 

over

#define BUFFER 1234   

Thanks

+21  A: 

enum defines a syntactical element.

#define is a pre-preprocessor directive, executed before the compiler sees the code, and therefore is not a language element of C itself.

Generally enums are preferred as they are type-safe and more easily discoverable. Defines are harder to locate and can have complex behavior, for example one piece of code can redefine a #define made by another. This can be hard to track down.

Jason Cohen
You can also put enums in namespaces whereas macros can't be.
Greg Rogers
+6  A: 

define is a preprocessor command, it's just like doing "replace all" in your editor, it can replace a string with another and then compile the result.

enum is a special case of type, for example, if you write:

enum ERROR_TYPES
{
   REGULAR_ERR =1,
   OK =0
}

there exists a new type called ERROR_TYPES. it is true that REGULAR_ERR yields to 1 but casting from this type to int should produce a casting warning (if you configure you're compiler to high verbosity).

summary: they are both alike, but when using enum you profit the type checking and by using defines you simply replace code strings.

kiwi
"casting from this type to int" is back to front, I think; C++ treats enums as specializations of int.
Simon Buchan
+1  A: 

If you have a group of constants (like "Days of the Week") enums would be preferable, because it shows that they are grouped; and, as Jason said, they are type-safe. If it's a global constant (like version number), that's more what you'd use a #define for; although this is the subject of a lot of debate.

Smashery
+8  A: 

#define statements are handled by the pre-processor before the compiler gets to see the code so it's basically a text substitution (it's actually a little more intelligent with the use of parameters and such).

Enumerations are part of the C language itself and have the following advantages.

1/ They may have type and the compiler can type-check them.

2/ Since they are available to the compiler, symbol information on them can be passed through to the debugger, making debugging easier.

paxdiablo
+4  A: 

Enums are generally prefered over #define wherever it makes sense to use an enum:

  • Debuggers can show you the symbolic name of an enums value ("openType: OpenExisting", rather than "openType: 2"
  • You get a bit more protection from name clashes, but this isn't as bad as it was (most compilers warn about re#defineition.

The biggest difference is that you can use enums as types:

// Yeah, dumb example
enum OpenType {
    OpenExisting,
    OpenOrCreate,
    Truncate
};

void OpenFile(const char* filename, OpenType openType, int bufferSize);

This gives you type-checking of parameters (you can't mix up openType and bufferSize as easily), and makes it easy to find what values are valid, making your interfaces much easier to use. Some IDEs can even give you intellisense code completion!

Simon Buchan
+1  A: 

In addition to the good points listed above, you can limit the scope of enums to a class, struct or namespace. Personally, I like to have the minimum number of relevent symbols in scope at any one time which is another reason for using enums rather than #defines.

Shane MacLaughlin
+1  A: 

Another advantage of an enum over a list of defines is that compilers (gcc at least) can generate a warning when not all values are checked in a switch statement. For example:

enum {
    STATE_ONE,
    STATE_TWO,
    STATE_THREE
};

...

switch (state) {
case STATE_ONE:
    handle_state_one();
    break;
case STATE_TWO:
    handle_state_two();
    break;
};

In the previous code, the compiler is able to generate a warning that not all values of the enum are handled in the switch. If the states were done as #define's, this would not be the case.

Russell Bryant
+1  A: 

It's always better to use an enum if possible. Using an enum gives the compiler more information about your source code, a preprocessor define is never seen by the compiler and thus carries less information.

For implementing e.g. a bunch of modes, using an enum makes it possible for the compiler to catch missing case-statements in a switch, for instance.

unwind
+1  A: 

enums are more used for enumerating some kind of set, like days in a week. If you need just one constant number, const int (or double etc.) would be definetly better than enum. I personally do not like #define (at least not for the definition of some constants) because it does not give me type safety, but you can of course use it if it suits you better.

PeterK
+2  A: 

If you only want this single constant (say for buffersize) then I would not use an enum, but a define. I would use enums for stuff like return values (that mean different error conditions) and wherever we need to distinguish different "types" or "cases". In that case we can use an enum to create a new type we can use in function prototypes etc., and then the compiler can sanity check that code better.

Henno Brandsma
+1  A: 

enum can group multiple elements in one category:

enum fruits{ apple=1234, orange=12345};

while #define can only create unrelated constants:

#define apple 1234
#define orange 12345
Andy
+1  A: 

#define is a preprocessor command, enum is in the C or C++ language.

It is always better to use enums over #define for this kind of cases. One thing is type safety. Another one is that when you have a sequence of values you only have to give the beginning of the sequence in the enum, the other values get consecutive values.

enum {
  ONE = 1,
  TWO,
  THREE,
  FOUR
};

instead of

#define ONE 1
#define TWO 2
#define THREE 3
#define FOUR 4

As a side-note, there is still some cases where you may have to use #define (typically for some kind of macros, if you need to be able to construct an identifier that contains the constant), but that's kind of macro black magic, and very very rare to be the way to go. If you go to these extremities you probably should use a C++ template (but if you're stuck with C...).

kriss
+1  A: 

Besides all the thing already written, one said but not shown and is instead interesting. E.g.

enum action { DO_JUMP, DO_TURNL, DO_TURNR, DO_STOP };
//...
void do_action( enum action anAction, info_t x );

Considering action as a type makes thing clearer. Using define, you would have written

void do_action(int anAction, info_t x);
ShinTakezou
+1  A: 

For integral constant values I've come to prefer enum over #define. There seem to be no disadvantages to using enum (discounting the miniscule disadvantage of a bit more typing), but you have the advantage that enum can be scoped, while #define identifiers have global scope that tromps everything.

Using #define isn't usually a problem, but since there are no drawbacks to enum, I go with that.

In C++ I also generally prefer enum to const int even though in C++ a const int can be used in place of a literal integer value (unlike in C) because enum is portable to C (which I still work in a lot) .

Michael Burr
If one if working on embedded systems, would not be the additional memory consumed by the enum be a (minor) drawback? A #define, being essentially a pre-processor replacement does not consume the additional memory in the Data segment.
Demi
@Demi: enums themselves take no space - they're just names for numbers. If you declare a variable of the enum type, that variable will takes space, but that's the same as declaring a variable of int (or whatever) type and initializing or assigning it with a macro.
Michael Burr
A: 

Creating an enum creates not only literals but also the type that groups these literals: This adds semantic to your code that the compiler is able to check.

Moreover, when using a debugger, you have access to the values of enum literals. This is not always the case with #define.

mouviciel