关于产品scanf()的使用LF符和printf()与LF符? [英] Question about scanf() with lf specifier and printf() with lf specifier?
问题描述
我学习C和我有以下的code:
I am learning C and I have the following code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char *argv[])
{
double x;
printf("x = ");
scanf("%ld", &x);
printf("x = %lf\n", x);
system("PAUSE");
return 0;
}
(我用的开发C4.9中,Windows XP SP3)
(I am using Dev C4.9, Windows XP SP3)
当我运行上面的程序,进入5.3;该计划印刷 X = 0.000000
When I run the above program and entered 5.3; the program printed x = 0.000000
谁能解释这是为什么吗?
Can anyone explain why is that, please?
非常感谢。
推荐答案
的%LD
格式字符串意味着它预计在读长符号int
,但你传递它,而不是一个双击
。您应该使用%LF
格式说明说,你想有一个双击
。
The %ld
format string means that it's expecting to read in a long signed int
, but you're passing it instead a double
. You should instead use the %lf
format specifier to say that you want a double
.
请注意,对于 scanf函数
的→
需要双击
S(并且要求是缺席浮动
S),而对于的printf
的→
在%LF
没有效果:既%F
和%LF
有两个浮动
S中相同的输出和双击
取值由于默认参数推广。
Note that for scanf
, the l
is required for double
s (and is required to be absent for float
s), whereas for printf
, the l
in %lf
has no effect: both %f
and %lf
have the same output for both float
s and double
s, due to default argument promotion.
这篇关于关于产品scanf()的使用LF符和printf()与LF符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!