I wondering why the compiler let this pass and is giving the right output, although sqrt()
from its prototype normally should only get an double
value as argument:
In C99 the declaration of the prototype is:
double sqrt (double x);
#include <stdio.h>
#include <math.h>
int main (void)
{
int i = 9;
printf("\t Number \t\t Square Root of Number\n\n");
printf("\t %d \t\t\t %f \n",i, sqrt(i));
}
Output:
Number Square Root of Number
9 3.000000
Why does the compiler not throw a warning at least and the given output is right, if I´m giving the sqrt()
function an int
as argument?
Is this crossing into Undefined Behavior?
I´m using gcc.
The Question was already asked twice for C++, but not for C, so my question is up for C. I provide the links to the questions for C++ anyway:
Why does sqrt() work fine on an int variable if it is not defined for an int?
char
? Like f.e. when i take your example:char c = 4; double d = c;
or as in my given example:printf("\t %d \t\t\t %f \n",i, sqrt(c));
Or does that only applies to the conversion fromint
todouble
? – Karleen