From K&R
-
Use
#
as replacement text
-
And Use
##
to concatenate
The preprocessor operator##
provides a way to concatenate actual arguments during macro expansion.If a parameter in the replacement text is adjacent to athe parameter is replaced by the actual argument, the##
and surrounding white space are removed, and the result is re-scanned.For example, the macro paste concatenates its two arguments:
#define paste(front, back) front ## back
Nonetheless, macros are valuable.One practical example comes from <stdio.h>
, in which getchar
and putchar
are often defined as macros to avoid the e run-time overhead of a function call per character processed.The functions in <ctype.h>
are also usually implemented as macros.