使用%u说明符和float变量时出现意外输出?

时间:2014-09-27 19:44:46

标签: c floating-point unsigned

使用%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 ?
  }

1 个答案:

答案 0 :(得分:2)

u格式说明符表示unsigned int。您致printf的电话并不知道将float转换为无符号整数,因此printf只需将float的位重新解释为unsigned int }。

如果你真的想将它输出为整数,你需要明确地将其输出:

printf("%u", (unsigned int) f);

或者如果您打算将其输出为浮点数,请使用%f

printf("%f", f);
相关问题