Re: [問題] printf() 整數, 浮點數放的順序影響輸出

看板C_and_CPP作者 (「雄辯是銀,沉默是金」)時間12年前 (2013/07/18 00:14), 編輯推噓0(001)
留言1則, 1人參與, 最新討論串3/4 (看更多)
這真的很奇怪 int main(void) { int a=10; int b=20; float d=0; d=(float)a/b; printf("%d\n",d); return 0; } gcc 以 64 bit compile 每次執行結果都不同 descent@w-linux:progs$ gcc p2.c -o p2 p2.c: In function ‘main’: p2.c:12: warning: incompatible implicit declaration of built-in function ‘ printf’ p2.c:12: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’ descent@w-linux:progs$ ./p2 407583640 descent@w-linux:progs$ ./p2 -18687880 descent@w-linux:progs$ ./p2 1304181240 descent@w-linux:progs$ ./p2 -1749420376 descent@w-linux:progs$ ./p2 -17885016 descent@w-linux:progs$ ./p2 -2128310680 descent@w-linux:progs$ ./p2 -841829112 descent@w-linux:progs$ ./p2 -1996767768 以 32 bit compile 都是 0 (這是比較符合預期的值) descent@w-linux:progs$ gcc -m32 p2.c -o p2 p2.c: In function ‘main’: p2.c:12: warning: incompatible implicit declaration of built-in function ‘ printf’ p2.c:12: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’ descent@w-linux:progs$ ./p2 0 descent@w-linux:progs$ ./p2 0 descent@w-linux:progs$ ./p2 0 descent@w-linux:progs$ ./p2 0 descent@w-linux:progs$ ./p2 0 descent@w-linux:progs$ ./p2 0 我用了 gdb debug, 結果在 64 bit 下 descent@w-linux:progs$ gdb p2 GNU gdb (GDB) 7.1-ubuntu Copyright (C) 2010 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-linux-gnu". For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>... Reading symbols from /home/descent/my-git/progs/p2...(no debugging symbols found)...done. (gdb) r Starting program: /home/descent/my-git/progs/p2 -7400 Program exited normally. (gdb) r Starting program: /home/descent/my-git/progs/p2 -7400 Program exited normally. (gdb) r Starting program: /home/descent/my-git/progs/p2 -7400 Program exited normally. (gdb) r Starting program: /home/descent/my-git/progs/p2 -7400 Program exited normally. 詭異, 猜不出原因。 不確定是不是這樣? sizeof(double): 8 sizeof(float): 4 sizeof(int): 4 sizeof(char*): 8 所以 printf 總是有 4byte 抓的是記憶體 random 的值。 可能不是, 因為我把 float 改成 double 也是一樣。 可是是 printf 對 %d %f 有不同的作法, 造成得到的值每次都不同。 可是 gdb 環境下, 怎麼又都一樣?? ※ 引述《twayfind (橘水壎)》之銘言: : 開發平台(Platform): (Ex: VC++, GCC, Linux, ...) : gcc version 3.4.4 : 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) : no : 問題(Question): : 使用printf 同時顯示整數及浮點數,整數在前跟浮點數在前結果會不同。 : printf("%d,%.3f\n",d,d); : printf("%.3f,%d\n",d,d); : 這兩個結果會呈現不同的結果。 : 餵入的資料(Input): : no : 預期的正確結果(Expected Output): : 錯誤結果(Wrong Output): : 程式碼(Code):(請善用置底文網頁, 記得排版) : #include <stdio.h> : int main(void) : { : int a=10; : int b=20; : float d=0; : d=(float)a/b; : printf("%d,%.3f\n",d,d); : printf("%.3f,%d\n",d,d); : return 0; : } : 補充說明(Supplement): -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.125.239.51

07/18 09:47, , 1F
因為那些一般程式中會被塞垃圾值的地方 在gdb會固定
07/18 09:47, 1F
※ 編輯: descent 來自: 59.125.239.51 (07/18 10:39)
文章代碼(AID): #1HvpDQuX (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #1HvpDQuX (C_and_CPP)