为什么程序提供 -143155...当我只从 int 切换到 double?
Why does program provide -143155...when I only switched from int to double?
如果我使用 float 或 double 而不是 int,为什么这段代码会给出完全错误的答案?
//C How to Program Exercises 2.33
#include <stdio.h>
#include <conio.h>
double dailyDrivingCost(double miles, double costPerGallon,double averageMilesPerGallon);
int main(void){
double a,b,c,d,e;
printf ("Please enter the number of miles daily\n");
scanf("%lf",&a);
printf ("Please enter the cost per gallon\n");
scanf("%lf",&b);
printf ("Please enter the average miles per gallon\n");
scanf("%lf",&c);
printf ("The number of miles per gallon is:%d",dailyDrivingCost(a,b,c));
getch();
return 0;
}
double dailyDrivingCost(double miles, double costPerGallon,double averageMilesPerGallon){
double overall_cost;
overall_cost= (miles/averageMilesPerGallon)+ costPerGallon;
return overall_cost;
}
改变
printf ("The number of miles per gallon is:%d",dailyDrivingCost(a,b,c));
^
到
printf ("The number of miles per gallon is:%f",dailyDrivingCost(a,b,c));
d
转换说明符用于打印int
,打印double
(或float
)需要f
转换说明符.
如果我使用 float 或 double 而不是 int,为什么这段代码会给出完全错误的答案?
//C How to Program Exercises 2.33
#include <stdio.h>
#include <conio.h>
double dailyDrivingCost(double miles, double costPerGallon,double averageMilesPerGallon);
int main(void){
double a,b,c,d,e;
printf ("Please enter the number of miles daily\n");
scanf("%lf",&a);
printf ("Please enter the cost per gallon\n");
scanf("%lf",&b);
printf ("Please enter the average miles per gallon\n");
scanf("%lf",&c);
printf ("The number of miles per gallon is:%d",dailyDrivingCost(a,b,c));
getch();
return 0;
}
double dailyDrivingCost(double miles, double costPerGallon,double averageMilesPerGallon){
double overall_cost;
overall_cost= (miles/averageMilesPerGallon)+ costPerGallon;
return overall_cost;
}
改变
printf ("The number of miles per gallon is:%d",dailyDrivingCost(a,b,c));
^
到
printf ("The number of miles per gallon is:%f",dailyDrivingCost(a,b,c));
d
转换说明符用于打印int
,打印double
(或float
)需要f
转换说明符.