ifdef with ||

J

James Kuyper

Is there a way to do something like:

#ifdef A || B
... some code ...
#endif

I can think of two possibilities that might be what you're thinking of:

#if A || B
/* the expression "A || B" has a non-zero value */
#else
/* The expression "A || B" has a value of zero */
#endif


Note that during the preprocessing phase, no types, variables or
functions have been recognized as such. In a #if condition, 'defined(X)
has a value of 0 if X is not defined as a macro, and 1 if it is. All
other identifiers that remain after macro expansion has been carried out
are treated as having a value of 0. The condition is then evaluated as a
constant expression. All signed integer types are treated as if they
were intmax_t, and all unsigned integer types are treated as if they
were uintmax_t. As a result of these rules, "A || B" can have a very
different meaning in a #if than it would in an if().


Other alternative:

#if defined(A) || defined(B)
/* Either A or B is defined, possibly both */
#else
/* Neither one is defined. */
#endif
 
M

Martin Ambuhl

Is there a way to do something like:

#ifdef A || B
... some code ...
#endif

#include <stdio.h>

/* make sure these macros haven't been defined behind our back */

#if defined(COND_A)
#undef COND_A
#endif

#if defined(COND_B)
#undef COND_B
#endif

int main(void)
{
printf("COND_A and COND_B are undefined ... ");
#if defined(COND_A) || defined(COND_B)
printf(" wrong.\n");
#else
printf(" right.\n");
#endif

#define COND_A
printf("COND_A is defined, and COND_B is not... ");
#if !defined(COND_A) || defined(COND_B)
printf("wrong.\n");
#else
printf("right.\n");
#endif

#undef COND_A
#define COND_B
printf("COND_A is not defined, and COND_B is ... ");
#if defined(COND_A) || !defined(COND_B)
printf(" wrong.\n");
#else
printf(" right.\n");
#endif

#define COND_A
printf("COND_A and COND_B are both defined ... ");
#if !defined(COND_A) || !defined(COND_B)
printf("wrong.\n\n");
#else
printf("right.\n\n");
#endif

printf("The last test using the form !(x && y) "
"instead of !x || !y:\n"
"COND_A and COND_B are both defined ... ");
#if !(defined(COND_A) && defined(COND_B))
printf(" wrong.\n");
#else
printf(" right.\n ");
#endif

return 0;
}

[output]
COND_A and COND_B are undefined ... right.
COND_A is defined, and COND_B is not... right.
COND_A is not defined, and COND_B is ... right.
COND_A and COND_B are both defined ... right.

The last test using the form !(x && y) instead of !x || !y:
COND_A and COND_B are both defined ... right.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Help with #define 0
#ifdef __SunOS_5.10 3
Python #ifdef 1
#ifdef question 2
#ifdef within a #define 6
#ifdef management 12
[C language] Issue in the Lotka-Volterra model. 0
ifdef 1

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top