I was working on this program and I noticed that using %f for a double and %d for a float gives me something completely different. Anybody knows why this happens?
int main ()
{
float a = 1F;
double b = 1;
printf("float =%d\ndouble= %f", a, b);
}
This is the output
float = -1610612736
double = 190359837192766135921612671364749893774625551025007120912096639276776057269784974988808792093423962875123204096.0000
Because of the way variable parameters work, C has no idea of the type of value you are actually passing it other than
%dand%f. When you pass in a variable parameter you are basically doing(void*)&myvaluebecause neither the compiler, nor the function at run time can determine the type of the variable except for what is given in the formatting string. So even though there is an implicit conversion available, the compiler does not know it is needed.Well, double is 8 bytes on most systems while float is 4 bytes. So the two types are not binary compatible as it is. And it is like trying to interpret a string as a double, or some other incompatible type.