Boolean in ifdef: "#ifdef A && B" matches "#if defined (A) && defined (B)"?

In C ++, this is:

#ifdef A && B 

same as:

 #if defined(A) && defined(B) 

?

I thought this was not the case, but I could not find the difference with my compiler (VS2005).

+66
c ++ c-preprocessor conditional-compilation
Aug 21 '09 at 14:01
source share
5 answers

They do not match. The first one does not work (I tested gcc 4.4.1). Error message:

test.cc:1:15: warning: additional tokens in the end of the #ifdef directive

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

+71
Aug 21 '09 at 14:03
source share

Conditional compilation

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

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

Without using a specific operator, you would have to enable following the two directives to execute in the above example:

 #ifdef max #ifndef min 
+39
Aug 21 '09 at 14:03
source share

The following results are the same:

one.

 #define A #define B #if(defined A && defined B) printf("define test"); #endif 

2.

 #ifdef A #ifdef B printf("define test"); #endif #endif 
+2
Jul 20 '16 at 7:06
source share

As of VS2015, none of the above works. The correct directive is:

 #if (MAX && !MIN) 

more details here

-2
Sep 17 '16 at 6:14
source share

I think maybe OP wants to ask about the status of "# COND_A && COND_B" and not "#ifdef COND_A & & COND_B" ...

They are also different. "#if COND_A & & COND_B" can judge a logical expression, for example:

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

a variable in your code can also be used in this macro status:

 int a = 1; #if a==1 ... #endif 
-9
Jan 29 '13 at 13:36
source share



All Articles