User`s manual

Dynamic C Users Manual digi.com 27
4.5 Macros
Macros may be defined in Dynamic C by using #define. A macro is a name replacement feature.
Dynamic C has a text preprocessor that expands macros before the program text is compiled. The pro-
grammer assigns a name, up to 31 characters, to a fragment of text. Dynamic C then replaces the macro
name with the text fragment wherever the name appears in the program. In this example,
the variable i gets the value x * 72 + 12. Macros can have parameters such as in the following code.
The compiler removes the surrounding white space (comments, tabs and spaces) and collapses each
sequence of white space in the macro definition into one space. It places a \ before any " or \ to preserve
their original meaning within the definition.
4.5.1 Macro Operators # and ##
Dynamic C implements the # and ## macro operators.
The # operator forces the compiler to interpret the parameter immediately following it as a string literal.
For example, if a macro is defined
#define report(value,fmt)\
printf( #value "=" #fmt "\n", value )
then the macro in
report( string, %s );
will expand to
printf( "string" "=" "%s" "\n", string );
and because C always concatenates adjacent strings, the final result of expansion will be
printf( "string=%s\n", string );
The ## operator concatenates the preceding character sequence with the following character sequence,
deleting any white space in between. For example, given the macro
#define set(x,y,z) x ## z ## _ ## y()
the macro in
set( AASC, FN, 6 );
will expand to
AASC6_FN();
For parameters immediately adjacent to the ## operator, the corresponding argument is not expanded
before substitution, but appears as it does in the macro call.
#define OFFSET 12
#define SCALE 72
int i, x;
i = x * SCALE + OFFSET;
#define word( a, b ) (a<<8 | b)
char c;
int i, j;
i = word( j, c ); // same as i = (j << 8 | c)