<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Paul J. Lucas</title>
    <description>The latest articles on Forem by Paul J. Lucas (@pauljlucas).</description>
    <link>https://forem.com/pauljlucas</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2304%2F7366785.jpeg</url>
      <title>Forem: Paul J. Lucas</title>
      <link>https://forem.com/pauljlucas</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/pauljlucas"/>
    <language>en</language>
    <item>
      <title>A Fix for Time Machine Always Making Full Backups</title>
      <dc:creator>Paul J. Lucas</dc:creator>
      <pubDate>Mon, 23 Feb 2026 13:50:14 +0000</pubDate>
      <link>https://forem.com/pauljlucas/a-fix-for-time-machine-always-making-full-backups-44cj</link>
      <guid>https://forem.com/pauljlucas/a-fix-for-time-machine-always-making-full-backups-44cj</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;At some point, I upgraded the macOS version on my iMac from &lt;a href="https://en.wikipedia.org/wiki/MacOS_Sequoia" rel="noopener noreferrer"&gt;Sequoia&lt;/a&gt; to &lt;a href="https://en.wikipedia.org/wiki/MacOS_Tahoe" rel="noopener noreferrer"&gt;Tahoe&lt;/a&gt;.  I think this was the trigger that starting making &lt;a href="https://en.wikipedia.org/wiki/Time_Machine_(macOS)" rel="noopener noreferrer"&gt;Time Machine&lt;/a&gt; always perform full backups to my Mac Mini server.  (Normally, it’s supposed to perform incremental backups.)  Not only was this a &lt;em&gt;lot&lt;/em&gt; slower, but it filled up my back-up disk and caused “disk full” errors. I Google’d, I asked LLMs, tried several things — nothing fixed it. I had to disable Time Machine backups until I found a solution.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Fix
&lt;/h2&gt;

&lt;p&gt;I eventually stumbled across &lt;a href="https://apple.stackexchange.com/a/476007/30226" rel="noopener noreferrer"&gt;this answer&lt;/a&gt;. I’ve distilled the fix into a script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#! /usr/bin/env bash&lt;/span&gt;

&lt;span class="nb"&gt;set&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="c"&gt;# stop on error&lt;/span&gt;

