473,397 Members | 2,084 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Undefined Behavior of printf

Following program is erroneous as I am passing a float as int. The
output is

---
|
V
Int f is 0 float f is 0.000000
[return value 32 error number 0]

float f is 3.000000 int f is 0
[return value 33 error number 0]
---

In the first print why is the second value not right? I am using 'gcc
version 3.2.2' on Linux.
The error should be contained only in first value as it will try to
read float as integer.

Is it totally unpredictable?
#include <stdio.h>
#include <errno.h>

extern int errno;

int main()
{
int n;
float f=3.0f;
/* Print f as int and then float */
n = printf("\nInt f is %d float f is %f\n",f,f);
printf("[return value %d error number %d]\n",n,errno);
/* Print f as float and then int */
n = printf("\nfloat f is %f int f is %d \n",f,f);
printf("[return value %d error number %d]\n",n,errno);
return 0;
}

Feb 9 '07 #1
8 2647
quarkLore wrote:
Following program is erroneous as I am passing a float as int. The
output is

---
|
V
Int f is 0 float f is 0.000000
[return value 32 error number 0]

float f is 3.000000 int f is 0
[return value 33 error number 0]
---

In the first print why is the second value not right? I am using 'gcc
version 3.2.2' on Linux.
The error should be contained only in first value as it will try to
read float as integer.
Once any aspect of the behaviour is undefined, so is the complete
behaviour. You're not even sure to the text that separates the
numbers.
Is it totally unpredictable?
For a specific implementation, you may be able to look at how printf
is written, or figure out how it might have been written. Consider how
the va_ macros in <stdarg.hmust work, and consider that printf
typically uses the same method to access its arguments.

But as far as standard C is concerned, yes, it is totally
unpredictable.

Feb 9 '07 #2
quarkLore said:
Following program is erroneous as I am passing a float as int. The
output is

---
|
V
Int f is 0 float f is 0.000000
[return value 32 error number 0]

float f is 3.000000 int f is 0
[return value 33 error number 0]
---

In the first print why is the second value not right?
Because your program is erroneous, and exhibits undefined behaviour as a
consequence. Once you break the rules, C no longers defines what your
program does.
I am using 'gcc
version 3.2.2' on Linux.
The error should be contained only in first value as it will try to
read float as integer.
C doesn't guarantee that.
>
Is it totally unpredictable?
From the point of view of the C Standard, yes. Whether the results are
predictable for a given implementation depends on the implementation.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 9 '07 #3
On 8 Feb 2007 21:46:51 -0800, "quarkLore" <ag*************@gmail.com>
wrote:
>Following program is erroneous as I am passing a float as int. The
output is

---
|
V
Int f is 0 float f is 0.000000
[return value 32 error number 0]

float f is 3.000000 int f is 0
[return value 33 error number 0]
---

In the first print why is the second value not right? I am using 'gcc
version 3.2.2' on Linux.
The error should be contained only in first value as it will try to
read float as integer.

Is it totally unpredictable?
As far as the C Standard is concerned, yes.
>

#include <stdio.h>
#include <errno.h>

extern int errno;

int main()
{
int n;
float f=3.0f;
/* Print f as int and then float */
n = printf("\nInt f is %d float f is %f\n",f,f);
printf("[return value %d error number %d]\n",n,errno);
/* Print f as float and then int */
n = printf("\nfloat f is %f int f is %d \n",f,f);
printf("[return value %d error number %d]\n",n,errno);
return 0;
}
You shouldn't need any help from tools to tell you that undefined
behavior of this sort is asking for trouble. Nevertheless, here's what
PC-lint says:

PC-lint for C/C++ (NT) Vers. 8.00u, Copyright Gimpel Software
1985-2006

--- Module: stdc.c (C)
_
extern int errno;
stdc.c(4) : Info 762: Redundantly declared symbol 'errno' previously
declared at line 68, file errno.h
_
n = printf("\nInt f is %d float f is %f\n",f,f);
stdc.c(11) : Warning 559: Size of argument no. 2 inconsistent with
format
_
n = printf("\nfloat f is %f int f is %d \n",f,f);
stdc.c(14) : Warning 559: Size of argument no. 3 inconsistent with
format

Best regards
--
jay
Feb 9 '07 #4
"quarkLore" <ag*************@gmail.comwrote:

