Replacing the %f with %d | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Replacing the %f with %d

What happens if you replace the f with a d? Th output is a different number, but where does it come from? https://code.sololearn.com/cQNjXkk5TA4o/?ref=app

10th Dec 2018, 11:43 AM
Laura Schamul
Laura Schamul - avatar
2 Answers
+ 7
The output is due to undefined behaviour. The float is represented usually in IEEE754 binary format (sign, then exponent, and then mantissa) and maybe that was interpreted as an integer. Or something else happened. The reason for this is explained here: https://stackoverflow.com/questions/7480097/what-happens-to-a-float-variable-when-d-is-used-in-a-printf
10th Dec 2018, 11:55 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 5
Very interesting question! Great answer Kinshuk. 8) Also, my favorite Wiki page of all times is https://en.wikipedia.org/wiki/Undefined_behavior LOL!
10th Dec 2018, 12:08 PM
Babak
Babak - avatar