I was working on an embedded project when I ran into something which I thought was strange behaviour. I managed to reproduce it on codepad (see below) to confirm, but don’t have any other C compilers on my machine to try it on them.
Scenario: I have a #define for the most negative value a 32-bit integer can hold, and then I try to use this to compare with a floating point value as shown below:
#define INT32_MIN (-2147483648L)
void main()
{
float myNumber = 0.0f;
if(myNumber > INT32_MIN)
{
printf("Everything is OK");
}
else
{
printf("The universe is broken!!");
}
}
Codepad link: http://codepad.org/cBneMZL5
To me it looks as though this this code should work fine, but to my surprise it prints out The universe is broken!!.
This code implicitly casts the INT32_MIN to a float, but it turns out that this results in a floating point value of 2147483648.0 (positive!), even though the floating point type is perfectly capable of representing -2147483648.0.
Does anyone have any insights into the cause of this behaviour?
CODE SOLUTION: As Steve Jessop mentioned in his answer, limits.h and stdint.h contain correct (working) int range defines already, so I’m now using these instead of my own #define
PROBLEM/SOLUTION EXPLANATION SUMMARY: Given the answers and discussions, I think this is a good summary of what’s going on (note: still read the answers/comments because they provide a more detailed explanation):
- I’m using a C89 compiler with 32-bit
longs, so any values greater thanLONG_MAXand less or equal toULONG_MAXfollowed by theLpostfix have a type ofunsigned long (-2147483648L)is actually a unary-on anunsigned long(see previous point) value:-(2147483648L). This negation operation ‘wraps’ the value around to be theunsigned longvalue of2147483648(because 32-bitunsigned longs have the range0–4294967295).- This
unsigned longnumber looks like the expected negativeintvalue when it gets printed as anintor passed to a function because it is first getting cast to anint, which is wrapping this out-of-range2147483648around to-2147483648(because 32-bitints have the range -2147483648 to 2147483647) - The cast to
float, however, is using the actualunsigned longvalue2147483648for conversion, resulting in the floating-point value of2147483648.0.
In C89 with a 32 bit
long,2147483648Lhas typeunsigned long int(see 3.1.3.2 Integer constants). So once modulo arithmetic has been applied to the unary minus operation,INT32_MINis the positive value 2147483648 with typeunsigned long.In C99,
2147483648Lhas typelongiflongis bigger than 32 bits, orlong longotherwise (see 6.4.4.1 Integer constants). So there is no problem andINT32_MINis the negative value -2147483648 with typelongorlong long.Similarly in C89 with
longlarger than 32 bits,2147483648Lhas typelongandINT32_MINis negative.I guess you’re using a C89 compiler with a 32 bit
long.One way to look at it is that C99 fixes a “mistake” in C89. In C99 a decimal literal with no
Usuffix always has signed type, whereas in C89 it may be signed or unsigned depending on its value.What you should probably do, btw, is include
limits.hand useINT_MINfor the minimum value of anint, andLONG_MINfor the minimum value of along. They have the correct value and the expected type (INT_MINis anint,LONG_MINis along). If you need an exact 32 bit type then (assuming your implementation is 2’s complement):stdint.hthat works on your C89 compiler, and useint32_tandINT32_MINfrom that.stdint.hyourself, and use the expression in WiSaGaN’s answer. It has typeintifintis at least 32 bits, otherwiselong.