为什么 scanf() 需要“%lf"?对于双打,当 printf() 只用“%f"就可以了? [英] Why does scanf() need "%lf" for doubles, when printf() is okay with just "%f"?
本文介绍了为什么 scanf() 需要“%lf"?对于双打,当 printf() 只用“%f"就可以了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么scanf()
在读取double
时需要%lf
"中的l
, 当 printf()
可以使用 "%f
" 时,无论其参数是 double
还是 float
?
Why is it that scanf()
needs the l
in "%lf
" when reading a double
, when printf()
can use "%f
" regardless of whether its argument is a double
or a float
?
示例代码:
double d;
scanf("%lf", &d);
printf("%f", d);
推荐答案
因为 C 会将带有可变参数的函数的浮点数提升为双精度数.指针不会被提升为任何东西,所以你应该使用 %lf
、%lg
或 %le
(或 %la
在 C99 中)以双精度读取.
Because C will promote floats to doubles for functions that take variable arguments. Pointers aren't promoted to anything, so you should be using %lf
, %lg
or %le
(or %la
in C99) to read in doubles.
这篇关于为什么 scanf() 需要“%lf"?对于双打,当 printf() 只用“%f"就可以了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文