What is the output for the following code


main{
int x=90;
float *ptr;
ptr=(float *)&x;
*ptr=50.0;
printf("%d",x);
printf("%f",*pf);
}

Though the address of i & pf are same but the value which i get when i print x is a different integer value and when *pf is printed it gives the value 50.0 .why?

Showing Answers 1 - 1 of 1 Answers

answer

  • Oct 12th, 2007
 

It is always helpful to write valid code.  Your example would not compile and the output would not be readable.


1:    int x = 90;
2:    float* ptr;
3:    ptr = (float*)&x;
4:    *ptr = 50.0f;            // NOTE: 50.0 is a double, 50.0f or 50.f is a float
5:    printf( "%xn", x );    // changed to hex, added newline
6:    printf( "%fn", *ptr ); // added newline

1: assigns the local stack variable x the value 90
3: assigns ptr the address of x
4: writes the value 50.f into the memory location
5: prints the hexadecimal value of 50.f
6. prints the value as a float stored in x (which is now 50.f)

The result should be

42480000
50.000000

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions