简体   繁体   English

使用 scanf 读取浮点值时出错

[英]Error while reading floating point values using scanf

I'm having problem while reading two floating point values for this c code snippet:我在读取这个 c 代码片段的两个浮点值时遇到问题:

#include<stdio.h>
long double add(long double a, long double b)
{ return a+b; }

int main()
{
 long double a, b;
 printf("Input two FP values: ");
 //Here scanf isn't reading the 2nd value.
 scanf("%lf %lf", &a, &b);
 printf("%lf", add(a,b));
 return 0;
}

When providing 2 and 4 as input, program is displaying 0.000000 as output.当提供 2 和 4 作为输入时,程序显示 0.000000 作为输出。

Learn how to enable warnings in your compiler and don't ignore them.了解如何在编译器中启用警告并且不要忽略它们。

ac:10:11: warning: format '%lf' expects argument of type 'double *', but argument 2 has type 'long double *' [-Wformat=] ac:10:11: 警告:格式 '%lf' 需要类型为 'double *' 的参数,但参数 2 的类型为 'long double *' [-Wformat=]

ac:10:15: warning: format '%lf' expects argument of type 'double *', but argument 3 has type 'long double *' [-Wformat=] ac:10:15: 警告:格式 '%lf' 需要类型为 'double *' 的参数,但参数 3 的类型为 'long double *' [-Wformat=]

ac:11:12: warning: format '%lf' expects argument of type 'double', but argument 2 has ac:11:12: 警告: 格式 '%lf' 需要类型为 'double' 的参数,但参数 2 有
type 'long double' [-Wformat=]输入'long double' [-Wformat=]

%lf is for reading double while %Lf is used for reading long double . %lf用于读取double%Lf用于读取long double So through out your code if you replace %lf with %Lf then it will work fine.因此,在您的代码中,如果您将%lf替换为%Lf那么它将正常工作。

demo演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM