-3

What will be output if you will compile and execute the following c code?

void main()
{
   int i;
   float a=5.2;
   char *ptr;
   ptr=(char *)&a;

   for(i=0;i<=3;i++)
      printf("%d ",*ptr++);
}

Can anyone answer and justify?

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
Bhargav
  • 21
  • 5

3 Answers3

1
ptr=(char *)&a;

You are type casting the address of float variable to as character pointer.

and next you are accessing that memory.

So here you need to understand how float variable are stored. and then see architecture of your system(Little endian or big endian) then only you can map its content

see How to represent FLOAT number in memory in C

Community
  • 1
  • 1
Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222
0

I guess You are trying to understand how IEE754 works.

The following code could show you how float is represented:

#include <stdio.h>
#include <math.h>

typedef union {
  float f;
  struct {
    unsigned int mantissa : 23;
    unsigned int exponent : 8;
    unsigned int sign : 1;
  } parts;
} myStruct;

int main()
{
    myStruct a;

    a.f=5.2;
    float Calculated;

    unsigned int Temp = a.parts.mantissa;
    double significand = 1;

    // LSB will be 1/(2^24)
    double bitValue = (double)(1)/(double)(16777216);

    // Calculate the significand value
    do
    {
        bitValue *= 2;
        significand += ((Temp&0x00000001) == 1) ? bitValue : 0;

    }while (Temp>>=1);

    printf("Hex Value   = %04x\n",*((unsigned int *)&a.f));
    printf("Float Value = %f\n",a.f);
    printf("exp         = %i, 0x%02x\n",a.parts.exponent, (unsigned int)(a.parts.exponent));
    printf("sign        = %i\n",a.parts.sign);
    printf("mantissa    = %i, 0x%02x\n",a.parts.mantissa,(unsigned int)(a.parts.mantissa));
    printf("significand = %1.10f\n\n", significand);


    Calculated = pow(-1, a.parts.sign) * pow(2,a.parts.exponent-127) * significand;
    printf ("Calculated: %f\n", Calculated);

    return 0;
}

Compile it with command:

gcc -o float_IEEE754 float_IEEE754.c -lm -Wall
LPs
  • 16,045
  • 8
  • 30
  • 61
-1

Without knowing what it is running on, and what it was compiled on it would be hard to say? Are you are aware some systems store bytes that make up longer types the opposite way around, that the size of an int or float will be a different number of bytes, and that compilers may space out parameters on the stack, or reorder them....

So you (conceivably) could end up with a two byte float (ok, not terrible realistic, but possible) that laid out in memory next to the int is:

Float a [Low byte]
Float a [High byte]
Int i [Low byte]
Int i [High byte]
char * ptr [Low byte]
char * ptr ... etc

And your iteration of 4 bytes' worth would go straight through both stack variables....

More interestingly what if the compiler laid it out the way you have listed but again has that (silly) two byte float (ignore the byte order):

Int i [Low byte]
Int i [High byte]
Float a [Low byte]
Float a [High byte]
char * ptr [Low byte]
char * ptr ... etc

your four byte iteration will start examining the contents of the pointer that is examining (itself).

So, when examining data in the loop, you may want to use the size of the type you are examining:

for (i=0; i<= sizeof(a); i++)
Meh-Lindi
  • 29
  • 6