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.
Markdown
Source Link
unwind
  • 401.8k
  • 64
  • 492
  • 620

I'm using Visual Studio TC compiler for Little Endian. The following is the piece of code:

void main() { float c = 1.0; int a = 0x3F800000; int *ptr = (int *)&c; printf("\n0x%X\n", *ptr); printf("\na = %f", a); printf("\nc = %f", c); return; } 

The output is: 0x3F800000 a = 0.000000 c = 1.000000

0x3F800000 a = 0.000000 c = 1.000000 

Float value 1.0 is 0x3F800000 and stored as 00 00 80 3F in memory for Little Endian. The same value is assigned to int a. How printf prints 0.000000 for int a while 1.000000 for float c? I've seen it prints all integer values as 0.000000, when printed using %f in printf.

Also, since printf is variable argument function, how does it knows whether the passed value in the register is int or float?

I'm using Visual Studio TC compiler for Little Endian. The following is the piece of code:

void main() { float c = 1.0; int a = 0x3F800000; int *ptr = (int *)&c; printf("\n0x%X\n", *ptr); printf("\na = %f", a); printf("\nc = %f", c); return; } 

The output is: 0x3F800000 a = 0.000000 c = 1.000000

Float value 1.0 is 0x3F800000 and stored as 00 00 80 3F in memory for Little Endian. The same value is assigned to int a. How printf prints 0.000000 for int a while 1.000000 for float c? I've seen it prints all integer values as 0.000000, when printed using %f in printf.

Also, since printf is variable argument function, how does it knows whether the passed value in the register is int or float?

I'm using Visual Studio TC compiler for Little Endian. The following is the piece of code:

void main() { float c = 1.0; int a = 0x3F800000; int *ptr = (int *)&c; printf("\n0x%X\n", *ptr); printf("\na = %f", a); printf("\nc = %f", c); return; } 

The output is:

0x3F800000 a = 0.000000 c = 1.000000 

Float value 1.0 is 0x3F800000 and stored as 00 00 80 3F in memory for Little Endian. The same value is assigned to int a. How printf prints 0.000000 for int a while 1.000000 for float c? I've seen it prints all integer values as 0.000000, when printed using %f in printf.

Also, since printf is variable argument function, how does it knows whether the passed value in the register is int or float?

Source Link
bugger
  • 169
  • 1
  • 1
  • 5

Printing int as float in C

I'm using Visual Studio TC compiler for Little Endian. The following is the piece of code:

void main() { float c = 1.0; int a = 0x3F800000; int *ptr = (int *)&c; printf("\n0x%X\n", *ptr); printf("\na = %f", a); printf("\nc = %f", c); return; } 

The output is: 0x3F800000 a = 0.000000 c = 1.000000

Float value 1.0 is 0x3F800000 and stored as 00 00 80 3F in memory for Little Endian. The same value is assigned to int a. How printf prints 0.000000 for int a while 1.000000 for float c? I've seen it prints all integer values as 0.000000, when printed using %f in printf.

Also, since printf is variable argument function, how does it knows whether the passed value in the register is int or float?