Ask HN: Best book to learn C in 2022?

Ask HN: Best book to learn C in 2022?
Ask HN: Best book to learn C in 2022?

Over the years, I have acquired most of what people call seminal C books, and after reading them, I recommend the following two books to people who already know programming:

Peter A. Darnell and Philip E. Margolis
C – A Software Engineering Approach (3rd ed.)
https://www.amazon.com/Software-Engineering-Approach-Peter-D…

David Hanson
C Interfaces and Implementations: Techniques for Creating Reusable Software (!st ed.)
https://www.amazon.com/Interfaces-Implementations-Techniques…

The first is clearly written and focuses on ANSI C (lexis, syntax and semantics), with a healthy dose of software engineering education along the way, which you may already have. (It does not yet cover the latest ISO changes.)

The second book is a true gem that teaches even seasoned programmers how to implement correct, clean and portable components as libraries in ISO C, with plenty of source code to show how the true masters of the language craft the finest code you can in C.
Most books how fragments of linked list implementations, but only this book shows complete a whole range of important ADTs you cannot live without (e.g. List, Ring, Atom) in C portably, with strong typing, bullet-proof error handling etc. (Hanson is also the author of lcc, a portable C compiler.)

I’m actually surprised that these are both are so little known, and that the second one hasn’t seen more editions.

Ask HN: Best book to learn C in 2022?

One can only learn a language one time, so I’ve got a hard time recommending first books.
But I definitely second the recommendation for the second book here 😉

Hanson’s style is a bit idiosyncratic, with his somewhat Modula-3-ish naming style and literate programming use, but there are a lot of usable data structures in there, and I especially like that the focus is on the module level, not on the algorithms themselves.

Now, having said that, if you’re in a particularly weird sub-domain of C, books from there might supersede or at least color a lot of what you’re doing on a day to day basis. In the days of yore, if you wanted to do Win16/Win32 programming, getting to work with your Petzold early is a good idea, and I would assume the same to be true if you do glib/gobject based Gtk programming.

Two free resources that looked good to me and that I forwarded in the past were “An Introduction to the C Programming Language and Software Design”[1] by Tim Baily and “Beej’s Guide to C Programming”[2]. Those are a bit more concise than your average intro to C book, but for some paths (prior experience, quick headway to C++ etc), they might be sufficient.

[1]: https://www-personal.acfr.usyd.edu.au/tbailey/ctext/ctext.pd…

[2]: https://beej.us/guide/bgc/

Ask HN: Best book to learn C in 2022?

Yes, Absolutely.

However, i am not clear what you mean by “language agnostic”? Obviously Code cannot be language independent but if you can read C then you should be able to translate the data structures/algorithms given in this book to a language of your choice; the difficulty might vary (eg. how would you represent pointers in your language to implement say recursive tree structures?) but it can be done. The data structures/algorithms given in Hanson’s book are “industrial strength” with no missing parts (i.e. no exercise left to the reader). So you get complete code all written in the “Literate Programming” style which takes a little getting used to but is eminently worth it.

Ask HN: Best book to learn C in 2022?

I think it’s a lot less useful for that. The problem is that doing that with C is that you’re already strapping some lead to your feet from the very start, so a lot of what CII does is removing those weights by adding stuff from other languages, either by merit of coding style or new libraries. This would already go to far for a lot of C purists, but it doesn’t totally try to work deny the language you’re working in.

So it does basic error handling & modularization. You get that “for free” in a lot of other languages, or sent in totally different directions anyway (good modularization in OO and/or FP looks somewhat different)

Then there’s a lot of practical data structures and algorithms, with good references to other versions and sources, so both something you could use immediately but also serving as jumping off points to advanced study.

So I appreciate it a lot more in combination with bare bones C than with other languages or even C environments with more standard library functions.

For style & technical guidelines, I think Code Complete is still a great book, or the Pragmatic Programmer. For a trad-C book, maybe even Pike’s Practice of Programming. Avoid anything avuncular.

