gpt4 book ai didi

c - 为什么 's PRIx16 not equal to "hx"?

转载 作者:太空宇宙 更新时间:2023-11-04 04:20:56 25 4
gpt4 key购买 nike

为什么是PRIx16 == "x" in C (和 in C++ )在 GCC 下?

我希望它是 "hx",以便以下工作:

#include <inttypes.h>

int16_t v = -1;
printf("%04" PRIx16 "\n", v); // prints ffffffff, not ffff

最佳答案

简答:

要打印签名类型,请使用 di

要打印无符号类型,请使用xuo


int16_t v 是一个有符号整数类型。

PRIxNPRIx16 中被列为“无符号整数的 fprintf 宏”。 PRIxN 未针对“带符号整数的 fprintf 宏”列出。 @Kerrek SB

要将 v 打印为十进制文本,请使用 PRId16PRIi16

printf("%04" PRId16 "\n", v);

要将 v 打印为十六进制文本,转换/转换为一些无符号的。

printf("%04" PRIx16 "\n", (uint16_t)v);

我还希望 "hx" 用于 PRIx16,这就是我的 GCC 编译(GNU C11 (GCC) 版本 6.4.0)的方式,但这更多可能是 standard library version问题(我的:ldd(cygwin)2.9.0)。我怀疑 OP 的编译器/库是否是最新的。

#include <stdio.h>
#include <inttypes.h>

int main(void) {
printf(PRIx16);
return 0;
}

输出

hx

关于c - 为什么 <inttypes.h >'s PRIx16 not equal to "hx"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46900123/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com