Re: [問題] printf %d ,0從哪裡來?

看板C_and_CPP (C/C++)作者 ((short)(-15074))時間16年前 (2009/05/05 17:23), 編輯推噓1(101)
留言2則, 1人參與, 最新討論串2/2 (看更多)
※ 引述《chrisjon (隨機數能吃嗎?)》之銘言: : #include <stdio.h> : int64_t fmrg_p = 2147483647; : int64_t fmrg_b = 1047849; : int64_t fmrg_index =101; : int fmrg_k = 102; : int64_t fmrg_seed[102]; : int main(void) : { : int i,a; : rand(); : printf("%d,%d,%d,%d,%d,%d,%d\n", fmrg_p, fmrg_b, fmrg_index, fmrg_k); : } : ================================================= : 執行結果 : 2147483647,0,1047849,0,101,0,102 : 請問一下,中間的零是從哪裡生出來的?? 你用 %d 去印 int64_t 了... 64-bit 的整數要用 %I64d 或者 (如果支援 long long 的話) %lld --- 至於為什麼會冒 0 出來這就要扯到 C 語言的可變參數的實作了 (以及 va_* 系列 macro 是怎麼讀到你的資料的) 要說詳細的話大概要再一篇... 簡單說的話可以跑一下下面這段code: #include <stdio.h> int main() { /* 因為 4294967296 放不進 32-bit 的 int 所以加個 I64 代表這個常數其實是 64-bit 的整數常數 支援 long long 的 compiler 加上 LL 應該也可以 */ int64_t var=4294967296I64; printf("%d,%d\n",var); return 0; } 印出的會是 0,1 -- 'You've sort of made up for it tonight,' said Harry. 'Getting the sword. Finishing the Horcrux. Saving my life.' 'That makes me sound a lot cooler then I was,' Ron mumbled. 'Stuff like that always sounds cooler then it really was,' said Harry. 'I've been trying to tell you that for years.' -- Harry Potter and the Deathly Hollows, P.308 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.30.84 ※ 編輯: LPH66 來自: 140.112.30.84 (05/05 17:23)

05/05 17:25, , 1F
感謝詳解(m_ _m),我一直奇怪why有的數有定義,有的沒定義
05/05 17:25, 1F

05/05 17:38, , 2F
錯了,是簡答:p
05/05 17:38, 2F
文章代碼(AID): #1A00M619 (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #1A00M619 (C_and_CPP)