C's designated initializers give us named arguments
joshcsimmons
77 points
47 comments
November 18, 2026
Related Discussions
Found 5 related stories in 37.6ms across 3,874 title embeddings via pgvector HNSW
- Why Zig over C for Systems Programming torutofu · 216 pts · November 13, 2026 · 40% similar
- C3 0.8.6:Library fetching,30% smaller safe-mode binaries,and stricter type rules DASD · 52 pts · November 12, 2026 · 39% similar
- It's all about the allocator ibobev · 62 pts · November 15, 2026 · 36% similar
- Partial Operator Precedence tosh · 106 pts · November 02, 2026 · 35% similar
- Pointer Provenance in Rust vs. C++: Pointers Are Not Just Glorified Ints sofiabelenlopez · 36 pts · November 05, 2026 · 35% similar
Discussion Highlights (12 comments)
binaryturtle
I always thought Amiga's "tag list" concept was really nice. You easily could extend the API w/o breaking the ABI, which was essential for its shared library concept. And it essentially gave you named arguments too.
robmccoll
It's fun watching people rediscover this every so often. If you like this sort of thing, go check out creating generator functions using a switch embedded throughout your function. You can wrap it in macros to end up with a nice yield keyword emulation.
quietbritishjim
C++'s version of this is more verbose but still sometimes worth it: make the caller instantiate the struct and initialise it as separate statements: FooParams fooParams; // Gives sensible defaults to all members fooParams.x = 1; fooParams.y = "xyz"; foo(fooParams); It doesn't work so well if you want to move construct things because you need to move at least twice: once when moving the value into the member, then you need to move the whole struct to the function argument `foo(std::move(fooParams))`. If you want it to copy something but avoid unnecessary copying then it's even more annoying because you need to move to the function argument still which looks odd when your goal is to copy overall.
cryptonector
I proposed something like this ~20 years ago in the context of GSS-API, where there are several functions with lots of arguments but where most arguments have reasonable defaults, so one could use named arguments for all the values that one must give and not mention the others that get default values. But I wasn't serious about it.
coldpie
I both love & hate this. I would genuinely consider using it in my own projects, and also I would be very pissed off if I encountered it in someone else's project. Brilliant.
flohofwoe
Tbh I would skip all the preprocessor magic proposed in the article and just go for plain C99 'option bag struct' arguments, the called function only needs to be aware that "zero means default". I've been using this style for almost 10 years now in the sokol headers, and it still feels like the right thing to do and actually more ergonomic than most other languages for initializing complex structs, e.g. see here for creating a 3D render pipeline object with dozens of creation options, but most of them keeping their default values: state.pip = sg_make_pipeline(&(sg_pipeline_desc){ .layout = { .attrs = { [ATTR_texcube_pos].format = SG_VERTEXFORMAT_FLOAT3, [ATTR_texcube_color0].format = SG_VERTEXFORMAT_UBYTE4N, [ATTR_texcube_texcoord0].format = SG_VERTEXFORMAT_SHORT2N } }, .shader = shd, .index_type = SG_INDEXTYPE_UINT16, .cull_mode = SG_CULLMODE_BACK, .depth = { .compare = SG_COMPAREFUNC_LESS_EQUAL, .write_enabled = true }, .label = "texcube-pipeline" }); (or see here: https://github.com/floooh/sokol-samples/blob/11bbe1a6865e93c... ) ...this code snippet also makes use of C99 features that are not available in C++20 though (or most other languages), and it's really remarkable how well designed the C99 designated init feature as a whole is (even though it was an afterthought) and how badly its individual subfeatures were understood by most later languages (basically, only Odin got it "mostly right": https://github.com/floooh/sokol-odin/blob/7d47a31cad83a26ba1... )
cozzyd
The idiom I usually use is to define a DEFAULT_FOO_PARAMS like struct foo { int Z; int A; double E, double v }; #define DEFAULT_FOO_PARAMS .Z = 1, v = 3e8 foo_fn ( &(struct foo){ DEFAULT_FOO_PARAMS, .Z =2, .A = 4, .E = 1 }); But I do sort of wish there was a language-supported way to set defaults for a designated initializer in the struct definition (like C++ default values) to avoid having to write DEFAULT_FOO_PARAMS there.
david2ndaccount
I always considered this kind of a hack, needing to wrap the actual function call with a function-like macro. Moreover, you’re now using a different calling convention with passing a struct instead of basic types which can be less efficient. I wish C would add actual named arguments like I added in my DrC project: https://github.com/drpriver/DrC/blob/master/EXTENSIONS.md#na...
cesaref
I do wonder how many times setting the same member twice is a bug rather than a feature. I can imagine quite a bit of copy/paste leading to errors like this.
jcalvinowens
The macro is cute, but passing structures by value like that is going to cause real measurable overhead where the functions don't get inlined (the author sort of admits this I guess by calling it "heavyweight").
nixpulvis
Treating named arguments as structs is my proposal for Rust as well: https://nixpulvis.com/ramblings/2026-10-19-anonymous-struct-...
s3graham
Love it too! https://mstdn.social/@sgraham/112486947826240650 As I mentioned there, I think it's even cooler when you collapse the "constructor" macro, and tag to be the same name.