C 中浮点数的平方

发布于 2024-12-20 15:30:41 字数 612 浏览 2 评论 0原文

我已经用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无尽的现实 2024-12-27 15:30:42

square() 的声明移出函数并确保原型匹配:

float square(float b);  //  Make sure this matches the definition.

int main()
{
    float a,y;
    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);
}

至于为什么它对 int 有效,您必须向我们展示您在该案例中使用的确切代码。

Move the declaration of square() out of the function and make sure the prototype matches:

float square(float b);  //  Make sure this matches the definition.

int main()
{
    float a,y;
    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);
}

As for why it "worked" for int, you'll have to show us the exact code you used for that case.

奈何桥上唱咆哮 2024-12-27 15:30:42

你只是错过了你给出的原型中的论点。你

float square();

不需要

float square(float);

将它移到函数之外,但你确实需要确保原型具有与你稍后定义的函数相同的签名(返回类型、名称和参数计数/类型) 。

You're just missing the argument in the prototype you gave. You had

float square();

When it should be

float square(float);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文