-5
這是c的簡單程序,我想知道在使用Dev-Cpp編譯器運行時輸出是如何爲0的?解釋給定代碼的輸出?
int main() {
printf ("%d\n",(float)((int)3.5/2));
return 0;
}
這是c的簡單程序,我想知道在使用Dev-Cpp編譯器運行時輸出是如何爲0的?解釋給定代碼的輸出?
int main() {
printf ("%d\n",(float)((int)3.5/2));
return 0;
}
你傳遞一個float
爲%d
(你應該已經通過int
爲%d
) - 行爲是不確定的,什麼是允許的。
請標誌-Wextra -Wall -Werror
編譯)
printf ("%d\n",(float)((int)3.5/2)); // error compile
format ‘%d’ expects argument of type ‘int’, but argument 2 has type double
測試:
printf ("%d\n",((int)3.5/2)); // 3.5/2 = 1.75 cast to int: output = 1;
printf ("%f\n", 3.5/2); // 3.5/2 = 1.75 no cast: output = 1.75;
看的人的printf
'-W'現在是'-Wextra'。儘管'-W'仍然可以識別,但我建議使用新的表單。 – 2013-02-28 14:02:45
謝謝;)@DanielFischer – 2013-02-28 14:04:28