I am trying to print out the floating point values 0x40a00000 and 0xc0200000. But the values that I print out and the correct values according to the IEEE-754 Floating Point Con
These are assigning the float representation of the hexadecimal numbers to the floats.
Instead, do this:
int i1 = 0x40a00000;
int i2 = 0xc0200000;
float tmp1, tmp2;
memcpy(&tmp1, &i1, sizeof(int));
memcpy(&tmp2, &i2, sizeof(int));
Print them:
printf("tmp1 = %.2f\n", tmp1);
printf("tmp2 = %.2f\n", tmp2);
Output:
tmp1 = 5.00
tmp2 = -2.50
Full example:
#include <stdio.h>
#include <string.h>
int main(void)
{
int i1 = 0x40a00000;
int i2 = 0xc0200000;
float tmp1, tmp2;
memcpy(&tmp1, &i1, sizeof(int));
memcpy(&tmp2, &i2, sizeof(int));
printf("tmp1 = %.2f\n", tmp1);
printf("tmp2 = %.2f\n", tmp2);
}
int i1 = 0x40a00000;
int i2 = 0xc0200000;
float f1 = *(float*)&i1;
float f2 = *(float*)&i2;
printf("f1 = %.2f\n", f1);
printf("f2 = %.2f\n", f2);
These aren't doing what you think they do:
float tmp1 = 0x40a00000;
float tmp2 = 0xc0200000;
You are simply using the hexadecimal representation of the decimal integers that are getting printed; they do not shove these bytes in so they can be interpreted as floats.
It sounds like what you want to do is (somehow) get the bytes you want somewhere, get the address of that, and cast it to be a pointer to a float, which when dereferenced will be interpreted as a float.
union
{
int i;
float f;
}k1,k2;
k1.i = 0x40a00000;
k2.i = 0xc0200000;
printf("f1 = %.2f\n", k1.f);
printf("f2 = %.2f\n", k2.f);