Learn C
#include "preprocessor/stringification.h"

Stringification (#) & Token Pasting (##) in C

#concept

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.

#how it works
1

# makes strings

#define STR(x) #x turns STR(hello) into "hello" — failed-expression echo in asserts comes from this.

2

## joins tokens

#define MKFN(n) void init_##n(void) — MKFN(db) declares init_db; families of near-identical functions from one macro.

3

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.

4

Reserve for spelling work

Only code-shape tasks (names, messages, tables) justify these — value computation belongs in functions.

#example
#include <stdio.h>
#include <assert.h>
 
int main(void) {
int x = 2;
assert(x > 5);
return 0;
}
Output
a.out: main.c:6: main: Assertion `x > 5' failed.
1assert stringifies its argument with # — the failure message echoes the exact expression x > 5.
2File, line, and function join automatically via predefined macros.
3The aborted program reports precisely what failed and where — stringification doing its signature trick.
#use cases
Assertion messages echoing failed expressions (the assert.h pattern)
Generating function families (init_X/free_X) from name lists
Register-map tables with pasted peripheral names
Enum-to-string mapping via stringified enumerators

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C yourself — building beats watching.