square of a float number in C

Sudhanshu Gupta picture Sudhanshu Gupta · Dec 10, 2011 · Viewed 48.8k times · Source

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

Answer

Mysticial picture Mysticial · Dec 10, 2011

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.