What are the # (stringize) and ## (token paste) operators?
Short answer
In a macro, # turns a parameter into a string literal (stringizing), and ## concatenates two tokens into one (token pasting). They enable macro tricks like generating names or debug output, but are advanced and easy to misuse.
Advertisement
AModel answer
In a macro, # turns a parameter into a string literal (stringizing), and ## concatenates two tokens into one (token pasting). They enable macro tricks like generating names or debug output, but are advanced and easy to misuse. They're used in logging/assert macros and code-generation patterns.
#define STR(x) #x // STR(hi) -> "hi"
#define CAT(a,b) a##b // CAT(foo,bar) -> foobarReal-world example
An assert-style macro uses # to embed the failing expression's text in the error message.
Likely follow-ups
- What does # do? | What does ## do?
Related topics