什么编译器不支持“%#x”的样式在 printf 标志中?

发布于 2024-12-24 21:27:23 字数 71 浏览 1 评论 0原文

我知道 %#x 具有与 0x%x 相同的效果,并且它符合 POSIX 标准。但人们提到有些编译器不支持它。是这样的吗,有例子吗?

I understand %#x give the same effect of 0x%x and it meets POSIX standard. But people mention that some compilers do not support it. Is that true, any example?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤寂小茶 2024-12-31 21:27:23

除了一些损坏的嵌入式系统 C 库之外,# 修饰符应该得到普遍支持。但是 %#x0x%x 并不相同。对于值 0,它们会产生不同的结果,并且 # 修饰符将始终以与十六进制数字相同的大小写打印 x(例如 %#x 给出 0xa%#X 给出 0XA),而使用 0x%X 将允许你有一个小写 x 和大写十六进制数字(视觉上更令人愉悦,至少对我来说)。因此,我发现 # 修饰符在实践中很少有用。

Aside from perhaps some broken embedded-systems C libraries, the # modifier should be universally supported. However %#x and 0x%x are not the same. They yield different results for the value 0, and the # modifier will always print the x in the same case as the hex digits (e.g. %#x gives 0xa and %#X gives 0XA) while using 0x%X would allow you to have a lowercase x and capital hex digits (much more visually pleasing, at least to me). As such, I find the # modifier is rarely useful in practice.

意犹 2024-12-31 21:27:23

%#x 是 C89、C99 和 C11 中 printf 格式字符串的有效转换规范。

%#x is a valid conversion specification in printf format string in C89, C99 and C11.

围归者 2024-12-31 21:27:23

# 标志字符不是来自 POSIX,而是来自 C 标准 (§7.21.6.1)。如果编译器或库不支持它,那么它就不是 C 编译器/标准库。

The # flag character is not from POSIX, but rather the C standard (§7.21.6.1). If a compiler or library does not support it then it is not a C compiler / standard library.

债姬 2024-12-31 21:27:23

根据 C 规范,这是完全有效的 - 7.21.6.1 fprintf 函数 - 点 #6

#

结果被转换为“替代形式”。对于 o 转换,它增加
精度,当且仅当必要时,强制结果的第一个数字为
零(如果值和精度均为 0,则打印单个 0)。对于 x(或 X)
转换时,非零结果的前缀为 0x(或 0X)。对于 a、A、e、E、f、F、g,
和 G 转换,始终转换浮点数的结果
包含小数点字符,即使其后没有数字。 (通常,一个
仅当数字为小数点字符时,这些转换的结果中才会出现小数点字符
)对于 g 和 G 转换,尾随零不会从
结果。对于其他转换,行为未定义。

This is perfectly valid as per C Specification - 7.21.6.1 The fprintf function - point #6

#

The result is converted to an ‘‘alternative form’’. For o conversion, it increases
the precision, if and only if necessary, to force the first digit of the result to be a
zero (if the value and precision are both 0, a single 0 is printed). For x (or X)
conversion, a nonzero result has 0x (or 0X) prefixed to it. For a, A, e, E, f, F, g,
and G conversions, the result of converting a floating-point number always
contains a decimal-point character, even if no digits follow it. (Normally, a
decimal-point character appears in the result of these conversions only if a digit
follows it.) For g and G conversions, trailing zeros are not removed from the
result. For other conversions, the behavior is undefined.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文