C宏:#if检查是否相等

有没有办法检查宏中的数字相等性?

我想做点什么

#define choice 3 #if choice == 3 .... #endif #if choice == 4 ... #endif 

C宏是否支持这样的事情?

确实应该有效。 见http://gcc.gnu.org/onlinedocs/cpp/If.html#If

该引用是准确的,但以“标准格式”编写:抽象地没有示例。

编写代码的另一种方法是使用链式#elif指令:

 #if choice == 3 ... #elif choice == 4 ... #else #error Unsupported choice setting #endif 

请注意,如果choice不是#defined ,则预处理器会将其视为值为0

据我所知,应该工作。 你用的是什么编译器?

PS:仅供参考,定义名称通常用大写字母书写!

#define CHOICE 3