Re: [問題] printf %d ,0從哪裡來?
看板C_and_CPP (C/C++)作者LPH66 ((short)(-15074))時間16年前 (2009/05/05 17:23)推噓1(1推 0噓 1→)留言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
05/05 17:25, 1F
→
05/05 17:38, , 2F
05/05 17:38, 2F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章