Jens Gustedt's Blog

programming in modern C


syntax

  • Contracts for C

    C++ seems to finally converge with their contracts proposal, https://wg21.link/p2900. I decided to give it a try and come up with ideas how such a thing would look for C. This is in early stages, not a full proposal yet, and still would need implementation by some of the major compilers. In particular, the C++… Continue reading

  • Tail recursion for macros in C

    In view of the addition of __VA_OPT__ first to C++ and now to C23, there had been interest in extending the C preprocessor to include recursion. The basic idea would be that __VA_OPT__ can be used as a test within a macro such as in the following Indeed, the __VA_OPT__ clause only inserts its contents… Continue reading

  • The deprecated attribute in C23 does much more than marking obsolescence

    You may already have heard that C23 proposes a new syntax feature called attributes and that one of the standard attributes in C23 is called deprecated. Actually, we got this whole attribute thing and also some of the standard attributes from C++, where they had been introduced in C++11 (I think). One of the uses of… Continue reading

  • testing compile time constness and null pointers with C11’s _Generic

    Sometimes in C it is useful to distinguish if an expression is an “integral constant expression” or a “null pointer constant”. E.g for an object that is allocated statically, only such expressions are valid initializers. Usually we are able to determine that directly when writing an initializer, but if we want to initialize a more… Continue reading

  • the C data model

    For beginners in C the different terms that C applies to data items are often confusing. There are variables, literals, integer constants, compound literals, expressions, lvalues, rvalues… In this post I will try to put a bit of systematic into this and at the end move towards a simplified model. First of all any sort… Continue reading

  • try, throw and catch clauses with P99

    Before C11 implementing try/throw/catch clauses with C alone was difficult. C lacked three features for that. First, there was no clear concept of threads. A throw that wants to unwind the stack to a calling function would have to capture the nearest try clause on the stack by inspecting a global variable. If you are… Continue reading

  • Emulating C11 compiler features with gcc: _Generic

    Besides the new interfaces for threads and atomic operations that I already mentioned earlier, others of the new features that come with C11 are in reality already present in many compilers. Only that not all of them might agree upon the syntax, and especially not with the new syntax of C11. So actually emulating some… Continue reading

  • Statement unrolling with P99_FOR

    Specifying code that does same or equivalent things to a list of variables, tokens, items or so is an important source of annoyance. It can easily make code unreadable and difficult to maintain. E.g consider the following Understanding what that expression does is difficult in one glance and updating it is error prone. Adding another… Continue reading

  • const and arrays

    Qualifying a type with const may have some surprises. Consider the following where toto is an opaque typedef that we don’t control. The question would be if the initialization of p is valid or not? From the way I pose that question, you have probably guessed the answer: it depends. Continue reading

  • Handling control flow inside macros

    When people program macros that they want to be executed anywhere where a statement could be, they often use the construct. This construct has a big disadvantage in that it may change control flow in an unexpected way when you’d use it as a generic macro tool to collect statements: Continue reading

Design a site like this with WordPress.com
Get started