printf 中 double 的正确格式说明符
printf
中 double
的正确格式说明符是什么?是%f
还是%lf
?我相信它是 %f
,但我不确定。
代码示例
#include <stdio.h>
int main()
{
double d = 1.4;
printf("%lf", d); // Is this wrong?
}
What is the correct format specifier for double
in printf
? Is it %f
or is it %lf
? I believe it's %f
, but I am not sure.
Code sample
#include <stdio.h>
int main()
{
double d = 1.4;
printf("%lf", d); // Is this wrong?
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
"%f"
是(或至少一种)双精度数的正确格式。 没有float
的格式,因为如果您尝试将float
传递给printf
,它会在printf
接收到1之前,将其提升为double
。"%lf"
在当前标准下也是可接受的 - 如果后跟f
转换说明符,则l
被指定为无效(除其他外)。请注意,这是
printf
格式字符串与scanf
(和fscanf
等)格式字符串显着不同的地方。对于输出,您将传递一个值,当作为可变参数传递时,该值将从float
提升为double
。对于输入,您要传递一个指针,该指针不会被提升,因此您必须告诉scanf
您是否想要读取float
或adouble
,因此对于scanf
,%f
表示您要读取float
和%lf< /code> 表示您想要读取
double
(并且,对于long double
来说,您可以使用%Lf
来读取printf
或scanf
)。<子>
1. C99,§6.5.2.2/6:“如果表示被调用函数的表达式具有不包含原型的类型,则对每个参数执行整数提升,并且具有 float 类型的参数将提升为 double。这些被称为默认参数提升。”在 C++ 中,措辞有些不同(例如,它不使用“原型”一词),但效果是相同的:所有可变参数在被函数接收之前都会经历默认提升。
"%f"
is the (or at least one) correct format for a double. There is no format for afloat
, because if you attempt to pass afloat
toprintf
, it'll be promoted todouble
beforeprintf
receives it1."%lf"
is also acceptable under the current standard -- thel
is specified as having no effect if followed by thef
conversion specifier (among others).Note that this is one place that
printf
format strings differ substantially fromscanf
(andfscanf
, etc.) format strings. For output, you're passing a value, which will be promoted fromfloat
todouble
when passed as a variadic parameter. For input you're passing a pointer, which is not promoted, so you have to tellscanf
whether you want to read afloat
or adouble
, so forscanf
,%f
means you want to read afloat
and%lf
means you want to read adouble
(and, for what it's worth, for along double
, you use%Lf
for eitherprintf
orscanf
).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.
鉴于C99标准(即N1256草案),规则取决于
函数种类:fprintf (printf, sprintf, ...) 或 scanf。
以下是摘录的相关部分:
为
fprintf
指定的相同规则也适用于printf
、sprintf
和类似函数。长话短说,对于
fprintf
,指定了以下说明符和相应的类型:%f
->双%Lf
->长双.对于
fscanf
来说,它是:%f
->浮动%lf
->双%Lf
->长双.Given the C99 standard (namely, the N1256 draft), the rules depend on the
function kind: fprintf (printf, sprintf, ...) or scanf.
Here are relevant parts extracted:
The same rules specified for
fprintf
apply forprintf
,sprintf
and similar functions.The long story short, for
fprintf
the following specifiers and corresponding types are specified:%f
-> double%Lf
-> long double.and for
fscanf
it is:%f
-> float%lf
-> double%Lf
-> long double.它可以是
%f
、%g
或%e
,具体取决于您希望数字的格式。请参阅此处了解更多详细信息。l
修饰符在scanf
和double
中是必需的,但在printf
中不需要。It can be
%f
,%g
or%e
depending on how you want the number to be formatted. See here for more details. Thel
modifier is required inscanf
withdouble
, but not inprintf
.格式
%lf
是double
的完全正确的printf
格式,与您使用的完全一样。你的代码没有任何问题。旧版本(C99 之前的)C 语言不支持
printf
中的格式%lf
,这在doubleprintf
和scanf
中的 code>。这种表面上的不一致已在 C99 中得到修复。您不需要在
printf
中将%lf
与double
一起使用。如果您愿意,也可以使用%f
(%lf
和%f
在printf
中是等效的)。但在现代 C 语言中,更喜欢将%f
与float
一起使用,将%lf
与double
一起使用,%Lf
与long double
,在printf
和scanf
中保持一致。Format
%lf
is a perfectly correctprintf
format fordouble
, exactly as you used it. There's nothing wrong with your code.Format
%lf
inprintf
was not supported in old (pre-C99) versions of C language, which created superficial "inconsistency" between format specifiers fordouble
inprintf
andscanf
. That superficial inconsistency has been fixed in C99.You are not required to use
%lf
withdouble
inprintf
. You can use%f
as well, if you so prefer (%lf
and%f
are equivalent inprintf
). But in modern C it makes perfect sense to prefer to use%f
withfloat
,%lf
withdouble
and%Lf
withlong double
, consistently in bothprintf
andscanf
.%Lf
(注意大写的L
)是长双精度数的“noreferrer">格式说明符。对于普通的
双精度
,可以是%e
、%E
、%f
、%g
或%G
即可。%Lf
(note the capitalL
) is the format specifier for long doubles.For plain
doubles
, either%e
,%E
,%f
,%g
or%G
will do.