top | item 33130533

Ask HN: Best book to learn C in 2022?

283 points| CodeSgt | 3 years ago

I know a few other languages but have really been wanting to learn C. I see a lot of books all with a lot of praise and it's difficult choosing one.

Any recommendations?

160 comments

order
[+] jll29|3 years ago|reply
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.

[+] mhd|3 years ago|reply
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/

[+] chubot|3 years ago|reply
I read the Hanson book ~10 years ago (he was briefly a coworker). It's interesting, and maybe worth checking out for experienced programmers, but NOT for someone who wants to learn C in 2022 and be productive.

The style doesn't match any C code you're likely to see. It basically presenting an STL-like set of data structures and standard library in C, not C++.

That style of programming isn't idiomatic in C. One thing that took me a long time to realize is that C is NOT a modular language! The best way to use C is to roll your own (or copy and paste) data structures for your problem, not try to use canned ones!

If you want the latter approach, C++ is better. It has better support for type safety, abstraction, and modularity (at great cost).

i.e. if you compare code of commonly used codebases like Lua, CPython, sqlite, Redis, BSD kernels, Linux, etc. you will see patterns, and they don't look anything like Hanson's book

[+] belter|3 years ago|reply
For the professional programmer, knowing other languages and getting into C, besides the two great recommendations above, would recommend:

"C Programming: A Modern Approach, 2nd Edition" - https://www.amazon.com/C-Programming-Modern-Approach-2nd/dp/...

Note the above book is good for its quality as a reference, but not enterily considered as "modern". For a more updated approach I would also recommend:

"Modern C" - https://www.manning.com/books/modern-c

and

"Effective C" - https://nostarch.com/Effective_C

The first book on the language:

"C Programming Language, 2nd Edition" - https://www.amazon.com/Programming-Language-2nd-Brian-Kernig...

Is interesting from a historical perspective, but considered as not a recommended resource for learning C.

Edit: If you are learning C there is something quite important you need to know... C and C++ are two completely different languages, and that is how you should approach your learning. In other words, don't go learning the C in C++ :-)

[+] rramadass|3 years ago|reply
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.

[+] AlexeyBrin|3 years ago|reply
I found the first book in a second hand shop a few years ago, but unfortunately it didn't came with the diskette. Any change you can upload the code from the diskette somewhere or point me to where I can find it ?
[+] wmwragg|3 years ago|reply
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.
[+] Syzygies|3 years ago|reply
I read the first edition of K&R when it came out, and switched to C for a computer algebra system that got me tenure as a math professor. K&R is a religious scripture, and remains the benchmark against which I measure all other programming books. It's in the same league as the aspirational SICP, and a far easier read.

In college I'd programmed Fortran on punched cards. APL was like dropping acid, and I worked one summer as a commercial APL programmer, but it wasn't appropriate for what I needed. Pascal didn't quite fit either. C was in that context mind-blowing. The future had arrived! I had full access to the machine!

If one is learning to become a plumber, one might not be so interested in how plumbing worked in 1840. If however, one is a herding dog, and needs serious work to avoid getting bored and destructive, reading K&R would put C in historical context.

Later, as I depended on C, I bought each edition of C: A Reference Manual by Harbison and Steele, and read it as if I were studying to be a lawyer.

I now program in Haskell, and wish I was equally versed in Clojure and Rust. If I were returning to C, I'd instead choose Rust. For my work, I have a free choice.

[+] WastingMyTime89|3 years ago|reply
I will be a contrarian - but as you pointed we are many - and recommend against K&R. C is a simple language to learn but a hard language to master.

C has few concepts. You can learn to write C in an hour with a couple of web pages. Then you have to learn how to implement the most useful data structures but that’s not difficult either. What’s hard is writing safe code in a maintainable way and K&R does very little to teach you that properly.

[+] tchaffee|3 years ago|reply
> K&R

I've read a lot of books about programming over the many decades I've been doing this, and the K&R book still stands out as one of the best examples of how to do a good job of documenting the tool you've just built. The K&R book gave me the heuristic of "read the book by the folks who wrote the language". That heuristic turned out to be mostly wrong. But the book has stood the test of time.

