Difference between Enum and Define Statement

What is the difference between using the define statement and an enumeration operator in C / C ++ (and is there a difference when using them with C or C ++)?

For example, when to use

enum {BUFFER = 1234}; 

over

 #define BUFFER 1234 
+42
c ++ c enums c-preprocessor
Sep 25 '08 at 23:47
source share
18 answers

enum defines a syntax element.

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

Enumerations are generally preferred because they are type safe and more easily detected. Definitions are harder to find and can have complex behavior, for example, one piece of code can override #define made by another. This can be difficult to track.

+53
Sep 25 '08 at 23:48
source share
Operators

#define handled by the pre-processor before the compiler gets the code so that it basically replaces the text (it is actually a bit more intelligent using parameters, etc.).

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

1 / They can be of type and the compiler can test them.

2 / Since they are accessible to the compiler, information about the symbols on them can be passed to the debugger, which facilitates debugging.

+14
Sep 25 '08 at 23:58
source share

Define is a preprocessor command, it’s just like “replacing everything” in your editor, it can replace the string with another and then compile the result.

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

 enum ERROR_TYPES { REGULAR_ERR =1, OK =0 } 

there is a new type called ERROR_TYPES. It is true that REGULAR_ERR gives a value of 1, but casting from this type to int should trigger a warning about casting (if you set your compiler to be very verbose).

Summary: they are the same, but when using an enumeration, you get access to type checking and using definitions, you just replace the lines of code.

+8
Sep 25 '08 at 23:53
source share

Enumerations are usually preferable to #define, where it makes sense to use an enumeration:

  • Debuggers can show you the symbolic name of the enum value (" openType: OpenExisting ", not " openType: 2 "
  • You get a bit more protection against name conflicts, but it's not as bad as it was (most compilers warn about re #define ition.

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 a check for parameter types (you cannot mix openType and bufferSize easily) and makes it easy to find which values ​​are valid, which makes your interfaces much easier to use. Some IDEs may even give you intellisense code completion !

+5
Sep 26 '08 at 0:26
source share

It is always better to use an enumeration, if possible. Using an enumeration gives the compiler more information about your source code, the definition of a preprocessor is never considered by the compiler, and therefore carries less information.

For implementation, for example, a bunch of modes, using an enumeration, allows the compiler, for example, to catch missing case states in the switch.

+4
Jun 14 '10 at 7:28
source share

enum can group several elements into one category:

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

and #define can only create unrelated constants:

 #define apple 1234 #define orange 12345 
+3
Jun 14 2018-10-10T00:
source share

#define - preprocessor command, enumeration - in C or C ++.

It is always better to use enumerations for #define for such cases. One thing is type safety. Another is that when you have a sequence of values, you only need to indicate the beginning of the sequence in the enumeration, other values ​​will receive sequential values.

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

instead

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

As a side note, there are still some cases where you may need to use #define (usually for some macros, if you need to build an identifier that contains a constant), but this kind of macro is black magic, and very very rare to be way to go. If you go to these limbs, you should probably use the C ++ pattern (but if you're stuck with C ...).

+3
Jun 14 2018-10-10T00:
source share

If you only need this constant (say, for buffering), I would not use an enumeration, but define it. I would use enumerations for things like return values ​​(which meant different error conditions) and wherever we need to distinguish between different “types” or “cases”. In this case, we can use the enumeration to create a new type that we can use in function prototypes, etc., and then the compiler can check if this code is better.

+2
Jun 14 2018-10-10T00:
source share

In addition to everything that has already been written, one said, but is not shown and interesting. For example.

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

Seeing action as a type makes things clearer. Using define, you would write

 void do_action(int anAction, info_t x); 
+2
Jun 14 2018-10-10T00:
source share

For integral constant values, I prefer enum over #define . There seems to be no flaw in using enum (discounting a minimal flaw in a bit more typing), but you have the advantage that enum can be limited, and #define identifiers have a global scope that throws everything.

Using #define usually not a problem, but since there are no flaws for enum , I go with that.

In C ++, I usually prefer from enum to const int , although in C ++ a const int can be used instead of the value of a literal integer (as opposed to C), because enum carries over to C (which I still work a lot).

+2
Jun 14 2018-10-10T00:
source share

If you have a group of constants (for example, "Days of the week"), enumerations will be preferable because it shows that they are grouped; and, as Jason said, they are type safe. If it is a global constant (for example, version number), it is more than you would use #define for; although this is the subject of much discussion.

+1
Sep 25 '08 at 23:54
source share

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

+1
Sep 26 '08 at 10:37
source share

Another advantage of an enumeration over a definition list is that compilers (at least gcc) may 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 may generate a warning that not all enumeration values ​​are processed in the switch. If the states were executed as # define, it is not.

+1
Sep 29 '08 at 1:32
source share

transfers are more used to list a set, for example, days a week. If you only need one constant number, const int (or double, etc.) will definitely be better than an enumeration. I personally don't like #define (at least not for defining some constants) because it does not give me the type of security, but you can, of course, use it if it suits you.

+1
Jun 14 '10 at 7:30
source share

Creating enum creates not only literals, but also a type that groups these literals: this adds semantics to your code, which the compiler can verify.

In addition, when using the debugger, you have access to enumeration literal values. This does not always apply to #define.

+1
Jun 14 '10 at 8:13
source share

Enum:

1. Commonly used for multiple values.

2. There are two things in the enumeration: one name, and the other - the value of the name, which should be highlighted, but the value can be the same. If we do not define a value, then the first value of the enumeration name is 0 second 1, etc., unless the value is explicitly specified.

3. They may have a type and a compiler that can type them.

4. Make debugging easier

5. We can limit its volume to class.

Define:

1. When we need to define only one value

2. It usually replaces one line with another. 3 .. The scope is global; we cannot limit its scope.

In general, we should use enum

+1
Aug 11 '17 at 18:08
source share

There is a slight difference. The C standard states that enums are of integral type and that enum constants are of type int, so both can mix freely with other integral types without errors. (If, on the other hand, such a confusion was forbidden without explicit tricks, the prudent use of enumerations could catch certain programming errors.)

Some of the advantages of enumerations are that numeric values ​​are automatically assigned, that the debugger can display symbolic values ​​when checking enumeration variables, and that they obey the block area. (The compiler can also generate non-fatal warnings when enumerations are mixed indiscriminately, since doing so can still be considered a bad style, even if it is not strictly illegal.) The disadvantage is that the programmer does not have much control over these non-fatal warnings; some programmers are also outraged by the lack of control over the size of enumeration variables.

0
Jun 23 '14 at 13:38 on
source share

While the few answers above recommend using enumeration for various reasons, I would like to point out that using definitions has a real advantage when developing interfaces. You can enter new parameters, and you can let them use them conditionally.

For example:

     #define OPT_X1 1 / * introduced in version 1 * /
     #define OPT_X2 2 / * introduced in version 2 * /

Then software that can be compiled with any version can run

     #ifdef OPT_X2
     int flags = OPT_X2;
     #else
     int flags = 0;
     #endif

While enumeration is not possible without a mechanism for detecting the run-time function.

0
Jul 20 '16 at 11:00
source share



All Articles