Question 12.9

Someone told me it was wrong to use %lf with printf. How can printf use %f for type double, if scanf requires %lf?


It's true that printf's %f specifier works with both float and double arguments. Due to the ``default argument promotions'' (which apply in variable-length argument lists such as printf's, whether or not prototypes are in scope), values of type float are promoted to double, and printf therefore sees only doubles. See also questions 12.13 and 15.2.

References: K&R1 Sec. 7.3 pp. 145-47, Sec. 7.4 pp. 147-50
K&R2 Sec. 7.2 pp. 153-44, Sec. 7.4 pp. 157-59
ANSI Sec. 4.9.6.1, Sec. 4.9.6.2
ISO Sec. 7.9.6.1, Sec. 7.9.6.2
H&S Sec. 15.8 pp. 357-64, Sec. 15.11 pp. 366-78
CT&P Sec. A.1 pp. 121-33


Read sequentially: prev next up top


This page by Steve Summit // Copyright 1995 // mail feedback