sprintf 用于无符号 _int64

发布于 2024-10-19 19:30:44 字数 512 浏览 3 评论 0原文

我有以下代码。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

幸福丶如此 2024-10-26 19:30:44

我不知道 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 );
}

如果您的编译器不兼容 C99,请使用不同的编译器。 (是的,我正在看着你,Visual Studio。)

PS:如果您担心可移植性,不要使用%lld 。这是针对 long long 的,但不能保证 long long 实际上与 _int64 (POSIX) 或 int64_t 相同代码> (C99)。

编辑: Mea culpa - 我或多或少无脑地“搜索并替换”了 _int64int64_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:

#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.

伏妖词 2024-10-26 19:30:44

您需要将 %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.

最偏执的依靠 2024-10-26 19:30:44

如果您正在寻找便携式解决方案,请使用 < 中的 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++.

戴着白色围巾的女孩 2024-10-26 19:30:44

我会将 sprintf_s%dzuint64_t 一起使用,当我尝试并发现 %dz 时,立即发现我是个白痴, unsigned int 的格式说明符适用于 uint32_t,这是个好消息,但它不适用于 uint64_t。虽然我认为 Keith Thompson 已经就使用 PRIu64 充分回答了这个问题,但我要做的一个更改是使用缓冲区溢出安全版本 sprintf_s 和 printf_s,另一个更改是在声明 char 时(直接)初始化它,如下所示由 Jens Gustedt 建议;

#include <stdio.h>
#include <inttypes.h>

uint64_t dbFileSize = 99;
uint64_t fileSize = 100;
char buf[128]{ 0 };
sprintf_s(buf, "\nOD DB File Size = %" PRIu64 " bytes \t XML file size = %" PRIu64 " bytes\n", fileSize, dbFileSize);
printf_s("The string is %s ", buf);

I would use sprintf_s with %dz and uint64_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;

#include <stdio.h>
#include <inttypes.h>

uint64_t dbFileSize = 99;
uint64_t fileSize = 100;
char buf[128]{ 0 };
sprintf_s(buf, "\nOD DB File Size = %" PRIu64 " bytes \t XML file size = %" PRIu64 " bytes\n", fileSize, dbFileSize);
printf_s("The string is %s ", buf);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文