Scanf 没有双读
Scanf not reading in double
我正在尝试使用 scanf
.
连续从用户那里读取 double
值
代码:
printf("Enter A value: \n");
double input;
int result = scanf("%f", &input);
printf("INPUT: %f\n", input);
输出为
INPUT: 0.000
你对编译器撒谎:扫描时,%f
说你提供了指向 float
的指针。但是您提供了指向 double
.
的指针
要修复,请使用 %lf
或 将 input
声明为 float
。
请注意,printf
格式存在不对称性,它对 float
和 double
参数都使用 %f
。这是有效的,因为 printf
参数被提升为 double
(并且不是指针)。
I am trying to read in a double value continuously from the user using scanf.
为此,您需要一个循环,如下所示:
while(scanf("%lf", &input) == 1) {
//code goes here...
printf("INPUT: %lf\n", input);
//code goes here...
}
请注意,由于input
的原始类型是double
,您需要使用%lf
而不是%f
(%f
用于float
).
我正在尝试使用 scanf
.
double
值
代码:
printf("Enter A value: \n");
double input;
int result = scanf("%f", &input);
printf("INPUT: %f\n", input);
输出为
INPUT: 0.000
你对编译器撒谎:扫描时,%f
说你提供了指向 float
的指针。但是您提供了指向 double
.
要修复,请使用 %lf
或 将 input
声明为 float
。
请注意,printf
格式存在不对称性,它对 float
和 double
参数都使用 %f
。这是有效的,因为 printf
参数被提升为 double
(并且不是指针)。
I am trying to read in a double value continuously from the user using scanf.
为此,您需要一个循环,如下所示:
while(scanf("%lf", &input) == 1) {
//code goes here...
printf("INPUT: %lf\n", input);
//code goes here...
}
请注意,由于input
的原始类型是double
,您需要使用%lf
而不是%f
(%f
用于float
).