views:

67

answers:

5

Hello, I can check predefined value like:

#ifdef SOME_VAR
  // Do something
#elif
  // Do something 2
#endif

If I have to check 2 values instead of 1. Are there any operator:

#ifdef SOME_VAR and SOME_VAR2
  // ...
#endif

Or I have to write:

#ifdef SOME_VAR
   #ifdef SOME_VAR2
      // At least! Do something
   #endif
#endif
+4  A: 

The standard short-circuiting and operator (&&) along with the defined keyword is what is used in this circumstance.

#if defined(SOME_VAR) && defined(SOME_VAR2)
    /* ... */
#endif

Likewise, the normal not operator (!) is used for negation:

#if defined(SOME_VAR) && !defined(SOME_OTHER_VAR)
    /* ... */
#endif
Mark Rushakoff
psmears
+3  A: 

You can use the defined operator:

#if defined (SOME_VAR) && defined(SOME_VAR2)

#endif

#ifdef and #ifndef are just shortcuts for the defined operator.

James McNellis
+2  A: 

You can write:

#if defined(SOME_VAR) && defined(SOME_VAR2)
// ...
#endif
psmears
+1  A: 

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

tenfour
A: 

One alternative is to get away from using #ifdef and just use #if, since "empty symbols" evaluate to false in the #if test.

So instead you could just do

#if SOME_VAR && SOME_OTHER_VAR
    // -- things
#endif

But the big side effect to that is that you not only need to #define your variables, you need to define them to be something. E.g.

#define SOME_VAR         1
#define SOME_OTHER_VAR   1

Commenting out either of those #defines will make the above #if test fail (insofar as the enclosed stuff not being evaluated; the compiler will not crash or error out or anything).

dash-tom-bang