Take the 2-minute tour ×

In C++, is this:

#ifdef COND_A && COND_B

the same as:

#if defined(COND_A) && defined(COND_B)


I was thinking it wasn't, but I haven't been able to find a difference with my compiler (VS2005).

share|improve this question
add comment

3 Answers

up vote 26 down vote accepted

They are not the same. The first one doesn't work (I tested in gcc 4.4.1). Error message was:

test.cc:1:15: warning: extra tokens at end of #ifdef directive

If you want to check if multiple things are defined, use the second one.

share|improve this answer
Thanks for checking. I'm using Microsoft's compiler and it seems to allow it, but it just didn't seem right to me. –  criddell Aug 21 '09 at 14:23
You cannot prove things using some single compiler. Refer to the standard, which states that this is invalid. –  Lightness Races in Orbit Oct 11 at 17:01
add comment

I think maybe OP want to ask about the statment "#if COND_A && COND_B", not "#ifdef COND_A && COND_B"...

They are also different. "#if COND_A && COND_B" can judge logic express, just like this:

#if 5+1==6 && 1+1==2

even, a variable in your code also can be used in this macro statment:

int a = 1; 
#if a==1 
share|improve this answer
No, it can't... –  Lightness Races in Orbit Oct 11 at 17:02
add comment

Conditional Compilation

You can use the defined operator in the #if directive to use expressions that evaluate to 0 or 1 within a preprocessor line. This saves you from using nested preprocessing directives. The parentheses around the identifier are optional. For example:

#if defined (MAX) && ! defined (MIN)

Without using the defined operator, you would have to include the following two directives to perform the above example:

#ifdef max 
#ifndef min
share|improve this answer
While what you say is correct, this does not answer the question at all, he asked if the two are the same...they are not. –  Evan Teran Aug 21 '09 at 14:07
I think it says "they are not the same", you can see that it explains how to make an equivalent of #if defined(COND_A) && defined(COND_B) which is different from #ifdef COND_A && COND_B –  Svetlozar Angelov Aug 21 '09 at 14:15
add comment

Your Answer


