Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

returning a float type from function in c

I have this simple code:

int main()
{
    float x = foo();
    printf("returned value: %f", x);
    return 0;
}

float foo ()
{
    return 5;
}

when i run the code , the output is: "returned value: -858993472.000000"

can somebody please explaing to me why does the returned value isnt 5.000000?

like image 679
MOP Avatar asked Jan 03 '11 12:01

MOP


1 Answers

At the point at which you call the function there is no prototype in scope. This means that the function is assumed to return int. Add a prototype before the point where you first call the function.

float f(void);
like image 126
CB Bailey Avatar answered Oct 04 '22 00:10

CB Bailey