C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How does #define operate ?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Timothy Madden
Guest





PostPosted: Mon Sep 27, 2004 2:18 pm    Post subject: How does #define operate ? Reply with quote



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


Back to top
Howard
Guest





PostPosted: Mon Sep 27, 2004 2:40 pm    Post subject: Re: How does #define operate ? Reply with quote




"Timothy Madden" <batman (AT) rmv (DOT) spam.home.ro> wrote

Quote:
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



Back to top
Xenos
Guest





PostPosted: Mon Sep 27, 2004 4:44 pm    Post subject: Re: How does #define operate ? Reply with quote




"Howard" <alicebt (AT) hotmail (DOT) com> wrote

Quote:

"Timothy Madden" <batman (AT) rmv (DOT) spam.home.ro> wrote in message
news:2rqlopF1dg1v2U1 (AT) uni-berlin (DOT) de...
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



Back to top
Howard
Guest





PostPosted: Mon Sep 27, 2004 8:20 pm    Post subject: Re: How does #define operate ? Reply with quote


"Xenos" <dont.spam.me (AT) spamhate (DOT) com> wrote

Quote:

"Howard" <alicebt (AT) hotmail (DOT) com> wrote in message
news:bDV5d.441932$OB3.21760 (AT) bgtnsc05-news (DOT) ops.worldnet.att.net...

"Timothy Madden" <batman (AT) rmv (DOT) spam.home.ro> wrote in message
news:2rqlopF1dg1v2U1 (AT) uni-berlin (DOT) de...
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





Back to top
Timothy Madden
Guest





PostPosted: Tue Sep 28, 2004 6:39 am    Post subject: Re: How does #define operate ? Reply with quote


"Howard" <alicebt (AT) hotmail (DOT) com> wrote

Quote:

"Xenos" <dont.spam.me (AT) spamhate (DOT) com> wrote in message
news:cj9g21$gi52 (AT) cui1 (DOT) lmms.lmco.com...

"Howard" <alicebt (AT) hotmail (DOT) com> wrote in message
news:bDV5d.441932$OB3.21760 (AT) bgtnsc05-news (DOT) ops.worldnet.att.net...

"Timothy Madden" <batman (AT) rmv (DOT) spam.home.ro> wrote in message
news:2rqlopF1dg1v2U1 (AT) uni-berlin (DOT) de...
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



Back to top
Xenos
Guest





PostPosted: Tue Sep 28, 2004 11:41 am    Post subject: Re: How does #define operate ? Reply with quote


"Timothy Madden" <batman (AT) rmv (DOT) spam.home.ro> wrote

Quote:

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



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.