Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added 193 characters in body
Source Link
Chris.Huang
  • 555
  • 1
  • 4
  • 10

I meet the similar issue , and finally I develop a way to solve it , not sure whether that's what you want.The key point is that : you should pass a float instead of a integer .

#include <stdio.h> void printIntAsFloat(); int main(void){ printIntAsFloat(); } void printIntAsFloat(){ float c = 1.0; int a = 0x3F800000; int *ptr = (int *)&c; float *fp = (float*)((void*)&a); /*this is the key point*/ printf("\n0x%X\n", *ptr); printf("\na = %f", a); printf("\nc = %f", c); printf("\nfp = %f", *fp); return; } 

the output is like this :

0x3F800000 a = 0.000000 c = 1.000000 fp = 1.000000 

OS : Fedora21 64 bit GCC version :gcc version 4.9.2 20141101 (Red Hat 4.9.2-1) (GCC)

I meet the similar issue , and finally I develop a way to solve it , not sure whether that's what you want.The key point is that : you should pass a float instead of a integer .

#include <stdio.h> void printIntAsFloat(); int main(void){ printIntAsFloat(); } void printIntAsFloat(){ float c = 1.0; int a = 0x3F800000; int *ptr = (int *)&c; float *fp = (float*)((void*)&a); /*this is the key point*/ printf("\n0x%X\n", *ptr); printf("\na = %f", a); printf("\nc = %f", c); printf("\nfp = %f", *fp); return; } 

I meet the similar issue , and finally I develop a way to solve it , not sure whether that's what you want.The key point is that : you should pass a float instead of a integer .

#include <stdio.h> void printIntAsFloat(); int main(void){ printIntAsFloat(); } void printIntAsFloat(){ float c = 1.0; int a = 0x3F800000; int *ptr = (int *)&c; float *fp = (float*)((void*)&a); /*this is the key point*/ printf("\n0x%X\n", *ptr); printf("\na = %f", a); printf("\nc = %f", c); printf("\nfp = %f", *fp); return; } 

the output is like this :

0x3F800000 a = 0.000000 c = 1.000000 fp = 1.000000 

OS : Fedora21 64 bit GCC version :gcc version 4.9.2 20141101 (Red Hat 4.9.2-1) (GCC)

Source Link
Chris.Huang
  • 555
  • 1
  • 4
  • 10

I meet the similar issue , and finally I develop a way to solve it , not sure whether that's what you want.The key point is that : you should pass a float instead of a integer .

#include <stdio.h> void printIntAsFloat(); int main(void){ printIntAsFloat(); } void printIntAsFloat(){ float c = 1.0; int a = 0x3F800000; int *ptr = (int *)&c; float *fp = (float*)((void*)&a); /*this is the key point*/ printf("\n0x%X\n", *ptr); printf("\na = %f", a); printf("\nc = %f", c); printf("\nfp = %f", *fp); return; }