November 28, 2025
Format wars: printf edition
Generalizing Printf in C
Dev drama: can one function replace all printf
TLDR: A proposal to collapse C’s many print functions into one sparked a split: GNU users say vfprintf plus system helpers already solves it, while others defend careful use of sprintf and nitpick types. Embedded devs point to a lightweight library. It matters for safety, performance, and simplicity.
The plan sounds simple: take the dozen ways C prints text and funnel them through a single mega-function. The article pitches a “one function to rule them all” approach, arguing you can build everything—from printing to files to buffers—on top of a core formatter. But the comments turned it into a full-on formatting flame war.
GNU fans showed up first: theamk basically rolled in with “just use vfprintf,” then flexed the trio of system tricks—fmemopen, open_memstream, and fopencookie—like cheat codes that make generic printing a solved problem. Meanwhile, old-school safety hawks said dumping sprintf (the one that can overflow buffers) is overkill. kazinator came in hot with: you can cap lengths and know your bounds, so “sprintf isn’t evil, it’s just sharp.”
Then came the pedants, and yes, they brought receipts: kevin_thibedeau dropped the classic nitpick—“idx should be a size_t”—sparking the age-old debate of correctness vs. practicality. Embedded folks chimed in too, plugging a popular standalone printf library that dodges heavy system dependencies and basically says: if you want light and fast, it already exists. And for the nostalgia crowd, jmclnx reminisced about Zortech’s disp_printf, which let you print anywhere on the screen—like retro UI magic.
Verdict? The community’s torn between elegant theory, GNU power tools, pragmatic safety, and vintage vibes. Classic C drama, zero chill.
Key Points
- •The article proposes generalizing the C printf-family into a single backing implementation, with existing functions as wrappers.
- •Many printf variants wrap their variadic forms, reducing the core set to vsprintf, vfprintf, vsnprintf, and vasprintf.
- •sprintf is considered unsafe, similar to gets, and compilers like GCC warn against its use; snprintf can emulate sprintf by using a large size limit (e.g., SIZE_MAX).
- •A unification via vasprintf (format to a dynamic buffer, then output with memcpy/fputs and free) is possible but inefficient due to redundant allocations.
- •A more efficient streaming approach requires state management; the article suggests a generalized _vfsprintf with a submit callback and a state struct (bufinfo) passed via void*.