Determining whether a macro has been defined

A

Andrey Tarasevich

BigMan said:
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'.
 
G

gb

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
 
B

BigMan

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.
 
D

Default User

BigMan said:
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
 
R

Ron Natalie

Default said:
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.
 
D

Default User

Ron said:
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
 
A

Andrey Tarasevich

BigMan said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top