简体   繁体   English

有没有办法修复stdint类型的格式说明符警告?

[英]Is there a way to fix format specifiers warnings for stdint types?

问题是,在一个平台(Windows,mvsc2015) uint64_t被定义为unsigned long long ,并在另一个(Ubuntu的,铛),它是unsigned long和有它看起来像代码sprintf(buffer, "%#llx", u64key);

The solution is to use C99's format macros, in particular PRIu64 for an uint64_t : 解决方案是使用C99的格式宏,特别是uint64_t PRIu64

#include <inttypes.h>
…
sprintf(buffer, "%#" PRIu64 "\n", u64key);

Pascal's solution is the most direct and most idiomatic for this particular type, but for the record, an alternative for printing arbitrary integer types whose definitions you don't know is simply casting to intmax_t or uintmax_t then using the j modifier (eg %jd or %ju ). Pascal的解决方案对于这种特定类型来说是最直接和最惯用的,但是对于记录来说,打印任意整数类型的替代方法,其定义您不知道只是使用j修饰符(例如%jd转换为intmax_tuintmax_t %ju )。 This might not work on most/all versions of MSVC's standard library implementation, however, because they're way behind on standards conformance. 然而,这可能不适用于大多数/所有版本的MSVC标准库实现,因为它们在标准一致性方面落后了。

您可以使用预处理程序指令来检测数据类型的定义方式,并使用不同的字符串编译另一个sprintf()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM