Previous: Patsubst, Up: Text handling
Formatted output can be made with format
:
Works much like the C function
printf
. The first argument format-string can contain `%' specifications which are satisfied by additional arguments, and the expansion offormat
is the formatted string.The macro
format
is recognized only with parameters.
Its use is best described by a few examples:
define(`foo', `The brown fox jumped over the lazy dog') => format(`The string "%s" uses %d characters', foo, len(foo)) =>The string "The brown fox jumped over the lazy dog" uses 38 characters format(`%.0f', `56789.9876') =>56790 len(format(`%-*X', `300', `1')) =>300
Using the forloop
macro defined in See Loops, this
example shows how format
can be used to produce tabular output.
include(`forloop.m4') => forloop(`i', `1', `10', `format(`%6d squared is %10d ', i, eval(i**2))') => 1 squared is 1 => 2 squared is 4 => 3 squared is 9 => 4 squared is 16 => 5 squared is 25 => 6 squared is 36 => 7 squared is 49 => 8 squared is 64 => 9 squared is 81 => 10 squared is 100 =>
The builtin format
is modeled after the ANSI C `printf'
function, and supports these `%' specifiers: `c',
`s', `d', `o', `x', `X', `u', `e',
`E', `f', `F', `g', `G', and `%'; it
supports field widths and precisions, and the
modifiers `+', `-', ` ', `0', `#', `h' and
`l'. For more details on the functioning of printf
, see the
C Library Manual.
For now, unrecognized specifiers are silently ignored, but it is
anticipated that a future release of GNU m4
will support more
specifiers, and give warnings when problems are encountered. Likewise,
escape sequences are not yet recognized.