Sign up ×

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}; 


#define BUFFER 1234   
share|improve this question

16 Answers 16

up vote 46 down vote accepted

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.

share|improve this answer
You can also put enums in namespaces whereas macros can't be. – Greg Rogers Sep 26 '08 at 3:48

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:

   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 your 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.

share|improve this answer
"casting from this type to int" is back to front, I think; C++ treats enums as specializations of int. – Simon Buchan Sep 26 '08 at 0:09

There is little difference. The C Standard says that enumerations have integral type and that enumeration constants are of type int, so both may be freely intermixed with other integral types, without errors. (If, on the other hand, such intermixing were disallowed without explicit casts, judicious use of enumerations could catch certain programming errors.)


share|improve this answer

#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,

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...).

share|improve this answer

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.

share|improve this answer

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) .

share|improve this answer
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 Oct 25 '10 at 16:48
@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 Oct 25 '10 at 17:08
+1 For 'For integral constant values I've come to prefer', there are situations where one needs to specify floating point constants (e.g. ratios) and for those situations, enums are useless – Shmil The Cat May 25 '14 at 9:36

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);
share|improve this answer

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
share|improve this answer
Thank you for the most useful answer on the internet. (and I'm really not kidding) – Chris K Sep 5 '13 at 5:05

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.

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

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 {


switch (state) {

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.

share|improve this answer

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.

share|improve this answer

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 {

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!

share|improve this answer

#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.

share|improve this answer

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.

share|improve this answer

Your Answer


