-1
将%u指定符与float变量一起使用时出现意外输出?我的代码如下,请帮我理解?使用%u指定符与浮点变量时出现意外的输出?
int main()
{
float f=9.8;
printf("%u",f); //unexpected out put what will be out put and why please help to understand ?
}
将%u指定符与float变量一起使用时出现意外输出?我的代码如下,请帮我理解?使用%u指定符与浮点变量时出现意外的输出?
int main()
{
float f=9.8;
printf("%u",f); //unexpected out put what will be out put and why please help to understand ?
}
u
格式说明符表示unsigned int
。您致电printf
不知道将您的float
转换为无符号整数,因此printf
只是将float
的位重新解释为unsigned int
。
如果你确实想输出它作为一个整数,你需要明确地投它:
printf("%u", (unsigned int) f);
或者,如果你的意思是输出为浮点,使用%f
:
printf("%f", f);
这并不是意想不到的。你试图做的是废话,你会期望发生什么? '%u'需要一个'unsigned int',你不能将一个'float'传递给它,并期望它工作。 'printf(“%u”,(unsigned)f);'会好的。 – 2014-09-27 19:45:52
这只是一个愚蠢的问题。我想不出任何其他方式来描述它。任何printf文档都会显示出这样的疯狂。 – 2014-09-27 20:47:14