Of recent years (and not including OO stuff in general), Ousterhout’s “A Philosophy of Software Design” packs a lot in a few pages.

Ask HN: Best book to learn C in 2022?

I would like to add two more to your recommendations;

1) An old classic to understand low-level C “System” interface: The C Companion by Allen Holub.

2) Followup with the more modern Computer Systems: A Programmer’s Perspective by Randal Bryant and David O’Hallaron.

Ask HN: Best book to learn C in 2022?

Despite what many think, I’d say the “The C Programming Language (2nd Edition – ANSI C)” by Brian Kernighan & Dennis Ritchie (K&R) book is still a good introduction to C. It doesn’t go into best practices, or other forms of more advanced C programming, but it teaches C well. I’d pair it with “Understanding and Using C Pointers: Core Techniques for Memory Management” by Richard M Reese to get a good understanding of the pointer underpinnings of C. They are both small, well written and easily digestible.

Ask HN: Best book to learn C in 2022?

I am surprised to see nobody mention Practical C Programming (https://www.amazon.com/Practical-Programming-Does-Nutshell-H…) by Steve Oualline. Before I studied computer science in school, I read this book and successfully taught myself C. Readers here may consider it to be a bit remedial, but if you’re struggling to understand the other recommended books, start here. I attempted to read K&R half a dozen times back then, unsuccessfully. Looking back at it now, I can see why: K&R’s examples are hard for true beginners to wrap their minds around. I now teach C as a part of an upper level undergraduate course; the key to understanding most of C’s counterintuitive behavior is to have a clear understanding of its relationship with memory. In fact, I quite like C. It is simple and elegant in many ways. It is also just super unsafe.

Ask HN: Best book to learn C in 2022?

I recommend “Computer Systems: A Programmer’s Perspective”, as it also give a good explanation about how a computer works under the hoods.

Ask HN: Best book to learn C in 2022?

Follow up question should probably be what is a good IDE setup for plain C. CGO support in VSCode for instance never really worked for me. How well does C/C++ plugin by Microsoft work on linux?

Ask HN: Best book to learn C in 2022?

I’d recommend starting in VSCode with the MS C/C++ plugin (this works across Windows, Linux and macOS) and do the very first steps without a build system (the VSCode C/C++ plugin has some support for building single-file projects).

Or just build in the terminal with:

    cc hello.c -o hello

After those first steps I would recommend installing the MS CMake Tools extensions (https://github.com/microsoft/vscode-cmake-tools/blob/main/do…), this automatically discovers CMakeLists.txt files in the project and then uses those to setup build- and debugging targets and configure the C/C++ language server / Intellisense.

I prefer this setup because it is more ‘transparent’ than a ‘fat IDE’ like Visual Studio, Xcode or CLion, but YMMV of course 🙂

Ask HN: Best book to learn C in 2022?

C Programming: A Modern Approach by King

Edit: I just saw your post history – if you don’t mind me asking what approach/resources did you take with self-teaching yourself?

Ask HN: Best book to learn C in 2022?

The problem with K&R is that it stops at C89, while C99 almost feels like a new and much “friendlier” language (mainly because of compound literals and designated initialization).

(e.g. shameless plug: https://floooh.github.io/2019/09/27/modern-c-for-cpp-peeps.h…)

K&R 2nd Edition is still a very good read of course, but mainly for all the little interesting details that are not directly related to the C syntax, but there really should have been a K&R 3rd Edition when C99 came around.

Ask HN: Best book to learn C in 2022?

Those standards barely change anything. You could discuss the “new features” over the last 30 years in about 10 pages after learning the fundamentals. (10 pages may be high.)

Ask HN: Best book to learn C in 2022?

The C programming language, 2nd edition. Then if you are into BSD/Posix,
Advanced Programming Unix environment. Here’s a course on C programming
for Unix, NetBSD actually. You can install NetBSD with ease.
If not, head to sdf.org and try getting an user account.
You can validate a basic account for cheap. But if you help
the rest of the community with programming under Unix, you may get
a validated account for free.
Do the Unix programming that after TCPL 2nd Ed, BTW.
https://stevens.netmeister.org/631/

Ask HN: Best book to learn C in 2022?

King is good, also Seacord’s books: Effective C and Secure Coding ..

Just ignore suggestions (parroting really) for K&R. It’s probably the worst introduction to C when it comes to good habits, undefined behavior and writing secure code, since it doesn’t even attempt to cover any of these but presents a total facade regarding what programming in C is like.

K&R has created generations of people who call themselves C programmers but are essentially clueless when it comes to deep understanding of the language. Anyone that recommends K&R today is someone you want to avoid taking any advice on programming from.

Ask HN: Best book to learn C in 2022?

It’s hard to take someone seriously or believe that they’ve even read the material when they say stuff like this. K&R (2nd ed.) is constantly calling out undefined behavior, etc. and gives you information you need to start out on a path to be a real stickler about the language.

I spent time on my own learning C from K&R since the CS program at my school didn’t expose anyone to C (or C++) during the first year. Later, in e.g. the computer architecture course or the data structures course when C was being used liberally (and sloppily), and as I was running across lots of C in the wild, I was continually frustrated by the frequency I saw people relying on common-but-undefined behavior that’s explicitly identified as unsafe/unreliable in K&R.

Here’s my advice to the original poster:

If you come across unsubstantiated comments telling you to ignore K&R for all the bad things it contains, then ignore those. Or you can ask for evidence of the bad things they say about it. It’s become a meme in the last few years to write meatless swipes about how bad K&R is, but in all the times I’ve asked for evidence of the things it’s purportedly rife with, no one has ever produced anything. The most egregious thing I know of is that it uses `gets` and `puts` in some places—while taking extra effort to tell you it’s wrong to use them. This is arguably exactly how do-not-do-this wisdom should be imparted.

There is one criticism that holds true about K&R, if you’re being generous (to the naysayers, that is), which is that it really does just teach you C the programming language (with some extra stuff about the UNIX system interface at the end). It’s not a general software engineering book. You don’t learn about build tools or how to structure large, maintainable programs. It exposes you to the fact that C has various nooks and crannies, and it teaches you them, and that’s all it really tries to be.

Ask HN: Best book to learn C in 2022?

Agreed. It’s also worth pointing out that Effective C is the most current in terms of being up to date with present-day C standards.

K&R is so obsolete it’s now wrong about some things. It’s a pretty good read sometime down the road – when you can recognize its shortcomings – but it’s not a book from which to start learning C.

Ask HN: Best book to learn C in 2022?

K&R was a very interesting read for me, but not actually for the description of the C language, but because it describes why certain features in C look the way they do, and because it is also surprisingly and refreshingly critical towards the language (e.g. it basically admits that the function pointer syntax was a mistake).

It also corrected my oversimplified view that C is a simple and small language simply because it is an old language, but it was already a simple and small language compared to other languages in the late 60’s and early 70’s ‘programming language zoo’.

All the code examples are deeply rooted in the 80’s programmer mindset though, and are not very relevant for “modern C”.

Ask HN: Best book to learn C in 2022?

Well when K&R C was released the compiler landscape did look very different. I’m sure back then you could get away with a lot more UB than nowadays, where LLVM and GCC will use every millimetre of leeway you give them to break your programs.

Ask HN: Best book to learn C in 2022?

The memetic nature of the term “K&R” also seems to have resulted in a lot of people who think Kernighan created Unix, and don’t know who Ken Thompson is.

Ask HN: Best book to learn C in 2022?

Learning more programming languages will make you a better programmer irregardless as it will expose you to different programming patterns, paradigms and the limitations/improvements that result from those.

This is especially true for a popular language, no matter if the language is considered “good” or not. This allows you to understand the libraries/code you will encounter down the road.

I would only discourage one from learning a programming language which is tied to a specific commercial product (most of those just die in the same niche and thus lack the practical utility).

Read More

Share:

Leave a Reply

Your email address will not be published. Required fields are marked *