C technique uses designated initializers to simulate named, default function arguments
A blog post revisits a pattern from Ben Klemens' 2014 book 21st Century C that uses C's designated initializers, combined with a struct and macro, to let function calls take optional, named arguments with default values. The technique relies on two C-specific behaviors not adopted by C++20: initializers can appear in any order, and repeated designators simply overwrite earlier ones, with the last value taking effect.
GoKawiil's interpretation of the reporting above, not reported fact.
The approach lets developers add new optional parameters to existing C functions without breaking callers' existing code, since unspecified fields just take their defaults. This could make C APIs more flexible and easier to evolve over time, though the author notes the technique depends on compiler behavior that tools like Clang flag by default as a potential style issue (-Winitializer-overrides), suggesting it must be used deliberately rather than accidentally.
- The pattern combines a struct, a macro wrapper, and designated initializers to emulate named/default arguments in C.
- It depends on two C-only features of designated initializers that C++20 did not adopt: arbitrary ordering and last-write-wins for repeated fields.
- Using this idiom means new function parameters can be added later without breaking existing call sites, though Clang warns about the repeated-field behavior by default.
21st Century C by Ben Klemens — This is the very book where the author discovered the designated-initializer named-arguments trick discussed in the article. It's a great practical guide to modern C idioms like this one, perfect for anyone wanting to dig deeper into these patterns.
See 21st Century C by Ben Klemens on Amazon → Affiliate link — we may earn a commission on purchases, at no extra cost to you. Product picked by AI based on this article; it is not a tested recommendation.Source: quuxplusone.github.io — Arthur O Dwyer, 2026-11-18
Published there as: “C's designated initializers give us named arguments”
Read the original report → The summary and analysis above are GoKawiil's own, written from reporting by the source above. Facts and quotes belong to the original publisher.