'Cast to unsigned long in macro definition causes compile error
The following code generates the following compile error:
The Code:
int main(void)
{
#define a ((unsigned long) 0)
#if a > 2
while(1);
#endif
}
The error:
main.c: In function 'main':
main.c:4:26: error: missing binary operator before token "long"
#define a ((unsigned long) 0)
^
main.c:5:9: note: in expansion of macro 'a'
#if a > 2
^
The command 'arm-none-eabi-gcc.exe' failed with exit code '1'.
Can somebody help to figure out why is that happen only if use 'a' macro in '#if' statement?
Solution 1:[1]
The expansion of the two lines:
#define a ((unsigned long) 0)
#if a > 2
effectively becomes:
#if ((unsigned long) 0) > 2
Since the pre-processor knows nothing about type casting (it is a relatively simple text substitution thing), the expression you're checking there is not valid.
Rather than ((unsigned long) 0), you would be better off just using 0UL (or 0), which the pre-processor will understand in the context of your #if expression.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | paxdiablo |
