I know this is a simple question but I’m confused. I have a fairly typical gcc warning that’s usually easy to fix:
warning: comparison between signed and unsigned integer expressions
Whenever I have a hexadecimal constant with the most significant bit, like 0x80000000L, the compiler interprets it as unsigned. For example compiling this code with -Wextra will cause the warning (gcc 4.4x, 4.5x):
int main()
{
long test = 1;
long *p = &test;
if(*p != 0x80000000L) printf("test");
}
I’ve specifically suffixed the constant as long, so why is this happening?
The answer to Unsigned hexadecimal constant in C? is relevant. A hex constant with
Lsuffix will have the first of the following types that can hold its value:See the C99 draft, section [ 6.4.4.1 ], for details.
On your platform,
longis probably 32 bits, so it is not large enough to hold the (positive) constant0x80000000. So your constant has typeunsigned long, which is the next type on the list and is sufficient to hold the value.On a platform where
longwas 64 bits, your constant would have typelong.