ven*_*rty 19 c c++ format-specifiers uint64 long-long
我有以下代码.sprintf中第二个%d的输出始终显示为零.我想我指的是错误的说明符.任何人都可以帮助我获得具有正确值的写字符串.这必须以posix标准实现.感谢您的投入
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);
}
Run Code Online (Sandbox Code Playgroud)
输出:
The string is
OD DB File Size = 100 bytes XML file size = 0 bytes
Run Code Online (Sandbox Code Playgroud)
Dev*_*lar 16
我不知道POSIX对此有何看法,但核心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 );
}
Run Code Online (Sandbox Code Playgroud)
如果您的编译器不符合C99,请使用其他编译器.(是的,我正在看着你,Visual Studio.)
PS:如果您担心便携性,请不要使用%lld
.这是为了long long
,但没有保证long long
实际上与_int64
(POSIX)或int64_t
(C99)相同.
编辑:过失-我或多或少brainlessly"搜索和替换" D的_int64
有int64_t
没有真正在看我在做什么.感谢您的评论指出它uint64_t
不是unsigned int64_t
.纠正.
Sha*_*fiz 14
您需要在Visual C++中使用%I64u.
但是,在大多数C/C++编译器中,64位整数很长.因此,采用长使用%llu.