Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boolean in ifdef: is "#ifdef A && B" the same as "#if defined(A) && defined(B)"?

In C++, is this:

#ifdef A && B 

the same as:

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

?

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

like image 570
criddell Avatar asked Aug 21 '09 14:08

criddell


People also ask

What does Ifdef mean?

The #ifdef identifier statement is equivalent to #if 1 when identifier has been defined. It's equivalent to #if 0 when identifier hasn't been defined, or has been undefined by the #undef directive.

How do you write Ifdef?

The #ifdef preprocessor directive checks if macro is defined by #define. If yes, it executes the code otherwise #else code is executed, if present. Syntax: #ifdef MACRO.

What is else of #ifdef?

If an #ifdef is evaluated to be false, then the #elseif directive is processed as if it was an #ifdef directive. If all #elseif directives evaluate to be false, the #else directive is processed if it exists. Input is evaluated until an #endif directive is encountered.

Why do we use #ifdef?

#ifdef means if defined. If the symbol following #ifdef is defined, either using #define in prior source code or using a compiler command-line argument, the text up to the enclosing #endif is included by the preprocessor and therefore compiled. #if works similarly, but it evaluates the boolean expression following it.


Video Answer


1 Answers

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.

like image 63
Evan Teran Avatar answered Oct 02 '22 15:10

Evan Teran