In fact, the #if directive treats what is its right as integers.
So, if you want to achieve what you requested, you need to do some arithmetic. For example, run test.c with
#define VERSION 7 #if VERSION #if VERSION - (VERSION % 10 ) #warning Number out of range (1-9) #else #warning Number in range (1-9) #endif #else #warning Zero or not a number #endif
Compile with
gcc -c -o /dev/null test.c
You will receive a message: "Zero of not number" ... if your VERSION is 0 or does not evaluate (preprocessor) as an integer.
And if VERSION evalutates as an integer, you will get the first or second message according to its value.
This will allow you to do what you were looking for.
#If documentation: http://gcc.gnu.org/onlinedocs/cpp/If.html
Note that an integer can be expressed as: 123 or 0xCC or anything that evaluates to an integer constant after a recursive extension of a macro.
If the number is a floating point, for example: 3.14, it is considered zero.
You cannot just distinguish 0 (integer) from something that is not an integer. There is probably a chance using macro concatenation, but it remains to be explored.
user941239
source share