使用%u说明符和float变量时出现意外输出?我的代码如下,请帮助我理解?
int main()
{
float f=9.8;
printf("%u",f); //unexpected out put what will be out put and why please help to understand ?
}
答案 0 :(得分:2)
u
格式说明符表示unsigned int
。您致printf
的电话并不知道将float
转换为无符号整数,因此printf
只需将float
的位重新解释为unsigned int
}。
如果你真的想将它输出为整数,你需要明确地将其输出:
printf("%u", (unsigned int) f);
或者如果您打算将其输出为浮点数,请使用%f
:
printf("%f", f);