測試程序:
#include <stdio.h>
int main(void)
{
printf("%f\n", 0);
printf("%f\n", (float)123);
printf("%f\n", 123.0);
return 0;
}
VC6中反彙編一下,看看:
7: printf("%f\n", 123);
00401028 push 7Bh // 這時,123當做int型
0040102A push offset string "%f\n" (0042601c)
0040102F call printf (00401090)
00401034 add esp,8
8: printf("%f\n", (float)123);
00401037 push 405EC000h
0040103C push 0 // 這時,123被轉換爲float型
0040103E push offset string "%f\n" (0042601c)
00401043 call printf (00401090)
00401048 add esp,0Ch
9: printf("%f\n", 123.0);
0040104B push 405EC000h
00401050 push 0
00401052 push offset string "%f\n" (0042601c)
00401057 call printf (00401090)
0040105C add esp,0Ch
================ 我是分割線 ======================
根據堆棧,稍稍修改一下,測試:
#include <stdio.h>
int main(void)
{
_asm push 405EC000H; // 這裏模擬一個123.0的輸出
printf("%f\n", 0);
_asm pop eax; // 這裏 只是爲了平衡堆棧,不用管
printf("%f\n", (float)123);
printf("%f\n", 123.0);
return 0;
}
VC6中反彙編結果如下:
7: _asm push 405EC000H;
00401028 push 405EC000h
8: printf("%f\n", 0);
0040102D push 0
0040102F push offset string "%f\n" (0042601c)
00401034 call printf (00401090)
00401039 add esp,8
9: _asm pop eax;
0040103C pop eax
10: printf("%f\n", (float)123);
0040103D push 405EC000h
00401042 push 0
00401044 push offset string "%f\n" (0042601c)
00401049 call printf (00401090)
0040104E add esp,0Ch
11: printf("%f\n", 123.0);
0040104B push 405EC000h
00401050 push 0
00401052 push offset string "%f\n" (0042601c)
00401057 call printf (00401090)
0040105C add esp,0Ch
小結:printf函數的格式輸出符沒有類型轉換的功能,只能顯式的類型轉換。
例如printf("%f", (double)123);
如果你認爲它可以自動類型轉換,就會出現意想不到的結果。
例如printf("%f", 123);這個時候,%f會要求系統從棧中取8個字節數據進行解析,可惜啊···我們的123不夠,所以取出來的數···如果VC-Debug模式下貌似是edi的值,其他的···大家自己研究一下,還有那個%f對應的數據表示的如何解析的?大家實際測試計算一下,有結果希望告知~~~