The preprocessor equality test always evaluates to true

Using this:

-DME=AWESOME

      

and this:

#if ME==AWESOME

#error Im awesome!

#endif

      

gives the following:

Registers.cpp: 59: 2: error: #error Im awesome!

but this:

#if ME==NOTSOGREAT

#error Im confused!

#endif

      

gives the following:

Registers.cpp: 59: 2: error: #error Im confused!

+3


source to share


1 answer


Note that the execution is -DME=AWESOME

equivalent to the original file starting at:

#define ME AWESOME

      

Now let's look at #if ME==AWESOME

. Replacing notes replaces ME

with AWESOME

, so the final version of this line is:

#if AWESOME==AWESOME

      

When you use it ==

in the preprocessor, a literal token that is not #define

d for anything else is replaced with 0

. So this checks #if 0 == 0

what is correct, which is why your error is displayed.

Now looking at:

#if ME==NOTSOGREAT

      



After replacing the token, this is:

#if AWESOME==NOTSOGREAT

      

which is again equivalent #if 0 == 0

, which is true.


If you also had #define AWESOME 5

this before, you will find that the first test is true, but the second test is false.

I am assuming that you are trying to determine if it has been ME

determined AWESOME

, but there is no way to do it; you can only check if was ME

defined as something equal to what AWESOME

was defined as.

+7


source







All Articles