繁体   English   中英

C中浮点数的平方

[英]square of a float number in 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

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 “工作”的原因,你必须向我们展示你用于该案例的确切代码。

你只是错过了你给出的原型中的参数。 你有过

float square();

什么时候应该

float square(float);

您不需要将其移动到函数外部,但您需要确保原型具有与稍后定义的函数相同的签名(返回类型,名称和参数计数/类型)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM