Question: Floating point and printf()

Geoffrey Rogers grogers at sushi.uucp
Wed Dec 13 08:24:12 AEST 1989


In article <2777 at cbnewsj.ATT.COM> asd at cbnewsj.ATT.COM (adam.denton) writes:
>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'.
>

The correct format specifier are %e, %g or %f to print values of type
float or 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.'

That because the compiler is going to convert all arguments of type
float to type double, if that argument does not have a protype and
remember that the only arguments that have a protype with printf is
the first (or first 2 for fprintf and sprintf).

>
>   K&R2:   printf("%f", 3.1416)      type=`double.'
>   K&R2:   printf("%lf", 3.1416)     not discussed
[text deleted]
>
>   TC2:    printf("%lf", 3.1416)     type=`double'
By pANS the above is undefined.

>   TC2:    printf("%f", 3.1416)      referred to as `floating point' only;
>                                     I assume it's `float.'  Am I right?
By pANS the above should work on types of double (remember: value of type
get converted to double before the call).

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

It appears that TC2 is wrong with regards to pANS. Yes there is a conflict here.
No, K&R2 did not omit anything. K&R2 document the same thing as pANS, since
it is based from the standrad.

+------------------------------------+---------------------------------+
| Geoffrey C. Rogers   		     | "Whose brain did you get?"      |
| grogers at convex.com                 | "Abbie Normal!"                 |
| {sun,uunet,uiucdcs}!convex!grogers |                                 |
+------------------------------------+---------------------------------+



More information about the Comp.lang.c mailing list