3

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?

Zombo
  • 1
  • 62
  • 391
  • 407
  • 2
    Best guess: `based on a notation similar to` != `identical to`. – Ed Morton Jan 29 '17 at 18:05
  • If I try with POSIX awk, I get `weird printf conversion %a` Do you get the output you expect under a version of POSIX awk? – dawg Jan 29 '17 at 19:59
  • @dawg perhaps I am not being clear: POSIX Awk is Awk as is defined by the standard, not necessarily any one implementation. It appears that no implementation is POSIX compliant with respsect to `%a`, or maybe I am misreading the standard – Zombo Jan 29 '17 at 20:08
  • According to the Gnu Awk User's Guide: https://www.gnu.org/software/gawk/manual/html_node/Control-Letters.html#Control-Letters %a is not a supported format character. – Leslie Jan 31 '17 at 17:24

1 Answers1

3

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:

  • glibc-2.1 and later (1997), it's required for C99 features
  • coreutils-5.1(ish) printf (i.e. /usr/bin/printf)
  • bash's builtin printf, since bash-2.05 if libc has support
  • perl's sprintf since perl-5.22.0

See the accepted answer here for some additional background: The format specifier %a for printf() in C

Community
  • 1
  • 1
mr.spuratic
  • 9,767
  • 3
  • 34
  • 24