Help!Printing a single byte!
I wanted to see how float numbers were stored in computer's memory. So I typed the following code:
1 2 3 4 5 6 7 8 9 10
|
#include <stdio.h>
int main()
{
float y = 5.0;
int i;
for(i=0;i<sizeof(float);i++)
printf ("\n%x",*((char*)&y+i));
return 0;
}
|
The output was:
I can't understand why in the third line instead of the third byte's value I got "ffffffa0".
Could someone provide me with an explanation?
A: %x expects an int type, you're giving it a char type.
B: You're using signed representations when you want unsigned.
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#include <stdio.h>
int main()
{
float y = 5.0;
int i;
unsigned char* p = (unsigned char*)&y ;
for(i=0;i<sizeof(float);i++)
printf ("\n%X", (unsigned)*p++);
return 0;
}
|
Thanks for help. Now it's working!
Topic archived. No new replies allowed.