Stringification (#) & Token Pasting (##) in C
Two preprocessor operators work on macro arguments as text: # stringifies (#x turns the argument's spelling into a string literal — perfect for assertion messages echoing the failed expression), and ## pastes tokens (make_fn(name) generating init_name and free_name families). Together they let macros manipulate code spelling, not just values.
Both operate pre-expansion quirks included: arguments paste unexpanded unless routed through helper macros (the double-expansion trick). Niche but irreplaceable — test frameworks, register maps, and dispatch tables lean on them heavily.
# makes strings
#define STR(x) #x turns STR(hello) into "hello" — failed-expression echo in asserts comes from this.
## joins tokens
#define MKFN(n) void init_##n(void) — MKFN(db) declares init_db; families of near-identical functions from one macro.
Double-expand for values
#X pastes the literal spelling; helper-macro indirection (STR2(X) via STR(X)) pastes the expansion instead — know which you need.
Reserve for spelling work
Only code-shape tasks (names, messages, tables) justify these — value computation belongs in functions.
#include <stdio.h>#include <assert.h> int main(void) { int x = 2; assert(x > 5); return 0;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C yourself — building beats watching.