www.delorie.com/gnu/docs/m4/m4_57.html   search  
 
Buy GNU books!


GNU macro processor

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.7 Formatted output

Formatted output can be made with format:

 
format(format-string, ...)

which works much like the C function printf. The first argument is a format string, which can contain `%' specifications, and the expansion of format is the formatted string.

Its use is best described by a few examples:

 
define(`foo', `The brown fox jumped over the lazy dog')
=>
format(`The string "%s" is %d characters long', foo, len(foo))
=>The string "The brown fox jumped over the lazy dog" is 38 characters long

Using the forloop macro defined in See section 5.3 Loops and recursion, this example shows how format can be used to produce tabular output.

 
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 the normal `%' specifiers: `c', `s', `d', `o', `x', `X', `u', `e', `E' and `f'; 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.


  webmaster     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003