c++ - 在 C++11 中可移植地打印 std::uint64_t 变量的格式说明符

标签 c++ c++11 printf portability

打印 std::uint64_t 类型值的格式说明符是什么? (来自 <cstdint> )使用来自 std::printf() 的函数C++ 中的家庭?

C99 有 PRIu64 (来自 <inttypes.h> )但我并不完全清楚 PRIu64是有效的 C++11,虽然我能找到它可能是的提示。

没有PRIu64 ,据我所知,没有一种格式说明符适用于所有情况:

  • 在 32 位平台上,std::uint64_t将被定义为 unsigned long long并且格式说明符将为 %llu .
  • 在 64 位平台上,std::uint64_t将被定义为 unsigned long并且格式说明符将为 %lu .
  • 但是对于 Visual Studio,%llu在这两种情况下都有效。

最佳答案

PRIu64仍然是您将在 C++ 中使用的内容。每[cinttypes.syn] PRIu64

[...]

#define PRIuN see below

[...]

The contents and meaning of the header <cinttypes> are the same as the C standard library header <inttypes.h> [...]

因此它存在并且具有与 C ISO/IEC 9899:2011 标准中相同的行为。

关于c++ - 在 C++11 中可移植地打印 std::uint64_t 变量的格式说明符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56172857/

相关文章:

javascript - 如何在 cef 框架中支持 window.external.xxx

string - 有没有办法有条件地 `fmt` 一个字符串?

c++ - 隐藏事件 Emacs 缓冲区中的所有 doxygen 注释

C++,将字符串中的文件名传递给类函数,将其用于ifstream对象

C++:使用 fstream 修补二进制文件

python - 如何将 Boost.Python 中的 map_indexing_suite 与自定义非 std 对象一起使用?

c++ - 大数模幂运算

C++ - std::function 作为仿函数的参数

c - 为什么这段代码返回垃圾值?

c++ - 打印相同变量时为 cout 和 printf 获取不同的输出