POSIX Awk says:
The printf statement shall produce output based on a notation similar to the File Format Notation used to describe file formats in this volume of POSIX.1-2008 (see XBD File Format Notation).
And File Format Notation defines %a
:
The floating-point number argument representing a floating-point number shall be converted in the style "[-]0xh.hhhhp±d" [...]
However neither Gawk nor Mawk support this:
$ gawk 'BEGIN {printf "%a", 1}'
%a
$ mawk 'BEGIN {printf "%a", 1}'
mawk: run time error: improper conversion(number 1) in printf("%a")
Why is this?
You're looking at the POSIX 2008 SUSv4 (Single Unix Specification) documentation. A lot of software pre-dates this, gawk
included. I suspect the gawk
implementation is 2001 SUSv2, and has not been updated completely over time.
The Linux (glibc) printf(3)
man page alludes to this problem (see the description of %a
about half way through, sorry no anchors to link to):
a, A
(C99; not in SUSv2) For a conversion, the double argument is converted to hexadecimal notation (using the letters abcdef) in the style [-]0xh.hhhhp±; [...]
nawk
/mawk
/gawk
don't simply call the underlying libc's printf()
or sprintf()
verbatim, they more or less reimplement format string processing. For mawk
see the do_printf()
function for example. Perl also implements its own format processing, the sprintf
man page is more up front about the details.
Things which do support %a
/%A
are:
printf
(i.e. /usr/bin/printf
)printf
, since bash-2.05 if libc has supportsprintf
since perl-5.22.0See the accepted answer here for some additional background: The format specifier %a for printf() in C