正确的格式说明符是什么double
在 printf 中?是吗%f
或者是%lf
?我相信这是%f
,但我不确定。
代码示例
#include <stdio.h>
int main()
{
double d = 1.4;
printf("%lf", d); // Is this wrong?
}
"%f"
is the (or at least one) correct format for a double. There is no format for a float
, because if you attempt to pass a float
to printf
, it'll be promoted to double
before printf
receives it1. "%lf"
is also acceptable under the current standard -- the l
is specified as having no effect if followed by the f
conversion specifier (among others).
请注意,这是一个地方printf
格式字符串与scanf
(and fscanf
等)格式字符串。对于输出,您传递一个value,将从float
to double
当作为可变参数传递时。对于输入,您传递一个pointer,这是不提倡的,所以你必须告诉scanf
您是否想阅读float
or a double
, 因此对于scanf
, %f
意味着你想阅读float
and %lf
意味着你想阅读double
(而且,就其价值而言,long double
, 你用%Lf
对于任一printf
or scanf
).
1. C99, §6.5.2.2/6: "If the expression that denotes the called function has a type that does not include a prototype, the integer promotions are performed on each argument, and arguments that have type float are promoted to double. These are called the default argument promotions." In C++ the wording is somewhat different (e.g., it doesn't use the word "prototype") but the effect is the same: all the variadic parameters undergo default promotions before they're received by the function.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)