[+] fargle|3 years ago|reply
Here's another vote for K&R. If you want to understand why C and UNIX were so successful, it comes down to simply excellent communication. And remember - no web, no internet, no WYSIWYG word processors, etc. at the time.

To appreciate C properly, I think at some point you need to become a bit of a historian. Look at BCPL. Look at what assembly listings looked like or old Fortran. Read some of the very early papers from C and UNIX history. Check out Lions' commentary on UNIX. When you see a little of the problem C was invented to solve, it becomes a lot easier to understand why it is what it is. There's a lot of angst about why C allows Bad-Thing-X or doesn't have New-Fancy-Thing-Y. Nobody seems to remember what a change it brought to writing systems code.

Then you can compare it to things written in modern C (C99+) and see that yes it has evolved quite nicely and can, if used correctly, be a nice application language too.

[+] talkingtab|3 years ago|reply
If you are starting with C I second this. C is a really good language to learn because it is not as far from what a cpu does. You end up understanding more of the workings. When someone says "garbage collection" you understand. In my opinion understanding an assembly language and C are fundamental to programming.
[+] apricot|3 years ago|reply
Fully agree that reading K&R is a must, because it puts C in context, it gives you the background that explains why C is like it is. Also, as a non-trivial bonus, it teaches you, by example, how to write great technical documentation.
[+] raddan|3 years ago|reply
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.
[+] fhchl|3 years ago|reply
I recommend "Modern C" by Jens Gusted.

https://www.manning.com/books/modern-c

The C language can be quite tricky to navigate (at least to me) and this book helped me a lot to understand writing idiomatic code.

[+] taxcoder|3 years ago|reply
Agreed. The tool chain help in here is very useful for a beginner.
[+] anthk|3 years ago|reply
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/
[+] equilibrium|3 years ago|reply
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?

[+] stusmall|3 years ago|reply
This is my favorite of the bunch. I remember picking it up as a long time C dev hoping to learn a new, modern approaches. It helped hammer home that you can only modernize a 50 year old language so much. I didn't come away with from it with a lot of new tricks or techniques. It's just a dated language that has a very specific time and place and when using it, you have to deal with its age.
[+] CodeSgt|3 years ago|reply
Well for self-teaching Python and JS/TS/html/CSS I mainly just followed YouTube videos with a healthy dose of side projects.

For learning Elixir I bought "Elixir in Action" and then supplemented with my own side projects.

[+] throwaway2037|3 years ago|reply
I needed to re-learn C programming for a mid-career change. This book was a life saver. I will never forget how details the explaination was about printf. That book is incredibly thorough.
[+] miguendes|3 years ago|reply
+1.

King’s is amazing, IMO. Very didactic and covers C in depth.

[+] randcraw|3 years ago|reply
K&R 1e, 236 pages

K&R 2e, 272 pages

King, 832 pages

[+] photochemsyn|3 years ago|reply
"C in a Nutshell 2nd Ed" (O'Reilly, Prinz & Craqford, 2015) is a good reference although maybe not the best for a walk-through learning experience. It also has good chapters on tooling (gcc, make, gdb).

There's a recent book out I came across called "Bare Metal C" (No Starch Press, Oualline, 2022) which unpacks embedded programming in a very readable manner. I imagine a lot of, if not most, C programming these days is done in the low-level embedded world, and this book clears up a lot of the mysteries.

https://nostarch.com/bare-metal-c

Also it never hurts to look at a good open-source codebase written in C, for example the SQLite code is worth looking at (if a bit overwhelming):

https://github.com/sqlite/sqlite/tree/master/src

[+] mm007emko|3 years ago|reply
As other mentioned, The C Programming Language (2nd Edition - ANSI C)" by Brian Kernighan & Dennis Ritchie (K&R).

I learnt from this book. It's still good for the basics.

[+] sureglymop|3 years ago|reply
If you have ADHD like me I would recommend "Head First C" from o reilly. Was very visual and covered a lot. Then I continued with The C Programming Language. Sadly I haven't found something similar for rust yet.
[+] UnpossibleJim|3 years ago|reply
it's the dirty little secret in my programming library but these have small, digestible chunks that I can read or do in 5 to 10 minutes at a time and then walk away and do something else (or do more, if my brain is firing right that day).

I never used to need to study this way when I was younger, but things change now that I'm older and I've adapted =)

