【发布时间】:2022-01-06 16:49:22
【问题描述】:
每个人。 我需要帮助! 我试图在 HackerRank 的挑战之后提交这个: 任务 给定一餐的餐费(餐费的基本成本)、小费百分比(作为小费添加的餐费百分比)和税费百分比(作为税款添加的餐费百分比),查找并打印一餐的总费用。将结果四舍五入到最接近的整数。
#include <stdio.h>
#include <math.h>
int main()
{
int tax,tip;
double mealc;
scanf("%f",&mealc);
scanf("d",&tip);
scanf("%d",&tax);
mealc = mealc+(mealc*tip/100))+(mealc*tax/100);
printf ("%d",round(mealc));
return 0;
}
编译上面的代码后。我总是收到这些错误:
Hk2.c:33:9: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double *’ [-Wformat=]
Hk2.c:37:11: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat=]
有什么问题?
【问题讨论】:
-
为什么你认为
scanf("%f",&mealc)是正确的,你认为警告信息试图告诉你什么? -
%f格式化float类型,因此需要传递给scanf的参数应该是浮点数 -
在这两种情况下都使用
%lf。 -
scanf("d",&tip);你忘了% -
你使用了
double类型是对的;在大多数情况下它是可取的(而不是使用float,这通常太不精确)。所以赞成 Wiliam Pursell 的评论,忽略 Rafaelplayerxd 和 Tzatziki 的评论。
标签: c double scanf conversion-specifier