First, a warning: According to the C++ Standard, this is undefined behavior. The compiler is free to make the program do anything when it executes this line, and in many cases it will not do what the programmer expects. However, for those who are familiar with C++, there is a clear meaning about what was intended, and in most cases the compiler will probably do that.
First, let’s examine what happens if you just use (float)l
, as expected. The program will take the value of l
, interpreted as an int
, and return a floating-point number with the same value; in this case, that’s 10.0
. This is usually what you want, and is perfectly well-defined.
Now, let’s look at the code you actually have: *(float*)&l;
. Based on the particular syntax of this expression, it should be read right-to-left. The &l
part means “get the location where l
is stored in memory”, usually called a “pointer to l
”. Because l
is an int
, the type of this expression is “pointer to int
”.
Then, the (float*)
part is a cast. It says “Treat this pointer-to-int
as a pointer-to-float
”. This should be a pointer to the same location in memory, but when the program accesses it, it will read the bits at that location as a float
.
The first *
dereferences this pointer-to-float
, and gets a float
out. This is the part which is undefined; the language assumes you won’t access the same pointer as two different types. It’s called “pointer aliasing”, and depending on the platform, compiler, optimization flags, etc. can do anything. The intended behavior is probably to get the float
with the same bit pattern as the int
with value 10.
To see what this means, we need to go into how float
s and int
s are stored at the hardware level. An int
is just stored as a binary number with the same value, with some complications around negative numbers. A float
is more complicated, and there are even more complications if float
s and int
s are stored with different endianness.
The most likely value of f
is, as described by rex, 1.4e-44 (that is, 1.4 times ten to the negative 44). Another reasonable value would be about 6.16e-33, if float
s and int
s had different endianness.