我看到格式说明符%qd
浏览时github https://github.com/Microsoft/clang/blob/master/test/Sema/format-strings.c代码。然后我检查了 GCC 编译器,它工作正常。
#include <stdio.h>
int main()
{
long long num = 1;
printf("%qd\n", num);
return 0;
}
格式说明符的目的是什么%qd
in printf()
?
%qd
旨在轻松处理所有 64 位
机器,最初是一个 bsd-ism (quad_t
).
然而,egcs
(and gcc
在此之前)将其视为等同于ll
, 哪个
并不总是等价的:openbsd-alpha 的配置是这样的long
是
64 位,因此quad_t
被类型定义为long
。
在这种特殊情况下,类似 printf 的属性不起作用
故意的。
If sizeof(long long) == sizeof(long)
在 openbsd-alpha 上,它应该可以工作
无论如何 - 即%ld
, %lld
, and %qd
应该可以互换。在 OpenBSD/alpha 上,sizeof(long) == sizeof(long long) == 8
.
在广泛使用 ISO C99 扩展之前,已经存在了多种特定于平台的长度选项,q
就是其中之一。它用于整数类型,这导致printf
期望 64 位(四字)整数参数。它常见于 BSD 平台中。
然而,C99 和 C11 都没有提到长度修饰符q
。 macOS (BSD) 手册页fprintf()
marks q
已弃用。所以,使用ll
推荐代替q
.
参考:
https://gcc.gnu.org/ml/gcc-bugs/1999-02n/msg00166.html https://gcc.gnu.org/ml/gcc-bugs/1999-02n/msg00166.html
https://en.wikipedia.org/wiki/Printf_format_string https://en.wikipedia.org/wiki/Printf_format_string
https://port70.net/~nsz/c/c11/n1570.html#7.21.6.1p7 https://port70.net/%7Ensz/c/c11/n1570.html#7.21.6.1p7
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)