Re: [C] erroneous fprintf output

From: Arthur J. O'Dwyer (ajo_at_nospam.andrew.cmu.edu)
Date: 05/11/04


Date: Mon, 10 May 2004 19:44:17 -0400 (EDT)


On Mon, 10 May 2004, Curley Q. wrote:
>
> Pedro Graca wrote:
> > Your previous printf() call correctly uses %f.
> > I suggest you remove the "l"s from the specifiers in fprintf().
> >
> >
> > fprintf(output, "sum1: %.2f sum2: %.2f\n", sum1, sum2);
>
> I believe %lf is the correct double format specifier for fprintf.

  You believe incorrectly. %lf *is* the correct format specifier
for *scanf, but not *printf --- because whereas there is a significant
difference between (float*) and (double*), any values of type (float)
get implicitly converted to values of type (double) when passed to
variadic functions such as *printf. Since there is no way for
*printf to receive a value of type (float), it makes sense that there
be no corresponding format specifier.

> If this were the problem why would one of the %lf formatted
> values be printed correctly and not the other?

  Does it matter?

-Arthur



Relevant Pages

  • Re: Question for a REAL expert on casting double to float...
    ... Scanf actually treats "%d" as a signed integer. ... I blew off full checking of the complete format specifier for "floats" ... sscanf uses %f for a float rather than a double. ... floating point input format. ...
    (comp.lang.c)
  • Re: scanf to handle double
    ... "%lf" with printf as it may not work. ... If he can do away with l in printf why cant he do so in scanf? ... If float and double have different sizes, then one _cannot_ use "%f" for both -- if we assume different sizes, then we would only change the first sizeof bytes of the double object which almost certainly does not yield the same value. ...
    (comp.lang.c)
  • printf() and scanf() questions
    ... printf() provides "%f" format specifier for double. ... Usually implemented as a variadic function how can it discern if the argument is float or double? ... The same question also applies when printing a signed char value with "%d" format specifier. ...
    (comp.lang.c)
  • Re: Writing To Binary File In C Program
    ... "Karl Heinz Buchegger" wrote in message ... > %f float ... > Some compilers provide a %lf in the printf case for double. ... did that for the scanf now also, still only 6 decimal places displayed ...
    (alt.comp.lang.learn.c-cpp)
  • Re: printf() and scanf() questions
    ... printf() provides "%f" format specifier for double. ... implemented as a variadic function how can it discern if the argument is ... Shouldn't we cast to double when passing a float value? ...
    (comp.lang.c)