6
#define MY_CONST 20
#define OTHER_CONST 10

My code only makes sense if MY_CONST > OTHER_CONST. How can I guarantee this with the preprocessor? Is there any command like this?

#assert MY_CONST < OTHER_CONST
gre_gor
  • 6,669
  • 9
  • 47
  • 52
Anna
  • 2,645
  • 5
  • 25
  • 34

2 Answers2

15

Is there any command like this?

#assert MY_CONST < OTHER_CONST

#if OTHER_CONST >= MY_CONST
#error "Error, OTHER_CONST >= MY_CONST"
#endif
Attersson
  • 4,755
  • 1
  • 15
  • 29
0

as @attersson said #if will do it. (as a good habbit try to parenthesize your macros to guarantee order of evaluation for more complex expressions. 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;
}
ramrunner
  • 1,362
  • 10
  • 20