[+] readme|3 years ago|reply
I read K&R 20 years ago and today it's still the closest programming book to my heart -- yeah there's more modern choices but the exercises are unmatched, it's short, and it's one of the most fun books you'll ever read.

The standards for C99, C11 are open. Look up what you don't learn in them and grab one of the other more modern suggestions from here for that, but K&R is a must have for any C programmer.

You probably won't be able to put the book down if you start reading it.

[+] squarefoot|3 years ago|reply
Technical aspects aside, K&R has what some modern books seem to lack: a really clear and comprehensive layout. I've picked up a few modern books and immediately noticed how they make use of thinner (thinner != smaller) fonts that make then hard to distinguish. At first I thought it was my fault for being old and wearing glasses, but as soon as I opened back some other old books like the K&R, the Niklaus Wirth (Algorithms+Data Structures = programs) and a couple other ones, I could read again without effort at the same distance wearing the same pair of glasses. Why do they use fonts that thin today?
[+] iExploder|3 years ago|reply
I suggest OP rewrites their post in Rust.
[+] nix23|3 years ago|reply
I suggest you first rewrite a operatingsystem in rust, maybe ask the openbsd community.
[+] tmtvl|3 years ago|reply
I personally quite like James Aspnes' Notes on Data Structures and Programming Techniques, it teaches you enough C to be dangerous and proceeds on a whirlwind tour through most of the data structures you'll encounter in the wild.

You can find it here: https://cs.yale.edu/homes/aspnes/classes/223/notes.html

[+] _osorin_|3 years ago|reply
I would like to take it a step further and ask a question that has been bothering me a while. On my time in the academy I studied the following two books (regarding C):

[1] Advanced Programming in the UNIX Environment https://www.amazon.com/Advanced-Programming-UNIX-Environment...

[2] C Programming Language https://www.amazon.com/Programming-Language-2nd-Brian-Kernig...

In combination with other classes (and books) on networking, operation systems, data structures we covered a big variance of use cases with C. My question is: How do I take this to the next level? For example I feel I never missed a concept from those classes but when I see C code posted on a thread here it's probably something completely unreadable and complex. Can anyone provide resources to advance a little? What should I study if my goal is to make good and useful modern C software?

I feel like my typing is a bit abstract but I will be happy to clarify.

PS Yes, I've heard of C++ and Rust ;P

[+] zh3|3 years ago|reply
[1] is a very good book - it's not really one for learning C though, it's more for when you know C and you need to use it in - well - an advanced unix environment. Sort of like you shouldn't be learning how to use a scalpel in an operating theatre :)
[+] pjmlp|3 years ago|reply
Regarding K&R C, you should read it for its historical relevance to the C programming language, that is about it, it isn't 1980 any longer.

Secure C from Oreilly and Modern C from Manning are more relevant in 2022.

Then have a look into Frama-C and SAL security frameworks, MISRA C, and CERT C coding standards.

[+] gizlu|3 years ago|reply
Hm, C language (itself) hasn't got much major changes since ANSI standardization. I would say that one is able to grasp changes relevant to him, by whipping through the changelog for 10-15 minutes

Edit: If you are critizing K&R for not teaching how to write safest code then, well, you are right. It is simply not meant to do that

[+] jmercan|3 years ago|reply
I would say "Effective C" is a good book (and Robert is awesome) but personally I feel like I learned the most by writing and disassembling small quines under different compilers and flags more than anything else.

Also, reading through some codebases gave me good (and horrible) ideas and habits. However, C projects are usually messy and I am not sure whether recommending it would be helpful or just confusing. But I would say TenDRA, BearSSL, pkgconf, s6, kivaloo, apk-tools, libsodium, musl, OpenSSH and cosmopolitan libc are worth checking out.

Finally increasing my tiny understanding of architectures, memory models and decades of safety issues was significantly helpful as well.