As mentioned in many of my previous questions, I'm working through K&R, and am currently into the preprocessor. One of the more interesting things — something I never knew before from any of my prior attempts to learn C — is the ##
preprocessor operator. According to K&R:
The preprocessor operator
##
provides a way to concatenate actual
arguments during macro expansion. If a
parameter in the replacement text is
adjacent to a##
, the parameter is
replaced by the actual argument, the
##
and surrounding white space are
removed, and the result is re-scanned.
For example, the macropaste
concatenates its two arguments:
#define paste(front, back) front ## back
so
paste(name, 1)
creates the token
name1
.
How and why would someone use this in the real world? What are practical examples of its use, and are there gotchas to consider?
Best Answer
One thing to be aware of when you're using the token-paste ('
##
') or stringizing ('#
') preprocessing operators is that you have to use an extra level of indirection for them to work properly in all cases.If you don't do this and the items passed to the token-pasting operator are macros themselves, you'll get results that are probably not what you want:
The output: