当前位置: 技术问答>linux和unix
printf 打印long long 变量的问题
来源: 互联网 发布时间:2016-11-27
本文导语: 本帖最后由 boborookie 于 2009-12-30 11:59:54 编辑 long count = 9763251; long size = 16384; long avail = 9763250; long long totalsize = (long long)count * (long long)size; long long availsize = (long long)avail * (long long)size; 方法A:用以下printf打印,...
long count = 9763251;
long size = 16384;
long avail = 9763250;
long long totalsize = (long long)count * (long long)size;
long long availsize = (long long)avail * (long long)size;
方法A:用以下printf打印,打印结果为:
printf("total_size: %ld(GB)n, free size: %ld(GB)n ",
totalsize / (1024*1024*1024),
availsize / (1024*1024*1024));
打印结果为:
total_size: 148(GB),
free size: 0(GB)//错误的结果!!
方法B:(把2句分开放)
printf("total_size: %ld(GB)n",
totalsize / (1024*1024*1024));
printf("free size: %ld(GB)n ",
availsize / (1024*1024*1024));
打印结果为:
total_size: 148(GB),
free size: 148(GB) //正确的结果
以前也碰到过同样的问题
请教 printf 为什么会有这种表现?
谢谢!
|
man 3 printf,please!
ll
(ell-ell). A following integer conversion corresponds to a long long int or unsigned long long int argument, or a following n conversion corresponds to a pointer to a long long int argument.
printf ("%c,%cn", 0x6162, 'x');//你认为会输出什么呢?
ll
(ell-ell). A following integer conversion corresponds to a long long int or unsigned long long int argument, or a following n conversion corresponds to a pointer to a long long int argument.
printf ("%c,%cn", 0x6162, 'x');//你认为会输出什么呢?
|
printf("%lldn",());
对于LONG LONG 型 ,要用%LLD。
对于LONG LONG 型 ,要用%LLD。
|
printf("total_size: %lld(GB)n",
totalsize / (1024*1024*1024)LL);
printf("free size: %lld(GB)n ",
availsize / (1024*1024*1024)LL);