By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
 446,159 Members | 943 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

# Determining whether a macro has been defined

 P: n/a Are these two lines equivalent? #ifdef MACRO #if MACRO==0 Jul 23 '05 #1
Share this Question
8 Replies

 P: n/a BigMan wrote: Are these two lines equivalent? #ifdef MACRO #if MACRO==0 No. Why would they be? If 'MACRO' is defined as #define MACRO 2 the first variant will "evaluate" to 'true', while the second one - to 'false'. -- Best regards, Andrey Tarasevich Jul 23 '05 #2

 P: n/a No. The first one evalutes to true only if MACRO has been defined (regardless of its definition). The second evalutes to true if either of these is true: MACRO is undefined MACRO is defined but evalutes at compile time to 0. See section 16.1.4 in the standard. Gregg Jul 23 '05 #3

 P: n/a Ooops, I've put it the wrong way. Here's what I mean: does it matter which line I use to check if a macro has been defined. Jul 23 '05 #4

 P: n/a BigMan wrote: Ooops, I've put it the wrong way. Here's what I mean: does it matter which line I use to check if a macro has been defined. Yes, it matters. Didn't you read the other posts? The first way is correct. Brian Jul 23 '05 #5

 P: n/a Default User wrote: BigMan wrote:Ooops, I've put it the wrong way. Here's what I mean: does it matterwhich line I use to check if a macro has been defined. Yes, it matters. Didn't you read the other posts? The first way is correct. If he doesn't want to worry about the case where it is defined and is zero. It doesn't matter. Jul 23 '05 #6

 P: n/a Ron Natalie wrote: Default User wrote: BigMan wrote:Ooops, I've put it the wrong way. Here's what I mean: does it matterwhich line I use to check if a macro has been defined. Yes, it matters. Didn't you read the other posts? The first way is correct. If he doesn't want to worry about the case where it is defined and is zero. It doesn't matter. I don't recall that in his problem description. Brian Jul 23 '05 #7

 P: n/a BigMan wrote: Ooops, I've put it the wrong way. Here's what I mean: does it matter which line I use to check if a macro has been defined. Yes it does. The former method (with '#ifdef') checks whether it has been defined. The latter one (with '#if') checks whether it is substituted with integral zero (if well-formed). This is not the same. For example, defining 'MACRO' as either '0' or '1' will give you the same results from the former method (i.e. '#ifdef' will say that it is defined in both cases), but different results from the latter method. Defining 'MACRO' as follows #define MACRO will work with the former method, but it makes the latter one ill-formed. -- Best regards, Andrey Tarasevich Jul 23 '05 #8

 P: n/a Thanks to all of you! I think I got it. Jul 23 '05 #9

### This discussion thread is closed

Replies have been disabled for this discussion.