Usage of #ifndef directive

677 views Asked by At

I'm trying to use #ifndef as below.

#ifndef MACRO1 || #ifndef MACRO2
....
#endif

I already tried:

#ifndef (MACRO1 || MACRO2) 
..
#endif

But for both cases am getting below error

error: extra tokens at end of #ifndef directive

4

There are 4 answers

0
too honest for this site On BEST ANSWER

#ifdef and #ifndef are special abbreviations for #if defined(...) and #if !defined(...). However, they can only be used for a single macro and do not allow for logical operations. So, if checking for multiple macros, use #if with the defined() operator instead. Being a regular operatior, this can be combined with logical operations, as the for !defined() already does.

0
Some programmer dude On

Use the #if preprocessor directive instead:

#if !defined(MACRO1) || !defined(MACRO2)
0
Eugene Sh. On

You can use logical operators in preprocessor directives, but in order to check something defined, use the defined directive:

#if !defined MACRO1 || !defined MACRO2
....
#endif
0
Amol Saindane On

You can use following code

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

You can use the defined operator in the #if directive to use expressions that evaluate to 0 or 1 within a preprocessor line.