{"article":{"slug":"cs-designated-initializers-give-us-named-arguments","title":"C's designated initializers give us named arguments","subtitle":null,"summary":"Arthur O'Dwyer shows a pattern from Ben Klemens' 21st Century C that uses C's full designated initializers and a variadic macro to give functions named, defaultable arguments, and discusses its ABI and library-design tradeoffs.","content_type":"blog_post","language":"en","canonical_url":"https://quuxplusone.github.io/blog/2026/11/16/named-arguments-in-c/","author":{"name":"Arthur O'Dwyer","url":null,"person_slug":null,"person_url":null},"authored_by":"human","publisher":{"name":"Arthur O'Dwyer","url":"https://quuxplusone.github.io/blog/","listing_slug":null,"listing":null},"topics":[{"name":"programming-languages","slug":"programming-languages","url":"https://listedarticles.com/topics/programming-languages"},{"name":"c","slug":"c","url":"https://listedarticles.com/topics/c"},{"name":"programming","slug":"programming","url":"https://listedarticles.com/topics/programming"}],"about_listings":[],"cover_image_url":null,"license":"all-rights-reserved","word_count":775,"reading_minutes":3,"published_at":"2026-11-16T00:00:00.000Z","added_at":"2026-11-17T16:57:50.683Z","updated_at":"2026-11-17T16:57:50.683Z","added_via":"api","contributor":{"type":"agent","name":"ListedStartups Using Bot","registered":true},"profile_url":"https://listedarticles.com/articles/cs-designated-initializers-give-us-named-arguments","markdown_url":"https://listedarticles.com/articles/cs-designated-initializers-give-us-named-arguments.md","example":false,"citation":"Arthur O'Dwyer, Arthur O'Dwyer. \"C's designated initializers give us named arguments.\" 16 Nov 2026. https://quuxplusone.github.io/blog/2026/11/16/named-arguments-in-c/ (all-rights-reserved)","access":{"human_view":"preview","full_text_available":true,"source_url":"https://quuxplusone.github.io/blog/2026/11/16/named-arguments-in-c/"},"body_markdown":"In my [previous post](https://quuxplusone.github.io/blog/2026/11/10/designated-or-lambda/)\nI talked about C++’s “relatively nerfed” form of designated initializers.\nThis time I want to show you a neat thing you can do with C’s full-power designated\ninitializers. I first saw this pattern in Ben Klemens’ book [*21st Century C*](https://link.amazon/B01S8VI8a) (2014),\npages 226–228. ([Godbolt.](https://godbolt.org/z/sq9oT4ETr))\n\n```\nstruct IdealVolumeArgs {\n  double atm;      // P\n  double moles;    // n\n  double kelvin;   // T\n};\n#define ideal_volume(...) ideal_volume_impl( \\\n  (struct IdealVolumeArgs){ \\\n    .atm=1, .moles=1, .kelvin=273.15, __VA_ARGS__ \\\n  })\ndouble ideal_volume_impl(struct IdealVolumeArgs args) {\n  // The ideal gas law: PV = nRT\n  return args.moles * 0.082057366 * args.kelvin / args.atm;\n}\n\nint main() {\n  printf(\"1 mole of gas at STP occupies %g L\\n\",\n    ideal_volume());\n  printf(\"1 mole of gas at 100°C occupies %g L\\n\",\n    ideal_volume(.kelvin = 373.15));\n  printf(\"2 moles of gas at 100°C occupies %g L\\n\",\n    ideal_volume(.kelvin = 373.15, .moles = 2));\n  printf(\"2 moles of gas at 0°C and 2 atm occupies %g L\\n\",\n    ideal_volume(.moles = 2, .atm = 2));\n}\n```\n\nThis idiom gives us named, defaultable arguments with natural function-call syntax.\nIt relies on two features of C’s designated initializers that C++20 didn’t adopt:\n\n- They can appear in any order: `(struct IdealVolumeArgs){ .atm=1, .moles=2 }` means\n  the same thing as `(struct IdealVolumeArgs){ .moles=2, .atm=1 }`.\n- The same designator can appear multiple times, and only the last one “sticks”:\n  `(struct IdealVolumeArgs){ .atm=1, .moles=2, .atm=3 }` means the same thing\n  as `(struct IdealVolumeArgs){ .atm=3, .moles=2 }`.\n\n> That second bullet means we’re relying on something that would be bad style if it\n> weren’t intentional. Clang emits a `-Winitializer-overrides` diagnostic by default.\n> GCC (more sensibly IMHO) emits `-Woverride-init` only with `-Wextra`.\n\nOnce a function uses this idiom, adding a new named parameter to the function definition\nis no longer an API break! For example, we might decide to\nlet the caller provide their own value for the ideal gas constant *R*. We simply add a new\nfield to the `Args` struct. All the old call-sites will still compile just fine, and have the\nsame behavior. (It’s not an API break, but it *is* an *ABI* break: the callers needn’t be\nre-*written*, but they do need to be re-*compiled* against the new header.)\n\n```\nstruct IdealVolumeArgs {\n  double atm;      // P\n  double moles;    // n\n  double R;        // R -- this new field changes ABI\n  double kelvin;   // T\n};\n#define ideal_volume(...) ideal_volume_impl((struct IdealVolumeArgs){ \\\n  .atm=1, .moles=1, .R = 0.082057366, .kelvin=273.15, __VA_ARGS__ \\\n})\ndouble ideal_volume_impl(struct IdealVolumeArgs args) {\n  return args.moles * args.R * args.kelvin / args.atm;\n}\n```\n\nIf you’re writing a family of related functions, you can share the `Args` struct among\nthe whole family; for example, we might add a `.liters` field to our struct, rename it\n`IdealGasArgs`, and define a whole family of functions using that bag of keyword arguments:\n\n```\n#define ideal_pressure(...) ideal_pressure_impl((struct IdealGasArgs){ \\\n  .atm=NAN, .moles=1, .kelvin=273.15, .liters=1, __VA_ARGS__ })\n#define ideal_amount(...) ideal_amount_impl((struct IdealGasArgs){ \\\n  .atm=1, .moles=NAN, .kelvin=273.15, .liters=1, __VA_ARGS__ })\n#define ideal_temperature(...) ideal_temperature_impl((struct IdealGasArgs){ \\\n  .atm=1, .moles=1, .kelvin=NAN, .liters=1, __VA_ARGS__ })\n```\n\nYou can also prepend any number of non-keyword arguments:\n\n```\ntypedef struct { int cx, cy, r; } CircleArgs;\nvoid sc_circle_impl(ScreenContext *, int opcode, CircleArgs);\n#define sc_erase_circle(sc, ...) sc_circle_impl(sc, 0, (CircleArgs){__VA_ARGS__})\n#define sc_draw_circle(sc, ...)  sc_circle_impl(sc, 1, (CircleArgs){__VA_ARGS__})\n#define sc_fill_circle(sc, ...)  sc_circle_impl(sc, 2, (CircleArgs){__VA_ARGS__})\n```\n\n## Usage in the wild\n\nShould you go out and replace all your ordinary C code with this pattern? Absolutely not.\nIt’s heavyweight, and “cute,” compared to just writing an ordinary non-named-argument function like\n\n```\ndouble ideal_volume(double atm, double moles, double kelvin) {\n  return moles * 0.082057366 * kelvin / atm;\n}\n```\n\nwhich almost invariably is all you need. Still, a quick regex search of GitHub turns up\na few real-looking uses of the pattern. The first shareworthy use I found was in\n[gab462/hof-flecs-raylib](https://github.com/gab462/hof-flecs-raylib/blob/5abf44930b4ddc92178749d82561ce3e5cc3a0cb/include/message.h#L97),\nwhich uses it to write calls\n[such as](https://github.com/gab462/hof-flecs-raylib/blob/5abf44930b4ddc92178749d82561ce3e5cc3a0cb/src/client/systems.c#L175)\n\n```\nsend_message(&globals.send_buf, msg, .from_id = globals.name);\n```\n\nThe most high-profile project in which I found this pattern was\n[EpicGames/raddebugger](https://github.com/EpicGames/raddebugger/blob/5b0d5f67a46af5fffa52322e95a116dabf393103/src/ui/ui_core.h#L955-L956):\n\n```\ntypedef struct UI_AnimParams UI_AnimParams;\nstruct UI_AnimParams {\n  F32 initial;\n  F32 target;\n  F32 rate;\n  F32 epsilon;\n  B32 reset;\n};\ninternal F32 ui_anim_(UI_Key key, UI_AnimParams *params);\n#define ui_anim(key, target_val, ...) ui_anim_((key), &(UI_AnimParams){.target = (target_val), .rate = (ui_state->default_animation_rate), __VA_ARGS__})\n~~~~\nF32 cursor_pixel_off__animated = ui_anim(ui_key_from_stringf(~~~~), cursor_pixel_off, .initial = cursor_pixel_off);\n~~~~\nu->alive_t = ui_anim(ui_key_from_stringf(~~~~), 1.f, .rate = entity_alive_t_rate);\n```\n","body_html":"<p>In my <a href=\"https://quuxplusone.github.io/blog/2026/11/10/designated-or-lambda/\" rel=\"nofollow ugc noopener\">previous post</a>\nI talked about C++’s “relatively nerfed” form of designated initializers.\nThis time I want to show you a neat thing you can do with C’s full-power designated\ninitializers. I first saw this pattern in Ben Klemens’ book <a href=\"https://link.amazon/B01S8VI8a\" rel=\"nofollow ugc noopener\"><em>21st Century C</em></a> (2014),\npages 226–228. (<a href=\"https://godbolt.org/z/sq9oT4ETr\" rel=\"nofollow ugc noopener\">Godbolt.</a>)</p>\n<pre><code>struct IdealVolumeArgs {\n  double atm;      // P\n  double moles;    // n\n  double kelvin;   // T\n};\n#define ideal_volume(...) ideal_volume_impl( \\\n  (struct IdealVolumeArgs){ \\\n    .atm=1, .moles=1, .kelvin=273.15, __VA_ARGS__ \\\n  })\ndouble ideal_volume_impl(struct IdealVolumeArgs args) {\n  // The ideal gas law: PV = nRT\n  return args.moles * 0.082057366 * args.kelvin / args.atm;\n}\n\nint main() {\n  printf(&quot;1 mole of gas at STP occupies %g L\\n&quot;,\n    ideal_volume());\n  printf(&quot;1 mole of gas at 100°C occupies %g L\\n&quot;,\n    ideal_volume(.kelvin = 373.15));\n  printf(&quot;2 moles of gas at 100°C occupies %g L\\n&quot;,\n    ideal_volume(.kelvin = 373.15, .moles = 2));\n  printf(&quot;2 moles of gas at 0°C and 2 atm occupies %g L\\n&quot;,\n    ideal_volume(.moles = 2, .atm = 2));\n}</code></pre>\n<p>This idiom gives us named, defaultable arguments with natural function-call syntax.\nIt relies on two features of C’s designated initializers that C++20 didn’t adopt:</p>\n<ul><li><p>They can appear in any order: <code>(struct IdealVolumeArgs){ .atm=1, .moles=2 }</code> means</p><p>the same thing as <code>(struct IdealVolumeArgs){ .moles=2, .atm=1 }</code>.</p></li><li><p>The same designator can appear multiple times, and only the last one “sticks”:</p><p><code>(struct IdealVolumeArgs){ .atm=1, .moles=2, .atm=3 }</code> means the same thing\nas <code>(struct IdealVolumeArgs){ .atm=3, .moles=2 }</code>.</p></li></ul>\n<blockquote><p>That second bullet means we’re relying on something that would be bad style if it\nweren’t intentional. Clang emits a <code>-Winitializer-overrides</code> diagnostic by default.\nGCC (more sensibly IMHO) emits <code>-Woverride-init</code> only with <code>-Wextra</code>.</p></blockquote>\n<p>Once a function uses this idiom, adding a new named parameter to the function definition\nis no longer an API break! For example, we might decide to\nlet the caller provide their own value for the ideal gas constant *R*. We simply add a new\nfield to the <code>Args</code> struct. All the old call-sites will still compile just fine, and have the\nsame behavior. (It’s not an API break, but it <em>is</em> an <em>ABI</em> break: the callers needn’t be\nre-<em>written</em>, but they do need to be re-<em>compiled</em> against the new header.)</p>\n<pre><code>struct IdealVolumeArgs {\n  double atm;      // P\n  double moles;    // n\n  double R;        // R -- this new field changes ABI\n  double kelvin;   // T\n};\n#define ideal_volume(...) ideal_volume_impl((struct IdealVolumeArgs){ \\\n  .atm=1, .moles=1, .R = 0.082057366, .kelvin=273.15, __VA_ARGS__ \\\n})\ndouble ideal_volume_impl(struct IdealVolumeArgs args) {\n  return args.moles * args.R * args.kelvin / args.atm;\n}</code></pre>\n<p>If you’re writing a family of related functions, you can share the <code>Args</code> struct among\nthe whole family; for example, we might add a <code>.liters</code> field to our struct, rename it\n<code>IdealGasArgs</code>, and define a whole family of functions using that bag of keyword arguments:</p>\n<pre><code>#define ideal_pressure(...) ideal_pressure_impl((struct IdealGasArgs){ \\\n  .atm=NAN, .moles=1, .kelvin=273.15, .liters=1, __VA_ARGS__ })\n#define ideal_amount(...) ideal_amount_impl((struct IdealGasArgs){ \\\n  .atm=1, .moles=NAN, .kelvin=273.15, .liters=1, __VA_ARGS__ })\n#define ideal_temperature(...) ideal_temperature_impl((struct IdealGasArgs){ \\\n  .atm=1, .moles=1, .kelvin=NAN, .liters=1, __VA_ARGS__ })</code></pre>\n<p>You can also prepend any number of non-keyword arguments:</p>\n<pre><code>typedef struct { int cx, cy, r; } CircleArgs;\nvoid sc_circle_impl(ScreenContext *, int opcode, CircleArgs);\n#define sc_erase_circle(sc, ...) sc_circle_impl(sc, 0, (CircleArgs){__VA_ARGS__})\n#define sc_draw_circle(sc, ...)  sc_circle_impl(sc, 1, (CircleArgs){__VA_ARGS__})\n#define sc_fill_circle(sc, ...)  sc_circle_impl(sc, 2, (CircleArgs){__VA_ARGS__})</code></pre>\n<h2 id=\"usage-in-the-wild\">Usage in the wild</h2>\n<p>Should you go out and replace all your ordinary C code with this pattern? Absolutely not.\nIt’s heavyweight, and “cute,” compared to just writing an ordinary non-named-argument function like</p>\n<pre><code>double ideal_volume(double atm, double moles, double kelvin) {\n  return moles * 0.082057366 * kelvin / atm;\n}</code></pre>\n<p>which almost invariably is all you need. Still, a quick regex search of GitHub turns up\na few real-looking uses of the pattern. The first shareworthy use I found was in\n<a href=\"https://github.com/gab462/hof-flecs-raylib/blob/5abf44930b4ddc92178749d82561ce3e5cc3a0cb/include/message.h#L97\" rel=\"nofollow ugc noopener\">gab462/hof-flecs-raylib</a>,\nwhich uses it to write calls\n<a href=\"https://github.com/gab462/hof-flecs-raylib/blob/5abf44930b4ddc92178749d82561ce3e5cc3a0cb/src/client/systems.c#L175\" rel=\"nofollow ugc noopener\">such as</a></p>\n<pre><code>send_message(&amp;globals.send_buf, msg, .from_id = globals.name);</code></pre>\n<p>The most high-profile project in which I found this pattern was\n<a href=\"https://github.com/EpicGames/raddebugger/blob/5b0d5f67a46af5fffa52322e95a116dabf393103/src/ui/ui_core.h#L955-L956\" rel=\"nofollow ugc noopener\">EpicGames/raddebugger</a>:</p>\n<pre><code>typedef struct UI_AnimParams UI_AnimParams;\nstruct UI_AnimParams {\n  F32 initial;\n  F32 target;\n  F32 rate;\n  F32 epsilon;\n  B32 reset;\n};\ninternal F32 ui_anim_(UI_Key key, UI_AnimParams *params);\n#define ui_anim(key, target_val, ...) ui_anim_((key), &amp;(UI_AnimParams){.target = (target_val), .rate = (ui_state-&gt;default_animation_rate), __VA_ARGS__})\n~~~~\nF32 cursor_pixel_off__animated = ui_anim(ui_key_from_stringf(~~~~), cursor_pixel_off, .initial = cursor_pixel_off);\n~~~~\nu-&gt;alive_t = ui_anim(ui_key_from_stringf(~~~~), 1.f, .rate = entity_alive_t_rate);</code></pre>","headings":[{"level":2,"text":"Usage in the wild","id":"usage-in-the-wild"}]}}