How can I construct preprocessor if expressions which compare strings?
C Interview Questions and Answers
(Continued from previous question...)
How can I construct preprocessor if expressions which compare strings?
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.)
(Continued on next question...)
Other Interview Questions
|