How can I construct preprocessor if expressions which compare strings?

Q

How can I construct preprocessor if expressions which compare strings?

✍: Guest

A

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.)

2016-02-05, 580👍, 0💬