sprintf的无符号_int64 [英] sprintf for unsigned _int64
问题描述
我有以下code。在sprintf的第二个%的D输出始终显示为零。我想我是错了指定符。任何一个可以帮助我获得写入字符串正确的价值观。而这已在POSIX标准实现。感谢您的输入
无效的主要(){
无符号_int64 dbFileSize = 99;
无符号_int64档案大小= 100;
CHAR BUF [128];
memset的(BUF,0×00,128);
sprintf的(BUF,\\点头DB文件大小=%d字节\\ t XML文件大小=%d字节,档案大小,dbFileSize);
的printf(字符串%S,BUF);
}
输出:
的字符串是
OD DB文件大小= 100字节的XML文件的大小= 0字节
我不知道是什么POSIX不得不说的这一点,但是这是很好的核心C99处理:
的#include<&stdio.h中GT;
#包括LT&;&inttypes.h GT;诠释主要(无效){
uint64_t中dbFileSize = 99;
uint64_t中档案大小= 100;
CHAR BUF [128];
memset的(BUF,0×00,128);
sprintf的(BUF,\\点头DB文件大小=%PRIu64字节\\ t的
XML文件大小=%PRIu64字节\\ n
,档案大小,dbFileSize);
输出(字符串为%s \\ n,BUF);
}
如果你的编译器不兼容的C99,得到不同的编译器。 (是的,我看着你,Visual Studio的。)
PS:如果你担心的便携性,的不的使用%LLD
。这对长长
,但没有保证长长
其实是一样的 _int64
(POSIX)或的int64_t
(C99)。
编辑:认错 - 我或多或少brainlessly搜索和替换D的 _int64
与的int64_t
并没有真正在看我在做什么。感谢您的意见指出,这是 uint64_t中
,而不是无符号的int64_t
。纠正。
I am having following code. output of second %d in sprintf is always shown as zero. I think i am specifying wrong specifiers. Can any one help me in getting write string with right values. And this has to achieved in posix standard. Thanks for inputs
void main() {
unsigned _int64 dbFileSize = 99;
unsigned _int64 fileSize = 100;
char buf[128];
memset(buf, 0x00, 128);
sprintf(buf, "\nOD DB File Size = %d bytes \t XML file size = %d bytes", fileSize, dbFileSize);
printf("The string is %s ", buf);
}
Output:
The string is
OD DB File Size = 100 bytes XML file size = 0 bytes
I don't know what POSIX has to say about this, but this is nicely handled by core C99:
#include <stdio.h>
#include <inttypes.h>
int main(void) {
uint64_t dbFileSize = 99;
uint64_t fileSize = 100;
char buf[128];
memset(buf, 0x00, 128);
sprintf( buf, "\nOD DB File Size = %" PRIu64 " bytes \t"
" XML file size = %" PRIu64 " bytes\n"
, fileSize, dbFileSize );
printf( "The string is %s\n", buf );
}
If your compiler isn't C99 compliant, get a different compiler. (Yes, I'm looking at you, Visual Studio.)
PS: If you are worried about portability, don't use %lld
. That's for long long
, but there are no guarantees that long long
actually is the same as _int64
(POSIX) or int64_t
(C99).
Edit: Mea culpa - I more or less brainlessly "search & replace"d the _int64
with int64_t
without really looking at what I am doing. Thanks for the comments pointing out that it's uint64_t
, not unsigned int64_t
. Corrected.
这篇关于sprintf的无符号_int64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!