I wanted to see the float value 3.14159265 in IEEE754 format representation. So wrote this test code.
#include <stdio.h>
main()
{
int i;
float a = 3.141592654;
printf("a = %f\n", a);
// print test 1
printf("test 1 : a = %x\n", a);
printf("test 1 2nd time : a = %x\n", a);
// print test 2
char *pt = (char *)&a;
printf("test 2 :\n");
for(i=0;i<4;i++){
printf("%x ", (char)(*pt++));
}
printf("\n");
}
When I run it, several times, it shows like this :
ckim@stph45:~/test5] test
a = 3.141593
test 1 : a = e0cd2000
test 1 2nd time : a = e0cd2000
test 2 :
ffffffdb f 49 40
ckim@stph45:~/test5] test
a = 3.141593
test 1 : a = 520db000
test 1 2nd time : a = 520db000
test 2 :
ffffffdb f 49 40
ckim@stph45:~/test5] test
a = 3.141593
test 1 : a = 3b373000
test 1 2nd time : a = 3b373000
test 2 :
ffffffdb f 49 40
ckim@stph45:~/test5] test
a = 3.141593
test 1 : a = 18a6d000
test 1 2nd time : a = 18a6d000
test 2 :
ffffffdb f 49 40
First question is : when using printf with %x, I expected to see 40490fdb which is 3.14159265 in IEEE754 format. But the value changes everytime I run it as you can see. What's wrong with this here? Maybe I'm missing something very basic.
Anoother question is, when I use character pointer and print the hex values as in test 2 above, how can I print 'db' instead of 'fffffdb'? (the value is shown sign extened). I think I've done these things before but can't remember it now. (My machine is little endian so LSB is shown first.)