C 中浮点数的平方
我已经用 C 编写了一段代码,该代码对于 int 工作正常,但是当我尝试使用 float 执行此操作时,它显示错误,我该怎么做才能使其正确。
#include<stdio.h>
int main()
{
float a,y;
float square();
scanf("%f", &a);
y = square( a );
printf("%f %f ",a ,y);
}
float square(float b)
{
float z;
z = b*b;
printf("%f %f",z ,b);
return(z);
}
错误:
return.c:12: error: conflicting types for 'square'
return.c:13: note: an argument type that has a default promotion can't match an empty parameter name list declaration
return.c:6: note: previous declaration of 'square' was here
I have written a code in C which works fine for int but when I try to do this with float it is showing error what can i do to make it correct.
#include<stdio.h>
int main()
{
float a,y;
float square();
scanf("%f", &a);
y = square( a );
printf("%f %f ",a ,y);
}
float square(float b)
{
float z;
z = b*b;
printf("%f %f",z ,b);
return(z);
}
error:
return.c:12: error: conflicting types for 'square'
return.c:13: note: an argument type that has a default promotion can't match an empty parameter name list declaration
return.c:6: note: previous declaration of 'square' was here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
square()
的声明移出函数并确保原型匹配:至于为什么它对
int
有效,您必须向我们展示您在该案例中使用的确切代码。Move the declaration of
square()
out of the function and make sure the prototype matches:As for why it "worked" for
int
, you'll have to show us the exact code you used for that case.你只是错过了你给出的原型中的论点。你
不需要
将它移到函数之外,但你确实需要确保原型具有与你稍后定义的函数相同的签名(返回类型、名称和参数计数/类型) 。
You're just missing the argument in the prototype you gave. You had
When it should be
You don't need to move it outside the function, but you do need to make sure the prototype has the same signature (return type, name, and parameter count/types) as the function that you define later.