printf 中 double 的正确格式说明符

发布于 2024-10-03 18:22:15 字数 263 浏览 0 评论 0原文

printfdouble 的正确格式说明符是什么?是%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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

漆黑的白昼 2024-10-10 18:22:15

"%f" 是(或至少一种)双精度数的正确格式。 没有 float 的格式,因为如果您尝试将 float 传递给 printf,它会在 printf 接收到1之前,将其提升为 double"%lf" 在当前标准下也是可接受的 - 如果后跟 f 转换说明符,则 l 被指定为无效(除其他外)。

请注意,这是 printf 格式字符串与 scanf(和 fscanf 等)格式字符串显着不同的地方。对于输出,您将传递一个,当作为可变参数传递时,该值将从float 提升为double。对于输入,您要传递一个指针,该指针不会被提升,因此您必须告诉scanf您是否想要读取float或a double,因此对于 scanf%f 表示您要读取 float%lf< /code> 表示您想要读取 double (并且,对于 long double 来说,您可以使用 %Lf 来读取printfscanf)。


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

Note that this is one place that printf format strings differ substantially from scanf (and fscanf, etc.) format strings. For output, you're passing a value, which will be promoted from float to double when passed as a variadic parameter. For input you're passing a pointer, which is not promoted, so you have to tell scanf whether you want to read a float or a double, so for scanf, %f means you want to read a float and %lf means you want to read a double (and, for what it's worth, for a long double, you use %Lf for either 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.

小…红帽 2024-10-10 18:22:15

鉴于C99标准(即N1256草案),规则取决于
函数种类:fprintf (printf, sprintf, ...) 或 scanf。

以下是摘录的相关部分:

前言

第二版取消并取代第一版 ISO/IEC 9899:1990,并经 ISO/IEC 9899/COR1:1994、ISO/IEC 9899/AMD1:1995 和 ISO/IEC 9899/COR2 修订和更正:1996。
与上一版本相比的主要变化包括:

  • %lf 允许在 printf 中使用转换说明符

7.19.6.1 fprintf 函数

7 长度修饰符及其含义是:

l (ell) 指定 (...) 对后面的 a、A、e、E、f、F、g 或 G 转换说明符没有影响。

L 指定后面的 a、A、e、E、f、F、g 或 G 转换说明符适用于长双精度参数。

fprintf 指定的相同规则也适用于 printfsprintf 和类似函数。

7.19.6.2 fscanf 函数

11 长度修饰符及其含义是:

l (ell) 指定 (...) 随后的 a、A、e、E、f、F、g 或 G 转换说明符适用于类型指针为双;

L 指定以下 a、A、e、E、f、F、g 或 G 转换
说明符适用于类型指针指向 long double 的参数。

12 转换说明符及其含义为:
a,e,f,g 匹配可选带符号的浮点数,(...)

14 转换说明符 A、E、F、G 和 X 也是有效的,并且其行为分别与 a、e、f、g 和 x 相同。

长话短说,对于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:

Foreword

This second edition cancels and replaces the first edition, ISO/IEC 9899:1990, as amended and corrected by ISO/IEC 9899/COR1:1994, ISO/IEC 9899/AMD1:1995, and ISO/IEC 9899/COR2:1996.
Major changes from the previous edition include:

  • %lf conversion specifier allowed in printf

7.19.6.1 The fprintf function

7 The length modifiers and their meanings are:

l (ell) Specifies that (...) has no effect on a following a, A, e, E, f, F, g, or G conversion specifier.

L Specifies that a following a, A, e, E, f, F, g, or G conversion specifier applies to a long double argument.

The same rules specified for fprintf apply for printf, sprintf and similar functions.

7.19.6.2 The fscanf function

11 The length modifiers and their meanings are:

l (ell) Specifies that (...) that a following a, A, e, E, f, F, g, or G conversion specifier applies to an argument with type pointer to double;

L Specifies that a following a, A, e, E, f, F, g, or G conversion
specifier applies to an argument with type pointer to long double.

12 The conversion specifiers and their meanings are:
a,e,f,g Matches an optionally signed floating-point number, (...)

14 The conversion specifiers A, E, F, G, and X are also valid and behave the same as, respectively, a, e, f, g, and x.

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.
把回忆走一遍 2024-10-10 18:22:15

它可以是 %f%g%e,具体取决于您希望数字的格式。请参阅此处了解更多详细信息。 l 修饰符在 scanfdouble 中是必需的,但在 printf 中不需要。

It can be %f, %g or %e depending on how you want the number to be formatted. See here for more details. The l modifier is required in scanf with double, but not in printf.

残疾 2024-10-10 18:22:15

格式 %lfdouble 的完全正确的 printf 格式,与您使用的完全一样。你的代码没有任何问题。

旧版本(C99 之前的)C 语言不支持 printf 中的格式 %lf,这在 doubleprintfscanf 中的 code>。这种表面上的不一致已在 C99 中得到修复。

您不需要在 printf 中将 %lfdouble 一起使用。如果您愿意,也可以使用 %f%lf%fprintf 中是等效的)。但在现代 C 语言中,更喜欢将 %ffloat 一起使用,将 %lfdouble 一起使用, %Lflong double,在 printfscanf 中保持一致。

Format %lf is a perfectly correct printf format for double, exactly as you used it. There's nothing wrong with your code.

Format %lf in printf was not supported in old (pre-C99) versions of C language, which created superficial "inconsistency" between format specifiers for double in printf and scanf. That superficial inconsistency has been fixed in C99.

You are not required to use %lf with double in printf. You can use %f as well, if you so prefer (%lf and %f are equivalent in printf). But in modern C it makes perfect sense to prefer to use %f with float, %lf with double and %Lf with long double, consistently in both printf and scanf.

暖树树初阳… 2024-10-10 18:22:15

%Lf(注意大写的L)是长双精度数的“noreferrer">格式说明符。

对于普通的双精度,可以是%e%E%f%g%G 即可。

%Lf (note the capital L) is the format specifier for long doubles.

For plain doubles, either %e, %E, %f, %g or %G will do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文