question about macro and enum

Discussion in 'C Programming' started by gaoqiang, Oct 11, 2011.

  1. gaoqiang

    gaoqiang Guest

    recently, I came into some code like this:

    #define ABCD_A 0x01
    #define ABCD_B 0x02
    #define ABCD_C 0x04

    but someelse, there's code like that:

    enum{
    EFGH_E=0x01,
    EFGH_F=0x02,
    EFGH_G=0x04,
    };

    then,when will I use macro, and when enum ??
    gaoqiang, Oct 11, 2011
    #1
    1. Advertising

  2. gaoqiang

    James Kuyper Guest

    On 10/11/2011 03:40 AM, gaoqiang wrote:
    > recently, I came into some code like this:
    >
    > #define ABCD_A 0x01
    > #define ABCD_B 0x02
    > #define ABCD_C 0x04
    >
    > but someelse, there's code like that:
    >
    > enum{
    > EFGH_E=0x01,
    > EFGH_F=0x02,
    > EFGH_G=0x04,
    > };
    >
    > then,when will I use macro, and when enum ??


    A good rule of thumb, violated by the ABCD_* macros above, is that a
    closely related series of int constants should be packed together as
    enumeration constants of a single enumerated type, while unrelated
    constants and constants that cannot be of type int should be defined as
    macros. However, there's nothing particularly wrong with violating that
    rule of thumb, it's mainly a matter of personal preference. Like all
    rules of thumb, there can be reasonable exceptions. For instance, use of
    a macro allows user code to check

    #ifdef ABCD_B

    there's no comparable way of checking whether a given enumeration
    constant exists without generating a syntax error when it is not.
    --
    James Kuyper
    James Kuyper, Oct 11, 2011
    #2
    1. Advertising

  3. China Blue Corn Chips <> writes:
    > In article <>,
    > gaoqiang <> wrote:
    >> recently, I came into some code like this:
    >>
    >> #define ABCD_A 0x01
    >> #define ABCD_B 0x02
    >> #define ABCD_C 0x04
    >>
    >> but someelse, there's code like that:
    >>
    >> enum{
    >> EFGH_E=0x01,
    >> EFGH_F=0x02,
    >> EFGH_G=0x04,
    >> };
    >>
    >> then,when will I use macro, and when enum ??

    >
    > #defines have been with us since the beginning, but enums were a subsequent
    > feature. So some people and some code still use #defines when enums would be
    > better.


    Enums have the drawback that the values can only be of type int.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Oct 11, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric Sosman
    Replies:
    3
    Views:
    778
    Mayeul
    Feb 26, 2010
  2. Brian
    Replies:
    4
    Views:
    2,636
    Brian
    Feb 27, 2010
  3. Wojtek
    Replies:
    1
    Views:
    496
  4. Lew
    Replies:
    0
    Views:
    535
  5. Roedy Green
    Replies:
    0
    Views:
    729
    Roedy Green
    Feb 27, 2010
Loading...

Share This Page