我正在完成任务,我收到了这个警告:
C4_4_44.c:173:2: warning: format ‘%f’ expects argument of type ‘float *’,
but argument 2 has type ‘double *’ [-Wformat]
变量在main中声明为:
double carpetCost;
我将该函数称为:
getData(&length, &width, &discount, &carpetCost);
这是功能:
void getData(int *length, int *width, int *discount, double *carpetCost)
{
// get length and width of room, discount % and carpetCost as input
printf("Length of room (feet)? ");
scanf("%d", length);
printf("Width of room (feet)? ");
scanf("%d", width);
printf("Customer discount (percent)? ");
scanf("%d", discount);
printf("Cost per square foot (xxx.xx)? ");
scanf("%f", carpetCost);
return;
} // end getData
这让我抓狂,因为这本书说你不使用&在
scanf("%f", carpetCost);
从您传递它的函数访问它时是参考。
我在这里做错了什么想法?
答案 0 :(得分:8)
更改
scanf("%f", carpetCost);
与
scanf("%lf", carpetCost);
%f
转换规范用于float *
参数,%lf
参数需要double *
。
答案 1 :(得分:2)
使用%lf
规范代替double *
参数。
scanf("%lf", carpetCost);
答案 2 :(得分:1)
Use %lf instead of %f if you are scanning a double type of variable.
you can check this in detail also about %lf & %f from the discussed thread's link
http://stackoverflow.com/questions/210590/why-does-scanf-need-lf-for-doubles-when-printf-is-okay-with-just-f