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


GNU macro processor

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

4.3 Special arguments to macros

There is a special notation for the number of actual arguments supplied, and for all the actual arguments.

The number of actual arguments in a macro call is denoted by $# in the expansion text. Thus, a macro to display the number of arguments given can be

 
define(`nargs', `$#')
=>
nargs
=>0
nargs()
=>1
nargs(arg1, arg2, arg3)
=>3

The notation $* can be used in the expansion text to denote all the actual arguments, unquoted, with commas in between. For example

 
define(`echo', `$*')
=>
echo(arg1,    arg2, arg3 , arg4)
=>arg1,arg2,arg3 ,arg4

Often each argument should be quoted, and the notation $@ handles that. It is just like $*, except that it quotes each argument. A simple example of that is:

 
define(`echo', `$@')
=>
echo(arg1,    arg2, arg3 , arg4)
=>arg1,arg2,arg3 ,arg4

Where did the quotes go? Of course, they were eaten, when the expanded text were reread by m4. To show the difference, try

 
define(`echo1', `$*')
=>
define(`echo2', `$@')
=>
define(`foo', `This is macro `foo'.')
=>
echo1(foo)
=>This is macro This is macro foo..
echo2(foo)
=>This is macro foo.

See section 6.2 Tracing macro calls, if you do not understand this.

A `$' sign in the expansion text, that is not followed by anything m4 understands, is simply copied to the macro expansion, as any other text is.

 
define(`foo', `$$$ hello $$$')
=>
foo
=>$$$ hello $$$

If you want a macro to expand to something like `$12', put a pair of quotes after the $. This will prevent m4 from interpreting the $ sign as a reference to an argument.


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