[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: weird printf() behavior
From: |
Matthias Urlichs |
Subject: |
Re: weird printf() behavior |
Date: |
Wed, 06 Aug 2003 10:52:20 +0200 |
User-agent: |
Pan/0.14.0.93 (He's Upstairs, Helping Porcelain Make the Bed) |
Hi, Kai-Min Sung wrote:
> Hi,
> I'm not sure if this is a valid bug or just a known quirk, but I
> wrote the following test code and saw some rather strange output:
>
Floats are promoted to double when passed as arguments.
> printf("%x, %f\n", f, f);
> lever:~/tmp>./foo
> a0000000, -0.000000
>
So what you're really doing here is print a dword-swapped double. ;-)
To demonstrate what happens behind the scenes:
@linux tmp $ ./foo
1.100000, 3ff19999a0000000, 3f8ccccd, 1.100000
@linux tmp $ cat foo.c
#include <stdio.h>
int main()
{
float f = 1.1;
double ff = f;
/* Yes, the first two format arguments are swapped intentionally. */
printf("%f, %llx, %lx, %f\n", *(long long *)&ff, f, *(long *)&f, f);
return 0;
}
--
Matthias Urlichs | {M:U} IT Design @ m-u-it.de | address@hidden
Disclaimer: The quote was selected randomly. Really. | http://smurf.noris.de
--
She's so tough she won't take 'yes' for an answer.