Modern C for C++ peeps (2019)
C hasn&#x27;t seen a lot of relevant changes since C89 as far as I&#x27;m concerned. The most important for me is definitely declare-anywhere (which had been widely supported prior to standardization in C99), it removes most of the friction when quickly trying out new stuff.<p>Here are some things I almost always do to improve ergonomics (some of this is debatable and none of this should be exposed in a library, at least prefix things)<p><pre><code>    &#x2F;&#x2F; common.h

    #pragma once

    #include &lt;assert.h&gt;
    #include &lt;stddef.h&gt;
    #include &lt;string.h&gt;
    #include &lt;stdint.h&gt;
    #include &lt;inttypes.h&gt;

    #define ARRAY_COUNT(a) ((int) (sizeof (a) &#x2F; sizeof (a)[0]))
    #define STRUCT(name) typedef struct name name; struct name
    #define CONTAINER_OF(ptr, type, member) ((type *) ((char *) (ptr) - offsetof(type, member)))
    #define ALIGN(n) __declspec(align(n))
    #define CACHE_ALIGN ALIGN(64) &#x2F;&#x2F;XXX


    typedef uint8_t u8;
    typedef uint16_t u16;
    &#x2F;&#x2F;...
</code></pre>
Each .c file includes &quot;common.h&quot; as the first thing.
Each .h file has #pragma once at the beginning.<p>ARRAY_COUNT(a) is used to get the capacity of a C array without using defines (which is brittle). Unfortunately it&#x27;s imperfect because code breaks when you change arrays to dynamically allocated buffers and you forget to switch to using dynamic capacity values. This is one case where I&#x27;d like to see some standards update that allows us to improve safety.<p>STRUCT(x) is used to declare struct x with typedef -- I&#x27;ve grown to hate tag namespaces for their boilerplate. I simply uppercase types, and the problem (that struct tags were invented to solve) is gone.<p>Probably I&#x27;ll make something like this very soon to get started<p><pre><code>    static void *__xmalloc(size_t size);
    static inline void __xmalloc_array(size_t size, size_t count) { &#x2F;* ... *&#x2F; }

    #define xmalloc(type) ((type *) __xmalloc(sizeof (type)))
    #define xmalloc_array(type, count) ((type *) __xmalloc_array(sizeof (type), (count)))
</code></pre>
Later obviously more sophisticated allocation is needed, but the point here is how macros can be employed to improve safety and ergonomics. This is how I do polymorphism in C basically, not much more is needed than abstracting over size and maybe alignment for almost everything. In some cases, manually set up v-tables make sense from an architectural perspective.<p>I also often make a very simple &quot;logging&quot; module that does basically printf logging but with \n automatically added and optionally printing out __FILE__ and __LINE__ but without requiring to re-type this all the time. Something like<p><pre><code>    void __msgf(const char *file, int line, const char *fmt, ...)
    {
        va_list ap; va_start(ap, fmt);
        if (__debug) printf(&quot;At %s.%d: &quot;, file, line);
        vprintf(fmt, ap);
        printf(&quot;\n&quot;);
        va_end(ap);
    }
    #define msgf(...) __msgf(__FILE__, __LINE__, __VA_ARGS__)
</code></pre>
Other than that, I like to not think about the language but about what the machine is going to do. How to decrease size of working set and generally speed up the program. How to speed up compilation (don&#x27;t expose all the internals in the .h files). Things like that.<p>There are very few container data structures actually needed, most of the time it&#x27;s just C-arrays, dynamically allocated buffers (pointer + capacity), and some simple queues &#x2F; synchronization primitives. Also linked lists. A favorite of mine are chunk-lists. All of these are very simple to implement -- there is hardly a point of making them in a super-general library, it&#x27;s ok to code them from scratch for all but the smallest projects.