scanf函数/ printf的双变量C [英] Scanf/Printf double variable C
问题描述
让我们说我有这个以下code位在C:
Let's say I have this following bit of code in C:
double var;
scanf("%lf", &var);
printf("%lf", var);
printf("%f", var);
这在标准输出'变种'的两倍标准输入变量VAR,然后打印读取。
我明白这就是你如何从stdin读取一个双变量,但我的问题是:
1.为什么会打印带有LF%双?
2.为什么你可以打印与%F双?
3.哪一个更好,正确使用?
It reads from stdin variable 'var' and then prints twice in stdout 'var'. I understand that's how you read a double variable from stdin, but my questions are: 1. Why can you print a double with %lf? 2. Why can you print a double with %f? 3. Which one is better and correct to use?
推荐答案
有关可变参数的功能,如的printf
和 scanf函数
,参数提升,例如,任何较小的整数类型都提升到 INT
,浮动
提升为双击
。
For variable argument functions like printf
and scanf
, the arguments are promoted, for example, any smaller integer types are promoted to int
, float
is promoted to double
.
scanf函数
采用指针参数,所以推广规则从没有影响。它必须使用%F
为浮法*
和%LF
为双*
。
scanf
takes parameters of pointers, so the promotion rule takes no effect. It must use %f
for float*
and %lf
for double*
.
的printf
将永远不会看到一个浮动
参数,浮动
总是晋升为双击
。该格式说明%F
。但C99还表示,%LF
相同%F
在的printf
:
printf
will never see a float
argument, float
is always promoted to double
. The format specifier is %f
. But C99 also says %lf
is the same as %f
in printf
:
C99§7.19.6.1的 fprintf中
函数
C99 §7.19.6.1 The
fprintf
function
→
(ELL)指定后续的 D
, I
, 0
, U
, X
或 X
转换说明适用于长整型
或 unsigned long int类型
参数;其后面的 N
转换说明适用于指针长整型
参数;其后面的 C
转换说明适用于所述wint_t
参数;其后面的取值
转换说明适用于指针 wchar_t的
参数;或有一个如下的 A
, A
,电子$无影响C $ C>,
电子
,˚F
,˚F
, 先按g
或
l
(ell) 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.
这篇关于scanf函数/ printf的双变量C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!