作者LPH66 ((short)(-15074))
看板C_and_CPP
标题Re: [问题] printf %d ,0从哪里来?
时间Tue May 5 17:23:16 2009
※ 引述《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)
1F:推 chrisjon:感谢详解(m_ _m),我一直奇怪why有的数有定义,有的没定义 05/05 17:25
2F:→ chrisjon:错了,是简答:p 05/05 17:38