DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
m4 macro processor

Defining macros

The primary built-in m4 macro is define, which is used to define new macros. The following input

   define(name, stuff)
causes the string name to be defined as stuff. All subsequent occurrences of name will be replaced by stuff. The defined string must contain only ASCII alphanumeric or printable supplementary characters and must begin with a letter or printable supplementary character (underscore counts as a letter). The defining string is any text that contains balanced parentheses; it may stretch over multiple lines. As a typical example
   define(N, 100)
    ...
   if (i > N)
defines N to be 100 and uses the ``symbolic constant'' N in a later if statement. As noted, the left parenthesis must immediately follow the word define to signal that define has arguments. If the macro name is not immediately followed by a left parenthesis, it is assumed to have no arguments. In the previous example, then, N is a macro with no arguments.

A macro name is only recognized as such if it appears surrounded by characters which cannot be used in a macro name. In the following example

   define(N, 100)
    ...
   if (NNN > 100)
the variable NNN is unrelated to the defined macro N even though the variable contains Ns.

m4 expands macro names into their defining text as soon as possible.

   define(N, 100)
   define(M, N)
defines M to be 100 because the string N is immediately replaced by 100 as the arguments of define(M, N) are collected. To put this another way, if N is redefined, M keeps the value 100.

There are two ways to avoid this behavior. The first, which is specific to the situation described here, is to interchange the order of the definitions:

   define(M, N)
   define(N, 100)
Now M is defined to be the string N, so when the value of M is requested later, the result will always be the value of N at that time (because the M will be replaced by N which will be replaced by 100).
Next topic: Quoting
Previous topic: m4 macro processor

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004