use*_*993 13 c format-specifiers uint16
我需要使用printf()来打印uint16_t.这个SO答案(如何打印uint32_t和uint16_t变量值?)说我需要使用inttypes.h.
但是,我正在使用嵌入式系统,并且inttypes.h不可用.当uint16_t的格式说明符不可用时,如何打印uint16_t?
M.M*_*M.M 14
一个明显的方法是:
printf("%u\n", (unsigned int)x);
Run Code Online (Sandbox Code Playgroud)
unsigned int保证至少为16位,因此这不是有损转换.
Jef*_*man 14
您应该使用inttypes.h的样式,但您自己定义符号.例如:
#define PRIu8 "hu"
#define PRId8 "hd"
#define PRIx8 "hx"
#define PRIu16 "hu"
#define PRId16 "hd"
#define PRIx16 "hx"
#define PRIu32 "u"
#define PRId32 "d"
#define PRIx32 "x"
#define PRIu64 "llu" // or possibly "lu"
#define PRId64 "lld" // or possibly "ld"
#define PRIx64 "llx" // or possibly "lx"
Run Code Online (Sandbox Code Playgroud)
找出你的机器并使用它们.看看inttypes.h中的其他人,并找出你需要的数据.
这样,您的代码将更加便携.自70年代末以来,我一直在做嵌入式系统工作.相信我:可移植性很重要.