Hi all!
Please somebody explain me how this snippet is working!!

main()
{
float a 4.379;
char*p;
int i;
p=(char*)&a;
for(i=1;i<=3;i++)
printf("%02X",(unsigned char)p[i]);
}

Hi all!
Please somebody explain me how this snippet is working!!

main()
{
float a 4.379;
char*p;
int i;
p=(char*)&a;
for(i=1;i<=3;i++)
printf("%02X",(unsigned char)p[i]);
}

The code simply assigns the address of "a" to the character pointer "p" and then proceeds to check the value of the four bytes at address p[0], p[1],p[2], and p[3]...

Well it looks like that's what it's supposed to do.
It should print the hex representation of the floating point number 4.379.

But I don't see how it will compile and it will only print 3 of the 4 hex bytes.

function to display the floating number
the float numbers are stored in sign exponent mantissa fashion
furthur reading
http://steve.hollasch.net/cgindex/coding/ieeefloat.html

void test()
{
    float f=5.25;
    char *p=(char *)&f;
    int i;
    for(i = 3; i >= 0; i-- )
             printf("%x\n",p[i]);
}

refer the attachment for out put on my system .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.