Undefined Behavior of printf

Q

quarkLore

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;
}
 
G

Guest

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? 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.h> must 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.
 
R

Richard Heathfield

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]

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.
 
J

jaysome

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
 
R

Richard Bos

[ 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
 
Q

quarkLore

[ 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.
 
D

Dave Hansen

[ 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
 
K

Keith Thompson

quarkLore said:
[ 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>
 
R

Richard Bos

Dave Hansen said:
[ 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
 

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

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top