C++ 未签名的 _int64 的 sprintf

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5140871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:30:13  来源:igfitidea点击:

sprintf for unsigned _int64

c++cformat-specifiersuint64long-long

提问by venkysmarty

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

我有以下代码。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);
    }

Output:

输出:

The string is
OD DB File Size = 100 bytes      XML file size = 0 bytes 

采纳答案by DevSolar

I don't know what POSIX has to say about this, but this is nicely handled by core C99:

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

If your compiler isn't C99 compliant, get a different compiler. (Yes, I'm looking at you, Visual Studio.)

如果您的编译器不符合 C99,请使用其他编译器。(是的,我在看着你,Visual Studio。)

PS:If you are worried about portability, don'tuse %lld. That's for long long, but there are no guarantees that long longactually is the same as _int64(POSIX) or int64_t(C99).

PS:如果您担心便携性,请不要使用%lld. 那是为了long long,但不能保证long long实际上与_int64(POSIX) 或int64_t(C99) 相同。

Edit:Mea culpa - I more or less brainlessly "search & replace"d the _int64with int64_twithout really looking at what I am doing. Thanks for the comments pointing out that it's uint64_t, not unsigned int64_t. Corrected.

编辑:Mea culpa - 我或多或少无脑地“搜索和替换”了_int64with,int64_t而没有真正看我在做什么。感谢您的评论指出它是uint64_t,不是unsigned int64_t。更正。

回答by Shamim Hafiz

You need to use %I64uwith Visual C++.

您需要在Visual C++ 中使用%I64u

However, on most C/C++ compiler, 64 bit integer is long long. Therefore, adopt to using long longand use %llu.

但是,在大多数 C/C++ 编译器上,64 位整数是 long long。因此,采用long long并使用%llu

回答by Maxim Egorushkin

If you are looking for a portable solution, then use printf macrosfrom <inttypes.h>. You may need to define __STDC_FORMAT_MACROSto make these available in C++.

如果你正在寻找一个便携式解决方案,然后使用printf的宏<inttypes.h>。您可能需要定义__STDC_FORMAT_MACROS以使这些在 C++ 中可用。