Hacker News new | ask | show | jobs
by Fredej 1282 days ago
Oh man, no defer? Bummer. I'm switching from C++ to C recently and there sure are a bunch of things I'm missing!
2 comments

I made a small pre-processor that implements block-scoped defer in C, by transforming the source to add the deferred code at each exit point in a function. It is called Cedro: https://sentido-labs.com/en/library/cedro/202106171400/#defe...

That link goes directly to the relevant section in the manual.

It includes a cc wrapper called `cedrocc` so you don’t need intermediate files, and it works hard to produce clean code for the generated parts. The rest is not modified at all. The goal is to be useful even if you only use it once to generate that repetitive code.

The pre-processor `cedro` depends only on the C standard library, and `cedrocc` depends on POSIX.

Uh, not to be That Guy, but what did you expect? Just compare K&R [1] and Stroustrup (any edition) [2] next to each other, and you will get a pretty strong hint of C being a smaller language. That's kind of the point, or it used to feel like it was anyhow.

[1]: https://en.wikipedia.org/wiki/The_C_Programming_Language

[2]: https://www.stroustrup.com/4th.html

Being a small language is not an excuse. Scheme is a small language, still has dynamic-wind (since R5RS, so no spring chicken). Smalltalk is a tiny language, but has BlockClosure#ensure:.

You can have basic safety and QOL features without making the language a monstrous beast. Hell, you can cut old garbage like K&R declarations or digraphs to make room. You can even remove iso646 and most of string.h as a gimme.

First of all, if you are comparing K&R against Stroustrup, then you should use C++ARM, not later editions.

Secondly, Bjarne's books include the overview of the standard library, which neither K&R nor its ANSI C revision do.

Third, since everyone keeps using compiler extensions in C, moreso than in C++, those should be included as well.

At least in my ancient copy (second edition, from 1988 I think) K&R most definitely has an overview of the standard library, in Appendix B.
I stand corrected on that matter it seems.
I mean, it's not that I didn't expect changes or didn't expect the language to be smaller - it's just that there definitely things that I would want to bring with me over to C. One thing I really like is RAII - that I can ensure that things are cleaned up in a known, once-defined fashion and I don't have to worry about it everywhere I use a given object. I also generally like using early returns, which is somewhat more complicated with C, as I may need to have more cleanup code around. It can be somewhat mitigated by coding more functionaly and input the necessary parameters to a function, so I can have a different function just doing allocation and deallocation. But still, it's more verbose.

`defer` would to some degree solve that issue.

Similarly, I've been missing nullptr, just for the expressiveness. I like that C23 now includes it :)