gpt4 book ai didi

c - C 中的 %f 和 %lf 有什么区别?

转载 作者:太空狗 更新时间:2023-10-29 16:45:36 27 4
gpt4 key购买 nike

我在C书的C例子中看到这两个参数,但是作者没有详细说明两者有什么区别。我知道 %f 指定一个 float 应该取代它。我试着查找这个但很难找到这个 w 符号。 %lf 呢?

最佳答案

简短的回答是它对 printf 没有影响, 并表示使用 floatdoublescanf .

对于 printf , float 类型的参数被提升为double所以两者都是%f%lf用于double .对于 scanf , 你应该使用 %f对于 float%lf对于 double .

下面我们中的语言律师的更多详细信息:


%f之间没有区别和 %lfprintf家庭。 ISO C 标准(其中的所有引用均来自 C11),第 7.21.6.1 The fprintf function 节,款/7状态,对于 l修饰符(我的重点):

Specifies that a following d, i, o, u, x, or X conversion specifier applies to a long int or unsigned long int argument; that a following n conversion specifier applies to a pointer to a long int argument; that a following c conversion specifier applies to a wint_t argument; that a following s conversion specifier applies to a pointer to a wchar_t argument; or has no effect on a following a, A, e, E, f, F, g, or G conversion specifier.

它不需要修改 f 的原因说明符是因为该说明符已经表示double ,来自段落 /8在列出 %f 类型的同一部分说明符:

A double argument representing a floating-point number is converted to decimal notation

这与函数原型(prototype)中椭圆后面的参数按照 6.5.2.2 Function calls 部分的默认参数提升有关。 ,款/7 :

The ellipsis notation in a function prototype declarator causes argument type conversion to stop after the last declared parameter. The default argument promotions are performed on trailing arguments.

printf (以及 printf 类函数的整个)声明为 int printf(const char * restrict format, ...);对于省略号,该规则适用于此处。 6.5.2.2 Function calls 部分介绍了默认参数提升,款/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.


对于 scanf家庭,它要求使用 double而不是 float .科7.21.6.2 The fscanf function /11 :

Specifies that a following d, i, o, u, x, X, or n conversion specifier applies to an argument with type pointer to long int or unsigned long int; that a following a, A, e, E, f, F, g, or G conversion specifier applies to an argument with type pointer to double; or that a following c, s, or [ conversion specifier applies to an argument with type pointer to wchar_t.

这修改了 /12该部分的段落说明,对于 %f :

Matches an optionally signed floating-point number, infinity, or NaN, whose format is the same as expected for the subject sequence of the strtod function. The corresponding argument shall be a pointer to floating.

关于c - C 中的 %f 和 %lf 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25860850/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com