Jens Gustedt, a C standards contributor, writes about the C language, upcoming C standard revisions, and the book Modern C.
HN commenters recommend his Modern C book as an opinionated, sane guide for learning C, and cite his posts on unsigned loops and variably modified types.
I’m actually implementing that right now for my own use, as a pre-processor.
There is a much more advanced design and implementation at “A defer mechanism for C” (December 2020): https://gustedt.wordpress.com/2020/12/14/a-defer-mechanism-for-c/
For my own purposes, I think I can live without handling stack unwinding so I continue working on my pre-processor.
Since the pre-processor is not yet finished, there I use a vector¹ of {.pointer, .destructor} where I put objects after initialization, with one macro at the end of each managed scope that calls the destructors for that scope in reverse order, then another macro meant for the function exit points that calls all the destructors in that vector. This has been built many times before by other people of course, it’s just an exercise to see which difficulties arise.
¹ Vector, growable array: I did my own trivial type-generic growable array, with the classic {.capacity, .len, .items}, but again there are many previous implementations. The two I’ve found more interesting are:
- “C Template Library (CTL)”: https://github.com/glouw/ctl
- “Klib: a Generic Library in C”: https://github.com/attractivechaos/klib/
I follow regularly these people, these are mostly slow-going blogs or websites:
1. Fabrice Bellard: https://bellard.org/ This is not really a blog, but a traditional website with links to the work of its author (who is probably one of the top programmers ever). It updates rarely, but when it does, it is to expose yet another monumental piece of computing work that will blow your universe.
2. Linus Akesson: https://www.linusakesson.net/ He's always onto some funny crazy things: obfuscated programming, underhanded programming, c64 emulation, chiptunes...
3. Uriel l'Étranger: http://cat-v.org/ The only self-described "philosopher of software" that I have ever seen. His website is of course not updated anymore, but you can re-read it many times and always learn something new.
4. Ted Unangst: https://flak.tedunangst.com/ Nice to follow some cool news about openbsd development
5. Andy Chu: http://www.oilshell.org/blog/ Who has embarked in an amazing quest to turn the unix shell into a cleaner tool
6. Jens Gustedt: https://gustedt.wordpress.com/ If you really want to be up to date about the evolution of the C language
I wouldn't call it a "need", more like "occasionally desirable". The short version is that it's a convenient method of letting the compiler handle resource management when you have a variable number of values and sufficient stack space. As others pointed out, it's particularly handy for multi-dimensional arrays.
There are also some possible applications here:
http://gustedt.wordpress.com/2011/01/13/vla-as-function-arguments/
In fairness, I've used them only rarely, but those few times I've used them have saved me a bit of grief.
I could easily live without them if required...
As Jens Gustedt points out[1], the following intentional unsigned overflow works perfectly for downwards iteration (even when length is 0 or SIZE_MAX), though it looks a bit confusing at first:
for (size_t i = length - 1; i < length; i--) ...
You are also free to start at any other (not necessarily in-bounds) index, just like with ascending iteration.
[1] https://gustedt.wordpress.com/2013/07/15/a-praise-of-size_t-and-other-unsigned-types/
I think "defer" is coming [1], along with other goodies that simplified the life of C++ developers, but time will tell.
I really hope they approve the following:
* auto
* constexpr
* lambda function
* __typeof__ used as typeof; it behaves like decltype
It would lead to cleaner code without excessive macro magic; only where would be needed.
[1] https://gustedt.wordpress.com/2020/12/14/a-defer-mechanism-for-c/
Beej's guides are the best way to get started quickly (i.e. in the time frame of a university course). For a more thorough introduction, one should probably read "Advanced Programming in the UNIX Environment" and "UNIX Network Programming", both from Richard Stevens.
Jens Gustedt has also written a rather nice ebook on how to program in modern C: https://gustedt.wordpress.com/2016/11/25/modern-c-is-now-feature-complete/