I understand that this must be related to the binary representation
No, it is undefined behavior (UB).
"%d"
with a double
does not match.
If a conversion specification is invalid, the behavior is undefined. C17dr § 7.21.6.1 9
The output and behavior is not defined (UB) by the language. Anything may happen.
On some systems, the UB might see a portion of the binary pattern interpreted as an int
.
On another systems, int
and double
are passed in different ways and the output seen is based on garbage.
On another system or just another day, bad things happen.
It is all undefined behaviors (UB).
explain what really happened
To get an idea of what might have happened in OP's machine, use hexadecimal outputs.
printf("%x\n", (unsigned) -1717986918); // UB to print a negative `int` with `"%x"`
printf("%a\n", 0.4);
9999999a
0x1.999999999999ap-2
So it looks like the least significant 4 bytes of 0.4
significand were interpreted as the int
.
Still UB. Anything may happen.