C中浮点数的平方

Sud*_*pta 3 c floating-point floating-point-conversion

我已经在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);
}
Run Code Online (Sandbox Code Playgroud)

错误:

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
Run Code Online (Sandbox Code Playgroud)

Mys*_*ial 8

将声明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);
}
Run Code Online (Sandbox Code Playgroud)

至于它为何"起作用" int,你必须向我们展示你用于该案例的确切代码.