Best book to learn C in 2022?
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?
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.
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.
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.
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.
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/
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?
"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
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.
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.
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.
I suggest OP rewrites their post in Rust.
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
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
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.
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.
I recommend "Computer Systems: A Programmer's Perspective", as it also give a good explanation about how a computer works under the hoods.
K&R is a bit more intermediate. Come back to it when you're more experienced.
Besides K&R and Modern C, I like the classic Expert C Programming: Deep C Secrets by van der Linden.
As someone who teaches C to professional engineers, I'd recommend "Hacking: The Art of Exploration" by Jon Erickson. The first 100+ pages are the most succinct coverage of C I've ever come across (if you can already program in other languages).
Also, by following many of the exploits covered in the book, you get a real grasp of what is happening at the machine level - which is one of the major reasons you'd choose C over something more modern.
Not to derail the OP too much, but I've also been C-curious for a while, but specifically for writing wrappers C++ projects (for FFI calls from rust, swift, etc.).
I've written a few such wrappers that work, but I have no idea what I'm doing WRT const void * et al. and would love to understand it a little better.
Are there any of these resources that would help me understand how to write better FFI code in C, while maybe skimping on details that are less important if I don't plan to write much code that is "C first"?
Thanks for any suggestions. Apologies in advance for wrong terminology and generally being a noob.
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?
Mentioned by most, still another vote for K&R "The C Programming Language" - for the sheer joy of it! It still puts a smile on my face recalling how much fun it made learning C. :-) Don't think I've read as measured and not-boring for another language since. Close to as much fun to read was maybe Steve Maguire's "Writing Solid Code". Not strictly about learning C, but the examples are all in C. (some people dislike it's K&R not ANSI C)
I'm a C++ programmer that's not done anything directly in C. I stumbled by this book Fluent C By Christopher Preschern just published this month and I thought the organization looked rather interesting. It covers many programming concerns and details several patterns for dealing with them. One thing that's neat is it often refers to open source projects where the pattern is applied so one could go take a more detailed look.
If you don’t mind, I’d like to ask you why you want lo learn C. I’m asking because I’d like to improve my C but I can’t decide between that and learning Rust.
I highly recommend Head First C. Don’t be taken aback by its appearance. It’s an excellent book that drives the concepts home.
Learn C by hand and just writing toy programs. Then read Cert C to learn coding patterns. C requires unique coding patterns compared to other languages, most relating to safety or language features.
The first time I was interested in programming I started learning C. I was 14 and what I used was "The C Programming Language" book. It was a great experience,
> I know a few other languages
Just skip the tutes and books, and read drafts of the ISO C standard. C99 is the most widely used. For newcomers, C11 might make sense.
Here: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
Absorb that and then you can play the Game of Spot The Error in C books and tutorials. Not to mention git repos of C programs.
Any resources that take a project based approach to teaching C?
I find just learning language features in isolation doesn’t give a complete picture.
'Let us C' is a very good big book which you can brush through in a week since you already know a language. Easy English.
Many people recommend K&R but I'll only keep that book as a reference and not as a book I should read. You can read the link https://www.narendravardi.com/bad-book/ to understand why I don't recommend K&R.
C the complete reference - it's part tutorial, part reference, part project book. At the end, there is project where it walks you though making a mini-c interpreter. That project is very approachable and to be honest - that alone is worth the price of the book.
these comments are gem itself
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.