[comp.lang.c] Question: Floating point and printf

asd@cbnewsj.ATT.COM (adam.denton) (12/12/89)

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@mtqua.ATT.COM
...!att!mtqua!asd

grogers@sushi.uucp (Geoffrey Rogers) (12/13/89)

In article <2777@cbnewsj.ATT.COM> asd@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@convex.com                 | "Abbie Normal!"                 |
| {sun,uunet,uiucdcs}!convex!grogers |                                 |
+------------------------------------+---------------------------------+

chris@mimsy.umd.edu (Chris Torek) (12/13/89)

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

`%e', `%f', and `%g'.

>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.

K&R2 does, however, mention (possibly elsewhere) that it is NOT POSSIBLE
to pass a `float' to a variadic function.  All such values are converted
to double precision in the absence of a prototype, or in the presence of
a variadic prototype.

>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.'

... which is incredibly misleading, since the letter ell can be omitted
to indicate that the argument is type double as well.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@cs.umd.edu	Path:	uunet!mimsy!chris