&lt;span class="nv"&gt;ATTRIBUTES&lt;/span&gt;&lt;span class="o"&gt;=(&lt;/span&gt;
  com.apple.backupd.BackupMachineAddress
  com.apple.backupd.ComputerName
  com.apple.backupd.HostUUID
  com.apple.backupd.ModelID
  com.apple.timemachine.private.structure.metadata
&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="nv"&gt;DIR&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/System/Volumes/Data

&lt;span class="k"&gt;for &lt;/span&gt;attr &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;ATTRIBUTES&lt;/span&gt;&lt;span class="p"&gt;[@]&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;do &lt;/span&gt;xattr &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="nv"&gt;$attr&lt;/span&gt; &lt;span class="nv"&gt;$DIR&lt;/span&gt;
&lt;span class="k"&gt;done&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At this point, I figured I might as well try it.  So I:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Removed the remote disk as a backup destination.&lt;/li&gt;
&lt;li&gt;Deleted the old backup.&lt;/li&gt;
&lt;li&gt;Ran the script locally on the iMac.&lt;/li&gt;
&lt;li&gt;Re-added the remote disk as a backup destination.&lt;/li&gt;
&lt;li&gt;Kicked off a new backup.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It took several hours to do the first full backup, but after that, it started doing only incremental backups again. Huzzah!&lt;/p&gt;

&lt;p&gt;The reason for this article is to help spread this fix around the Internet to make it easier to stumble across for someone else having the same issue.&lt;/p&gt;

&lt;p&gt;Note that neither I nor very likely the original author of the fix can say that this fix will fix &lt;em&gt;all&lt;/em&gt; situations that might cause Time Machine always to do full backups.&lt;/p&gt;

</description>
      <category>mac</category>
      <category>macos</category>
      <category>timemachine</category>
    </item>
    <item>
      <title>Musings on Structure Declarations</title>
      <dc:creator>Paul J. Lucas</dc:creator>
      <pubDate>Sat, 21 Feb 2026 15:34:59 +0000</pubDate>
      <link>https://forem.com/pauljlucas/musings-on-structure-declarations-cfb</link>
      <guid>https://forem.com/pauljlucas/musings-on-structure-declarations-cfb</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;This article is sort-of a follow-up to my &lt;a href="https://dev.to/pauljlucas/musings-on-c-c-declarations-169o"&gt;Musings on C &amp;amp; C++ Declarations&lt;/a&gt; and &lt;a href="https://dev.to/pauljlucas/why-c-requires-the-struct-keyword-for-structures-nec"&gt;Why C Requires the “struct” Keyword for Structures&lt;/a&gt; articles specifically focusing on structure (&lt;code&gt;struct&lt;/code&gt;) declarations.  As a preview, all of the following are valid uses of &lt;code&gt;struct&lt;/code&gt; (independently, but not necessarily consecutively in the same program):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;point&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                                &lt;span class="c1"&gt;// 1&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;point&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;                  &lt;span class="c1"&gt;// 2a&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;point&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                &lt;span class="c1"&gt;// 2b&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                      &lt;span class="c1"&gt;// 3&lt;/span&gt;
&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;point&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;          &lt;span class="c1"&gt;// 4&lt;/span&gt;
&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;point&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;point&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;// 5a&lt;/span&gt;
&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;point_s&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;point&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// 5b&lt;/span&gt;
&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;point&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;point_t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// 5c&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  1. Forward Structure Declaration
&lt;/h2&gt;

&lt;p&gt;A declaration like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;point&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// Forward declaration.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;is a &lt;em&gt;forward declaration&lt;/em&gt; that effectively tells the compiler “&lt;code&gt;point&lt;/code&gt; is a structure” but without any other details.  What use is that?  It allows you to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create an &lt;em&gt;opaque type&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Break bidirectional type dependencies.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;An opaque type can be used as part of an API for a library where you want to keep the details of a structure hidden from the user.  This has two benefits:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It prevents the user from messing around with the members of the structure when they shouldn’t.&lt;/li&gt;
&lt;li&gt;It makes upgrading a library much simpler.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Consequently, users are forced to use only your API to create, manipulate, and destroy objects of such a type.  It allows you as the library maintainer to add, remove, or change its members at will without fear of breaking users’ programs.  It also allows users to upgrade the version of your library that they use as a drop-in replacement of the shared object file (&lt;code&gt;.so&lt;/code&gt;) without having to recompile their programs.  This is often very useful in live, production systems.&lt;/p&gt;

&lt;p&gt;However, opaque types aren’t without caveats:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;All objects must be dynamically allocated.  This is both slower and more taxing on the memory allocator, especially for many small objects. (However, you might be able to use an &lt;a href="https://dev.to/pauljlucas/using-arenas-for-easier-object-management-145k"&gt;arena&lt;/a&gt;.)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;No functions operating on an object of the type can be &lt;a href="https://dev.to/pauljlucas/inline-functions-in-c-and-c-2040"&gt;&lt;code&gt;inline&lt;/code&gt;&lt;/a&gt; that may yield worse performance.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A forward declaration can also be used to break interdependencies.  For example, in &lt;a href="https://github.com/paul-j-lucas/cdecl" rel="noopener noreferrer"&gt;cdecl&lt;/a&gt;, an &lt;a href="https://en.wikipedia.org/wiki/Abstract_syntax_tree" rel="noopener noreferrer"&gt;AST&lt;/a&gt; node for a declaration can have an optional alignment; that alignment can be given by a another AST node:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;c_alignas&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
  &lt;span class="k"&gt;union&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;c_ast&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;type_ast&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Forward declaration.&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;c_ast&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;c_alignas&lt;/span&gt; &lt;span class="n"&gt;align&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;          &lt;span class="c1"&gt;// Alignment, if any.&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unlike C++, a stand-alone forward declaration for a structure isn’t actually needed most of the time since the mere act of &lt;em&gt;mentioning&lt;/em&gt; a &lt;code&gt;struct&lt;/code&gt; type implicitly creates a forward declaration for it.  Above, inside &lt;code&gt;c_alignas&lt;/code&gt;, the &lt;code&gt;struct&lt;/code&gt; declaration simultaneously:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Forward-declares &lt;code&gt;c_ast&lt;/code&gt; as a structure; &lt;em&gt;and&lt;/em&gt;:&lt;/li&gt;
&lt;li&gt;Also declares &lt;code&gt;type_ast&lt;/code&gt; to be a pointer to such a structure.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  2. Ordinary Structure Declaration
&lt;/h2&gt;

&lt;p&gt;A declaration like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;point&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;is just an ordinary structure declaration; a declaration like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;point&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// Simultaneously declare 'p' as point.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;simultaneously:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Declares an ordinary structure; &lt;em&gt;and&lt;/em&gt;:&lt;/li&gt;
&lt;li&gt;A variable of that structure.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Personally, I don’t like doing this since it’s easy to miss the declaration of the variable way down at the bottom.  For readability, I’d rather be a bit redundant by using separate declarations:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;point&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;point&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// Separate declaration is clearer.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Unnamed Structure Declaration
&lt;/h2&gt;

&lt;p&gt;Perhaps curiously, &lt;code&gt;struct&lt;/code&gt; can be used &lt;em&gt;without&lt;/em&gt; a tag name:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That declares an &lt;em&gt;unnamed structure&lt;/em&gt; that’s effectively a one-off.  Note that an unnamed structure is &lt;em&gt;not&lt;/em&gt; the same as an &lt;a href="https://dev.to/pauljlucas/anonymous-structures-in-c-2ebk"&gt;anonymous structure&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Although unnamed structures by themselves are allowed by the grammar, the only use I can think of for them is &lt;a href="https://dev.to/pauljlucas/declaring-multiple-variables-in-a-for-loop-initialization-clause-20n3"&gt;declaring multiple variables in a &lt;code&gt;for&lt;/code&gt; loop initialization clause&lt;/a&gt;.  Additionally, two identical unnamed structures are considered different types:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;q&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                   &lt;span class="c1"&gt;// Error: different types.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. &lt;code&gt;typedef&lt;/code&gt; Unnamed Structure Declaration
&lt;/h2&gt;

&lt;p&gt;The only place an unnamed structure is useful is when it’s in combination with a &lt;code&gt;typedef&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;point&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Such declarations are common in C code because they bypass the odd tags namespace and directly make structures be first class citizen types.&lt;/p&gt;

&lt;p&gt;Personally, I don’t like doing this since it hides the name of the structure way down at the bottom just like the variable in #2 above.  As above, I’d rather be a bit redundant by using separate declarations:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;slist&lt;/span&gt; &lt;span class="n"&gt;slist&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;slist&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;slist&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;void&lt;/span&gt;  &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You &lt;em&gt;can&lt;/em&gt; put the &lt;code&gt;typedef&lt;/code&gt; first since, as stated in #1 above, the mere act of &lt;em&gt;mentioning&lt;/em&gt; a &lt;code&gt;struct&lt;/code&gt; type implicitly creates a forward declaration for it.&lt;/p&gt;

&lt;p&gt;The advantage of putting the &lt;code&gt;typedef&lt;/code&gt; first is that you can then use it in the definition of the structure itself as is common in self-referential structures like the singly linked list shown above.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. &lt;code&gt;typedef&lt;/code&gt; Structure Declaration
&lt;/h2&gt;

&lt;p&gt;The last three declarations:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;point&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;point&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;// 5a&lt;/span&gt;
&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;point_s&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;point&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// 5b&lt;/span&gt;
&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;point&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;point_t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// 5c&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;combine &lt;code&gt;typedef&lt;/code&gt; with that of a named structure into the same declaration:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Case (a) just uses the same name for the tag as the type.&lt;/li&gt;
&lt;li&gt;Case (b) appends &lt;code&gt;_s&lt;/code&gt; to the tag (“s” for &lt;code&gt;struct&lt;/code&gt;).  (I’ve seen some codebases do this.)&lt;/li&gt;
&lt;li&gt;Case (c) appends &lt;code&gt;_t&lt;/code&gt; to the type.  (I’ve seen other codebases do this — even though you really shouldn’t since the &lt;code&gt;_t&lt;/code&gt; suffix is reserved for use by &lt;a href="https://en.wikipedia.org/wiki/POSIX" rel="noopener noreferrer"&gt;POSIX&lt;/a&gt;.)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Consistent with case #4 above, I personally don’t like doing any of these either for the same reason.  Again, I’d prefer separate declarations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Unions &amp;amp; Enumerations
&lt;/h2&gt;

&lt;p&gt;Everything written above also applies to both &lt;a href="https://dev.to/pauljlucas/unions-in-c-1ojj"&gt;unions&lt;/a&gt; and &lt;a href="https://dev.to/pauljlucas/enumerations-in-c-ae7"&gt;enumerations&lt;/a&gt; with the exception that enumerations can’t be forward declared (until &lt;a href="https://en.wikipedia.org/wiki/C23_(C_standard_revision)" rel="noopener noreferrer"&gt;C23&lt;/a&gt; and with fixed-type enumerations).&lt;/p&gt;

&lt;h2&gt;
  
  
  C++
&lt;/h2&gt;

&lt;p&gt;As mentioned &lt;a href="https://dev.to/pauljlucas/why-c-requires-the-struct-keyword-for-structures-nec"&gt;previously&lt;/a&gt;, C++ effectively does an implicit &lt;code&gt;typedef&lt;/code&gt; for every structure declaration.  To keep compatibility, C++ allows all the ways in which a structure can be declared and referred to in C also:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;point&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="n"&gt;point&lt;/span&gt; &lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;         &lt;span class="c1"&gt;// Ordinary C++ declaration.&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;point&lt;/span&gt; &lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// C declaration for compatibility.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Due to the quirky tags namespace, there are several ways to declare structures that has led to inconsistencies across codebases.  For your own code, pick one style and use it consistently.&lt;/p&gt;

</description>
      <category>c</category>
      <category>cpp</category>
    </item>
    <item>
      <title>Why C Requires the “struct” Keyword for Structures</title>
      <dc:creator>Paul J. Lucas</dc:creator>
      <pubDate>Fri, 20 Feb 2026 02:16:00 +0000</pubDate>
      <link>https://forem.com/pauljlucas/why-c-requires-the-struct-keyword-for-structures-nec</link>
      <guid>https://forem.com/pauljlucas/why-c-requires-the-struct-keyword-for-structures-nec</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Regardless of whether you’ve been programming in C for many years or have only recently started, you might wonder why structure declarations require that the &lt;code&gt;struct&lt;/code&gt; keyword be included.  For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;point&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="n"&gt;point&lt;/span&gt; &lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;         &lt;span class="c1"&gt;// Error: "struct" required.&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;point&lt;/span&gt; &lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// OK.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Obviously, &lt;code&gt;struct&lt;/code&gt; is needed to &lt;em&gt;declare&lt;/em&gt; a structure, but that’s not what this article is about.  This article is about why, once declared, &lt;code&gt;struct&lt;/code&gt; is &lt;em&gt;still&lt;/em&gt; needed to &lt;em&gt;use&lt;/em&gt; the previously declared structure type.  If you omit &lt;code&gt;struct&lt;/code&gt;, the compiler still knows what you &lt;em&gt;mean&lt;/em&gt;, but it pedantically requires that you include the &lt;code&gt;struct&lt;/code&gt; keyword anyway.&lt;/p&gt;

&lt;p&gt;If you’ve programmed in other languages, this requirement seems even more curious because no other language that allows you to declare “structure” (Go), “record” (Pascal), or “class” (C++, Java, Python, and many others) types requires repeating a keyword when you &lt;em&gt;use&lt;/em&gt; the type.  In other languages, such a type becomes a “first class citizen” that can be used just the same as a built-in type like &lt;code&gt;int&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Indeed, when &lt;a href="https://www.stroustrup.com" rel="noopener noreferrer"&gt;Stroustrup&lt;/a&gt; designed C++, he made it so that structure (and class) names are useable as-is without needing to be prefixed by a keyword.  He gave his rationale in &lt;a href="https://www.stroustrup.com/arm.html" rel="noopener noreferrer"&gt;The Annotated C++ Reference Manual&lt;/a&gt; (§3.1c, p.26) an excerpt of which is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Requiring that such prefixing always appear would compromise the effort to make use of user defined types, such as &lt;code&gt;complex&lt;/code&gt;, as similar as possible to use of built-in types, such as &lt;code&gt;int&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Formally, structure, union, and enumeration names are in a separate “tags” namespace.  (I’ll get to unions and enumerations later, but let’s stick to structures for now.)  This means you can have both a structure name and some other identifier name (variable or function) with the same name.  For example, &lt;a href="https://pubs.opengroup.org/onlinepubs/9799919799/" rel="noopener noreferrer"&gt;POSIX&lt;/a&gt; defines &lt;code&gt;stat&lt;/code&gt; as both a &lt;a href="https://pubs.opengroup.org/onlinepubs/009696799/basedefs/sys/stat.h.html" rel="noopener noreferrer"&gt;structure&lt;/a&gt; and a &lt;a href="https://pubs.opengroup.org/onlinepubs/009696799/functions/stat.html" rel="noopener noreferrer"&gt;function&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;stat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;stat&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;buf&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One not-very-helpful reason why the compiler requires &lt;code&gt;struct&lt;/code&gt; is because that’s how &lt;a href="https://en.wikipedia.org/wiki/Dennis_Ritchie" rel="noopener noreferrer"&gt;Ritchie&lt;/a&gt; designed C.  But then the question becomes, “Why did Ritchie design C that way?”&lt;/p&gt;

&lt;p&gt;So as not to keep you in suspense, as far as I’ve been able to tell, nobody definitively knows the answer.  Sadly, we lost Ritchie in 2011, so nobody can ask him.  However, some people have offered plausible explanations for why &lt;code&gt;struct&lt;/code&gt; is the way it is.&lt;/p&gt;

&lt;h2&gt;
  
  
  Origin Story
&lt;/h2&gt;

&lt;p&gt;Where did the &lt;code&gt;struct&lt;/code&gt; keyword come from?  It’s most likely from &lt;a href="https://en.wikipedia.org/wiki/ALGOL_68" rel="noopener noreferrer"&gt;Algol 68&lt;/a&gt; that has both &lt;strong&gt;&lt;code&gt;STRUCT&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;UNION&lt;/code&gt;&lt;/strong&gt; as keywords.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note that the Algol family of languages uses &lt;a href="https://en.wikipedia.org/wiki/Stropping_(syntax)" rel="noopener noreferrer"&gt;stropping&lt;/a&gt; for keywords, so bold, or some other mechanism, is &lt;em&gt;required&lt;/em&gt; to represent keywords.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But even in Algol 68, there’s no tags namespace.  That idea seems unique to C.&lt;/p&gt;

&lt;h2&gt;
  
  
  C Archeology
&lt;/h2&gt;

&lt;p&gt;If you do some digging, you might come across Ritchie’s &lt;a href="https://www.nokia.com/bell-labs/about/dennis-m-ritchie/cman.pdf" rel="noopener noreferrer"&gt;C Reference Manual&lt;/a&gt; (CRM) published in 1974 that predates &lt;a href="https://en.wikipedia.org/wiki/The_C_Programming_Language" rel="noopener noreferrer"&gt;The C Programming Language&lt;/a&gt; (TCPL1) first published in 1978 by four years.  In CRM §8.2 &lt;em&gt;Type specifiers&lt;/em&gt;, it says:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type-specifier:
        int
        char
        float
        double
        struct { type-decl-list }
        struct identifier { type-decl-list }
        struct identifier
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two things to note:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;If you scan the entire CRM, nowhere is &lt;code&gt;typedef&lt;/code&gt; mentioned. That’s because early versions of C didn’t have &lt;code&gt;typedef&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This means that &lt;em&gt;all&lt;/em&gt; types start with a keyword that would make writing any compiler simpler.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The lack of &lt;code&gt;typedef&lt;/code&gt; can be independently verified by scanning the &lt;a href="https://github.com/dspinellis/unix-history-repo/tree/Research-V6/usr/source/c" rel="noopener noreferrer"&gt;source code for the C compiler&lt;/a&gt;, specifically its &lt;a href="https://github.com/dspinellis/unix-history-repo/blob/4b87ee08354dc081ad897853173e6f7f4b52c116/usr/source/c/c00.c#L28" rel="noopener noreferrer"&gt;keyword table&lt;/a&gt;, that’s part of &lt;a href="https://en.wikipedia.org/wiki/Version_6_Unix" rel="noopener noreferrer"&gt;Sixth Edition Unix&lt;/a&gt; (Unix V6) released in 1975 — no &lt;code&gt;typedef&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implications
&lt;/h2&gt;

&lt;p&gt;As for making it simpler to write a compiler, consider the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;A&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;B&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;          &lt;span class="c1"&gt;// What is this?&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Without &lt;code&gt;typedef&lt;/code&gt;, &lt;code&gt;A&lt;/code&gt; and &lt;code&gt;B&lt;/code&gt; &lt;em&gt;must&lt;/em&gt; be variables, so the above is an expression for &lt;code&gt;A&lt;/code&gt; times &lt;code&gt;B&lt;/code&gt;.  However, once you add &lt;code&gt;typedef&lt;/code&gt;, &lt;code&gt;A&lt;/code&gt; &lt;em&gt;could&lt;/em&gt; be a type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;A&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// "A" is a synonym for "int"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If it is a type, then the above declares &lt;code&gt;B&lt;/code&gt; to be a pointer to type &lt;code&gt;A&lt;/code&gt;.  Hence, it’s &lt;a href="https://en.wikipedia.org/wiki/Context-sensitive_grammar" rel="noopener noreferrer"&gt;context-sensitive&lt;/a&gt;. This definitely complicates both the parser and the lexer often requiring a &lt;a href="https://en.wikipedia.org/wiki/Lexer_hack" rel="noopener noreferrer"&gt;lexer hack&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Hence, it’s likely that Ritchie required &lt;code&gt;struct&lt;/code&gt; to make writing the compiler simpler.  He may have also believed it made code easier to read for humans.  (Generally, programming languages that are easier to parse are also easier for humans to understand.)&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;typedef&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;By the time TCPL1 is published in 1978, &lt;code&gt;typedef&lt;/code&gt; was added to C. This too can be independently verified by scanning the &lt;a href="https://github.com/dspinellis/unix-history-repo/tree/Research-V7/usr/src/cmd/c" rel="noopener noreferrer"&gt;source code for the C compiler&lt;/a&gt;, specifically its &lt;a href="https://github.com/dspinellis/unix-history-repo/blob/5beb5e90fa2d79c2b5608af7ab5829c182a564b6/usr/src/cmd/c/c00.c#L22" rel="noopener noreferrer"&gt;keyword table&lt;/a&gt;, that’s part of &lt;a href="https://en.wikipedia.org/wiki/Version_7_Unix" rel="noopener noreferrer"&gt;Seventh Edition Unix&lt;/a&gt; (Unix V7) released in 1979 — with &lt;code&gt;typedef&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;typedef&lt;/code&gt; is quite handy, both for hiding implementation details especially for types that vary across platforms and helping to write complicated declarations that C is infamous for.  Consider:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;sig&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;func&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)))(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;which is the declaration for the &lt;a href="https://pubs.opengroup.org/onlinepubs/9799919799/functions/signal.html" rel="noopener noreferrer"&gt;POSIX library function&lt;/a&gt;.  According to &lt;a href="https://github.com/paul-j-lucas/cdecl" rel="noopener noreferrer"&gt;cdecl&lt;/a&gt; (line breaks added for readability):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cdecl&amp;gt; explain void (*signal(int sig, void (*func)(int)))(int)
declare signal as function
        ( sig as int,
          func as pointer to function (int) returning void )
    returning pointer to function (int) returning void
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That can become &lt;em&gt;much&lt;/em&gt; simpler by using &lt;code&gt;typedef&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="nf"&gt;void&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;sig_t&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;sig_t&lt;/span&gt; &lt;span class="nf"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;sig&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sig_t&lt;/span&gt; &lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hence, Ritchie added &lt;code&gt;typedef&lt;/code&gt; despite making the compiler harder to write.&lt;/p&gt;

&lt;p&gt;Once &lt;code&gt;typedef&lt;/code&gt; was added, then Ritchie &lt;em&gt;could&lt;/em&gt; have retroactively altered the way &lt;code&gt;struct&lt;/code&gt; is handled by doing away with the tags namespace and making structure types first class citizens.  But by this time, there was already a lot of C code out there and such a change would have broken many programs.  For better or worse, the way &lt;code&gt;struct&lt;/code&gt; worked was metaphorically carved into stone by now.&lt;/p&gt;

&lt;p&gt;If it’s any consolation, &lt;code&gt;typedef&lt;/code&gt; can be used to &lt;em&gt;make&lt;/em&gt; structure types into first class citizens:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;point&lt;/span&gt; &lt;span class="n"&gt;point&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;point&lt;/span&gt; &lt;span class="n"&gt;p3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;         &lt;span class="c1"&gt;// No "struct" needed now.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is, &lt;code&gt;typedef&lt;/code&gt; makes &lt;code&gt;point&lt;/code&gt; in the global namespace be an alias for &lt;code&gt;point&lt;/code&gt; in the tags namespace.  The fact that they have the same name is fine since they’re in different namespaces.  Personally, I do this for my own C code. However, other people have &lt;a href="https://www.kernel.org/doc/html/v4.10/process/coding-style.html#typedefs" rel="noopener noreferrer"&gt;different views&lt;/a&gt; on when &lt;code&gt;typedef&lt;/code&gt; should be used.&lt;/p&gt;

&lt;h2&gt;
  
  
  Unions &amp;amp; Enumerations
&lt;/h2&gt;

&lt;p&gt;If you look back at the &lt;em&gt;type-specifier&lt;/em&gt; from CRM, you might also notice that neither &lt;code&gt;union&lt;/code&gt; nor &lt;code&gt;enum&lt;/code&gt; are there either.  &lt;a href="https://dev.to/pauljlucas/unions-in-c-1ojj"&gt;Unions&lt;/a&gt; became part of C by the time of Unix V7 and TCPL1, but &lt;a href="https://dev.to/pauljlucas/enumerations-in-c-ae7"&gt;enumerations&lt;/a&gt; didn’t become part of C until &lt;a href="https://en.wikipedia.org/wiki/ANSI_C" rel="noopener noreferrer"&gt;C89&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Regardless, both &lt;code&gt;union&lt;/code&gt; and &lt;code&gt;enum&lt;/code&gt; work similarly to &lt;code&gt;struct&lt;/code&gt; in that their names are in the same tags namespace.  Ritchie very likely did this for consistency.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;So the most likely answer as to why C requires &lt;code&gt;struct&lt;/code&gt; is that it originally made all types start with a keyword that in turn made writing the early compiler simpler.&lt;/p&gt;

</description>
      <category>c</category>
    </item>
    <item>
      <title>Using Arenas for Easier Object Management</title>
      <dc:creator>Paul J. Lucas</dc:creator>
      <pubDate>Thu, 19 Feb 2026 02:47:54 +0000</pubDate>
      <link>https://forem.com/pauljlucas/using-arenas-for-easier-object-management-145k</link>
      <guid>https://forem.com/pauljlucas/using-arenas-for-easier-object-management-145k</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;When I started updating &lt;a href="https://dev.to/pauljlucas/the-all-new-cdecl-30"&gt;Cdecl&lt;/a&gt;, one of the first things I did was, as I then described, to use an &lt;a href="https://en.wikipedia.org/wiki/Abstract_syntax_tree" rel="noopener noreferrer"&gt;abstract syntax tree&lt;/a&gt; (AST) data structure to represent a declaration.  During construction of the AST, placeholder nodes sometimes need to be created until the final type is able to be determined.  For example, consider the declaration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When parsing, upon encountering the first &lt;code&gt;[&lt;/code&gt;, we know it’s an &lt;em&gt;array 2 of “something” of&lt;/em&gt; &lt;code&gt;int&lt;/code&gt;, but we don’t yet know either what the “something” is or whether it will turn out to be nothing.  It’s not until the second &lt;code&gt;[&lt;/code&gt; that we know it’s an &lt;em&gt;array 2 of array 3 of&lt;/em&gt; &lt;code&gt;int&lt;/code&gt;.  (Had the &lt;code&gt;[3]&lt;/code&gt; not been there, then it would have been just &lt;em&gt;array 2 of&lt;/em&gt; &lt;code&gt;int&lt;/code&gt;.)  Once the final type is known, the placeholder node can be replaced by a new code of the correct type.&lt;/p&gt;

&lt;p&gt;You might think all that needs to be done is to call &lt;code&gt;free&lt;/code&gt; on the placeholder node.  You &lt;em&gt;could&lt;/em&gt;, but then you’d have to ensure that there are no other pointers pointing to it, not only in the tree, but in local variables in stack frames of functions on the call stack lest you get a &lt;a href="https://en.wikipedia.org/wiki/Dangling_pointer" rel="noopener noreferrer"&gt;dangling pointer&lt;/a&gt;.  Simpler, would be to use arenas.&lt;/p&gt;

&lt;h2&gt;
  
  
  Arenas
&lt;/h2&gt;

&lt;p&gt;An &lt;a href="https://en.wikipedia.org/wiki/Region-based_memory_management" rel="noopener noreferrer"&gt;arena&lt;/a&gt; typically is a large chunk of dynamically allocated memory from which actual objects of interest are subsequently allocated from using a custom allocator.  There are many ways to implement arenas depending upon the answers to the following questions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Will all objects that will be allocated from it be of the same type (or at least size) or different types (or sizes)?&lt;/li&gt;
&lt;li&gt;Will the maximum number of objects that will be allocated from it be known in advance?&lt;/li&gt;
&lt;li&gt;If not, will the arena need to grow as needed?&lt;/li&gt;
&lt;li&gt;Will individual objects need to be deallocated from the arena?&lt;/li&gt;
&lt;li&gt;Does it need to be thread-safe?  (If your particular program doesn’t use threads, then the answer is “no.”)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;About the only thing all arena implementations have in common is that freeing the arena frees all objects in it — which is one of the main reasons for using an arena in the first place.&lt;/p&gt;

&lt;h2&gt;
  
  
  An Arena for Cdecl
&lt;/h2&gt;

&lt;p&gt;In the case of Cdecl, the answers to the previous questions are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Yes.  (All objects are AST nodes.)&lt;/li&gt;
&lt;li&gt;No.  (Won’t know maximum number of objects.)&lt;/li&gt;
&lt;li&gt;Yes.  (Will need to grow as needed.)&lt;/li&gt;
&lt;li&gt;No.  (Individual objects will not need deallocation.)&lt;/li&gt;
&lt;li&gt;No.  (Does not need to be thread-safe.)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Given those answers, all that’s needed to implement an arena is a simple, singly linked list, something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;slist&lt;/span&gt; &lt;span class="n"&gt;slist&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;slist&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;slist&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;void&lt;/span&gt;  &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;slist_push_front&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;slist&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;phead&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;slist&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;malloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slist&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;phead&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;phead&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then to put all AST nodes into the arena:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;c_ast_t&lt;/span&gt; &lt;span class="nf"&gt;c_ast_new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="cm"&gt;/* params */&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;slist&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;parena&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;c_ast_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;ast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;malloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;ast&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
  &lt;span class="n"&gt;slist_push_front&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;parena&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ast&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;ast&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, to free all AST nodes in one go (which is the whole point):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;slist_cleanup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;slist&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;phead&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;free_fn&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;slist&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;phead&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;free_fn&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;free_fn&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;free&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;phead&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As arenas go, a linked list is pretty simple.  It might even be a stretch to call it an arena at all.  In the case of Cdecl, it’s good enough — don’t over-engineer things.&lt;/p&gt;

&lt;h2&gt;
  
  
  More Complex Arenas
&lt;/h2&gt;

&lt;p&gt;A slightly more complex arena would be one where you build on top of the linked list where each node would be a “block” of objects.  For example, the first block could hold a maximum of, say, 8 objects.  If you run out of space, you can allocate more blocks.  Each can either be the same size or double the previous size up to a maximum size:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;arena&lt;/span&gt; &lt;span class="n"&gt;arena&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;arena&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;slist&lt;/span&gt;  &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;blocks&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;size_t&lt;/span&gt;  &lt;span class="n"&gt;block_size_min&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;block_size_max&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;size_t&lt;/span&gt;  &lt;span class="n"&gt;obj_size&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;size_t&lt;/span&gt;  &lt;span class="n"&gt;avail&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;arena_init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;arena&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                 &lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;block_size_min&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                 &lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;block_size_max&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                 &lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;obj_size&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arena&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;block_size_min&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;block_size_min&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;block_size_max&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;block_size_max&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;obj_size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;obj_size&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A block will store both its size and the memory for objects:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;arena_block&lt;/span&gt; &lt;span class="n"&gt;arena_block&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;arena_block&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;alignas&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;max_align_t&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[];&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This uses a &lt;a href="https://dev.to/pauljlucas/obscure-c99-array-features-3270"&gt;flexible array member&lt;/a&gt; for the object storage. Given that, we can implement an allocator:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;arena_alloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;arena&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;avail&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;arena_block&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arena_node2block&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;blocks&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;block_size&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;block&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="n"&gt;block_size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;block_size_min&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;block_size_max&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="n"&gt;block_size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;
      &lt;span class="n"&gt;block_size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;block_size_max&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;block&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;malloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arena_block&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;block_size&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;obj_size&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;block&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;avail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;block_size&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;slist_push_front&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;blocks&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;blocks&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;avail&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;obj_size&lt;/span&gt; &lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If &lt;code&gt;a-&amp;gt;avail&lt;/code&gt; is 0, it means we have to allocate a new block:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ignoring &lt;code&gt;arena_node2block&lt;/code&gt; for now, if &lt;code&gt;block&lt;/code&gt; is &lt;code&gt;NULL&lt;/code&gt;, there are no blocks, so &lt;code&gt;block_size&lt;/code&gt; will be &lt;code&gt;block_size_min&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Otherwise, if doubling &lt;code&gt;block_size&lt;/code&gt; won’t exceed &lt;code&gt;block_size_max&lt;/code&gt;, then double it.&lt;/li&gt;
&lt;li&gt;Otherwise, use &lt;code&gt;block_size_max&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then allocate memory for a block &lt;em&gt;and&lt;/em&gt; &lt;code&gt;block_size&lt;/code&gt; objects and push &lt;code&gt;data&lt;/code&gt; — not &lt;code&gt;block&lt;/code&gt; — onto the head of the linked list of blocks.  This makes it simpler to calculate the address of any object in a block as it done on the &lt;code&gt;return&lt;/code&gt; line.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;size&lt;/code&gt; is “hidden” in memory &lt;em&gt;before&lt;/em&gt; the object pointed to by &lt;code&gt;slist&lt;/code&gt;’s &lt;code&gt;data&lt;/code&gt;.  This trick is commonly used in allocators.  Indeed, it’s typically how &lt;code&gt;malloc&lt;/code&gt; stores the size of a chunk of memory so that it later can be recovered.&lt;/p&gt;

&lt;p&gt;But, given an &lt;code&gt;slist*&lt;/code&gt; for a block, how can you get at the block’s size?  There is where &lt;code&gt;arena_node2block&lt;/code&gt; comes in:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kr"&gt;inline&lt;/span&gt; &lt;span class="n"&gt;arena_block&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;arena_node2block&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;slist&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arena_block&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;offsetof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arena_block&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The expression &lt;code&gt;node-&amp;gt;data&lt;/code&gt;, being an array, “decays” into a pointer to its first element.  In memory, &lt;code&gt;size&lt;/code&gt;, as mentioned, is stored &lt;em&gt;before&lt;/em&gt; that.  You might think that it’s just &lt;code&gt;sizeof(size_t)&lt;/code&gt; bytes before &lt;code&gt;node-&amp;gt;data&lt;/code&gt; — and it could be. The problem is that there &lt;em&gt;may&lt;/em&gt; be padding between &lt;code&gt;size&lt;/code&gt; and &lt;code&gt;data&lt;/code&gt;.  To subtract the correct number of bytes that includes the padding (if any), the standard macro &lt;a href="https://en.cppreference.com/w/c/types/offsetof.html" rel="noopener noreferrer"&gt;&lt;code&gt;offsetof&lt;/code&gt;&lt;/a&gt; can be used.  In this case, it will return the offset of &lt;code&gt;data&lt;/code&gt; into the structure that &lt;em&gt;includes&lt;/em&gt; the padding (if any).&lt;/p&gt;

&lt;p&gt;To implement a deallocator:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;arena_cleanup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;arena&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;free_fn&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;slist&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;blocks&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;next_node&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;next_node&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;arena_block&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;block&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arena_node2block&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;free_fn&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;avail&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;free_fn&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;obj_size&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;avail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;free&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;block&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;next_node&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;free&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;blocks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code is fairly straightforward.  The only thing to remember is that the first block may not be full and you should call &lt;code&gt;free_fn&lt;/code&gt; only only actual objects.  Hence, we start iterating at &lt;code&gt;a-&amp;gt;avail&lt;/code&gt; rather than &lt;code&gt;0&lt;/code&gt;; then set &lt;code&gt;a-&amp;gt;avail&lt;/code&gt; to &lt;code&gt;0&lt;/code&gt; for the remaining blocks that are all full.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Using arenas for object management can simplify memory management in the special case when an entire collection of objects are all deallocated together.  Arenas can also be more performant since it required fewer calls to &lt;code&gt;malloc&lt;/code&gt; and &lt;code&gt;free&lt;/code&gt;.  There are other possible arena implementations in addition to the two presented here.  Keep arenas in mind for your next project.&lt;/p&gt;

</description>
      <category>c</category>
    </item>
    <item>
      <title>Avoid the Temptation of Bit Fields</title>
      <dc:creator>Paul J. Lucas</dc:creator>
      <pubDate>Tue, 27 Jan 2026 01:43:54 +0000</pubDate>
      <link>https://forem.com/pauljlucas/avoid-the-temptation-of-bit-fields-1cnf</link>
      <guid>https://forem.com/pauljlucas/avoid-the-temptation-of-bit-fields-1cnf</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Every once in a while, I come across somebody else’s blog post who’s apparently recently discovered &lt;a href="https://en.cppreference.com/w/c/language/bit_field.html" rel="noopener noreferrer"&gt;bit fields&lt;/a&gt; and thinks they’re nifty in that they allow you to pack things like Boolean flags into single bits.  One example was along the lines of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="n"&gt;running&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="n"&gt;paused&lt;/span&gt;  &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;   &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is, instead of using three whole bytes (24 bits) to store three Boolean flags, you can use just &lt;em&gt;three bits&lt;/em&gt;!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In reality, the original 24 bits would have been padded up to 32 bits and 3 bits would be padded up to at least 8.  Hence, at best, you would have saved 24 bits.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;While that might seem like a pretty good memory savings, there’s no such thing as a free lunch.  With only a very small number of exceptions, such uses of bit fields are both misguided and actually inefficient.&lt;/p&gt;

&lt;h2&gt;
  
  
  Performance
&lt;/h2&gt;

&lt;p&gt;Since a byte is the smallest directly addressable unit on a computer, in order to access an individual bit or set of bits like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;set_running&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;running&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the compiler has to generate code equivalent to what you would have done yourself by hand to set just one bit manually.  For example, the &lt;a href="https://godbolt.org/z/3Pzc5d17E" rel="noopener noreferrer"&gt;armv8 generated code&lt;/a&gt; (annotated with C pseudocode) is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ldrb    w8, [x0]     ; char w8 = *x0;
orr     w8, w8, #0x1 ; w8 |= 1;
strb    w8, [x0]     ; *x0 = w8;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Read the existing value from memory (slow).&lt;/li&gt;
&lt;li&gt;Set the bit.&lt;/li&gt;
&lt;li&gt;Write the updated value to memory (slow).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For a normal &lt;code&gt;unsigned&lt;/code&gt;, you do only step 3.  Hence the generated code for either reading or writing bit fields is &lt;em&gt;always&lt;/em&gt; slower.&lt;/p&gt;

&lt;h2&gt;
  
  
  Other Caveats
&lt;/h2&gt;

&lt;p&gt;In addition to the performance penalty, the following things are either unspecified or implementation defined when it comes to bit fields:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Whether the order of the bits is left-to-right or right-to-left.  For the above example, the bits could be in the order &lt;code&gt;rpe&lt;/code&gt; where &lt;code&gt;r&lt;/code&gt; (for &lt;code&gt;running&lt;/code&gt;) is the most significant bit or &lt;code&gt;epr&lt;/code&gt; where &lt;code&gt;e&lt;/code&gt; (for &lt;code&gt;error&lt;/code&gt;) is.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;How the bytes containing the bit fields are aligned.  For the above example, they could be &lt;code&gt;rpeXXXXX&lt;/code&gt; or &lt;code&gt;XXXXXrpe&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Whether a multi-bit bit field can straddle a word boundary.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Whether a plain &lt;code&gt;int&lt;/code&gt; bit field is signed or unsigned.  Ordinarily, &lt;code&gt;int&lt;/code&gt; is always signed.  As the type of a bit field, &lt;code&gt;int&lt;/code&gt; becomes like &lt;code&gt;char&lt;/code&gt; in that it’s implementation defined whether it’s signed or unsigned.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Whether types other than &lt;code&gt;int&lt;/code&gt;, &lt;code&gt;signed int&lt;/code&gt;, &lt;code&gt;unsigned int&lt;/code&gt;, &lt;code&gt;_Bool&lt;/code&gt;, &lt;code&gt;_BitInt(N)&lt;/code&gt;, &lt;code&gt;unsigned _BitInt(N)&lt;/code&gt;, or &lt;code&gt;_Atomic&lt;/code&gt; variants can be used as bit fields.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hence, use of bit fields is extremely &lt;em&gt;not&lt;/em&gt; portable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Appropriate Uses
&lt;/h2&gt;

&lt;p&gt;Given that bit fields are slower and not portable, when is it a good idea to use bit fields?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;If you really, &lt;em&gt;really&lt;/em&gt; need the memory savings.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If you want code clarity and the performance is inconsequential.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If you need to deal with specific hardware that uses sub-byte fields and you have detailed knowledge of your compiler.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For saving memory, if you have other, non-bit field members in a structure, you can also often save memory by sorting members descending by size to minimize padding.&lt;/p&gt;

&lt;p&gt;For code clarity, admittedly code like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;is simpler and thus clearer than something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;ERROR_BIT&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So if you really want to trade performance for simplicity and clarity, fine, but fully understand the implications of your choice.&lt;/p&gt;

&lt;p&gt;If you need to deal with specific hardware, you can use bit fields to map structures directly to the hardware, but you &lt;em&gt;must&lt;/em&gt; ensure that the code your compiler generates is actually what you think it is — that is you have to know the details of what your particular implementation defines for its implementation defined behavior.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Unless you have a specific reason to use bit fields, don’t.  Especially don’t just because you think they’re either efficient or nifty.&lt;/p&gt;

</description>
      <category>c</category>
    </item>
    <item>
      <title>C23 Miscellany</title>
      <dc:creator>Paul J. Lucas</dc:creator>
      <pubDate>Sat, 03 Jan 2026 16:00:03 +0000</pubDate>
      <link>https://forem.com/pauljlucas/c23-miscellany-flc</link>
      <guid>https://forem.com/pauljlucas/c23-miscellany-flc</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;I’ve written a few articles on individual new features in &lt;a href="https://en.wikipedia.org/wiki/C23_(C_standard_revision)" rel="noopener noreferrer"&gt;C23&lt;/a&gt; covering &lt;a href="https://dev.to/pauljlucas/attributes-in-c23-and-c-5eg"&gt;attributes&lt;/a&gt;, &lt;a href="https://dev.to/pauljlucas/auto-in-c23-ij9"&gt;&lt;code&gt;auto&lt;/code&gt;&lt;/a&gt;, &lt;a href="https://dev.to/pauljlucas/bool-in-c23-4kj4"&gt;&lt;code&gt;bool&lt;/code&gt;&lt;/a&gt;, storage classes for &lt;a href="https://dev.to/pauljlucas/compound-literals-in-c-5d5"&gt;compound literals&lt;/a&gt;, &lt;a href="https://dev.to/pauljlucas/c-const-conundrum-j2l"&gt;&lt;code&gt;constexpr&lt;/code&gt;&lt;/a&gt;, explicit underlying types for &lt;a href="https://dev.to/pauljlucas/enumerations-in-c-ae7"&gt;enumerations&lt;/a&gt;, &lt;a href="https://dev.to/pauljlucas/nullptr-in-c23-1cn6"&gt;&lt;code&gt;nullptr&lt;/code&gt;&lt;/a&gt;, and &lt;a href="https://dev.to/pauljlucas/typeof-in-c23-55p2"&gt;&lt;code&gt;typeof&lt;/code&gt;&lt;/a&gt;.  There are a few miscellaneous new features that aren’t substantial enough to warrant individual articles for each, so this article will cover them together.&lt;/p&gt;

&lt;h2&gt;
  
  
  Aggregate Initialization
&lt;/h2&gt;

&lt;p&gt;You can now initialize aggregates (arrays, structures, and &lt;a href="https://dev.to/pauljlucas/unions-in-c-1ojj"&gt;unions&lt;/a&gt;) with an empty &lt;code&gt;= { }&lt;/code&gt;. Previously at a minimum, you had to include a &lt;code&gt;0&lt;/code&gt; between the &lt;code&gt;{&lt;/code&gt; and &lt;code&gt;}&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Binary Literals
&lt;/h2&gt;

&lt;p&gt;C has had decimal, octal, and hexadecimal integer literals since its creation.  C23 adopted binary literals from &lt;a href="https://en.wikipedia.org/wiki/C%2B%2B14#Binary_literals" rel="noopener noreferrer"&gt;C++14&lt;/a&gt; using either the same &lt;code&gt;0b&lt;/code&gt; or &lt;code&gt;0B&lt;/code&gt; prefix:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="n"&gt;b101010&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// 42 decimal&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;_BitInt&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;A new &lt;em&gt;bit-precise integer&lt;/em&gt; type has been added, &lt;code&gt;_BitInt(&lt;/code&gt;&lt;em&gt;n&lt;/em&gt;&lt;code&gt;)&lt;/code&gt; where &lt;em&gt;n&lt;/em&gt; is a positive constant integer expression.  It may be either signed (the default) or &lt;code&gt;unsigned&lt;/code&gt;.  (If signed, &lt;em&gt;n&lt;/em&gt; includes the sign bit.)  Some examples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="n"&gt;_BitInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;24&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="n"&gt;rgb24&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// 24-bit RGB color&lt;/span&gt;
&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="n"&gt;_BitInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;256&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;sha256&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// SHA-256&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The maximum value for &lt;em&gt;n&lt;/em&gt; is implementation defined, but is at least as many as for &lt;code&gt;unsigned long long&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Decimal Floating-Point Types
&lt;/h2&gt;

&lt;p&gt;Although they’ve existed as extensions for a while, the new decimal floating-point types of &lt;code&gt;_Decimal32&lt;/code&gt;, &lt;code&gt;_Decimal64&lt;/code&gt;, and &lt;code&gt;_Decimal128&lt;/code&gt; are now officially supported.&lt;/p&gt;

&lt;p&gt;Decimal-floating types are better for calculations involving money (dollars, euros, pounds, etc.) because they’re not subject to the same rounding errors that the standard-floating types are. The standard-floating types are still better for general floating-point calculations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Declarations After Labels
&lt;/h2&gt;

&lt;p&gt;You can now (finally) put declarations immediately after either &lt;code&gt;goto&lt;/code&gt; or &lt;code&gt;case&lt;/code&gt; labels:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="nl"&gt;error:&lt;/span&gt;             &lt;span class="c1"&gt;// C &amp;lt; C23: error; C23: OK&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Previously, you had to use something like the &lt;code&gt;:;&lt;/code&gt; trick (an empty statement after &lt;code&gt;:&lt;/code&gt;) to be legal.  The C Committee should have made this legal when they allowed intermingled declarations and code in &lt;a href="https://en.wikipedia.org/wiki/C99" rel="noopener noreferrer"&gt;C99&lt;/a&gt;, but better late than never.&lt;/p&gt;

&lt;h2&gt;
  
  
  Digit Separators
&lt;/h2&gt;

&lt;p&gt;C23 also adopted the &lt;code&gt;'&lt;/code&gt; character as a digits separator from C++14 as a readability aid:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="n"&gt;b0010&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="mi"&gt;1010&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;299&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="mi"&gt;792&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="mi"&gt;458&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The overall value is not affected.  You’re free to group the digits however you like.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you’re wondering why &lt;code&gt;,&lt;/code&gt; (comma) wasn’t used, it’s because &lt;code&gt;,&lt;/code&gt; is used to separate function arguments and is also the &lt;em&gt;comma operator&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  K&amp;amp;R-Style Functions
&lt;/h2&gt;

&lt;p&gt;Even though function prototypes were adopted from C++ way back in &lt;a href="https://en.wikipedia.org/wiki/ANSI_C" rel="noopener noreferrer"&gt;C89&lt;/a&gt; (the first ANSI C), C has &lt;em&gt;still&lt;/em&gt; supported “K&amp;amp;R style” function declarations and definitions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;strncpy&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// C &amp;lt; C23: unspecified arguments&lt;/span&gt;

&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;strncpy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;dst&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;dst&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, C23 has dropped support for such declarations and definitions.&lt;/p&gt;

&lt;h2&gt;
  
  
  New Keyword Spellings
&lt;/h2&gt;

&lt;p&gt;In addition to &lt;code&gt;bool&lt;/code&gt; replacing &lt;code&gt;_Bool&lt;/code&gt;, &lt;code&gt;alignas&lt;/code&gt; replaces &lt;code&gt;_Alignas&lt;/code&gt;, &lt;code&gt;alignof&lt;/code&gt; replaces &lt;code&gt;_Alignof&lt;/code&gt;, &lt;code&gt;static_assert&lt;/code&gt; replaces &lt;code&gt;_Static_assert&lt;/code&gt;, and &lt;code&gt;thread_local&lt;/code&gt; replaces &lt;code&gt;_Thread_local&lt;/code&gt;.  (The old spellings are still supported, but deprecated.)&lt;/p&gt;

&lt;h2&gt;
  
  
  New Preprocessor Directives
&lt;/h2&gt;

&lt;p&gt;The preprocessor now includes &lt;code&gt;#elifdef&lt;/code&gt;, &lt;code&gt;#elifndef&lt;/code&gt;, &lt;a href="https://cppreference.com/w/c/preprocessor/embed.html" rel="noopener noreferrer"&gt;&lt;code&gt;#embed&lt;/code&gt;&lt;/a&gt;, and &lt;code&gt;#warning&lt;/code&gt; directives.&lt;/p&gt;

&lt;h2&gt;
  
  
  Unnamed, Unused Parameters
&lt;/h2&gt;

&lt;p&gt;Unlike C++, function definitions in C have historically required unused parameters to still be named.  To eliminate an “unused” warning, you typically cast it to &lt;code&gt;void&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="nf"&gt;cdecl_rl_completion&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// means: unused&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In C23, you can simply omit the name just like you always could in function declarations.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;__VA_OPT__&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Though it’s been supported as an extension by &lt;code&gt;gcc&lt;/code&gt; and &lt;code&gt;clang&lt;/code&gt; for a while, &lt;a href="https://en.cppreference.com/w/c/preprocessor/replace" rel="noopener noreferrer"&gt;&lt;code&gt;__VA_OPT__&lt;/code&gt;&lt;/a&gt; is finally part of the C standard.&lt;/p&gt;

&lt;h2&gt;
  
  
  Variadic Functions
&lt;/h2&gt;

&lt;p&gt;As I mentioned in a previous article, &lt;a href="https://dev.to/pauljlucas/variadic-functions-in-c-53ml"&gt;variadic functions&lt;/a&gt; no longer insist on at least one required parameter; that is you can do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;      &lt;span class="c1"&gt;// C23: no required parameter&lt;/span&gt;
  &lt;span class="kt"&gt;va_list&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;va_start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// C23: no second argument&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This ability was adopted from C++.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Of all the changes to C23, &lt;code&gt;auto&lt;/code&gt; will probably be the most used; but the other changes, while not revolutionary, are nice (and sometimes long overdue).&lt;/p&gt;

</description>
      <category>c</category>
    </item>
    <item>
      <title>bool in C23</title>
      <dc:creator>Paul J. Lucas</dc:creator>
      <pubDate>Fri, 19 Dec 2025 02:33:14 +0000</pubDate>
      <link>https://forem.com/pauljlucas/bool-in-c23-4kj4</link>
      <guid>https://forem.com/pauljlucas/bool-in-c23-4kj4</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Originally, C had no boolean type. Instead, &lt;code&gt;int&lt;/code&gt; was invariably used such that &lt;code&gt;0&lt;/code&gt; was treated as false and any non-zero value was treated as true.  While this certainly worked, using &lt;code&gt;int&lt;/code&gt; as a boolean has problems.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problems with &lt;code&gt;int&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;One problem with using &lt;code&gt;int&lt;/code&gt; as a boolean is clarity. Consider the function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;strbuf_reserve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;strbut_f&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;sbuf&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;res_len&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What does that function return?  A simple boolean value? How many bytes were actually reserved? An error code? You have to read the documentation (if any) or the source code (if not — assuming you even have it) to know what the &lt;code&gt;int&lt;/code&gt; means.  If it returned a boolean instead, it would very likely indicate the success or failure.&lt;/p&gt;

&lt;p&gt;Consequently, many programmers, especially those who learned other languages first that had a boolean type (such as ALGOL, Fortran, or Pascal) often defined their own boolean “type” in C, something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#define bool   int  &lt;/span&gt;&lt;span class="cm"&gt;/* or: typedef int bool; */&lt;/span&gt;&lt;span class="cp"&gt;
#define false  0
#define true   1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or perhaps all-caps versions.  However, that can cause its own problem.  If you use a few C libraries from different authors in your program, each might define its own boolean type slightly differently that can lead to incompatibilities, warnings, or errors (depending on how each is defined).&lt;/p&gt;

&lt;p&gt;Another problem with &lt;code&gt;int&lt;/code&gt; as a boolean is that in structures, it takes up at least four times as much space as needed (assuming 32-bit integers).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Although a true boolean value needs only a single bit, it practically must be &lt;code&gt;sizeof(char)&lt;/code&gt; (which is always 1 byte).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;_Bool&lt;/code&gt; in C99
&lt;/h2&gt;

&lt;p&gt;One of the jobs of a language standard committee is to standardize common industry practice to eliminate variances and incompatibilities.  Eventually, the C Committee added a boolean type to C — sort of.&lt;/p&gt;

&lt;p&gt;In &lt;a href="https://en.wikipedia.org/wiki/C99" rel="noopener noreferrer"&gt;C99&lt;/a&gt;, the C Committee finally (after 27 years) added a boolean type to C: &lt;code&gt;_Bool&lt;/code&gt;.  Why was it spelled like that?  Why not simply &lt;code&gt;bool&lt;/code&gt;?  As I wrote in my book &lt;a href="https://dev.to/pauljlucas/why-learn-c-book-released-41c1"&gt;Why Learn C&lt;/a&gt;, §2.2, p. 29:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;To evolve C, the C committee occasionally adds new keywords. The problem is that every new keyword has the potential to break existing programs because some may already use the same identifier. To help minimize this possibility, the committee decided that new keywords would generally start with an underscore followed by a capital letter (e.g., &lt;code&gt;_Bool&lt;/code&gt;, §C.6), at least for a transition period to allow people time to update their programs. Such keywords may look odd, but it’s better than breaking programs.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;However, the Committee did &lt;em&gt;not&lt;/em&gt; add &lt;code&gt;_False&lt;/code&gt; and &lt;code&gt;_True&lt;/code&gt; keywords, so you &lt;em&gt;still&lt;/em&gt; had to use &lt;code&gt;0&lt;/code&gt; and non-zero.  But they also added &lt;code&gt;stdbool.h&lt;/code&gt; that did:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#define bool  _Bool
#define false 0
#define true  1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;so you could, at your option, &lt;code&gt;#include&lt;/code&gt; that header and use the conventional names.  This half measure worked well enough and, more importantly, didn’t break existing programs.&lt;/p&gt;

&lt;p&gt;But when &lt;a href="https://en.wikipedia.org/wiki/C11_(C_standard_revision)" rel="noopener noreferrer"&gt;C11&lt;/a&gt; with &lt;a href="https://dev.to/pauljlucas/generic-in-c-i48"&gt;&lt;code&gt;_Generic&lt;/code&gt;&lt;/a&gt; came out, &lt;code&gt;_Bool&lt;/code&gt; didn’t play nice with it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdbool.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;f_bool&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;f_int&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cp"&gt;#define F(X)      \
  _Generic( (X),  \
    bool: f_bool, \
    int : f_int   \
  )( (X) )
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;F&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;           &lt;span class="c1"&gt;// calls f_bool&lt;/span&gt;
  &lt;span class="n"&gt;F&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;       &lt;span class="c1"&gt;// calls f_int&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The call of &lt;code&gt;F&lt;/code&gt; passing &lt;code&gt;false&lt;/code&gt; calls &lt;code&gt;f_int&lt;/code&gt; because &lt;code&gt;false&lt;/code&gt; is only a macro for &lt;code&gt;0&lt;/code&gt; — which is an &lt;code&gt;int&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Another difference between &lt;code&gt;int&lt;/code&gt; and &lt;code&gt;_Bool&lt;/code&gt; is what happens when certain values are cast:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt;   &lt;span class="n"&gt;i1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;   &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;       &lt;span class="c1"&gt;// = 0 (truncation)&lt;/span&gt;
&lt;span class="kt"&gt;_Bool&lt;/span&gt; &lt;span class="n"&gt;b1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;_Bool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;       &lt;span class="c1"&gt;// = 1 (non-zero -&amp;gt; true)&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt;   &lt;span class="n"&gt;i2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;   &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;LONG_MAX&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// implementation defined&lt;/span&gt;
&lt;span class="kt"&gt;_Bool&lt;/span&gt; &lt;span class="n"&gt;b2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;_Bool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;LONG_MAX&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// = 1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unlike &lt;code&gt;int&lt;/code&gt;, the semantics of &lt;code&gt;_Bool&lt;/code&gt; are more intuitive and always well defined, specifically, &lt;em&gt;any&lt;/em&gt; non-zero value always gets implicitly converted to &lt;code&gt;1&lt;/code&gt; (true).&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;bool&lt;/code&gt; in C23
&lt;/h2&gt;

&lt;p&gt;In &lt;a href="https://en.wikipedia.org/wiki/C23_(C_standard_revision)" rel="noopener noreferrer"&gt;C23&lt;/a&gt;, the C Committee (after 51 years) added a complete boolean type to C: &lt;code&gt;bool&lt;/code&gt; (deprecating &lt;code&gt;_Bool&lt;/code&gt;).  Not only that, but &lt;code&gt;false&lt;/code&gt; and &lt;code&gt;true&lt;/code&gt; are finally keywords which means the problem with &lt;code&gt;_Generic&lt;/code&gt; goes away.&lt;/p&gt;

&lt;p&gt;Ideally, the C Committee should have added &lt;code&gt;bool&lt;/code&gt; to C11 alongside &lt;code&gt;_Generic&lt;/code&gt; — but better late than never.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Despite taking over a quarter of a century for &lt;code&gt;_Bool&lt;/code&gt; and over half a century for &lt;code&gt;bool&lt;/code&gt;, C finally has a proper boolean type that is clearer and smaller than &lt;code&gt;int&lt;/code&gt; and is always well defined.&lt;/p&gt;

</description>
      <category>c</category>
    </item>
    <item>
      <title>Advanced C Preprocessor Macros for a Type-Safe Varargs Substitute</title>
      <dc:creator>Paul J. Lucas</dc:creator>
      <pubDate>Tue, 16 Dec 2025 03:16:30 +0000</pubDate>
      <link>https://forem.com/pauljlucas/advanced-c-preprocessor-macros-for-a-type-safe-varargs-substitute-2aog</link>
      <guid>https://forem.com/pauljlucas/advanced-c-preprocessor-macros-for-a-type-safe-varargs-substitute-2aog</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;As you may know, &lt;a href="https://en.cppreference.com/w/c/variadic.html" rel="noopener noreferrer"&gt;variadic functions&lt;/a&gt; in C are those that can take a varying number of arguments, the most well-known of which is &lt;a href="https://en.cppreference.com/w/c/io/fprintf" rel="noopener noreferrer"&gt;&lt;code&gt;printf&lt;/code&gt;&lt;/a&gt; and related functions.  As you may also know, variadic functions have a number of caveats as I &lt;a href="https://dev.to/pauljlucas/variadic-functions-in-c-53ml"&gt;previously explained&lt;/a&gt;.  Can type-safe variadic arguments be implemented in standard C?  Yes!&lt;/p&gt;

&lt;h2&gt;
  
  
  A Type-Safe Variadic Argument
&lt;/h2&gt;

&lt;p&gt;First, we need a type that can store a value for any one of C’s built-in types — a type-safe variadic argument (TSVA):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;tsva_type&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;TSVA_BOOL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;TSVA_CHAR&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;TSVA_SCHAR&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;TSVA_SHORT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;TSVA_INT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;TSVA_LONG&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;TSVA_LONG_LONG&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;TSVA_UCHAR&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;TSVA_USHORT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;TSVA_UINT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;TSVA_ULONG&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;TSVA_ULONG_LONG&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;TSVA_FLOAT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;TSVA_DOUBLE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;TSVA_PTR_CHAR&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;TSVA_PTR_CONST_CHAR&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;TSVA_PTR_VOID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;TSVA_PTR_CONST_VOID&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;tsva_type&lt;/span&gt; &lt;span class="n"&gt;tsva_type_t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;tsva_value&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;union&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;bool&lt;/span&gt;                &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kt"&gt;char&lt;/span&gt;                &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;signed&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;         &lt;span class="n"&gt;sc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;short&lt;/span&gt;               &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt;                 &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;long&lt;/span&gt;                &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt;           &lt;span class="n"&gt;ll&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;       &lt;span class="n"&gt;uc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;short&lt;/span&gt;      &lt;span class="n"&gt;us&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;        &lt;span class="n"&gt;ui&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt;       &lt;span class="n"&gt;ul&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt;  &lt;span class="n"&gt;ull&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kt"&gt;float&lt;/span&gt;               &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;double&lt;/span&gt;              &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kt"&gt;char&lt;/span&gt;               &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;pc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt;         &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;pcc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kt"&gt;void&lt;/span&gt;               &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;pv&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt;         &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;pcv&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="n"&gt;tsva_type_t&lt;/span&gt;           &lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;tsva_value&lt;/span&gt; &lt;span class="n"&gt;tsva_value_t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is &lt;code&gt;tsva_value&lt;/code&gt; uses an anonymous &lt;a href="https://dev.to/pauljlucas/unions-in-c-1ojj"&gt;&lt;code&gt;union&lt;/code&gt;&lt;/a&gt; to store an argument’s value and &lt;code&gt;type&lt;/code&gt; to store its type.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;For &lt;code&gt;char&lt;/code&gt; and &lt;code&gt;void&lt;/code&gt;, the union has pointers to those types.  Ideally, the union should have &lt;code&gt;T*&lt;/code&gt; and &lt;code&gt;T const*&lt;/code&gt; for &lt;em&gt;all&lt;/em&gt; the built-in types.  They were elided here for for brevity.&lt;/p&gt;

&lt;p&gt;The union omits the &lt;code&gt;long double&lt;/code&gt; type since it might be double the size of &lt;code&gt;double&lt;/code&gt; that would double &lt;code&gt;sizeof(tsva_value_t)&lt;/code&gt; for a type that’s rarely used.&lt;/p&gt;

&lt;p&gt;The union also omits the &lt;a href="https://cppreference.com/w/c/language/arithmetic_types.html#Complex_floating_types" rel="noopener noreferrer"&gt;&lt;code&gt;_Complex&lt;/code&gt;&lt;/a&gt; floating-point types as well as the new &lt;a href="https://en.wikipedia.org/wiki/C23_(C_standard_revision)" rel="noopener noreferrer"&gt;C23&lt;/a&gt; &lt;code&gt;_Decimal32&lt;/code&gt;, &lt;code&gt;_Decimal64&lt;/code&gt;, and &lt;code&gt;_Decimal128&lt;/code&gt; types since those would also increase &lt;code&gt;sizeof(tsva_value_t)&lt;/code&gt; for types that are rarely used.&lt;/p&gt;

&lt;p&gt;That said, &lt;code&gt;sizeof(tsva_value_t)&lt;/code&gt; doesn’t really matter (as we’ll see).  If you need any of the omitted types, feel free to add them.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Using Type-Safe Variadic Arguments
&lt;/h2&gt;

&lt;p&gt;An example of using type-safe variadic arguments might be a function that can print its arguments like &lt;code&gt;printf&lt;/code&gt;, but in a type-safe way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;tsva_print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tsva_value_t&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;TSVA_CHAR&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s"&gt;"%c"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;TSVA_INT&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s"&gt;"%d"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="c1"&gt;// Other types ...&lt;/span&gt;

      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;TSVA_PTR_CHAR&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;TSVA_PTR_CONST_CHAR&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;fputs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;pc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stdout&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// switch&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is, type-safe variadic arguments will be passed as an “array” (really, a pointer).  Just as with standard variadic arguments, type-safe variadic arguments leave it to the user to determine how to know how many arguments there are.  In this example, the number of elements, &lt;code&gt;n&lt;/code&gt;, is passed as an argument also.&lt;/p&gt;

&lt;p&gt;But how is such a function called?  Specifically, how are the &lt;code&gt;tsva_value_t&lt;/code&gt; elements created?  We can use some macros:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#define TSVA_INIT(TYPE, FIELD, VALUE) \
  (tsva_value_t){ .type = TSVA_##TYPE, .FIELD = (VALUE) }
&lt;/span&gt;
&lt;span class="cp"&gt;#define TSVA_BOOL(VALUE)            TSVA_INIT( BOOL, b, (VALUE) )
#define TSVA_CHAR(VALUE)            TSVA_INIT( CHAR, c, (VALUE) )
#define TSVA_SCHAR(VALUE)           TSVA_INIT( SCHAR, sc, (VALUE) )
#define TSVA_SHORT(VALUE)           TSVA_INIT( SHORT, s, (VALUE) )
#define TSVA_INT(VALUE)             TSVA_INIT( INT, i, (VALUE) )
#define TSVA_LONG(VALUE)            TSVA_INIT( LONG, l, (VALUE) )
#define TSVA_LONG_LONG(VALUE)       TSVA_INIT( LONG_LONG, ll, (VALUE) )
#define TSVA_UCHAR(VALUE)           TSVA_INIT( UCHAR, uc, (VALUE) )
#define TSVA_USHORT(VALUE)          TSVA_INIT( USHORT, us, (VALUE) )
#define TSVA_UINT(VALUE)            TSVA_INIT( UINT, ui, (VALUE) )
#define TSVA_ULONG(VALUE)           TSVA_INIT( ULONG, ul, (VALUE) )
#define TSVA_ULONG_LONG(VALUE)      TSVA_INIT( ULONG_LONG, ull, (VALUE) )
#define TSVA_FLOAT(VALUE)           TSVA_INIT( float, f, (VALUE) )
#define TSVA_DOUBLE(VALUE)          TSVA_INIT( double, d, (VALUE) )
#define TSVA_PTR_CHAR(VALUE)        TSVA_INIT( PTR_CHAR, pc, (VALUE) )
#define TSVA_PTR_CONST_CHAR(VALUE)  TSVA_INIT( PTR_CONST_CHAR, pcc, (VALUE) )
#define TSVA_PTR_VOID(VALUE)        TSVA_INIT( PTR_VOID, pv, (VALUE) )
#define TSVA_PTR_CONST_VOID(VALUE)  TSVA_INIT( PTR_CONST_VOID, pcv, (VALUE) )
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Given those, we can call &lt;code&gt;tsva_print&lt;/code&gt; like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;tsva_print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tsva_value_t&lt;/span&gt;&lt;span class="p"&gt;[]){&lt;/span&gt;
  &lt;span class="n"&gt;TSVA_PTR_CONST_CHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Answer is: "&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;TSVA_INT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While that works, it’s a bit ugly and verbose.  Let’s see if we can clean that up.&lt;/p&gt;

&lt;h2&gt;
  
  
  Counting Variadic Arguments
&lt;/h2&gt;

&lt;p&gt;One of the things that makes the current code ugly, not to mention error-prone, is having to count (correctly!) the number of arguments and pass that.  Can the number of variadic arguments be counted?  Yes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#define VA_ARGS_COUNT(...) \
  ARG_11(__VA_ARGS__ __VA_OPT__(,) 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
&lt;/span&gt;
&lt;span class="cp"&gt;#define ARG_11(_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,...) _11
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As first shown in an &lt;a href="https://dev.to/pauljlucas/using-advanced-c-preprocessor-macros-for-a-pre-c23c20-vaopt-substitute-43c1"&gt;earlier article&lt;/a&gt;, the &lt;code&gt;ARG_11&lt;/code&gt; macro always returns its 11th argument.  This time, however, it’s being used to implement &lt;code&gt;VA_ARGS_COUNT&lt;/code&gt;.  Just as before, &lt;code&gt;__VA_ARGS__&lt;/code&gt; will “slide” the correct answer (in this case, the number of arguments) into the 11th argument position.  Given that, we can now do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#define tsva_print(...)                     \
  tsva_print( VA_ARGS_COUNT( __VA_ARGS__ ), \
              (tsva_value_t[]){ __VA_ARGS__ } )
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since we’re defining a macro anyway, might as well have it insert the &lt;code&gt;(tsva_value_t[])&lt;/code&gt; compound literal boilerplate code. Now explicitly specifying the count and the boilerplate can be eliminated:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;tsva_print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;TSVA_PTR_CONST_CHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Answer is: "&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;TSVA_INT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That’s better, but the required use of the &lt;code&gt;TSVA_&lt;/code&gt; macros is still quite verbose.  Can those be eliminated?  As it happens, yes (with a caveat).&lt;/p&gt;

&lt;h2&gt;
  
  
  Deducing the Type of Variadic Arguments
&lt;/h2&gt;

&lt;p&gt;What we need is a way to deduce the type of each argument automatically.  As I described in a &lt;a href="https://dev.to/pauljlucas/generic-in-c-i48"&gt;previous article&lt;/a&gt;, &lt;code&gt;_Generic&lt;/code&gt; can be used.&lt;/p&gt;

&lt;p&gt;However, as noted in that article, each type case for &lt;code&gt;_Generic&lt;/code&gt; &lt;em&gt;must&lt;/em&gt; be valid C.  Here, that means we can’t use the &lt;code&gt;TSVA_&lt;/code&gt; macros because it would attempt to assign a given &lt;code&gt;VALUE&lt;/code&gt; to &lt;em&gt;every&lt;/em&gt; union field that would either cause warnings (e.g., assigning a floating-point value to &lt;code&gt;.i&lt;/code&gt;) or errors (e.g., assuming a pointer value to &lt;code&gt;.f&lt;/code&gt;). Instead, we can use helper functions instead of macros:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kr"&gt;inline&lt;/span&gt; &lt;span class="n"&gt;tsva_value_t&lt;/span&gt; &lt;span class="nf"&gt;tsva__Bool&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="kt"&gt;_Bool&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tsva_value_t&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;TSVA_BOOL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kr"&gt;inline&lt;/span&gt; &lt;span class="n"&gt;tsva_value_t&lt;/span&gt; &lt;span class="nf"&gt;tsva_char&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tsva_value_t&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;TSVA_CHAR&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kr"&gt;inline&lt;/span&gt; &lt;span class="n"&gt;tsva_value_t&lt;/span&gt; &lt;span class="nf"&gt;tsva_signed_char&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="kt"&gt;signed&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tsva_value_t&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;TSVA_SCHAR&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kr"&gt;inline&lt;/span&gt; &lt;span class="n"&gt;tsva_value_t&lt;/span&gt; &lt;span class="nf"&gt;tsva_short&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="kt"&gt;short&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tsva_value_t&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;TSVA_SHORT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kr"&gt;inline&lt;/span&gt; &lt;span class="n"&gt;tsva_value_t&lt;/span&gt; &lt;span class="nf"&gt;tsva_int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tsva_value_t&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;TSVA_INT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kr"&gt;inline&lt;/span&gt; &lt;span class="n"&gt;tsva_value_t&lt;/span&gt; &lt;span class="nf"&gt;tsva_long&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tsva_value_t&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;TSVA_LONG&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kr"&gt;inline&lt;/span&gt; &lt;span class="n"&gt;tsva_value_t&lt;/span&gt; &lt;span class="nf"&gt;tsva_long_long&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tsva_value_t&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;TSVA_LONG_LONG&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ll&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kr"&gt;inline&lt;/span&gt; &lt;span class="n"&gt;tsva_value_t&lt;/span&gt; &lt;span class="nf"&gt;tsva_unsigned_char&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tsva_value_t&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;TSVA_UCHAR&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;uc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kr"&gt;inline&lt;/span&gt; &lt;span class="n"&gt;tsva_value_t&lt;/span&gt; &lt;span class="nf"&gt;tsva_unsigned_short&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;short&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tsva_value_t&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;TSVA_USHORT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;us&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kr"&gt;inline&lt;/span&gt; &lt;span class="n"&gt;tsva_value_t&lt;/span&gt; &lt;span class="nf"&gt;tsva_unsigned_int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tsva_value_t&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;TSVA_UINT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ui&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kr"&gt;inline&lt;/span&gt; &lt;span class="n"&gt;tsva_value_t&lt;/span&gt; &lt;span class="nf"&gt;tsva_unsigned_long&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tsva_value_t&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;TSVA_ULONG&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ul&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kr"&gt;inline&lt;/span&gt; &lt;span class="n"&gt;tsva_value_t&lt;/span&gt; &lt;span class="nf"&gt;tsva_unsigned_long_long&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tsva_value_t&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;TSVA_ULONG_LONG&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ull&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kr"&gt;inline&lt;/span&gt; &lt;span class="n"&gt;tsva_value_t&lt;/span&gt; &lt;span class="nf"&gt;tsva_float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tsva_value_t&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;TSVA_FLOAT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kr"&gt;inline&lt;/span&gt; &lt;span class="n"&gt;tsva_value_t&lt;/span&gt; &lt;span class="nf"&gt;tsva_double&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tsva_value_t&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;TSVA_DOUBLE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kr"&gt;inline&lt;/span&gt; &lt;span class="n"&gt;tsva_value_t&lt;/span&gt; &lt;span class="nf"&gt;tsva_ptr_char&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tsva_value_t&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;TSVA_PTR_CHAR&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kr"&gt;inline&lt;/span&gt; &lt;span class="n"&gt;tsva_value_t&lt;/span&gt; &lt;span class="nf"&gt;tsva_ptr_const_char&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tsva_value_t&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;TSVA_PTR_CONST_CHAR&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pcc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kr"&gt;inline&lt;/span&gt; &lt;span class="n"&gt;tsva_value_t&lt;/span&gt; &lt;span class="nf"&gt;tsva_ptr_void&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tsva_value_t&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;TSVA_PTR_VOID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kr"&gt;inline&lt;/span&gt; &lt;span class="n"&gt;tsva_value_t&lt;/span&gt; &lt;span class="nf"&gt;tsva_ptr_const_void&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tsva_value_t&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;TSVA_PTR_CONST_VOID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pcv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Given that, we can implement &lt;code&gt;TSVA_VALUE&lt;/code&gt; using &lt;code&gt;_Generic&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#define TSVA_VALUE(VALUE)                        \
  _Generic( (VALUE),                             \
    _Bool             : tsva__Bool,              \
    char              : tsva_char,               \
    signed char       : tsva_signed_char,        \
    short             : tsva_short,              \
    int               : tsva_int,                \
    long              : tsva_long,               \
    long long         : tsva_long_long,          \
    unsigned char     : tsva_unsigned_char,      \
    unsigned short    : tsva_unsigned_short,     \
    unsigned int      : tsva_unsigned_int,       \
    unsigned long     : tsva_unsigned_long,      \
    unsigned long long: tsva_unsigned_long_long, \
    float             : tsva_float,              \
    double            : tsva_double,             \
    char*             : tsva_ptr_char,           \
    char const*       : tsva_ptr_const_char,     \
    void*             : tsva_ptr_void,           \
    void const*       : tsva_ptr_const_void      \
  )( (VALUE) )
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, each type case is the same by returning a pointer to a function. Hence, now you could instead do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;tsva_print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;TSVA_VALUE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Answer is: "&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;TSVA_VALUE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and the type of each argument is automatically deduced. That’s better in that only a single macro is used, but it’s still verbose.  Can those macros be eliminated completely?&lt;/p&gt;

&lt;h2&gt;
  
  
  Transforming Variadic Arguments
&lt;/h2&gt;

&lt;p&gt;What’s needed is a way for each argument &lt;em&gt;x&lt;/em&gt;, to wrap it as &lt;code&gt;TSVA_VALUE(&lt;/code&gt;&lt;em&gt;x&lt;/em&gt;&lt;code&gt;)&lt;/code&gt;.  Is there a way to have the preprocessor iterate over variadic arguments?  Not directly, but you can via more macro voodoo:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#define APPLY_1(MACRO, ARG)        MACRO(ARG)
#define APPLY_2(MACRO, ARG, ...)   MACRO(ARG), APPLY_1(MACRO, __VA_ARGS__)
#define APPLY_3(MACRO, ARG, ...)   MACRO(ARG), APPLY_2(MACRO, __VA_ARGS__)
#define APPLY_4(MACRO, ARG, ...)   MACRO(ARG), APPLY_3(MACRO, __VA_ARGS__)
#define APPLY_5(MACRO, ARG, ...)   MACRO(ARG), APPLY_4(MACRO, __VA_ARGS__)
#define APPLY_6(MACRO, ARG, ...)   MACRO(ARG), APPLY_5(MACRO, __VA_ARGS__)
#define APPLY_7(MACRO, ARG, ...)   MACRO(ARG), APPLY_6(MACRO, __VA_ARGS__)
#define APPLY_8(MACRO, ARG, ...)   MACRO(ARG), APPLY_7(MACRO, __VA_ARGS__)
#define APPLY_9(MACRO, ARG, ...)   MACRO(ARG), APPLY_8(MACRO, __VA_ARGS__)
#define APPLY_10(MACRO, ARG, ...)  MACRO(ARG), APPLY_9(MACRO, __VA_ARGS__)
&lt;/span&gt;
&lt;span class="cp"&gt;#define APPLY_FOR_EACH(MACRO, ...) \
  NAME2(APPLY_, VA_ARGS_COUNT(__VA_ARGS__))(MACRO, __VA_ARGS__)
&lt;/span&gt;
&lt;span class="cp"&gt;#define NAME2(A,B)                NAME2_HELPER(A,B)
#define NAME2_HELPER(A,B)         A ## B
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is, we can use &lt;code&gt;VA_ARGS_COUNT&lt;/code&gt; to count the number of arguments (say, 4), then construct the corresponding &lt;code&gt;APPLY_&lt;/code&gt; macro (say, &lt;code&gt;APPLY_4&lt;/code&gt;) that recursively calls &lt;code&gt;APPLY_&lt;/code&gt;&lt;em&gt;n&lt;/em&gt; for &lt;em&gt;n&lt;/em&gt; &amp;lt; 4 in turn.  Hence, the &lt;code&gt;APPLY_FOR_EACH&lt;/code&gt; macro applies &lt;code&gt;MACRO&lt;/code&gt; to each variadic argument.  Given that, we can define:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#define tsva_print(...)                      \
  tsva_print( VA_ARGS_COUNT( __VA_ARGS__ ),  \
              (tsva_value_t[]){ APPLY_FOR_EACH(TSVA_VALUE, __VA_ARGS__) } )
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and finally do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;tsva_print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s"&gt;"Answer is: "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and it just works.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;As this extended example once again shows, the C preprocessor has its own &lt;em&gt;weird&lt;/em&gt; text processing language inside of C. The highlighted techniques of:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Argument “sliding” as shown in &lt;code&gt;ARG_11&lt;/code&gt;; and:&lt;/li&gt;
&lt;li&gt;Argument counting via &lt;code&gt;VA_ARGS_COUNT&lt;/code&gt;; and:&lt;/li&gt;
&lt;li&gt;Transforming arguments via &lt;code&gt;APPLY_FOR_EACH&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;are often used in advanced macros.  In this case, they can provide type-safe variadic arguments.&lt;/p&gt;

&lt;h2&gt;
  
  
  Caveats
&lt;/h2&gt;

&lt;p&gt;While the version of this code that uses the explicit macros of &lt;code&gt;TSVA_BOOL&lt;/code&gt;, &lt;code&gt;TSVA_CHAR&lt;/code&gt;, etc., has no caveats, the version that uses &lt;code&gt;_Generic&lt;/code&gt; does, specifically:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;char&lt;/code&gt; literals are deduced to &lt;code&gt;int&lt;/code&gt;, not &lt;code&gt;char&lt;/code&gt;; and:&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;_Bool&lt;/code&gt; literals of &lt;code&gt;false&lt;/code&gt; and &lt;code&gt;true&lt;/code&gt; are  also deduced to &lt;code&gt;int&lt;/code&gt;, not &lt;code&gt;_Bool&lt;/code&gt;.  (However, this has been fixed in C23; see below.)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Hence:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'*'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;_Bool&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;type_print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;      &lt;span class="c1"&gt;// prints "*"&lt;/span&gt;
&lt;span class="n"&gt;type_print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;      &lt;span class="c1"&gt;// prints "false"&lt;/span&gt;

&lt;span class="n"&gt;type_print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sc"&gt;'*'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;    &lt;span class="c1"&gt;// prints "42", not "*"&lt;/span&gt;
&lt;span class="n"&gt;type_print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// prints "0", not "false"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This isn’t a problem with &lt;code&gt;_Generic&lt;/code&gt;.  In C, the type of a &lt;code&gt;char&lt;/code&gt; literal is simply defined to be &lt;code&gt;int&lt;/code&gt;, not &lt;code&gt;char&lt;/code&gt;.  Prior to &lt;a href="https://en.wikipedia.org/wiki/C11_(C_standard_revision)" rel="noopener noreferrer"&gt;C11&lt;/a&gt; when &lt;code&gt;_Generic&lt;/code&gt; was added, this never mattered; but now it matters.&lt;/p&gt;

&lt;p&gt;The C++ Committee fixed it in C++ because they wanted the ability to overload functions by &lt;code&gt;char&lt;/code&gt; and you can’t do that consistently if &lt;code&gt;char&lt;/code&gt; variables are of type &lt;code&gt;char&lt;/code&gt; but &lt;code&gt;char&lt;/code&gt; literals are of type &lt;code&gt;int&lt;/code&gt;. The C Committee should have fixed the type of &lt;code&gt;char&lt;/code&gt; literals in C11.&lt;/p&gt;

&lt;p&gt;Now in C23 with both &lt;a href="https://dev.to/pauljlucas/auto-in-c23-ij9"&gt;&lt;code&gt;auto&lt;/code&gt;&lt;/a&gt; and &lt;a href="https://dev.to/pauljlucas/typeof-in-c23-55p2"&gt;&lt;code&gt;typeof&lt;/code&gt;&lt;/a&gt;, the problem is even worse:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sc"&gt;'*'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// deduces int, not char&lt;/span&gt;
&lt;span class="n"&gt;typeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sc"&gt;'*'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// type is int, not char&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similarly, when &lt;code&gt;_Bool&lt;/code&gt; was added in &lt;a href="https://en.wikipedia.org/wiki/C99" rel="noopener noreferrer"&gt;C99&lt;/a&gt;, it was a transitional type.  Specifically, &lt;code&gt;false&lt;/code&gt; and &lt;code&gt;true&lt;/code&gt; were &lt;em&gt;not&lt;/em&gt; added as keywords, but only macros as &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;1&lt;/code&gt; (integers), respectively, via &lt;code&gt;stdbool.h&lt;/code&gt;.  At least this has finally been &lt;a href="https://dev.to/pauljlucas/bool-in-c23-4kj4"&gt;fixed in C23&lt;/a&gt; now that &lt;code&gt;false&lt;/code&gt; and &lt;code&gt;true&lt;/code&gt; are proper keyword literals for &lt;code&gt;bool&lt;/code&gt;.&lt;/p&gt;

</description>
      <category>c</category>
    </item>
    <item>
      <title>Avoid the Temptation of Header-Only Libraries</title>
      <dc:creator>Paul J. Lucas</dc:creator>
      <pubDate>Mon, 01 Dec 2025 01:33:36 +0000</pubDate>
      <link>https://forem.com/pauljlucas/avoid-the-temptation-of-header-only-libraries-33an</link>
      <guid>https://forem.com/pauljlucas/avoid-the-temptation-of-header-only-libraries-33an</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Occasionally, you come across some library that advertises itself as a &lt;em&gt;header-only&lt;/em&gt; library, that is &lt;em&gt;all&lt;/em&gt; the code is entirely contained in one or more &lt;code&gt;.h&lt;/code&gt; files only — no &lt;code&gt;.c&lt;/code&gt; files.  The claim is that such libraries are simpler to use because all you have to do is &lt;code&gt;#include&lt;/code&gt; the headers.  Header-only libraries generally fall into a few types, those that provide:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Type-generic declarations and function definitions, e.g., “containers.”&lt;/li&gt;
&lt;li&gt;Ordinary declarations and function definitions.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This article is about &lt;em&gt;only&lt;/em&gt; the first two types.  My claim is that neither justifies making a library header-only.  The third type is less common; but when there really are &lt;em&gt;no&lt;/em&gt; non-trivial function definitions, then — and &lt;em&gt;only&lt;/em&gt; then — are header-only libraries OK.&lt;/p&gt;

&lt;h2&gt;
  
  
  Type-Generic Container Header-Only Libraries
&lt;/h2&gt;

&lt;p&gt;One thing that occurs in most programming languages is the desire to have type-generic code, that is code whose purpose or algorithms don’t depend on the type of data.  Most commonly, this manifests as the desire to have generic “containers,” e.g., arrays, lists, or sets of some type &lt;em&gt;T&lt;/em&gt; where T can not only be any type built into the language, but user-defined types as well, e.g., list of integers, sets of strings, etc.&lt;/p&gt;

&lt;p&gt;Different languages that support generic containers do so differently, e.g., C++ uses “templates” where type parameters are &lt;em&gt;instantiated&lt;/em&gt; at compile-time with specific types, e.g., a &lt;code&gt;list&amp;lt;T&amp;gt;&lt;/code&gt; can be instantiated with T = &lt;code&gt;int&lt;/code&gt; yielding &lt;code&gt;list&amp;lt;int&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;C has only minimal support for generic code via &lt;a href="https://dev.to/pauljlucas/generic-in-c-i48"&gt;&lt;code&gt;_Generic&lt;/code&gt;&lt;/a&gt; and &lt;a href="https://dev.to/pauljlucas/cc-preprocessor-macros-fh5"&gt;preprocessor macros&lt;/a&gt;. Some people try to use these things to implement generic containers by using very long and elaborate macros, e.g.:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#define LIST(T)                                              \
  struct list_node_##T {                                     \
    struct list_node_##T *next;                              \
    T data;                                                  \
  };                                                         \
                                                             \
  struct list_##T {                                          \
    struct list_node_##T *head, *tail;                       \
  };                                                         \
                                                             \
  static void list_init_##T( struct list_node_##T *list ) {  \
  // ...
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is the type T, a macro parameter, forms part of the names of the structures and functions, e.g., &lt;code&gt;list_int&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The benefit of using macros like this is that the code is &lt;em&gt;header only&lt;/em&gt;, that is all the code is in a single &lt;code&gt;.h&lt;/code&gt; file. While this works, it has a number of problems.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In the earliest days of C++ (C with Classes), macros were used to implement generic containers.  The problem with macros in general is that they don’t obey either scope or type rules, nor work well with tools.  Hence the addition of templates to C++.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Problems
&lt;/h3&gt;

&lt;p&gt;Using macros to define generic containers has a number of problems:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;All functions &lt;em&gt;must&lt;/em&gt; be declared &lt;code&gt;static&lt;/code&gt;, otherwise every &lt;code&gt;.o&lt;/code&gt; file whose corresponding &lt;code&gt;.c&lt;/code&gt; file includes the header will contain definitions for all functions. That would result in the linker complaining about duplicate symbols.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;While &lt;code&gt;static&lt;/code&gt; declarations solve the duplicate symbols problem, &lt;em&gt;every&lt;/em&gt; &lt;code&gt;.o&lt;/code&gt; file &lt;em&gt;still&lt;/em&gt; contains the definitions for all functions that are &lt;em&gt;all&lt;/em&gt; present within the final executable.  This results in &lt;em&gt;code bloat&lt;/em&gt; increasing the executable’s size, sometimes dramatically, both on disk and in memory.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It’s hard to debug the code in the macros themselves since it expands to be a &lt;em&gt;single&lt;/em&gt; line of code.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For example, even if you use only one type for T, say, &lt;code&gt;int&lt;/code&gt;, hence &lt;code&gt;LIST(int)&lt;/code&gt;, but include the header into, say, five &lt;code&gt;.c&lt;/code&gt; files, then the final executable will have &lt;em&gt;five&lt;/em&gt; copies of all the code.&lt;/p&gt;

&lt;p&gt;Note that &lt;em&gt;if&lt;/em&gt; and only if all functions are marked &lt;a href="https://dev.to/pauljlucas/inline-functions-in-c-and-c-2040"&gt;&lt;code&gt;inline&lt;/code&gt;&lt;/a&gt; in addition to &lt;code&gt;static&lt;/code&gt; &lt;em&gt;and&lt;/em&gt; the functions are trivial enough to &lt;em&gt;actually&lt;/em&gt; be inlined by the compiler, then the code bloat problem goes away. However, any useful containers library will invariably have non-trivial functions that can’t be inlined.&lt;/p&gt;

&lt;h3&gt;
  
  
  Mitigation Tactics
&lt;/h3&gt;

&lt;p&gt;For generic code as described, there’s no standard way (meaning, there’s no compiler-independent way) to solve the code bloat problem.&lt;/p&gt;

&lt;p&gt;If you’re using &lt;a href="https://gcc.gnu.org/" rel="noopener noreferrer"&gt;&lt;code&gt;gcc&lt;/code&gt;&lt;/a&gt; or &lt;a href="https://clang.llvm.org/" rel="noopener noreferrer"&gt;&lt;code&gt;clang&lt;/code&gt;&lt;/a&gt;, you can use the &lt;a href="https://gcc.gnu.org/onlinedocs/gcc-15.2.0/gcc/Common-Function-Attributes.html" rel="noopener noreferrer"&gt;&lt;code&gt;weak&lt;/code&gt;&lt;/a&gt; &lt;a href="https://gcc.gnu.org/onlinedocs/gcc-15.2.0/gcc/Attribute-Syntax.html" rel="noopener noreferrer"&gt;attribute&lt;/a&gt;; if you’re using &lt;a href="https://visualstudio.microsoft.com/" rel="noopener noreferrer"&gt;MSVC&lt;/a&gt;, you’re out of luck since no equivalent attribute exists.&lt;/p&gt;

&lt;p&gt;If you’re working on code only for a specific system or you’re the only user, then fine: you can use compiler-specific solutions; but if you want your code to be widely cross-platform, then you shouldn’t use compiler-specific solutions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ordinary Header-Only Libraries
&lt;/h2&gt;

&lt;p&gt;Ordinary header-only libraries have the code that otherwise would have gone into &lt;code&gt;.c&lt;/code&gt; files in the &lt;code&gt;.h&lt;/code&gt; files instead.  To avoid the aforementioned duplicate symbols problem, &lt;code&gt;static&lt;/code&gt; is used for everything as before.&lt;/p&gt;

&lt;p&gt;One problem this creates is that type and function declarations that would have been “private” to the implementation using &lt;code&gt;static&lt;/code&gt; as intended are now “public” that breaks encapsulation.&lt;/p&gt;

&lt;p&gt;One problem that creates is that users might use some of the private parts of the implementation when they shouldn’t.  If the library author changes any of the private parts, that’ll break user programs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Mitigation Tactics
&lt;/h3&gt;

&lt;p&gt;One way to fix that would be simply to put comments like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// PART OF FOO IMPLEMENTATION -- DO NOT USE&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another marginally better way to fix the encapsulation problem is to split the &lt;code&gt;.h&lt;/code&gt; file into “public” and “private” parts guarded by a macro like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// The ".h" part of the library.&lt;/span&gt;
&lt;span class="c1"&gt;// ... type and function declarations ...&lt;/span&gt;

&lt;span class="c1"&gt;// The ".c" part of the library.&lt;/span&gt;
&lt;span class="cp"&gt;#ifdef FOO_LIBRARY_IMPLEMENTATION
&lt;/span&gt;&lt;span class="c1"&gt;// ... function definitions ...&lt;/span&gt;
&lt;span class="cp"&gt;#endif
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then in one &lt;code&gt;.c&lt;/code&gt; file of the user’s choosing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#define FOO_LIBRARY_IMPLEMENTATION
#include&lt;/span&gt; &lt;span class="cpf"&gt;"foo_library.h"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The library author is forcing the user to compile the code into some &lt;code&gt;.c&lt;/code&gt; file that the author could have simply provided in the first place.  The user has to list the &lt;code&gt;.h&lt;/code&gt; file as a dependency in their makefiles anyway; also listing the &lt;code&gt;.c&lt;/code&gt; file as a dependency is no more of a burden than choosing an existing &lt;code&gt;.c&lt;/code&gt; and defining a macro.  Hence, it’s not at all clear to me why such a header-only library is allegedly simpler to use.&lt;/p&gt;

&lt;h3&gt;
  
  
  Other Problems
&lt;/h3&gt;

&lt;p&gt;Such header-only libraries have additional problems:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The header file(s) are much bigger which means compile times are longer in large projects since in all but one case, all the code for the implementation is read, parsed, and simply discarded.&lt;/li&gt;
&lt;li&gt;For the author, header-only libraries are harder to write and maintain because they preclude using build systems such as either &lt;a href="https://www.gnu.org/software/automake/manual/html_node/Autotools-Introduction.html" rel="noopener noreferrer"&gt;Autotools&lt;/a&gt; or &lt;a href="https://cmake.org" rel="noopener noreferrer"&gt;CMake&lt;/a&gt; to “probe” the host system.  After all, the user is &lt;em&gt;only&lt;/em&gt; supposed to &lt;code&gt;#include&lt;/code&gt; the header — &lt;em&gt;right&lt;/em&gt;??&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The problem with precluding using build systems is that if a library has any one of compiler-, operating-system-, or CPU-specific code (as any non-trivial library might), you have to handle all the cases manually via &lt;code&gt;#ifdef&lt;/code&gt;. For example, I recently came across the following in a header-only library:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#if defined(__LITTLE_ENDIAN__) || defined(__ARMEL__) || \
    defined(__THUMBEL__) || defined(__AARCH64EL__) || \
    defined(_MIPSEL) || defined(__MIPSEL) || defined(__MIPSEL__) || \
    defined(__x86_64__) || defined(__i386__) || defined(__X86__)
&lt;/span&gt;    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="cp"&gt;#else
&lt;/span&gt;    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Do you really want to research and maintain something like that?  Instead, if you used a build-tool like &lt;a href="https://dev.to/pauljlucas/autotools-introduction-p9b"&gt;Autotools&lt;/a&gt; and just &lt;a href="https://dev.to/pauljlucas/probing-the-compiler-in-autotools-576o"&gt;probe the platform&lt;/a&gt; directly by using the &lt;a href="https://www.gnu.org/software/autoconf/manual/autoconf-2.68/html_node/Canonicalizing.html" rel="noopener noreferrer"&gt;&lt;code&gt;AC_CANONICAL_HOST&lt;/code&gt;&lt;/a&gt; macro and let it do the work.  If using CMake, it you get &lt;a href="https://cmake.org/cmake/help/latest/variable/CMAKE_SYSTEM_PROCESSOR.html" rel="noopener noreferrer"&gt;&lt;code&gt;CMAKE_SYSTEM_PROCESSOR&lt;/code&gt;&lt;/a&gt; for free.  In either case, many people have contributed many person-years of work to get those right so you don’t have to.&lt;/p&gt;

&lt;h2&gt;
  
  
  Other Languages
&lt;/h2&gt;

&lt;p&gt;Of all the programming languages that exist, C is rare in that it was designed such that library “interfaces” and “implementations” are put into separate files, namely &lt;code&gt;.h&lt;/code&gt; and &lt;code&gt;.c&lt;/code&gt;, respectively.  Only a handful of other languages do this: &lt;a href="https://en.wikipedia.org/wiki/C%2B%2B" rel="noopener noreferrer"&gt;C++&lt;/a&gt;, &lt;a href="https://en.wikipedia.org/wiki/Objective-C" rel="noopener noreferrer"&gt;Objective-C&lt;/a&gt;, &lt;a href="https://en.wikipedia.org/wiki/Ada_(programming_language)" rel="noopener noreferrer"&gt;Ada&lt;/a&gt;, &lt;a href="https://en.wikipedia.org/wiki/D_(programming_language)" rel="noopener noreferrer"&gt;D&lt;/a&gt;, and &lt;a href="https://en.wikipedia.org/wiki/Modula-2" rel="noopener noreferrer"&gt;Modula-2&lt;/a&gt;.  And of those, only C++ is still widely used.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;If you learn any modern language that puts interfaces and implementations into the same file before learning C, you might be tempted to do the same in C — don’t! The hard reality is that C wasn’t designed for and therefore doesn’t really support header-only libraries (whether you like or not).  Some day, C might get a proper module facility, but that day is not today.  Don’t “fight” against the way C was designed.&lt;/p&gt;

&lt;p&gt;For type-generic code, you should implement a library using the generic &lt;code&gt;void*&lt;/code&gt; in a conventional &lt;code&gt;.h&lt;/code&gt; and &lt;code&gt;.c&lt;/code&gt; pair.  Since you have to add the &lt;code&gt;.h&lt;/code&gt; to your dependencies anyway, also adding the corresponding &lt;code&gt;.c&lt;/code&gt; is trivial.&lt;/p&gt;

&lt;p&gt;As an alternative to &lt;code&gt;void*&lt;/code&gt;, you can use &lt;a href="https://dev.to/pauljlucas/obscure-c99-array-features-3270"&gt;flexible array members&lt;/a&gt; to implement generic containers in C, but that’s a &lt;a href="https://dev.to/pauljlucas/generic-data-structures-in-c-3l6a"&gt;story for another time&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For ordinary header-only libraries, just use &lt;code&gt;.c&lt;/code&gt; files and build tools and all of the problems mentioned simply go away.&lt;/p&gt;

&lt;h2&gt;
  
  
  Epilogue
&lt;/h2&gt;

&lt;p&gt;You might be wondering, “&lt;em&gt;Don’t C++ templates have the same problems?&lt;/em&gt;”  Only partially.  First, since templates are part of the language proper and not the preprocessor, a C++ compiler can mark all instantiated functions as weak in whatever manner a given platform needs — but it’s the compiler’s problem, not your problem.&lt;/p&gt;

&lt;p&gt;Second, the library implementation can use factorization tricks.  For example, all &lt;code&gt;std::list&amp;lt;T*&amp;gt;&lt;/code&gt; can be implemented in terms of &lt;code&gt;std::list&amp;lt;void*&amp;gt;&lt;/code&gt;, that is the single instantiation of the latter can be used for &lt;em&gt;all&lt;/em&gt; pointers regardless of T.  There are other tricks possible as well.&lt;/p&gt;

&lt;p&gt;Note that with a lot more work, you could probably do some factorization tricks in C as well, e.g., have &lt;code&gt;PTR_LIST(T)&lt;/code&gt; that’s implemented in terms of &lt;code&gt;LIST(void*)&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;The Design and Evolution of C++&lt;/em&gt;, Bjarne Stroustrup, AT&amp;amp;T Bell Laboratories, Addison-Wesley, Reading, Massachusetts, 1994, §15.1.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>c</category>
    </item>
    <item>
      <title>Good Git Hygiene Practices</title>
      <dc:creator>Paul J. Lucas</dc:creator>
      <pubDate>Thu, 27 Nov 2025 15:45:37 +0000</pubDate>
      <link>https://forem.com/pauljlucas/good-git-hygiene-practices-2fnl</link>
      <guid>https://forem.com/pauljlucas/good-git-hygiene-practices-2fnl</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Whether you’re writing code for a personal project or for your day job, you very likely are (or should be) using a &lt;a href="https://en.wikipedia.org/wiki/Version_control" rel="noopener noreferrer"&gt;source code version control system&lt;/a&gt;.  That’s most likely &lt;a href="https://git-scm.com" rel="noopener noreferrer"&gt;Git&lt;/a&gt;.  While there are &lt;em&gt;many&lt;/em&gt; opinions on what the best branching strategy is (that I’m not going to discuss here), there are a few independent things you can do to maintain good Git “hygiene” (that I am going to discuss here).&lt;/p&gt;

&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;Over the years, one phenomenon I’ve noticed is that I often end up “under the car” — my own personal term to describe the following situation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You’re humming along working on some code and you discover a bug in existing code that should be fixed regardless of what you’re currently working on, or &lt;em&gt;needs&lt;/em&gt; to be fixed in order to continue working on what you’re currently working on.&lt;/li&gt;
&lt;li&gt;You decide to pause what you’re doing and fix that bug.&lt;/li&gt;
&lt;li&gt;While working on that bug, you might discover &lt;em&gt;another&lt;/em&gt; bug that should be fixed regardless of the current bug.&lt;/li&gt;
&lt;li&gt;And so on ....&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A slight variation is that, instead of discovering a bug, you realize that some bit of architecture needs to be rearchitected a bit, or a new small feature or other change were done, it would greatly simplify what you were originally working on.  So, again, you decide to pause what you’re doing and go and do that first.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I recently experienced this phenomenon again.  While working on a fix for an &lt;a href="https://github.com/paul-j-lucas/cdecl/issues/39" rel="noopener noreferrer"&gt;issue&lt;/a&gt; in &lt;a href="https://github.com/paul-j-lucas/cdecl/" rel="noopener noreferrer"&gt;&lt;code&gt;cdecl&lt;/code&gt;&lt;/a&gt;, I realized that the unrelated enhancement of allowing the &lt;code&gt;-t&lt;/code&gt; command-line option to be specified twice to suppress predefining all types would make debugging the original issue easier, so that’s what I &lt;a href="https://github.com/paul-j-lucas/cdecl/commit/2a56450a2b605ac8f9b246e8fb66d314f0345254" rel="noopener noreferrer"&gt;did&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Why do I refer to this as “under the car?”  It comes from the TV show &lt;a href="https://en.wikipedia.org/wiki/Malcolm_in_the_Middle" rel="noopener noreferrer"&gt;Malcolm in the Middle&lt;/a&gt;, specifically a &lt;a href="https://www.youtube.com/watch?v=5W4NFcamRhM" rel="noopener noreferrer"&gt;scene&lt;/a&gt; from S03E06, “Health Scare,” 2001. I think that video perfectly captures this phenomenon, that is you start out to do X, but realize you need to do Y (and possibly Z) first.  Like &lt;a href="https://en.wikipedia.org/wiki/List_of_Malcolm_in_the_Middle_characters#Hal" rel="noopener noreferrer"&gt;Hal&lt;/a&gt;, you’ve also very likely experienced this phenomenon in your every-day (non-software) life as well.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Not to Do
&lt;/h2&gt;

&lt;p&gt;Before getting to what you &lt;em&gt;should&lt;/em&gt; do, I’ll mention what you should &lt;em&gt;not&lt;/em&gt; do:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You should &lt;em&gt;not&lt;/em&gt; simply include either the fix for the unrelated bug or the code for the unrelated feature as part of the code you’re currently working on.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Why not?  While it is the least-effort thing you can do, you should &lt;em&gt;not&lt;/em&gt; do it because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It makes the diff for your original code longer.  (Ideally, the diff for any change should be as small as possible.)&lt;/li&gt;
&lt;li&gt;Should you need to &lt;a href="https://git-scm.com/docs/git-revert" rel="noopener noreferrer"&gt;revert&lt;/a&gt;, you can’t easily revert your new code independently from the unrelated bug or feature.&lt;/li&gt;
&lt;li&gt;Similarly, you can’t easily &lt;a href="https://git-scm.com/docs/git-cherry-pick" rel="noopener noreferrer"&gt;cherry-pick&lt;/a&gt; your new code independently from the unrelated bug or feature.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you’re working as part of a team, you should also &lt;em&gt;not&lt;/em&gt; do it because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It makes the diff more confusing to reviewers.&lt;/li&gt;
&lt;li&gt;It makes testing your changes independently harder on QA.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What to Do Instead
&lt;/h2&gt;

&lt;p&gt;There are a few choices, but the better choice depends on whether you are working by yourself on your own project, or part of a team.  If by yourself, you can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;“&lt;a href="https://git-scm.com/docs/git-stash" rel="noopener noreferrer"&gt;Stash&lt;/a&gt;” your current set of changes in a segregated stack returning the current branch to the way it was before you made any changes.&lt;/li&gt;
&lt;li&gt;Fix the other bug or add the other feature and commit the change.&lt;/li&gt;
&lt;li&gt;“Pop” the stash from the stack reapplying your previous changes and resume what you were originally working on.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git stash
&lt;span class="c"&gt;# Edit files to fix other bug or add other feature.&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;git add &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;git commit &lt;span class="nt"&gt;-m&lt;/span&gt;&lt;span class="s2"&gt;"&amp;lt;message&amp;gt;"&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;git stash pop
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the fix for the other bug or change for the other feature is a bit more involved, you might want to create a separate branch for it, in which case the steps go something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git stash
&lt;span class="nv"&gt;$ &lt;/span&gt;git checkout master
&lt;span class="nv"&gt;$ &lt;/span&gt;git branch other-bug-fix-branch
&lt;span class="nv"&gt;$ &lt;/span&gt;git checkout other-bug-fix-branch
&lt;span class="c"&gt;# Edit files to fix other bug or add other feature.&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;git add &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;git commit &lt;span class="nt"&gt;-m&lt;/span&gt;&lt;span class="s2"&gt;"&amp;lt;message&amp;gt;"&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;git checkout master
&lt;span class="nv"&gt;$ &lt;/span&gt;git merge other-bug-fix
&lt;span class="nv"&gt;$ &lt;/span&gt;git checkout original-branch
&lt;span class="nv"&gt;$ &lt;/span&gt;git merge master
&lt;span class="nv"&gt;$ &lt;/span&gt;git stash pop
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you’re using &lt;em&gt;pull requests&lt;/em&gt; (PRs), then you’d change the last few lines to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# ...&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;git commit &lt;span class="nt"&gt;-m&lt;/span&gt;&lt;span class="s2"&gt;"&amp;lt;message&amp;gt;"&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;git push &lt;span class="nt"&gt;-u&lt;/span&gt; origin other-bug-fix
&lt;span class="c"&gt;# Review on web site.&lt;/span&gt;
&lt;span class="c"&gt;# Merge on web site into master.&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;git checkout master
&lt;span class="nv"&gt;$ &lt;/span&gt;git pull
&lt;span class="nv"&gt;$ &lt;/span&gt;git checkout original-branch
&lt;span class="nv"&gt;$ &lt;/span&gt;git merge master
&lt;span class="nv"&gt;$ &lt;/span&gt;git stash pop
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This assumes you use some web user interface (such as that of Github or Gitlab) to do the review and eventual merge of the PR.&lt;/p&gt;

&lt;p&gt;The advantage of a separate branch is that you can return to your original branch and keep working on it &lt;em&gt;while&lt;/em&gt; your other-bug-fix branch is being reviewed.  If the bug fix doesn’t affect the code you were originally working on, you can skip the pull and merge from &lt;code&gt;master&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Note that if you were originally working on a branch other than &lt;code&gt;master&lt;/code&gt;, you can simply commit your current (incomplete) changes to that branch rather than stash.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stash Merge Conflicts
&lt;/h2&gt;

&lt;p&gt;Just as with an ordinary merge, &lt;code&gt;git stash pop&lt;/code&gt; can also result in merge conflicts.  You fix them as with ordinary merge conflicts, add the conflicted files, and commit.&lt;/p&gt;

&lt;p&gt;When a merge conflict occurs, the &lt;code&gt;pop&lt;/code&gt; doesn’t &lt;em&gt;actually&lt;/em&gt; pop: Git merges the changes from the stash, but leaves the stash on the stack just in case.  After you fix the conflicts and commit, you should delete the stash via:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git stash drop
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;If you ever find yourself “under the car,” you can use either stashing or branches to continue work while maintaining good Git hygiene.&lt;/p&gt;

</description>
      <category>git</category>
      <category>programming</category>
    </item>
    <item>
      <title>Advanced C Preprocessor Macros for a Pre-C23/C++20 __VA_OPT__ Substitute</title>
      <dc:creator>Paul J. Lucas</dc:creator>
      <pubDate>Tue, 18 Nov 2025 01:27:42 +0000</pubDate>
      <link>https://forem.com/pauljlucas/using-advanced-c-preprocessor-macros-for-a-pre-c23c20-vaopt-substitute-43c1</link>
      <guid>https://forem.com/pauljlucas/using-advanced-c-preprocessor-macros-for-a-pre-c23c20-vaopt-substitute-43c1</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In &lt;a href="https://dev.to/pauljlucas/generic-in-c-i48"&gt;_Generic in C&lt;/a&gt;, I showed how to implement &lt;code&gt;const&lt;/code&gt; function overloading in C via this macro:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#define CONST_OVERLOAD(FN, PTR, ...) \
  STATIC_IF( IS_PTR_TO_CONST(PTR),   \
    const_ ## FN,                    \
    (FN)                             \
  )( (PTR) __VA_OPT__(,) __VA_ARGS__ )
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;I actually use a macro similar to this in my &lt;a href="https://github.com/paul-j-lucas/cdecl" rel="noopener noreferrer"&gt;&lt;code&gt;cdecl&lt;/code&gt;&lt;/a&gt; project.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This macro makes use of &lt;a href="https://en.cppreference.com/w/c/preprocessor/replace" rel="noopener noreferrer"&gt;&lt;code&gt;__VA_OPT__&lt;/code&gt;&lt;/a&gt; that’s a C23/C++20 feature whereas &lt;code&gt;cdecl&lt;/code&gt; requires only C11 at a minimum.  Why not just require C23?  C23 is still too new and thus not ubiquitous, or many compilers don’t yet default to C23.&lt;/p&gt;

&lt;p&gt;That aside, it turns out that many compilers have supported &lt;code&gt;__VA_OPT__&lt;/code&gt; for a while even without C23/C++20. So why is this even an issue? Because it’s not &lt;em&gt;standard&lt;/em&gt; and you can’t &lt;em&gt;rely&lt;/em&gt; on such support.  While this might not keep me up at night, it comes close.&lt;/p&gt;

&lt;p&gt;I’ve searched for a long time for a way to simulate &lt;code&gt;__VA_OPT__&lt;/code&gt; using only standard pre-C23/C++20 and found nothing.  After continued searching, I’ve finally found all the pieces.  To do it requires some “advanced” (sometimes clever, but mostly &lt;em&gt;weird&lt;/em&gt;) use of &lt;a href="https://dev.to/pauljlucas/cc-preprocessor-macros-fh5"&gt;preprocessor macros&lt;/a&gt;, so buckle up: it’s going to be a wild ride.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reminder
&lt;/h2&gt;

&lt;p&gt;As a reminder, what &lt;code&gt;__VA_OPT__&lt;/code&gt; does is fairly simple. In a function-like macro, if the number of arguments given for the variadic parameter is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Greater than zero, that is &lt;code&gt;__VA_ARGS__&lt;/code&gt; will expand into something that is &lt;em&gt;not&lt;/em&gt; empty, then &lt;code&gt;__VA_OPT__&lt;/code&gt; will insert the token(s) given to it;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Zero, that is &lt;code&gt;__VA_ARGS__&lt;/code&gt; is &lt;em&gt;empty&lt;/em&gt;, then &lt;code&gt;__VA_OPT__&lt;/code&gt; will do nothing.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hence, for the &lt;code&gt;CONST_OVERLOAD()&lt;/code&gt; macro, &lt;code&gt;__VA_OPT__(,)&lt;/code&gt; will insert a comma to separate the first argument &lt;code&gt;(PTR)&lt;/code&gt; from the second argument — but only if there &lt;em&gt;is&lt;/em&gt; a second argument.&lt;/p&gt;

&lt;p&gt;So, how hard can it be to simulate &lt;code&gt;__VA_OPT__&lt;/code&gt;?  (Spoiler: it’s harder than it might seem.)&lt;/p&gt;

&lt;h2&gt;
  
  
  Is There a Comma?
&lt;/h2&gt;

&lt;p&gt;One thing that helps a lot is first to determine whether &lt;code&gt;__VA_ARGS__&lt;/code&gt; contains a comma.  For example, given:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#define M(...)  &lt;/span&gt;&lt;span class="cm"&gt;/* doesn't matter */&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then whether &lt;code&gt;__VA_ARGS__&lt;/code&gt; contains a comma or not:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;M&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;             &lt;span class="c1"&gt;// no arguments: no comma&lt;/span&gt;
&lt;span class="n"&gt;M&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;            &lt;span class="c1"&gt;// 1 argument: still no comma&lt;/span&gt;
&lt;span class="n"&gt;M&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;          &lt;span class="c1"&gt;// 2 arguments: 1 comma&lt;/span&gt;
&lt;span class="n"&gt;M&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;        &lt;span class="c1"&gt;// 3 arguments: 2 commas&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hence, we have to distinguish between the 0/1 argument case from the 2+ arguments case.  To be explicit for, say, a maximum of 10 arguments, we want a “has comma” macro to return either &lt;code&gt;0&lt;/code&gt; (false) for “no comma” or &lt;code&gt;1&lt;/code&gt; (true) for “has comma” as given in this small truth table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#Args: 0/1 2 3 4 5 6 7 8 9 10
Comma?  0  1 1 1 1 1 1 1 1 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Given that, we can define &lt;code&gt;ARGS_HAS_COMMA()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#define ARGS_HAS_COMMA(...) \
  ARG_11( __VA_ARGS__, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 )
&lt;/span&gt;
&lt;span class="cp"&gt;#define ARG_11(_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,...) _11
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;ARGS_HAS_COMMA()&lt;/code&gt; uses a helper macro &lt;code&gt;ARG_11()&lt;/code&gt; that, given 11 or more arguments (one more than our maximum of 10), always returns its 11th argument.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This implementation self-imposes a maximum of 10 arguments that should be enough for the majority of cases. However, if you want to allow for more, say, 100, change &lt;code&gt;ARG_11&lt;/code&gt; to &lt;code&gt;ARG_101&lt;/code&gt; with 101 parameters followed by &lt;code&gt;...&lt;/code&gt; returning &lt;code&gt;_101&lt;/code&gt; and call it with 99 &lt;code&gt;1&lt;/code&gt;s.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What use is always returning the 11th argument? The trick is &lt;code&gt;ARGS_HAS_COMMA()&lt;/code&gt; calls &lt;code&gt;ARG_11()&lt;/code&gt; supplying its &lt;code&gt;__VA_ARGS__&lt;/code&gt; followed by the above truth table (in reverse).  This has the effect of always “sliding” the correct answer into the 11th argument position.  For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ARGS_HAS_COMMA() =&amp;gt; ARG_11( , 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It works as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;ARGS_HAS_COMMA()&lt;/code&gt; with no arguments causes &lt;code&gt;__VA_ARGS__&lt;/code&gt; to expand into nothing. The preprocessor allows empty arguments, so “nothing” becomes the first argument, the first &lt;code&gt;1&lt;/code&gt; becomes the second argument (and so on for the remaining eight &lt;code&gt;1&lt;/code&gt;s), and the &lt;code&gt;0&lt;/code&gt; becomes the 11th argument that’s returned (false).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Similarly, &lt;code&gt;ARGS_HAS_COMMA(x)&lt;/code&gt; with one argument causes &lt;code&gt;__VA_ARGS__&lt;/code&gt; to expand into &lt;code&gt;x&lt;/code&gt;.  This time, the &lt;code&gt;x&lt;/code&gt; becomes the first argument, but everything else is the same, i.e., the &lt;code&gt;0&lt;/code&gt; still becomes the 11th argument that’s returned (false).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;However, &lt;code&gt;ARGS_HAS_COMMA(x,y)&lt;/code&gt; with two arguments causes &lt;code&gt;__VA_ARGS__&lt;/code&gt; to expand into &lt;code&gt;x,y&lt;/code&gt;.  This time, &lt;code&gt;x&lt;/code&gt; becomes the first argument, &lt;code&gt;y&lt;/code&gt; becomes the second argument, the first &lt;code&gt;1&lt;/code&gt; becomes the &lt;em&gt;third&lt;/em&gt; argument (and so on for the remaining eight &lt;code&gt;1&lt;/code&gt;s), and &lt;code&gt;0&lt;/code&gt; (false) becomes the &lt;em&gt;12th&lt;/em&gt; argument.  The &lt;em&gt;11th&lt;/em&gt; argument is the ninth &lt;code&gt;1&lt;/code&gt; that’s returned instead (true).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For &lt;code&gt;ARGS_HAS_COMMA(x,y,z)&lt;/code&gt; with three arguments, the eighth &lt;code&gt;1&lt;/code&gt; will be returned; and so on for up to 10 arguments.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;For this and subsequent examples, you can enter the macros into &lt;a href="https://github.com/paul-j-lucas/cdecl" rel="noopener noreferrer"&gt;cdecl&lt;/a&gt; and have it &lt;code&gt;expand&lt;/code&gt; them for you step-by-step to illuminate how the expansions are taking place.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Is &lt;code&gt;__VA_ARGS__&lt;/code&gt; Empty?
&lt;/h2&gt;

&lt;p&gt;To answer that question, there are four cases to consider:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Two or more arguments.&lt;/li&gt;
&lt;li&gt;An argument within parentheses.&lt;/li&gt;
&lt;li&gt;An argument that is a function-like macro that will expand subsequent parentheses that may generate a comma.&lt;/li&gt;
&lt;li&gt;Zero arguments, i.e., &lt;code&gt;__VA_ARGS__&lt;/code&gt; is empty.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The 4th case is the one we’re interested in.  So why the first 3 cases?  They rule out possible false-positives.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;ARGS_IS_EMPTY()&lt;/code&gt; macro is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#define ARGS_IS_EMPTY(...)                                \
  ARGS_IS_EMPTY_CASES(                                    \
    &lt;/span&gt;&lt;span class="cm"&gt;/*  Case 1: argument with a comma,                    \
        e.g. "ARG1, ARG2", "ARG1, ...", or ",". */&lt;/span&gt;&lt;span class="cp"&gt;        \
    ARGS_HAS_COMMA( __VA_ARGS__ ),                        \
    &lt;/span&gt;&lt;span class="cm"&gt;/*  Case 2: argument within parentheses,              \
        e.g., "(ARG)", "(...)", or "()". */&lt;/span&gt;&lt;span class="cp"&gt;               \
    ARGS_HAS_COMMA( ARGS_IS_EMPTY_COMMA __VA_ARGS__ ),    \
    &lt;/span&gt;&lt;span class="cm"&gt;/*  Case 3: argument that is a macro that will expand \
        the parentheses, possibly generating a comma. */&lt;/span&gt;&lt;span class="cp"&gt;  \
    ARGS_HAS_COMMA( __VA_ARGS__ () ),                     \
    &lt;/span&gt;&lt;span class="cm"&gt;/*  Case 4: __VA_ARGS__ doesn't generate a comma by   \
        itself, nor with ARGS_IS_EMPTY_COMMA behind it,   \
        nor with () after it.                             \
        Therefore, "ARGS_IS_EMPTY_COMMA __VA_ARGS__ ()"   \
        generates a comma only if __VA_ARGS__ is empty.   \
        So this is the empty __VA_ARGS__ case since the   \
        previous cases are false. */&lt;/span&gt;&lt;span class="cp"&gt;                      \
    ARGS_HAS_COMMA( ARGS_IS_EMPTY_COMMA __VA_ARGS__ () )  \
  )
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Case 1: Two or More Arguments
&lt;/h3&gt;

&lt;p&gt;This is the easy case we’ve already covered, that is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;ARGS_HAS_COMMA&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;__VA_ARGS__&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;will return &lt;code&gt;1&lt;/code&gt; (true) only if two or more arguments are given.  Hence, this case must return &lt;code&gt;0&lt;/code&gt; (false) in order for &lt;code&gt;__VA_ARGS__&lt;/code&gt; to be empty.&lt;/p&gt;

&lt;h3&gt;
  
  
  Case 2: An Argument within Parentheses
&lt;/h3&gt;

&lt;p&gt;This is to handle cases like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;ARGS_HAS_COMMA&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;that is an argument enclosed within parentheses.  This is handled by:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;ARGS_HAS_COMMA&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;ARGS_IS_EMPTY_COMMA&lt;/span&gt; &lt;span class="n"&gt;__VA_ARGS__&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;where &lt;code&gt;ARGS_IS_EMPTY_COMMA&lt;/code&gt; is a helper macro defined as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#define ARGS_IS_EMPTY_COMMA(...)  ,
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As a reminder, the preprocessor will expand a function-like macro only if it’s followed by &lt;code&gt;(&lt;/code&gt;.  Therefore, if &lt;code&gt;__VA_ARGS__&lt;/code&gt; is something like &lt;code&gt;(x)&lt;/code&gt;, then:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;ARGS_IS_EMPTY_COMMA&lt;/code&gt; followed by &lt;code&gt;(x)&lt;/code&gt; will yield &lt;code&gt;ARGS_IS_EMPTY_COMMA(x)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;That will expand into &lt;code&gt;,&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Then &lt;code&gt;ARGS_HAS_COMMA(,)&lt;/code&gt; will yield &lt;code&gt;1&lt;/code&gt; (true).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;However, if &lt;code&gt;__VA_ARGS__&lt;/code&gt; is empty, then:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;ARGS_IS_EMPTY_COMMA&lt;/code&gt; followed by nothing will stay the same.&lt;/li&gt;
&lt;li&gt;Not being followed by a &lt;code&gt;(&lt;/code&gt; means &lt;code&gt;ARGS_IS_EMPTY_COMMA&lt;/code&gt; will &lt;em&gt;not&lt;/em&gt; expand into &lt;code&gt;,&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Then &lt;code&gt;ARGS_HAS_COMMA(ARGS_IS_EMPTY_COMMA)&lt;/code&gt; will yield &lt;code&gt;0&lt;/code&gt; (false).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Case 3: Argument that is a Function-Like Macro
&lt;/h3&gt;

&lt;p&gt;This is to handle cases like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#define FOO(X)  X, 0
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;that is a function-like macro that expands into something that contains a comma.  This is handled by:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;ARGS_HAS_COMMA&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;__VA_ARGS__&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;),&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If &lt;code&gt;__VA_ARGS__&lt;/code&gt; is &lt;code&gt;FOO&lt;/code&gt;, then:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;FOO&lt;/code&gt; followed by &lt;code&gt;()&lt;/code&gt; will yield &lt;code&gt;FOO(x)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;That will expand into &lt;code&gt;X, 0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Then &lt;code&gt;ARGS_HAS_COMMA(X, 0)&lt;/code&gt; will yield &lt;code&gt;1&lt;/code&gt; (true).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Case 4: Zero Arguments
&lt;/h3&gt;

&lt;p&gt;The first three cases are to filter out false positives.  Finally, case 4 answers the question “Is &lt;code&gt;__VA_ARGS__&lt;/code&gt; empty?”  This is handled by:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;ARGS_HAS_COMMA&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;ARGS_IS_EMPTY_COMMA&lt;/span&gt; &lt;span class="n"&gt;__VA_ARGS__&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If &lt;code&gt;__VA_ARGS__&lt;/code&gt; is empty, then:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;ARGS_IS_EMPTY_COMMA&lt;/code&gt; followed by &lt;code&gt;()&lt;/code&gt; will yield &lt;code&gt;ARGS_IS_EMPTY_COMMA()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;That will expand into &lt;code&gt;,&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Then &lt;code&gt;ARGS_HAS_COMMA(,)&lt;/code&gt; will yield &lt;code&gt;1&lt;/code&gt; (true).&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Putting it All Together
&lt;/h3&gt;

&lt;p&gt;Given those 4 cases, we use a few helper macros:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#define ARGS_IS_EMPTY_CASES(_1,_2,_3,_4) \
  ARGS_HAS_COMMA( NAME5( ARGS_IS_EMPTY_RESULT_, _1, _2, _3, _4 ) )
#define ARGS_IS_EMPTY_COMMA(...)  ,
#define ARGS_IS_EMPTY_RESULT_0001 ,
#define NAME5(A,B,C,D,E)          NAME5_HELPER( A, B, C, D, E )
#define NAME5_HELPER(A,B,C,D,E)   A ## B ## C ## D ## E
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We use &lt;code&gt;NAME5&lt;/code&gt; to paste together &lt;code&gt;ARGS_IS_EMPTY_RESULT_&lt;/code&gt; and the results from the four cases yielding a token like &lt;code&gt;ARGS_IS_EMPTY_RESULT_xxxx&lt;/code&gt; where each &lt;code&gt;x&lt;/code&gt; is either &lt;code&gt;0&lt;/code&gt; or &lt;code&gt;1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If the result is &lt;code&gt;ARGS_IS_EMPTY_RESULT_0001&lt;/code&gt;, it’s the result we care about, that is the first three cases ruled out the three false positives (all &lt;code&gt;0&lt;/code&gt;) and the fourth case is &lt;code&gt;1&lt;/code&gt; (true) meaning &lt;code&gt;__VA_ARGS__&lt;/code&gt; is empty.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;ARGS_IS_EMPTY_RESULT_0001&lt;/code&gt; in turn expands to a &lt;code&gt;,&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;That &lt;code&gt;,&lt;/code&gt; is then passed to the outermost &lt;code&gt;ARGS_HAS_COMMA&lt;/code&gt; that returns &lt;code&gt;1&lt;/code&gt; (true) only if &lt;code&gt;__VA_ARGS__&lt;/code&gt; is empty.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If any of the first three false positive cases return &lt;code&gt;1&lt;/code&gt; (true), then:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;That would yield an &lt;code&gt;xxxx&lt;/code&gt; that is &lt;em&gt;not&lt;/em&gt; &lt;code&gt;0001&lt;/code&gt;, e.g., &lt;code&gt;0010&lt;/code&gt;, &lt;code&gt;0011&lt;/code&gt;, &lt;code&gt;1000&lt;/code&gt;, etc.&lt;/li&gt;
&lt;li&gt;That means the result of &lt;code&gt;ARGS_IS_EMPTY_RESULT_xxxx&lt;/code&gt; doesn’t expand further.&lt;/li&gt;
&lt;li&gt;Which means it doesn’t contain a &lt;code&gt;,&lt;/code&gt;, so the outermost &lt;code&gt;ARGS_HAS_COMMA&lt;/code&gt; returns &lt;code&gt;0&lt;/code&gt; (false) meaning &lt;code&gt;__VA_ARGS__&lt;/code&gt; is &lt;em&gt;not&lt;/em&gt; empty.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  A &lt;code&gt;__VA_OPT__&lt;/code&gt; Substitute
&lt;/h2&gt;

&lt;p&gt;Now that we’ve defined &lt;code&gt;ARGS_IS_EMPTY&lt;/code&gt;, we can use it to implement a &lt;code&gt;__VA_OPT__&lt;/code&gt; substitute when the compiler doesn’t support it.&lt;/p&gt;

&lt;p&gt;The first step is to determine whether the compiler does support &lt;code&gt;__VA_OPT__&lt;/code&gt;.  This can be done by &lt;a href="https://dev.to/pauljlucas/probing-the-compiler-in-autotools-576o"&gt;probing the compiler&lt;/a&gt; with &lt;code&gt;TRY_COMPILE&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight make"&gt;&lt;code&gt;&lt;span class="err"&gt;TRY_COMPILE([__VA_OPT__],&lt;/span&gt;
  &lt;span class="err"&gt;[&lt;/span&gt;&lt;span class="c"&gt;#define VA_OPT_TEST(...) __VA_OPT__(int) __VA_ARGS__
&lt;/span&gt;  &lt;span class="err"&gt;],&lt;/span&gt;
  &lt;span class="err"&gt;[int&lt;/span&gt; &lt;span class="nv"&gt;x1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; VA_OPT_TEST&lt;span class="o"&gt;()&lt;/span&gt; 1&lt;span class="p"&gt;;&lt;/span&gt; VA_OPT_TEST&lt;span class="o"&gt;(&lt;/span&gt;x2 &lt;span class="o"&gt;=&lt;/span&gt; 2&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="err"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;The first test using &lt;code&gt;x1&lt;/code&gt; ensures &lt;code&gt;__VA_OPT__&lt;/code&gt; is empty if &lt;code&gt;__VA_ARGS__&lt;/code&gt; is.&lt;/li&gt;
&lt;li&gt;The second test using &lt;code&gt;x2&lt;/code&gt; ensures &lt;code&gt;__VA_OPT__&lt;/code&gt; is &lt;em&gt;not&lt;/em&gt; empty when &lt;code&gt;__VA_ARGS__&lt;/code&gt; isn’t.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If both declarations compile successfully, Autoconf will define &lt;code&gt;HAVE___VA_OPT__&lt;/code&gt; in &lt;code&gt;config.h&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Given that, we can conditionally define our own &lt;code&gt;VA_OPT&lt;/code&gt; macro based on whether &lt;code&gt;__VA_OPT__&lt;/code&gt; is supported:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#ifdef HAVE___VA_OPT__
# define VA_OPT(TOKENS,...) \
    __VA_OPT__( STRIP_PARENS( TOKENS ) )
#else
# define VA_OPT(TOKENS,...) \
    NAME2( VA_OPT_EMPTY_, ARGS_IS_EMPTY( __VA_ARGS__ ) )( TOKENS, __VA_ARGS__ )
&lt;/span&gt;
&lt;span class="cp"&gt;# define VA_OPT_EMPTY_0(TOKENS,...) STRIP_PARENS(TOKENS)
# define VA_OPT_EMPTY_1(TOKENS,...) &lt;/span&gt;&lt;span class="cm"&gt;/* nothing */&lt;/span&gt;&lt;span class="cp"&gt;
#endif &lt;/span&gt;&lt;span class="cm"&gt;/* HAVE___VA_OPT__ */&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The idea is that, instead of the C23/C++20 way of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;__VA_OPT__&lt;/span&gt;&lt;span class="p"&gt;(,)&lt;/span&gt; &lt;span class="n"&gt;__VA_ARGS__&lt;/span&gt; &lt;span class="c1"&gt;// C23/C++20 way&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we instead have to do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;VA_OPT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="p"&gt;(,),&lt;/span&gt; &lt;span class="n"&gt;__VA_ARGS__&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;__VA_ARGS__&lt;/span&gt;  &lt;span class="c1"&gt;// our way&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It’s unfortunately necessary to have to specify &lt;code&gt;__VA_ARGS__&lt;/code&gt; twice since we need the first &lt;code&gt;__VA_ARGS__&lt;/code&gt; to pass to &lt;code&gt;VA_OPT&lt;/code&gt; to see whether it’s empty and the second &lt;code&gt;__VA_ARGS__&lt;/code&gt; for the arguments themselves.&lt;/p&gt;

&lt;p&gt;Why can’t the &lt;code&gt;VA_OPT&lt;/code&gt; macro simply include &lt;code&gt;__VA_ARGS__&lt;/code&gt; at the end?  While &lt;code&gt;__VA_OPT__&lt;/code&gt; commonly precedes &lt;code&gt;__VA_ARGS__&lt;/code&gt;, it doesn’t always.  For example, a macro that appends a &lt;code&gt;NULL&lt;/code&gt; to a comma separated list like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="cp"&gt;#define func(...) \
  func( (char const*[]){ __VA_ARGS__ VA_OPT( (,), __VA_ARGS__ ) \
        (void*)0 } )
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;has the &lt;code&gt;__VA_ARGS__&lt;/code&gt; &lt;em&gt;before&lt;/em&gt; the &lt;code&gt;VA_OPT&lt;/code&gt; because we want &lt;code&gt;, (void*)0&lt;/code&gt; (with a comma) if &lt;code&gt;__VA_ARGS__&lt;/code&gt; is non-empty and just &lt;code&gt;(void*)0&lt;/code&gt; (no comma) if &lt;code&gt;__VA_ARGS__&lt;/code&gt; is empty.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Why use &lt;code&gt;(void*)0&lt;/code&gt; here instead of &lt;code&gt;NULL&lt;/code&gt;?  Because I want to guarantee a null &lt;em&gt;pointer&lt;/em&gt; is passed here.  If some platform defines &lt;code&gt;NULL&lt;/code&gt; as simply &lt;code&gt;0&lt;/code&gt; (which is allowed), then &lt;code&gt;0&lt;/code&gt; will be passed as an &lt;code&gt;int&lt;/code&gt; since no type conversions take place for &lt;a href="https://dev.to/pauljlucas/variadic-functions-in-c-53ml"&gt;variadic arguments&lt;/a&gt;.  If &lt;code&gt;sizeof(int)&lt;/code&gt; != &lt;code&gt;sizeof(void)&lt;/code&gt;, the result would be &lt;a href="https://dev.to/pauljlucas/undefined-behavior-in-c-and-c-3a20"&gt;undefined behavior&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The fact that the definition of &lt;code&gt;NULL&lt;/code&gt; can vary across platforms is one of the motivations for adding &lt;a href="https://dev.to/pauljlucas/nullptr-in-c23-1cn6"&gt;&lt;code&gt;nullptr&lt;/code&gt; in C23&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If &lt;code&gt;HAVE___VA_OPT__&lt;/code&gt; is defined, we simply define &lt;code&gt;VA_OPT&lt;/code&gt; in terms of the real &lt;code&gt;__VA_OPT__&lt;/code&gt; using the helper macro:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#define STRIP_PARENS(ARG)         STRIP_PARENS_HELPER ARG
#define STRIP_PARENS_HELPER(...)  __VA_ARGS__
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;that strips the outermost &lt;code&gt;()&lt;/code&gt; from its arguments.  (Try a few examples to convince yourself that it does just that.)&lt;/p&gt;

&lt;p&gt;If &lt;code&gt;HAVE___VA_OPT__&lt;/code&gt; is &lt;em&gt;not&lt;/em&gt; defined:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;ARGS_IS_EMPTY&lt;/code&gt; is used to check whether &lt;code&gt;__VA_ARGS__&lt;/code&gt; is empty yielding either &lt;code&gt;0&lt;/code&gt; or &lt;code&gt;1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;That result is then pasted onto the end of &lt;code&gt;VA_OPT_EMPTY_&lt;/code&gt; via the &lt;code&gt;NAME2&lt;/code&gt; helper macro.&lt;/li&gt;
&lt;li&gt;If the result is &lt;code&gt;VA_OPT_EMPTY_0&lt;/code&gt; (&lt;code&gt;__VA_ARGS__&lt;/code&gt; is &lt;em&gt;not&lt;/em&gt; empty), that expands into &lt;code&gt;STRIP_PARENS(TOKENS)&lt;/code&gt; that expands &lt;code&gt;__VA_ARGS__&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If the result is &lt;code&gt;VA_OPT_EMPTY_1&lt;/code&gt; (&lt;code&gt;__VA_ARGS__&lt;/code&gt; is empty), that expands into nothing.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Voilà!  You now have &lt;code&gt;VA_OPT&lt;/code&gt; that can be used in pre-C23/C++20.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;As this extended example shows, the C preprocessor has its own &lt;em&gt;weird&lt;/em&gt; text processing language inside of C.  The two highlighted techniques of:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Argument “sliding” as shown in &lt;code&gt;ARG_11&lt;/code&gt;; and:&lt;/li&gt;
&lt;li&gt;Conditional macro expansion like &lt;code&gt;ARG __VA_ARGS__ ()&lt;/code&gt; by inserting &lt;code&gt;__VA_ARGS__&lt;/code&gt; between a macro and &lt;code&gt;(&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;are often used in advanced macros, so it’s good to become familiar with them.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://gustedt.wordpress.com/2010/06/08/detect-empty-macro-arguments/" rel="noopener noreferrer"&gt;Detect empty macro arguments&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>c</category>
      <category>cpp</category>
    </item>
    <item>
      <title>Autotools Miscellany</title>
      <dc:creator>Paul J. Lucas</dc:creator>
      <pubDate>Thu, 13 Nov 2025 23:53:16 +0000</pubDate>
      <link>https://forem.com/pauljlucas/autotools-miscellany-34hn</link>
      <guid>https://forem.com/pauljlucas/autotools-miscellany-34hn</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;This last part in this series on Autotools will cover miscellaneous things in Autotools that don’t really fit anywhere else.&lt;/p&gt;

&lt;h2&gt;
  
  
  Maintainer Tree
&lt;/h2&gt;

&lt;p&gt;For many software packages, there are typically two versions of its “source tree,” that is the complete set of files that comprise it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A &lt;em&gt;maintainer tree&lt;/em&gt; that contains the complete set of files as checked-out from source control.  For &lt;code&gt;git&lt;/code&gt;, this means there is also a &lt;code&gt;.git&lt;/code&gt; subdirectory containing the files &lt;code&gt;git&lt;/code&gt; needs to do what it does.&lt;/li&gt;
&lt;li&gt;A &lt;em&gt;distribution tree&lt;/em&gt; (or &lt;em&gt;dist tree&lt;/em&gt;) that contains the complete set of files resulting from un-tar’ing a downloaded tar file for a particular version of the software, and perhaps other files.  A dist tree does &lt;em&gt;not&lt;/em&gt; contain a &lt;code&gt;.git&lt;/code&gt; subdirectory, but does contain a pre-built &lt;code&gt;configure&lt;/code&gt; script.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Sometimes when running &lt;code&gt;configure&lt;/code&gt;, you want to be able to perform different actions depending on whether you’re running in a maintainer or dist tree.  To be able to tell, you can add the following early in &lt;code&gt;configure.ac&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight make"&gt;&lt;code&gt;&lt;span class="err"&gt;AS_IF([test&lt;/span&gt; &lt;span class="err"&gt;-d&lt;/span&gt; &lt;span class="s2"&gt;"$srcdir/.git"&lt;/span&gt;&lt;span class="err"&gt;],&lt;/span&gt; &lt;span class="err"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;is_maintainer_tree&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;yes&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is, if the &lt;code&gt;.git&lt;/code&gt; subdirectory is present, assume it’s a maintainer tree.&lt;/p&gt;

&lt;p&gt;What’s an example use?  If your program uses programs like &lt;a href="https://github.com/westes/flex" rel="noopener noreferrer"&gt;Flex&lt;/a&gt; or &lt;a href="https://www.gnu.org/software/bison/" rel="noopener noreferrer"&gt;Bison&lt;/a&gt;, a dist tree will contain the generated lexer and parser C source files, hence neither Flex nor Bison are required to compile your program which makes it easier for users.&lt;/p&gt;

&lt;p&gt;However, a maintainer tree should require Flex and Bison so the lexer and parser can be rebuilt as necessary.  To do this in &lt;code&gt;configure.ac&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight make"&gt;&lt;code&gt;&lt;span class="err"&gt;AS_IF([test&lt;/span&gt; &lt;span class="s2"&gt;"x$is_maintainer_tree"&lt;/span&gt; &lt;span class="err"&gt;=&lt;/span&gt; &lt;span class="err"&gt;xyes],&lt;/span&gt; &lt;span class="err"&gt;[&lt;/span&gt;
  &lt;span class="nv"&gt;flex_min_version&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"2.5.30"&lt;/span&gt;
  &lt;span class="nv"&gt;bison_min_version&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"3.4.2"&lt;/span&gt;

  &lt;span class="err"&gt;AC_PROG_LEX([noyywrap])&lt;/span&gt;
  &lt;span class="err"&gt;AM_PROG_LEX&lt;/span&gt;

  &lt;span class="err"&gt;AX_PROG_FLEX([&lt;/span&gt;&lt;span class="nv"&gt;FLEX&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$LEX&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;,
    &lt;span class="err"&gt;[AC_MSG_ERROR([required&lt;/span&gt; &lt;span class="err"&gt;program&lt;/span&gt; &lt;span class="s2"&gt;"flex"&lt;/span&gt; &lt;span class="err"&gt;not&lt;/span&gt; &lt;span class="err"&gt;found])])&lt;/span&gt;
  &lt;span class="err"&gt;AX_PROG_FLEX_VERSION([$flex_min_version],&lt;/span&gt; &lt;span class="err"&gt;[],&lt;/span&gt;
    &lt;span class="err"&gt;[AC_MSG_ERROR([flex&lt;/span&gt; &lt;span class="err"&gt;version&lt;/span&gt; &lt;span class="err"&gt;$flex_min_version&lt;/span&gt; &lt;span class="err"&gt;or&lt;/span&gt; &lt;span class="err"&gt;later&lt;/span&gt; &lt;span class="err"&gt;required])])&lt;/span&gt;

  &lt;span class="err"&gt;AX_PROG_BISON([&lt;/span&gt;&lt;span class="nv"&gt;BISON&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$YACC&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;,
    &lt;span class="err"&gt;[AC_MSG_ERROR([required&lt;/span&gt; &lt;span class="err"&gt;program&lt;/span&gt; &lt;span class="s2"&gt;"bison"&lt;/span&gt; &lt;span class="err"&gt;not&lt;/span&gt; &lt;span class="err"&gt;found])])&lt;/span&gt;
  &lt;span class="err"&gt;AX_PROG_BISON_VERSION([$bison_min_version],&lt;/span&gt; &lt;span class="err"&gt;[],&lt;/span&gt;
    &lt;span class="err"&gt;[AC_MSG_ERROR([bison&lt;/span&gt; &lt;span class="err"&gt;version&lt;/span&gt; &lt;span class="err"&gt;$bison_min_version&lt;/span&gt; &lt;span class="err"&gt;or&lt;/span&gt; &lt;span class="err"&gt;later&lt;/span&gt; &lt;span class="err"&gt;required])])&lt;/span&gt;
&lt;span class="err"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;a href="https://www.gnu.org/software/autoconf-archive/ax_prog_flex.html" rel="noopener noreferrer"&gt;&lt;code&gt;AX_PROG_FLEX&lt;/code&gt;&lt;/a&gt;, &lt;a href="https://www.gnu.org/software/autoconf-archive/ax_prog_flex_version.html" rel="noopener noreferrer"&gt;&lt;code&gt;AX_PROG_FLEX_VERSION&lt;/code&gt;&lt;/a&gt;, &lt;a href="https://www.gnu.org/software/autoconf-archive/ax_prog_bison.html" rel="noopener noreferrer"&gt;&lt;code&gt;AX_PROG_BISON&lt;/code&gt;&lt;/a&gt;, and &lt;a href="https://www.gnu.org/software/autoconf-archive/ax_prog_bison_version.html" rel="noopener noreferrer"&gt;&lt;code&gt;AX_PROG_BISON_VERSION&lt;/code&gt;&lt;/a&gt; macros are called only when in a maintainer tree.&lt;/p&gt;

&lt;h2&gt;
  
  
  C11 Compiler
&lt;/h2&gt;

&lt;p&gt;To require a C compiler that supports at least &lt;a href="https://en.wikipedia.org/wiki/C11_(C_standard_revision)" rel="noopener noreferrer"&gt;C11&lt;/a&gt;, add an &lt;code&gt;AS_IF&lt;/code&gt; after &lt;code&gt;AC_PROG_CC&lt;/code&gt; in &lt;code&gt;configure.ac&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight make"&gt;&lt;code&gt;&lt;span class="err"&gt;AC_LANG(C)&lt;/span&gt;
&lt;span class="err"&gt;AC_PROG_CC&lt;/span&gt;
&lt;span class="err"&gt;AS_IF([test&lt;/span&gt; &lt;span class="s2"&gt;"x$ac_cv_prog_cc_c11"&lt;/span&gt; &lt;span class="err"&gt;=&lt;/span&gt; &lt;span class="err"&gt;xno],&lt;/span&gt; &lt;span class="err"&gt;[&lt;/span&gt;
  &lt;span class="err"&gt;AC_MSG_ERROR([a&lt;/span&gt; &lt;span class="err"&gt;C11&lt;/span&gt; &lt;span class="err"&gt;compiler&lt;/span&gt; &lt;span class="err"&gt;is&lt;/span&gt; &lt;span class="err"&gt;required&lt;/span&gt; &lt;span class="err"&gt;to&lt;/span&gt; &lt;span class="err"&gt;compile&lt;/span&gt; &lt;span class="err"&gt;$PACKAGE_NAME])&lt;/span&gt;
&lt;span class="err"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To test for &lt;a href="https://dev.to/pauljlucas/generic-in-c-i48"&gt;&lt;code&gt;_Generic&lt;/code&gt;&lt;/a&gt; support specifically:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight make"&gt;&lt;code&gt;&lt;span class="err"&gt;AC_C__GENERIC&lt;/span&gt;
&lt;span class="err"&gt;AS_IF([test&lt;/span&gt; &lt;span class="s2"&gt;"x$ac_cv_c__Generic"&lt;/span&gt; &lt;span class="err"&gt;!=&lt;/span&gt; &lt;span class="err"&gt;xyes],&lt;/span&gt; &lt;span class="err"&gt;[&lt;/span&gt;
  &lt;span class="err"&gt;AC_MSG_ERROR([C11&lt;/span&gt; &lt;span class="err"&gt;_Generic&lt;/span&gt; &lt;span class="err"&gt;support&lt;/span&gt; &lt;span class="err"&gt;required&lt;/span&gt; &lt;span class="err"&gt;to&lt;/span&gt; &lt;span class="err"&gt;compile&lt;/span&gt; &lt;span class="err"&gt;$PACKAGE_NAME])&lt;/span&gt;
&lt;span class="err"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Checking for Functions
&lt;/h2&gt;

&lt;p&gt;If you want to check whether the platform support specific functions, you can use &lt;a href="https://www.gnu.org/software/autoconf/manual/autoconf-2.69/html_node/Generic-Functions.html" rel="noopener noreferrer"&gt;&lt;code&gt;AC_CHECK_FUNCS&lt;/code&gt;&lt;/a&gt;, e.g.:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight make"&gt;&lt;code&gt;&lt;span class="err"&gt;AC_CHECK_FUNCS([geteuid&lt;/span&gt; &lt;span class="err"&gt;getpwuid])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;would check for the existence of the &lt;a href="https://pubs.opengroup.org/onlinepubs/000095399/functions/geteuid.html" rel="noopener noreferrer"&gt;&lt;code&gt;geteuid&lt;/code&gt;&lt;/a&gt; and &lt;a href="https://pubs.opengroup.org/onlinepubs/000095399/functions/getpwuid.html" rel="noopener noreferrer"&gt;&lt;code&gt;getpwuid&lt;/code&gt;&lt;/a&gt; functions.  If present, the macros &lt;code&gt;HAVE_GETEUID&lt;/code&gt; and &lt;code&gt;HAVE_GETPWUID&lt;/code&gt; will be defined in &lt;code&gt;config.h&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Checking for Structure Members
&lt;/h2&gt;

&lt;p&gt;If you want to check whether the platform’s version of a particular &lt;code&gt;struct&lt;/code&gt; contains particular member, you can use &lt;a href="https://www.gnu.org/software/autoconf/manual/autoconf-2.64/html_node/Generic-Structures.html" rel="noopener noreferrer"&gt;&lt;code&gt;AC_CHECK_MEMBERS&lt;/code&gt;&lt;/a&gt;, e.g.:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight make"&gt;&lt;code&gt;&lt;span class="err"&gt;AC_CHECK_MEMBERS([struct&lt;/span&gt; &lt;span class="err"&gt;passwd.pw_dir],&lt;/span&gt; &lt;span class="err"&gt;[],&lt;/span&gt; &lt;span class="err"&gt;[],&lt;/span&gt; &lt;span class="err"&gt;[&lt;/span&gt;&lt;span class="c"&gt;#include &amp;lt;pwd.h&amp;gt;])
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;would check whether &lt;code&gt;struct passwd&lt;/code&gt; contains a &lt;code&gt;pw_dir&lt;/code&gt; member.  If present, the macro &lt;code&gt;HAVE_STRUCT_PASSWD_PW_DIR&lt;/code&gt; will be defined.&lt;/p&gt;

&lt;p&gt;For example, a function that determines the user’s home directory is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;home_dir&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;home&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;getenv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s"&gt;"HOME"&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="cp"&gt;#if HAVE_GETEUID &amp;amp;&amp;amp; HAVE_GETPWUID &amp;amp;&amp;amp; HAVE_STRUCT_PASSWD_PW_DIR
&lt;/span&gt;  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;home&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;passwd&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;pw&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;getpwuid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;geteuid&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;pw&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="n"&gt;home&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pw&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;pw_dir&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="cp"&gt;#endif
&lt;/span&gt;  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;home&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First try the &lt;code&gt;HOME&lt;/code&gt; environment variable; if &lt;code&gt;NULL&lt;/code&gt;:&lt;/li&gt;
&lt;li&gt;If we have &lt;code&gt;geteuid&lt;/code&gt;, &lt;code&gt;getpwuid&lt;/code&gt;, and the &lt;code&gt;pw_dir&lt;/code&gt; member exists, get that value (that’s the user’s home directory).&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;As stated in the &lt;a href="https://dev.to/pauljlucas/autotools-introduction-p9b"&gt;Autotools Introduction&lt;/a&gt;, if you’ve ever compiled and installed most any open-source software, you’ve most likely typed the commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;./configure &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; make &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; make &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and it all just works thanks to Autotools.  Using Autotools, you can create your own programs that are portable to a wide variety of Unix platforms.&lt;/p&gt;

</description>
      <category>c</category>
      <category>cpp</category>
      <category>autotools</category>
    </item>
  </channel>
</rss>
