%f undefined behavior?

M

Mantorok Redgormor

Why does the following print -1.997505? Is this because of undefined behavior?

#include <stdio.h>
int main(void)
{
printf("%f\n", 0x1f);
return 0;
}
 
C

Christopher Benson-Manica

Mantorok Redgormor said:
Why does the following print -1.997505? Is this because of undefined behavior?
#include <stdio.h>
int main(void)
{
printf("%f\n", 0x1f);
return 0;
}

Compiling with all warnings on...
test.c:6: warning: double format, different type arg (arg 2)
 
A

Andreas Kahari

Why does the following print -1.997505? Is this because of undefined behavior?

#include <stdio.h>
int main(void)
{
printf("%f\n", 0x1f);
return 0;
}

Here, it prints
-14167097396369144661962308996344933237638
562294362303773503482977858904653824.000000
(except for the line break in the middle)

But then again, you're printing an integer with a floating point
format string, which makes it interpret the bit pattern of the
integer as if it was the bit pattern of a floating point value
(possibly going outside the data that was the integer).
 
I

Irrwahn Grausewitz

Andreas Kahari said:
Here, it prints
-14167097396369144661962308996344933237638
562294362303773503482977858904653824.000000
(except for the line break in the middle)

But then again, you're printing an integer with a floating point
format string, which makes it interpret the bit pattern of the
integer as if it was the bit pattern of a floating point value
(possibly going outside the data that was the integer).

Even worse: it invokes Nasal Demons, aka undefined behaviour, because
%f forces printf to retrieve an argument of sizeof(double), but there
is only sth. of size of an int available.

[OT]
IOW: this will most certainly mess up your stack, whatever that is...
[/OT]

Regards

Irrwahn
 
P

Peter Nilsson

It needn't ever even attempt to interpret the bit pattern of the
integer.
Even worse: it invokes Nasal Demons, aka undefined behaviour, because
%f forces printf to retrieve an argument of sizeof(double), but there
is only sth. of size of an int available.

Huh? %f expects a double, not merely an object the same size of a
double.
[OT]
IOW: this will most certainly mess up your stack, whatever that is...
[/OT]

Not all the world is intel.

There are implementations (modern, in use) which pass parameters via
registers, not a hardware stack.
 
I

Irrwahn Grausewitz

It needn't ever even attempt to interpret the bit pattern of the
integer.


Huh? %f expects a double, not merely an object the same size of a
double.

Hm, I used strange wording here, indeed.
[OT]
IOW: this will most certainly mess up your stack, whatever that is...
[/OT]

Not all the world is intel.

Oh, really? :)
There are implementations (modern, in use) which pass parameters via
registers, not a hardware stack.

Right, and that's just the reason for [OT][/OT] tagging...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top