Today, I stumbled over something like this:
#define FOO 2u
#if (FOO == 2)
unsigned int foo = FOO;
#endif
Regardless of why the code is as it is (let's not question the why
), I was wondering to which degree the preprocessor can even handle integer literal suffixes. I was actually surprised that it works at all.
After doing some experiments with GCC and C99 with this code ...
#include <stdio.h>
int main()
{
#if (1u == 1)
printf("1u == 1\n");
#endif
#if (1u + 1l == 2ll)
printf("1u + 1l == 2ll\n");
#endif
#if (1ull - 2u == -1)
printf("1ull - 2u == -1\n");
#endif
#if (1u - 2u == 0xFFFFFFFFFFFFFFFF)
printf("1u - 2u == 0xFFFFFFFFFFFFFFFF\n");
#endif
#if (-1 == 0xFFFFFFFFFFFFFFFF)
printf("-1 == 0xFFFFFFFFFFFFFFFF\n");
#endif
#if (-1l == 0xFFFFFFFFFFFFFFFF)
printf("-1l == 0xFFFFFFFFFFFFFFFF\n");
#endif
#if (-1ll == 0xFFFFFFFFFFFFFFFF)
printf("-1ll == 0xFFFFFFFFFFFFFFFF\n");
#endif
}
... which just prints all the statements:
1u == 1
1u + 1l == 2ll
1ull - 2u == -1
1u - 2u == 0xFFFFFFFFFFFFFFFF
-1 == 0xFFFFFFFFFFFFFFFF
-1l == 0xFFFFFFFFFFFFFFFF
-1ll == 0xFFFFFFFFFFFFFFFF
... I guess the preprocessor simply ignores integer literal suffixes altogether and probably always does arithmetics and comparisons in the native integer size, in this case 64 bit?
So, here is the stuff I'd like to know:
- To which degree does the preprocessor regard integer literal suffixes? Or does it just ignore them?
- Are there any dependencies or different behaviors with different environments, e.g. different compilers, C vs. C++, 32 bit vs. 64 bit machine, etc.? I.e., what does the preprocessor's behavior depend on?
- Where is all that specified/documented?
I wanted to find out by myself and checked out Wikipedia and the C standard (working paper). I found information about integer suffixes and information about the preprocessor, but none about the combination of these. Obviously, I have also googled it but didn't get any useful results.
I have seen this Stack Overflow question that clarifies where it should be specified, but yet, I couldn't find an answer for my questions.