C Just what is this thing called ‘Modern C’?

In danger of immediately sounding “pompous”, I will bravely postulate, that most developers just simply do not understand how distant is so-called “Modern C”, from what they usually perceive as C programming language.

Allow me to push you into the deep end of the pool, straight away. Here is one sample I prepared earlier:

Godbolt is here.

Here is my formula:

Modern C  = Standard C + ( GNUC | clang )

  • Standard C is C17 as of today, managed by the WG14 committee.
  • GNUC is well-known (and still) the reference compiler for the C programming language.
  • clang is a “C language” compiler of the LLVM suite

So why this “GNUC or clang” part of the formula? Simply because these two compilers have developed a powerful set of C extensions still staying true to the standard as much as possible. The reasons are purely pragmatic. Dramatically improved code resilience, safety, and scalability.  And developed by teams understanding it is a waste of time trying to replace C.

We shall use SystemD open-source project as an optimal use case. It is a well-known, complex, mature, and brilliant C program. Studying its source will make you understand (sooner or later) what is Modern C. What are the reasons for the existence of (in this case) clang attributes, and how that philosophy makes SystemD first possible and second, the software part that is crucial for modern Linux. (hint: yes clang and GNUC attributes are almost the same.)

There are many seemingly equally fit for the task, programming languages in existence; yet somehow modern C is the most feasible one for the task at hand: mission-critical system software. The keyword is: feasible.

I might think in that little sample of mine I have managed to show how are attributes, macros, and the rest of the C paraphernalia used today. Where the key assumption is: you want the fastest and safest possible core code, but still not in assembler, and actually maintainable by someone years or decades after you. As a part of some living systems. Hint: no I do not claim that little code is something never seen before. I simply claim it shows the power of modern C.

For now and for the curious, I will present (hand cleaned) the output of the precompiler from the code behind that link:

Quite interesting isn’t it? (hint: here is that “auto-free” discussion; also the char array is the same as float obviously but for a type char)

As “we go along”, I will be quite happy to explain and answer all your questions.

PS: Please see here the discussion on the size of the VLA struct to be allocated.

Why would anybody code like this?

Safe, resilient, and fast modern C code.  Resilience is economical. Debugging takes time and time is money.