Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

How can I construct preprocessor #if expressions which compare strings?

0
Posted

How can I construct preprocessor #if expressions which compare strings?

0

You can’t do it directly; try #defining several manifest constants and implementing conditionals on those.

0

You can’t do it directly; preprocessor #if arithmetic uses only integers. An alternative is to #define several macros with symbolic names and distinct integer values, and implement conditionals on those: #define RED 1 #define BLUE 2 #define GREEN 3 #if COLOR == RED /* red case */ #else #if COLOR == BLUE /* blue case */ #else #if COLOR == GREEN /* green case */ #else /* default case */ #endif #endif #endif (Standard C specifies a new #elif directive which makes if/else chains like these a bit cleaner.) See also question 20.17. References: K&R2 Sec. 4.11.3 p. 91 ISO Sec. 6.8.1 H&S Sec. 7.11.1 p.

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123