here is my snippet of code:
float square_root(x)
float x;
{
.......
}
int main(){
printf("Square_root 2 = %f\n", square_root(4));
}
When I pass number 4.0 to the square_root() function, x parameter inside the function is 4.0000000 so its ok.
But when I pass just 4 (like in example), x variable inside the function becomes 1.976262583365e-323#DEN
Why does that happen?
You’re using the old style of function declaration, where the argument types are listed separately. See C function syntax, parameter types declared after parameter list
As you are passing an
int, it’s not being converted tofloatby default. Your function, though, is interpreting the argument as afloat, which gives undesirable results.The modern approach is to include the argument type in the function declaration, which will allow your
intargument to be automatically converted to afloat.