Simple macro definition
A simple macro is identified by its name and body.
Syntax
&define identifier body
- identifier is the name of the macro. Any valid identifier can be used.
- body is any sequence of tokens until the end of the line.
After substitution, the macro definition is replaced with blank lines.
A preprocessor directive must start at the beginning of the line. The &
ampersand character can be preceded by blanks.
Usage
As the preprocessor scans the text, it substitutes the macro body for the name identifier.
The following example show macro substitution with 2 simple macros:
&define MAX_TEST 12
&define HW "Hello world"
MAIN
DEFINE i INTEGER
FOR i=1 TO MAX_TEST
DISPLAY HW
END FOR
END MAIN
& 1 "A"
MAIN
DEFINE i INTEGER
FOR i=1 TO 12
DISPLAY "Hello world"
END FOR
END MAIN
The macro definition can be continued on multiple lines, but when the macro is expanded, it is joined to a single line as follows:
&define TABLE_VALUES 1, \
2, \
3
DISPLAY TABLE_VALUES
& 1 "A"
DISPLAY 1, 2, 3
The source file is processed sequentially, so a macro takes effect at the place it has been written:
DISPLAY X
&define X "Hello"
DISPLAY X
& 1 "A"
DISPLAY X
DISPLAY "Hello"
The macro body is expanded only when the macro is applied:
&define AA BB
&define BB 12
DISPLAY AA
& 1 "A"
DISPLAY 12
- AA is first expanded to BB.
- The text is re-scanned and BB is expanded to 12.
- When the macro AA is defined, BB is not known yet; but it is known when the macro AA is used.
In order to prevent infinite recursion, a macro cannot be expanded recursively.
&define A B
&define B A
&define C C
A C
& 1 "A"
A C
- A is first expanded to B.
- B is expanded to A.
- A is not expanded again as it appears in its own expansion.
- C expands to C and can not be expanded further.
It is also possible to define a macro with the -D
command line option of
compilers.