473,471 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

%f undefined behavior?

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;
}
Nov 13 '05 #1
5 1418
Mantorok Redgormor <ne*****@tokyo.com> spoke thus:
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)

--
Christopher Benson-Manica | Jumonji giri, for honour.
ataru(at)cyberspace.org |
Nov 13 '05 #2
In article <41*************************@posting.google.com> ,
Mantorok Redgormor wrote:
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).

--
Andreas Kähäri
Nov 13 '05 #3
Andreas Kahari <ak*******@freeshell.org> wrote:
In article <41*************************@posting.google.com> ,
Mantorok Redgormor wrote:
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).


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
--
What does this red button do?
Nov 13 '05 #4
Irrwahn Grausewitz <ir*****@freenet.de> wrote in message news:<78********************************@4ax.com>. ..
Andreas Kahari <ak*******@freeshell.org> wrote:
In article <41*************************@posting.google.com> ,
Mantorok Redgormor wrote:
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).


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.

--
Peter
Nov 13 '05 #5
ai***@acay.com.au (Peter Nilsson) wrote:
Irrwahn Grausewitz <ir*****@freenet.de> wrote in message news:<78********************************@4ax.com>. ..
Andreas Kahari <ak*******@freeshell.org> wrote: <SNIP>
>
>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).

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.


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...
--
What does this red button do?
Nov 13 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

48
by: marbac | last post by:
Hi, i heard a lot about "undefined behaviour" in this and other newsgroups dealing with c/c++. Is there a list where all cases with undefined behaviour in C++ are listed? regards marbac
19
by: E. Robert Tisdale | last post by:
In the context of the comp.lang.c newsgroup, the term "undefined behavior" actually refers to behavior not defined by the ANSI/ISO C 9 standard. Specifically, it is *not* true that "anything can...
66
by: Mantorok Redgormor | last post by:
#include <stdio.h> struct foo { int example; struct bar *ptr; }; int main(void) { struct foo baz; baz.ptr = NULL; /* Undefined behavior? */ return 0;
25
by: Nitin Bhardwaj | last post by:
Well, i'm a relatively new into C( strictly speaking : well i'm a student and have been doing & studying C programming for the last 4 years).....and also a regular reader of "comp.lang.c" I...
30
by: jimjim | last post by:
Hello, #include <stdio.h> int main(int argc, char *argv) { int x = 1; printf("%d %d %d\n", ++x, x, x++); return 0; }
33
by: dragoncoder | last post by:
Hi all, Does the following code invoke undefined behaviour ? $ cat a1.cc #include <iostream> #include <limits> int main() { int a = INT_MAX/2;
14
by: avsharath | last post by:
In "Bjarne Stroustrup's C++ Style and Technique FAQ" at: http://www.research.att.com/~bs/bs_faq2.html#evaluation-order for the statement: f(v,i++); he says that "the result is undefined...
12
by: Rajesh S R | last post by:
Can anyone tell me what is the difference between undefined behavior and unspecified behavior? Though I've read what is given about them, in ISO standards, I'm still not able to get the...
22
by: blargg | last post by:
Does ~0 yield undefined behavior? C++03 section 5 paragraph 5 seems to suggest so: The description of unary ~ (C++03 section 5.3.1 paragraph 8): But perhaps "one's complement" means the...
33
by: coolguyaroundyou | last post by:
Will the following statement invoke undefined behavior : a^=b,b^=a,a^=b ; given that a and b are of int-type ?? Be cautious, I have not written a^=b^=a^=b ; which, of course, is undefined....
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.