#include <stdio.h>
int main()
{
float i;
i=1;
printf("%d",i);
return 0;
}
What should be the output of the code? Online compilers are giving garbage value but according to my logic shouldn't the float be converted to int and the float part get truncated and just print 1 as the output?
No conversion is taking place in the way you assumed (*). The format specifier in the format string is used to determine what data type is expected next by va_arg. If the type specified in the format string does not match the type actually passed as parameter, behavior is undefined.
(*): With variadic arguments like
printfthere is a different type of conversion called "default argument promotion" happening, but that is a different subject altogether and is not being the issue you're facing.