I am learning C, and partly to get used to pointers and partly to learn about IEEE 754 floats, I tried the following:
#include <stdio.h>
int main() {
float f1 = -12.78;
const char *p = (char *)&f1;
for(int i=0; i < sizeof(f1); i++){
printf("i: %i and p: %p and byte is %#xn",i,p,*p);
p++;}
}
I don’t get any warnings or errors (unlike most of the versions I tried before!), but I do get strange results:
i: 0 and p: 0x16ce13368 and byte is 0xffffffe1
i: 1 and p: 0x16ce13369 and byte is 0x7a
i: 2 and p: 0x16ce1336a and byte is 0x4c
i: 3 and p: 0x16ce1336b and byte is 0xffffffc1
My (limited) understanding of IEEE 754 is that -12.78 is indeed e1 7a 4c c1
in bytes, but why is the first byte shown as ff ff ff e1
(and similarly for the last byte)?
Is there a better way to do this?
I also tried using char arrays, but didn’t get very far with that. I also tried using a void *p to access the memory holding the float, but couldn’t get that to work either.