I saw format specifier %qd
when browsing github code. Then I checked in GCC compiler, it's working fine.
#include <stdio.h>
int main()
{
long long num = 1;
printf("%qd\n", num);
return 0;
}
What is the purpose of format specifier %qd
in printf()
?
%qd
was intended to handle 64 bits comfortably on all machines, and was originally a bsd-ism (quad_t
).However,
egcs
(andgcc
before that) treats it as equivalent toll
, which is not always equivalent: openbsd-alpha is configured so thatlong
is 64 bits, and hencequad_t
is typedef'ed tolong
. In that particular case, the printf-like attribute doesn't work as intended.
If
sizeof(long long) == sizeof(long)
on openbsd-alpha, it should work anyway - i.e.%ld
,%lld
, and%qd
should be interchangeable. On OpenBSD/alpha,sizeof(long) == sizeof(long long) == 8
.
Several platform-specific length options came to exist prior to widespread use of the ISO C99 extensions, q
was one of them. It was used for integer types, which causes printf
to expect a 64-bit (quad word) integer argument. It is commonly found in BSD platforms.
However, both of the C99 and C11 says nothing about length modifier q
. The macOS (BSD) manual page for fprintf()
marks q
as deprecated. So, using ll
is recommended in stead of q
.
References:
https://gcc.gnu.org/ml/gcc-bugs/1999-02n/msg00166.html