简体   繁体   中英

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 :

#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 ). This might not work on most/all versions of MSVC's standard library implementation, however, because they're way behind on standards conformance.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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