sprintf 用于无符号 _int64
我有以下代码。 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);
}
输出:
The string is
OD DB File Size = 100 bytes XML file size = 0 bytes
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不知道 POSIX 对此有什么说法,但是核心 C99 很好地处理了这一点:
如果您的编译器不兼容 C99,请使用不同的编译器。 (是的,我正在看着你,Visual Studio。)
PS:如果您担心可移植性,不要使用
%lld
。这是针对long long
的,但不能保证long long
实际上与_int64
(POSIX) 或int64_t
相同代码> (C99)。编辑: Mea culpa - 我或多或少无脑地“搜索并替换”了
_int64
与int64_t
没有真正看看我在做什么。感谢您的评论指出它是uint64_t
,而不是unsigned int64_t
。已更正。I don't know what POSIX has to say about this, but this is nicely handled by core C99:
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 forlong long
, but there are no guarantees thatlong long
actually is the same as_int64
(POSIX) orint64_t
(C99).Edit: Mea culpa - I more or less brainlessly "search & replace"d the
_int64
withint64_t
without really looking at what I am doing. Thanks for the comments pointing out that it'suint64_t
, notunsigned int64_t
. Corrected.您需要将 %I64u 与 Visual C++ 结合使用。
然而,在大多数 C/C++ 编译器上,64 位整数是 long long。因此,采用long long并使用%llu。
You need to use %I64u with Visual C++.
However, on most C/C++ compiler, 64 bit integer is long long. Therefore, adopt to using long long and use %llu.
如果您正在寻找便携式解决方案,请使用
< 中的 printf 宏 ;inttypes.h>
。您可能需要定义__STDC_FORMAT_MACROS
才能使它们在C++中可用。If you are looking for a portable solution, then use printf macros from
<inttypes.h>
. You may need to define__STDC_FORMAT_MACROS
to make these available in C++.我会将
sprintf_s
与%dz
和uint64_t
一起使用,当我尝试并发现 %dz 时,立即发现我是个白痴, unsigned int 的格式说明符适用于 uint32_t,这是个好消息,但它不适用于 uint64_t。虽然我认为 Keith Thompson 已经就使用 PRIu64 充分回答了这个问题,但我要做的一个更改是使用缓冲区溢出安全版本 sprintf_s 和 printf_s,另一个更改是在声明 char 时(直接)初始化它,如下所示由 Jens Gustedt 建议;I would use
sprintf_s
with%dz
anduint64_t
, and promptly discover that I'm an idiot when I try it and find that %dz, the format specifier for an unsigned int, will work for uint32_t, which is good news, but it won't work for uint64_t. While I think Keith Thompson has already answered this adequately with regards to using PRIu64, one change I would make would be to use the buffer overrun safe versions sprintf_s and printf_s, another would be to (direct) initialize the char when it is declared, as suggested by Jens Gustedt;