How to “#define” multiple values then “#if” them in C Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to “#define” multiple values then “#if” them in C without wasting too much if your time.

The question is published on by Tutorial Guruji team.

The following code does not work but demonstrates what I want to do

#define TESTINGMACRO 2 | 3

#if TESTINGMACRO & 1 //Should be inactive
#endif

#if TESTINGMACRO & 2 //Should be active
#endif

#if TESTINGMACRO & 3 //Should be active
#endif

Another alternative:

#define TESTINGMACRO 2, 3

#if TESTINGMACRO == 1 //Should be inactive
#endif

#if TESTINGMACRO == 2 //Should be active
#endif

#if TESTINGMACRO == 3 //Should be active
#endif

I know an enum or an array would work too however some preprocessor blocks contains #includes that might not build. Using preprocessor code allows for testing the rest of the file.

I also know I can do the following but this will be a large file so a single central macro would be cleaner:

#define TESTINGMACRO2
#define TESTINGMACRO3

#ifdef TESTINGMACRO1 //Should be inactive
#endif

#ifdef TESTINGMACRO2 //Should be active
#endif

#ifdef TESTINGMACRO3 //Should be active
#endif

Edit: Just FYI in the first scenario all preprocessor blocks are active in practice. So doing 2 | 3 will activate & 1 as well. This is the case for the second code snippet too. This is not what I want, only 2 and 3 should be active when 2|3.

Edit2: I ended up doing:

#define BIT(n)  (1<<n)

#define TESTINGMACRO  ( BIT(2) | BIT(3) )

#if TESTINGMACRO & BIT(1) //Inactive
#endif

#if TESTINGMACRO & BIT(2) //Active
#endif

#if TESTINGMACRO & BIT(3) //Active
#endif

Format wise this is closest to my original and in my opinion quite readable. Thanks for all the help

Answer

You can use bit fields — but then you need to do it properly:

  • Define the values as distinct powers of two. Instead of 1, 2, 3, …, use 1 << 0, 1 << 1, 1 << 2, … (aka. 1, 2, 4 …).

  • Fix the operator precedence by adding parentheses. Remember that macros are expanded via textual replacement.

#define TESTINGMACRO ((1 << 1) | (1 << 2))

#if TESTINGMACRO & (1 << 0) //Should be inactive
#endif

#if TESTINGMACRO & (1 << 1) //Should be active
#endif

#if TESTINGMACRO & (1 << 2) //Should be active
#endif

That said, I would generally recommend using separate feature macros instead, the resulting code is more readble.

We are here to answer your question about How to “#define” multiple values then “#if” them in C - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji