In printf("%d", (float) 9/ (float) 5);
what will be the output?
Note: In
printf
function the format specifier is%d
It seems that output would be 0 just like the below code return answer 0.
float a = 9/5;
printf("%d",a);
[Edited]
But not at all. The actual output is -1073741824
as a Garbage Value And that's the question. Why?
We can do the explicit typecasting to convert integer data type to float data type.
But why typecasting is not working in this printf("%d", (float) 9/ (float) 5);
code?
I know this is a stupid question but I want to know this stupidity.
printf()
has no way to know what type you are passing in other than the "%d" format specifier you've given it. When it sees "%d" it will use va_arg()
to extract an integer from the arguments it received. There is no integer there, so anything could happen. Floating point values are often passed in different registers than integers, so it's not like you're just interpreting a float as an int, you're actually loading an int from some unknown place.