C++ Windows 上无符号 __int64 的 printf 格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18107426/
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-27 21:43:24  来源:igfitidea点击:

printf format for unsigned __int64 on Windows

c++cwindowsprintfunsigned

提问by Virus721

I need to print a ULONGLONGvalue (unsigned __int64). What format should i use in printf? I found %lluin another question but they say it is for linux only.

我需要打印一个ULONGLONG值 ( unsigned __int64)。我应该使用什么格式printf?我%llu在另一个问题中发现,但他们说它仅适用于 linux。

Thanks for your help.

谢谢你的帮助。

回答by Eric Postpischil

Using Google to search for “Visual Studio printf unsigned __int64” produces this pageas the first result, which says you can use the prefix I64, so the format specifier would be %I64u.

使用 Google 搜索“Visual Studio printf unsigned __int64”会生成此页面作为第一个结果,它表示您可以使用前缀I64,因此格式说明符将是%I64u.

回答by Yu Hao

%lluis the standard way to print unsigned long long, it's not just for Linux, it's actually in C99. So the problem is actually to use a C99-compatible compiler, i.e, not Visual Studio.

%llu是标准的打印方式unsigned long long,它不仅适用于 Linux,实际上也适用于 C99。所以问题实际上是使用 C99 兼容的编译器,即不是 Visual Studio。

C99 7.19.6 Formatted input/output functions

C99 7.19.6格式化输入/输出函数

ll(ell-ell) Specifies that a following d, i, o, u, x, or X conversion specifier applies to a long long int or unsigned long long int argument; or that a following n conversion specifier applies to a pointer to along long int argument.

ll(ell-ell) 指定后面的 d、i、o、u、x 或 X 转换说明符适用于 long long int 或 unsigned long long int 参数;或者后面的 n 转换说明符适用于指向 long int 参数的指针。

回答by Yu Hao

I recommend you use PRIu64format specified from a standard C library. It was designed to provide users with a format specifier for unsigned 64-bit integer across different architectures.

我建议您使用PRIu64从标准 C 库中指定的格式。它旨在为用户提供跨不同体系结构的无符号 64 位整数格式说明符。

Here is an example (in C, not C++):

这是一个示例(使用 C,而不是 C++):

#include <stdint.h>   /* For uint64_t */
#include <inttypes.h> /* For PRIu64 */
#include <stdio.h>    /* For printf */
#include <stdlib.h>   /* For exit status */

int main()
{
    uint64_t n = 1986;
    printf("And the winning number is.... %" PRIu64 "!\n", n);
    return EXIT_SUCCESS;
}

回答by Ivaylo Strandjev

Printf has different format specifiers for unsigned long longdepending on the compiler, I have seen %lluand %Lu. In general I would advice you to use std::coutand similar instead.

unsigned long long根据编译器的不同,Printf 有不同的格式说明符,我已经看到了%llu%Lu. 一般来说,我会建议你使用std::cout和类似的代替。

回答by Neil Kirk

Here is a work around for HEX output

这是 HEX 输出的解决方法

printf("%08X%08X", static_cast<UINT32>((u64>>32)&0xFFFFFFFF), static_cast<UINT32>(u64)&0xFFFFFFFF));