How does #define operate ?

Discussion in 'C++' started by Timothy Madden, Sep 27, 2004.

  1. Hello

    If I say

    #define MASK 0x00F0
    #define BIT_SET MASK
    #define MASK 0x000F

    than what value will BIT_SET macro expand to ?
    I mean the preprocessor does lasy evaluation or immediate evaluation ?
    I tryed to figure it out from MSDN once but I couldn't

    Thank you
    Timothy Madden
    Romania
    ----------------------------------------------------------------------------
    And I don't wanna miss a thing
    Timothy Madden, Sep 27, 2004
    #1
    1. Advertising

  2. Timothy Madden

    Howard Guest

    "Timothy Madden" <> wrote in message
    news:...
    > Hello
    >
    > If I say
    >
    > #define MASK 0x00F0
    > #define BIT_SET MASK
    > #define MASK 0x000F
    >
    > than what value will BIT_SET macro expand to ?
    > I mean the preprocessor does lasy evaluation or immediate evaluation ?
    > I tryed to figure it out from MSDN once but I couldn't
    >


    I don't think the standard deals with preprocessor stuff, but on my compiler
    it gives me a redefinition error. If I want to re-#define something, I have
    to #undef it first. Which would imply (assuming you threw in a #undef MASK
    before the second #define) that BIT_SET would still be 0x00F0, because
    otherwise what would the point be of requiring #undef? I see several places
    where something is defined just before including a file that needs it, then
    undefined so as not to interfere with other, later, includes.

    -Howard
    Howard, Sep 27, 2004
    #2
    1. Advertising

  3. Timothy Madden

    Xenos Guest

    "Howard" <> wrote in message
    news:bDV5d.441932$...
    >
    > "Timothy Madden" <> wrote in message
    > news:...
    > > Hello
    > >
    > > If I say
    > >
    > > #define MASK 0x00F0
    > > #define BIT_SET MASK
    > > #define MASK 0x000F
    > >
    > > than what value will BIT_SET macro expand to ?
    > > I mean the preprocessor does lasy evaluation or immediate evaluation ?
    > > I tryed to figure it out from MSDN once but I couldn't
    > >

    >
    > I don't think the standard deals with preprocessor stuff, but on my

    compiler
    > it gives me a redefinition error. If I want to re-#define something, I

    have
    > to #undef it first. Which would imply (assuming you threw in a #undef

    MASK
    > before the second #define) that BIT_SET would still be 0x00F0, because
    > otherwise what would the point be of requiring #undef? I see several

    places
    > where something is defined just before including a file that needs it,

    then
    > undefined so as not to interfere with other, later, includes.
    >
    > -Howard
    >
    >


    Not true, BIT_SET expands to MASK, not 0x00F0. If you redefine MASK, your
    redefine BIT_SET.

    DrX
    Xenos, Sep 27, 2004
    #3
  4. Timothy Madden

    Howard Guest

    "Xenos" <> wrote in message
    news:cj9g21$...
    >
    > "Howard" <> wrote in message
    > news:bDV5d.441932$...
    > >
    > > "Timothy Madden" <> wrote in message
    > > news:...
    > > > Hello
    > > >
    > > > If I say
    > > >
    > > > #define MASK 0x00F0
    > > > #define BIT_SET MASK
    > > > #define MASK 0x000F
    > > >
    > > > than what value will BIT_SET macro expand to ?
    > > > I mean the preprocessor does lasy evaluation or immediate evaluation ?
    > > > I tryed to figure it out from MSDN once but I couldn't
    > > >

    > >
    > > I don't think the standard deals with preprocessor stuff, but on my

    > compiler
    > > it gives me a redefinition error. If I want to re-#define something, I

    > have
    > > to #undef it first. Which would imply (assuming you threw in a #undef

    > MASK
    > > before the second #define) that BIT_SET would still be 0x00F0, because
    > > otherwise what would the point be of requiring #undef? I see several

    > places
    > > where something is defined just before including a file that needs it,

    > then
    > > undefined so as not to interfere with other, later, includes.
    > >
    > > -Howard
    > >
    > >

    >
    > Not true, BIT_SET expands to MASK, not 0x00F0. If you redefine MASK,

    your
    > redefine BIT_SET.
    >
    > DrX


    You're correct (at least using CodeWarrior on the Mac).

    And it's obvious why. My thinking was backwards. At the time/place of
    expansion, it evaluates the symbol, and by that time the symbol has changed.

    (But I definitely do need to #undef it before I can #define it again.)

    -Howard
    Howard, Sep 27, 2004
    #4
  5. "Howard" <> wrote in message
    news:VB_5d.443351$...
    >
    > "Xenos" <> wrote in message
    > news:cj9g21$...
    > >
    > > "Howard" <> wrote in message
    > > news:bDV5d.441932$...
    > > >
    > > > "Timothy Madden" <> wrote in message
    > > > news:...
    > > > > Hello
    > > > >
    > > > > If I say
    > > > >
    > > > > #define MASK 0x00F0
    > > > > #define BIT_SET MASK
    > > > > #define MASK 0x000F
    > > > >
    > > > > than what value will BIT_SET macro expand to ?
    > > > > I mean the preprocessor does lasy evaluation or immediate evaluation

    ?
    > > > > I tryed to figure it out from MSDN once but I couldn't
    > > > >
    > > >
    > > > I don't think the standard deals with preprocessor stuff, but on my

    > > compiler
    > > > it gives me a redefinition error. If I want to re-#define something,

    I
    > > have
    > > > to #undef it first. Which would imply (assuming you threw in a #undef

    > > MASK
    > > > before the second #define) that BIT_SET would still be 0x00F0, because
    > > > otherwise what would the point be of requiring #undef? I see several

    > > places
    > > > where something is defined just before including a file that needs it,

    > > then
    > > > undefined so as not to interfere with other, later, includes.
    > > >
    > > > -Howard
    > > >
    > > >

    > >
    > > Not true, BIT_SET expands to MASK, not 0x00F0. If you redefine MASK,

    > your
    > > redefine BIT_SET.
    > >
    > > DrX

    >
    > You're correct (at least using CodeWarrior on the Mac).
    >
    > And it's obvious why. My thinking was backwards. At the time/place of
    > expansion, it evaluates the symbol, and by that time the symbol has

    changed.
    >
    > (But I definitely do need to #undef it before I can #define it again.)


    Actualy I really meant:

    #define MASK 0x00F0
    #define BIT_SET MASK
    #undef MASK
    #define MASK 0x000F

    Should the standard not say something about this ?
    Could MASK in turn be defined as some other defined symbol ? How many levels
    can this go ? Why does any book not say anything about it ?
    Anyone can tell me how other compilers behave ?

    Thank you
    Timothy Madden
    Romania
    -------------------------------------------------
    And I don't wanna miss a thing
    Timothy Madden, Sep 28, 2004
    #5
  6. Timothy Madden

    Xenos Guest

    "Timothy Madden" <> wrote in message
    news:...
    >
    > Actualy I really meant:
    >
    > #define MASK 0x00F0
    > #define BIT_SET MASK
    > #undef MASK
    > #define MASK 0x000F
    >
    > Should the standard not say something about this ?
    > Could MASK in turn be defined as some other defined symbol ? How many

    levels
    > can this go ? Why does any book not say anything about it ?
    > Anyone can tell me how other compilers behave ?
    >

    It's not the compiler, but the preprocessor. They behave by following the
    standard. You are not setting one variable equal to another. You are
    telling the preprocessor that the macro BIT_SET is equal to the the text
    "MASK" (without the quotes). After the text replacement, the code will be
    rescanned looking for more macros. If MASK is defined when this is done,
    then MASK will be replaced. If not, it will be passed as is to the
    compiler.

    DrX
    Xenos, Sep 28, 2004
    #6
    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. benn
    Replies:
    2
    Views:
    589
  2. Maya Young

    Use C# to operate a mobile device

    Maya Young, Apr 15, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    615
    Maya Young
    Apr 17, 2004
  3. Jeremy
    Replies:
    3
    Views:
    399
  4. Sara
    Replies:
    6
    Views:
    249
    John W. Krahn
    Apr 12, 2004
  5. DEN

    toolbar.visible does not operate

    DEN, Jun 14, 2005, in forum: Javascript
    Replies:
    12
    Views:
    288
    Thomas 'PointedEars' Lahn
    Jul 12, 2005
Loading...

Share This Page