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

Arithmetic built-ins

m4 provides three built-in macros for doing integer arithmetic. incr increments its numeric argument by 1. decr decrements by 1. To handle the common programming situation in which a variable is to be defined as ``one more than N'' you would use

   define(N, 100)
   define(N1, `incr(N)')
N1 is defined as one more than the current value of N.

The more general mechanism for arithmetic is a built-in called eval, which is capable of arbitrary arithmetic on integers. Its operators in decreasing order of precedence are

   +  -  (unary)
   **
   *  /  %
   +  -
   ==  !=  <  <=  >  >=
   !  ~
   &
   |  ^
   &&
   ||

Parentheses may be used to group operations where needed. All the operands of an expression given to eval must ultimately be numeric. The numeric value of a true relation (like 1 > 0) is 1, and false is 0. The precision in eval is 32 bits on the UNIX® operating system.

As a simple example, you can define M to be 2**N+1 with

   define(M, `eval(2**N+1)')
Then the sequence
   define(N, 3)
   M(2)
gives 9 as the result.
Next topic: File inclusion
Previous topic: Arguments

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