Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using logical operators with macros

I have a piece of code which I want to include if either of two macros are defined

#ifdef MACRO1 || MACRO2

void foo()
{


}

#endif

How do I accomplish this in C?

like image 565
Bruce Avatar asked Dec 04 '12 21:12

Bruce


2 Answers

Here the NOT version if needed:

#if !defined(MACRO1) && !defined(MACRO2)
...
#endif
like image 118
nvd Avatar answered Oct 05 '22 16:10

nvd


Besides #ifdef, the preprocessor supports the more general #if instruction; actually, #ifdef MACRO is a shortcut for #if defined(MACRO), where defined is a "preprocessor function" that returns 1 if the macro is defined; so, you can do:

#if defined(MACRO1) || defined(MACRO2)

void foo()
{


}

#endif
like image 36
Matteo Italia Avatar answered Oct 05 '22 16:10

Matteo Italia