mikroC

mikroC - C Compiler for Microchip PIC microcontrollers

making it simple...

Macros with Parameters

The following syntax is used to define a macro with parameters:

#define macro_identifier(<arg_list>) token_sequence

Note there can be no whitespace between the macro_identifier and the “(”. The optional arg_list is a sequence of identifiers separated by commas, not unlike the argument list of a C function. Each comma-delimited identifier plays the role of a formal argument or placeholder.

Such macros are called by writing

macro_identifier(<actual_arg_list>)

in the subsequent source code. The syntax is identical to that of a function call; indeed, many standard library C “functions” are implemented as macros. However, there are some important semantic differences.

The optional actual_arg_list must contain the same number of comma-delimited token sequences, known as actual arguments, as found in the formal arg_list of the #define line – there must be an actual argument for each formal argument. An error will be reported if the number of arguments in the two lists is different.

A macro call results in two sets of replacements. First, the macro identifier and the parenthesis-enclosed arguments are replaced by the token sequence. Next, any formal arguments occurring in the token sequence are replaced by the corresponding real arguments appearing in the actual_arg_list. As with simple macro defini- tions, rescanning occurs to detect any embedded macro identifiers eligible for expansion.

page

 

128

MikroElektronika: Development tools - Books - Compilers