Re: libc/printf bug



Jan Panteltje <pNaonStpealmtje@xxxxxxxxx> writes:

On a sunny day (27 Feb 2006 12:09:54 -0800) it happened "bill pursell"
<bill.pursell@xxxxxxxxx> wrote in
<1141070994.179527.261050@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>:

The following code exhibits unexpected behavior. Either it's
a bug in libc, or the author (me) is blind as a bat. Am I missing
something here, or is something horribly wrong? It appears
that printf is mangling the address unless the second argument
gets cast. I don't see that the cast should have any effect at all
on the output. Any thoughts?

[tmp]$ cat printf_bug.c


int
main()
{

float a=0;

printf("%p\n", &a);
printf("%x %p\n", a, &a);
printf("%x %p\n", (int)a, &a);
}
[tmp]$ ./a.out
0xfefb6764
0 (nil)
0 0xfefb6764
[tmp]$

How would you print a float in hex?

Actually, floating point types now have a hexadecimal
representation. See if your system/C-implementation has the "a" and
"A" conversion specifiers.

I think print will convert it to integer, and that is 0.

It will not: variadic functions are "dumb" about what you're passing
it. You have to explicitly cast.
.



Relevant Pages

  • Re: libc/printf bug
    ... I don't see that the cast should have any effect at all ... How would you print a float in hex? ... So seems printf gets confused because you present it with a float when i t wants an integer. ... This behavior I have seen in other cases where wrong or missing arguments were presented too. ...
    (comp.os.linux.development.apps)
  • Re: libc/printf bug
    ... Am I missing ... that printf is mangling the address unless the second argument ... I don't see that the cast should have any effect at all ... When passed to a function with variable arguments, float arguments are ...
    (comp.os.linux.development.apps)
  • libc/printf bug
    ... or the author is blind as a bat. ... Am I missing ... I don't see that the cast should have any effect at all ...
    (comp.os.linux.development.apps)
  • Re: internals of typecasting
    ... > object of type int is applied an cast of float the result ... > would be of type float. ... casting from floating-point numbers to integers. ...
    (comp.lang.c)
  • Re: Problems With strtof()
    ... >I then tried to convert it into a float and store it in another variable. ... The cast is unnecessary but also not harmful. ... really have a C99 compiler or is this a typo? ...
    (comp.lang.c)