C语言 C 学生作业,“%f”需要“float *”类型的参数,但参数 2 的类型为“double *”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21945045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C Student Assignment, ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double *’
提问by David Peterson Harvey
I'm working on an assignment and I'm getting this warning:
我正在处理一项任务,但收到此警告:
C4_4_44.c:173:2: warning: format ‘%f' expects argument of type ‘float *',
but argument 2 has type ‘double *' [-Wformat]
The variabled is declared in main as:
变量在 main 中声明为:
double carpetCost;
I'm calling the function as:
我将该函数称为:
getData(&length, &width, &discount, &carpetCost);
And here's the function:
这是功能:
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
This is driving me crazy because the book says that you don't use the & in
这让我发疯,因为这本书说你不使用 & 在
scanf("%f", carpetCost);
when accessing it from a function where you passed it be reference.
当从你传递它的函数访问它时,它是引用。
Any ideas what I'm doing wrong here?
任何想法我在这里做错了什么?
采纳答案by ouah
Change
改变
scanf("%f", carpetCost);
with
和
scanf("%lf", carpetCost);
%fconversion specification is used for a float *argument, you need %lffor double *argument.
%f转换规范用于float *说法,你需要%lf的double *参数。
回答by haccks
Use %lfspecification instead for double *argument.
使用%lf规范代替double *参数。
scanf("%lf", carpetCost);
回答by RahulKT
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

