简体   繁体   中英

Error while reading floating point values using scanf

I'm having problem while reading two floating point values for this c code snippet:

#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.

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:15: warning: format '%lf' expects argument of type 'double *', but argument 3 has type 'long double *' [-Wformat=]

ac:11:12: warning: format '%lf' expects argument of type 'double', but argument 2 has
type 'long double' [-Wformat=]

%lf is for reading double while %Lf is used for reading long double . So through out your code if you replace %lf with %Lf then it will work fine.

demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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