-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