Say that my C code uses a constant RANGEMAX
that is the largest power of 10 fitting in an int
. I can define it with:
#include <limits.h>
#if (INT_MAX < 1)
#define RANGEMAX ERROR1
#elif (INT_MAX >= 1) && (INT_MAX < 10)
#define RANGEMAX 1
#elif (INT_MAX >= 10) && (INT_MAX < 100)
#define RANGEMAX 10
#elif (INT_MAX >= 100) && (INT_MAX < 1000)
#define RANGEMAX 100
... /* well, you get the picture */
#elif (INT_MAX >= 10000000000000000)
#define RANGEMAX ERROR2
#endif
Is there a smarter way of doing such simple computations in the macro preprocessing phase?
And since these are "simple" computations, I prefer solutions that an average guy like me will understand by just reading the code.