[ Rearranged for efficient reading... ]
float f=3.0f;
/* Print f as int and then float */
n = printf("\nInt f is %d float f is %f\n",f,f);
Int f is 0 float f is 0.000000
[return value 32 error number 0]
In the first print why is the second value not right? I am using 'gcc
version 3.2.2' on Linux.
In addition to what the others wrote (it's UB; all bets are off), note
that there are very real reasons why printf() could get confused here.

Assume, for example, that ints are smaller than floats. In our
hypothetical case, an int is 2 bytes, a float is 4 bytes, and a char *
is 4 bytes. What gets passed to printf() is:

[4 bytes char *] [4 bytes float] [4 bytes float]

What printf() could do is:

Read the first 4 bytes, and decode the string that resides at that
address.
See a %d specifier. Decide to read 2 bytes.
Read the next 2 bytes that were passed to it, and print them as if
they were an int.
See a %f specifier. Decide to read 4 bytes.
Read the next 4 bytes that were passed. If you've counted along,
you'll have noticed that this is not a float, but the second half
of the first float plus the first half of the second, glued together
the wrong way.
Print this chimaera-float as if it were a real one.

And that's just the innocuous version, where you're just getting
semi-random garbage printed. You _could_ get it to read a trap value and
crash. You could confuse printf()'s internal workings so badly that from
then on, even properly passed values get printed wrong. IOW, there are
quite real reasons why this has undefined behaviour.

Richard
Feb 9 '07 #5
On Feb 9, 4:28 pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
"quarkLore" <agarwal.prat...@gmail.comwrote:

[ Rearranged for efficient reading... ]
float f=3.0f;
/* Print f as int and then float */
n = printf("\nInt f is %d float f is %f\n",f,f);
Int f is 0 float f is 0.000000
[return value 32 error number 0]
In the first print why is the second value not right? I am using 'gcc
version 3.2.2' on Linux.

In addition to what the others wrote (it's UB; all bets are off), note
that there are very real reasons why printf() could get confused here.

Assume, for example, that ints are smaller than floats. In our
hypothetical case, an int is 2 bytes, a float is 4 bytes, and a char *
is 4 bytes. What gets passed to printf() is:
Thanks for the response I know all that.
@Richard Bos: I stated that I am using Linux. It uses 4 byte integer,
float and char * so the assumption and conclusions hold invalid in
this particular case. The plausible explanation looks right if
assumption is satisfied

I think Richard Heathfield is right. Hence I should post this to Linux
platform specific forum.

Feb 9 '07 #6
On Feb 9, 5:28 am, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
"quarkLore" <agarwal.prat...@gmail.comwrote:

[ Rearranged for efficient reading... ]
float f=3.0f;
/* Print f as int and then float */
n = printf("\nInt f is %d float f is %f\n",f,f);
Int f is 0 float f is 0.000000
[return value 32 error number 0]
In the first print why is the second value not right? I am using 'gcc
version 3.2.2' on Linux.

In addition to what the others wrote (it's UB; all bets are off), note
that there are very real reasons why printf() could get confused here.

Assume, for example, that ints are smaller than floats. In our
hypothetical case, an int is 2 bytes, a float is 4 bytes, and a char *
is 4 bytes. What gets passed to printf() is:

[4 bytes char *] [4 bytes float] [4 bytes float]
Ummm, no. Parameters to variadic functions (like printf) always
undergo the usual argument promotions. You can't pass a float to
printf: It always gets converted to a double.

Regards,
-=Dave

Feb 9 '07 #7
"quarkLore" <ag*************@gmail.comwrites:
On Feb 9, 4:28 pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
>"quarkLore" <agarwal.prat...@gmail.comwrote:

[ Rearranged for efficient reading... ]
float f=3.0f;
/* Print f as int and then float */
n = printf("\nInt f is %d float f is %f\n",f,f);
Int f is 0 float f is 0.000000
[return value 32 error number 0]
In the first print why is the second value not right? I am using 'gcc
version 3.2.2' on Linux.

In addition to what the others wrote (it's UB; all bets are off), note
that there are very real reasons why printf() could get confused here.

Assume, for example, that ints are smaller than floats. In our
hypothetical case, an int is 2 bytes, a float is 4 bytes, and a char *
is 4 bytes. What gets passed to printf() is:
Thanks for the response I know all that.
@Richard Bos: I stated that I am using Linux. It uses 4 byte integer,
float and char * so the assumption and conclusions hold invalid in
this particular case. The plausible explanation looks right if
assumption is satisfied
<OT>
Linux does not guarantee that char* is 4 bytes; I use Linux systems on
which all pointers are 8 bytes.
</OT>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 9 '07 #8
"Dave Hansen" <id**@hotmail.comwrote:
On Feb 9, 5:28 am, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
"quarkLore" <agarwal.prat...@gmail.comwrote:

[ Rearranged for efficient reading... ]
float f=3.0f;
/* Print f as int and then float */
n = printf("\nInt f is %d float f is %f\n",f,f);
Int f is 0 float f is 0.000000
[return value 32 error number 0]
In the first print why is the second value not right? I am using 'gcc
version 3.2.2' on Linux.
In addition to what the others wrote (it's UB; all bets are off), note
that there are very real reasons why printf() could get confused here.

Assume, for example, that ints are smaller than floats. In our
hypothetical case, an int is 2 bytes, a float is 4 bytes, and a char *
is 4 bytes. What gets passed to printf() is:

[4 bytes char *] [4 bytes float] [4 bytes float]

Ummm, no. Parameters to variadic functions (like printf) always
undergo the usual argument promotions. You can't pass a float to
printf: It always gets converted to a double.
*Shrug* Assume a double is also 4 bytes, then. Or assume an int is 4
bytes and a double 6, or 8. The point remains the same.

Richard
Feb 12 '07 #9

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

Similar topics

2
by: Keith Doyle | last post by:
I'm curious if setvbuf behavior is known to be undefined with regards to effects it may have on the file pointer. I've found that the following code works different on AIX 4.3.3 xlc and FreeBSD...
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; }
7
by: deepak | last post by:
Using 'char' as an array index is an undefined behavior?
13
by: bbaale | last post by:
Why would this code cause undefined behavior? #include <stdio.h> #include <stdlib.h> #include <string.h> #define TRUE 1 #define FALSE 0
2
by: Chris Thomasson | last post by:
I was wondering if the 'SLINK_*' and 'SLIST_*' macros, which implement a simple singly-linked list, will produce _any_ possible undefined behavior: ____________________________ #include...
12
by: Franz Hose | last post by:
the following program, when compiled with gcc and '-std=c99', gcc says test.c:6: error: jump into scope of identifier with variably modified type that is, it does not even compile. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...
0
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...

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.