Question: Floating point and printf()

adam.denton asd at cbnewsj.ATT.COM
Tue Dec 12 03:07:49 AEST 1989


Here's a seemingly easy question.  It has been nagging me for quite a while.

I would like to know what the CORRECT format specifier is in printf()
to print out values of type `float' and values of type `double'.

According to K&R2, page 244, the format specifiers e, f, and g all default
to type `double'.  K&R2 does not mention the use of the `l' (lower case ell)
in regard to floating point.  Reading the description, it doesn't appear
that there's any way to tell printf() you're passing it a `float' instead
of `double.'

   K&R2:   printf("%f", 3.1416)      type=`double.'
   K&R2:   printf("%lf", 3.1416)     not discussed

Now in the Turbo C 2.0 reference manual under ...printf(), it is mentioned
that the letter ell can be prefixed to e, f, or g to indicate that the
argument is type `double.'  I take this to mean that, if you don't include
the ell, then it assumes the argument is type `float' (since capital L
is available for `long double').

   TC2:    printf("%lf", 3.1416)     type=`double'
   TC2:    printf("%f", 3.1416)      referred to as `floating point' only;
                                     I assume it's `float.'  Am I right?

So...what gives?  Is there a conflict here?  Did K&R2 omit anything?
What does pANS say??  Enquiring minds want to know!

Adam S. Denton
asd at mtqua.ATT.COM
...!att!mtqua!asd



More information about the Comp.lang.c mailing list