我如何保证一个 #define 比另一个大?
How can I guarantee one #define is larger than another?
#define MY_CONST 20
#define OTHER_CONST 10
我的代码只有在 MY_CONST > OTHER_CONST
时才有意义。我怎样才能用预处理器保证这一点?有这样的命令吗?
#assert MY_CONST < OTHER_CONST
Is there any command like this?
#assert MY_CONST < OTHER_CONST
#if OTHER_CONST >= MY_CONST
#error "Error, OTHER_CONST >= MY_CONST"
#endif
正如@attersson 所说#if 会做到的。
(作为一个好习惯,尝试将宏括起来以保证更复杂表达式的计算顺序。this answer shows why。)
#include <stdio.h>
#define A 10
#define B 11
#if (A) > (B)
#define RES "yes"
#else
#define RES "no"
#endif
int
main(int argc, char *argv[])
{
printf("is A larger? %s\n", RES);
return 0;
}
#define MY_CONST 20
#define OTHER_CONST 10
我的代码只有在 MY_CONST > OTHER_CONST
时才有意义。我怎样才能用预处理器保证这一点?有这样的命令吗?
#assert MY_CONST < OTHER_CONST
Is there any command like this?
#assert MY_CONST < OTHER_CONST
#if OTHER_CONST >= MY_CONST
#error "Error, OTHER_CONST >= MY_CONST"
#endif
正如@attersson 所说#if 会做到的。 (作为一个好习惯,尝试将宏括起来以保证更复杂表达式的计算顺序。this answer shows why。)
#include <stdio.h>
#define A 10
#define B 11
#if (A) > (B)
#define RES "yes"
#else
#define RES "no"
#endif
int
main(int argc, char *argv[])
{
printf("is A larger? %s\n", RES);
return 0;
}