Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value of variable as integer and as double

So I have an unsigned integer Q with the value of 0xfccd11c0.

When I print it with printf("%i", Q); I get -53669440 which is the signed integer representation of 0xfccd11c0 so I get it.

But when I print it with printf("%i", (double)Q); I get 939524096. I DO know that in order to print a double value you need to use %f as the string format, but still, what happens when converting to double that is causing that value to be printed?

like image 415
Yonatan Nir Avatar asked Nov 29 '25 12:11

Yonatan Nir


2 Answers

Q is 4241297856

(double)Q has 64-bit IEEE-754 representation 0x41EF99A238000000

In little-endian, the lower 4 bytes of this occupy the same space as (int)Q would.

0x38000000 is 939524096


Handy online converter: http://babbage.cs.qc.edu/courses/cs341/IEEE-754.html

like image 101
Ben Voigt Avatar answered Dec 02 '25 00:12

Ben Voigt


Using wrong format specifier in function such as printf triggers undefined behaviour; in which case anything could happen.

like image 31
Giorgi Moniava Avatar answered Dec 02 '25 02:12

Giorgi Moniava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!