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
printf format for unsigned __int64 on Windows
提问by Virus721
I need to print a ULONGLONG
value (unsigned __int64
). What format should i use in printf
?
I found %llu
in another question but they say it is for linux only.
我需要打印一个ULONGLONG
值 ( unsigned __int64
)。我应该使用什么格式printf
?我%llu
在另一个问题中发现,但他们说它仅适用于 linux。
Thanks for your help.
谢谢你的帮助。
回答by Eric Postpischil
回答by Yu Hao
%llu
is 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 PRIu64
format 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 long
depending on the compiler, I have seen %llu
and %Lu
. In general I would advice you to use std::cout
and 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));