#include<stdio.h> int main(){ float a =5.375; char *p; int i; p =(char *)&a; for(i=0;i<1;i++) printf("%d",p[3]); return 0; } | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

#include<stdio.h> int main(){ float a =5.375; char *p; int i; p =(char *)&a; for(i=0;i<1;i++) printf("%d",p[3]); return 0; }

Answer of the question is 64. Can Someone explain it why ?

25th Mar 2020, 6:20 AM
Nadim Zed
Nadim Zed - avatar
1 Answer
+ 1
Floating point values have a special encoding on the binary level. Going into that would likely exceed this answer, so if you are curious about it, you would have to look it up. Anyway, the only really interesting thing here is the binary represantation of 5.375, which is 01000000 10101100 00000000 00000000 according to the latest floating point standard. The SoloLearn environment is a little endian system, which just means that the most significant byte is stored on the right, so you need to imagine the bytes (not the bits) above reversed. Through the conversion, 'p' is a pointer to a single byte, initially pointing to the first byte of 'a' (which is the least significant one). The call p[ 3 ] is pointer arithmetics translated to *( p + 3 ), so we access the byte three memory cells to the right, which is in our case the most significant byte of 'a': 01000000. Converting that value to decimal, you end up with 64.
25th Mar 2020, 7:10 AM
Shadow
Shadow - avatar