[英]Error while reading floating point values using scanf
我在讀取這個 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;
}
當提供 2 和 4 作為輸入時,程序顯示 0.000000 作為輸出。
了解如何在編譯器中啟用警告並且不要忽略它們。
ac:10:11: 警告:格式 '%lf' 需要類型為 'double *' 的參數,但參數 2 的類型為 'long double *' [-Wformat=]
ac:10:15: 警告:格式 '%lf' 需要類型為 'double *' 的參數,但參數 3 的類型為 'long double *' [-Wformat=]
ac:11:12: 警告: 格式 '%lf' 需要類型為 'double' 的參數,但參數 2 有
輸入'long double' [-Wformat=]
%lf
用於讀取double
而%Lf
用於讀取long double
。 因此,在您的代碼中,如果您將%lf
替換為%Lf
那么它將正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.