5 ms·
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
by jll29 4y ago
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-Darnell/dp/0387946756 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-Creating-Reusable/dp/0201498413 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 4y agoOne 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.pdf https://www-personal.acfr.usyd.edu.au/tbailey/ctext/ctext.pd... [2]: https://beej.us/guide/bgc/ https://beej.us/guide/bgc/
- tdrgabi 4y agoWould David Hanson's book be useful for non C programming? Will it help me understand how to build reusable, language agnostic, code?
- mhd 4y agoI 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.
- rramadass 4y agoYes, 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.
- rramadass 4y agoI 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.
- deleted 4y ago[deleted]
- AlexeyBrin 4y agoI 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 ?
- deleted 4y ago[deleted]
- chubot 4y agoI 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
- adalacelove 4y ago> 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! I committed atrocities until I realized this. C is not suitable for generic programming.
- chubot 4y agoYup, same! And I just looked it up -- C was invented BEFORE abstract data types were! This explains a lot. Programmers think "hash table", stack, queue, set, etc. are "the way you program", because that's how it's taught in CS 101. It's also asked a lot in interview questions. But that line of thinking was invented AFTER C. I recall that Liskov did foundational work on ADTs, and Wikipedia agrees Programming with abstract data types (Liskov and Zilles, 1974) https://dl.acm.org/doi/10.1145/942572.807045 https://dl.acm.org/doi/10.1145/942572.807045 i.e. the origin of abstract data types is in this work on the CLU language. Her 2008 Turing Award cites this work: https://amturing.acm.org/award_winners/liskov_1108679.cfm https://amturing.acm.org/award_winners/liskov_1108679.cfm At MIT she led the design and implementation of the CLU programming language, which emphasized the notions of modular programming, data abstraction, and polymorphism ----- Meanwhile C was developed around 1972-73 to make the Unix kernel portable. It's a minimal layer meant to generate machine different machine instructions that will work on machines; it doesn't feature strong abstraction. So what Hanson was trying to do is to add abstraction to C, but you need a language more like CLU or C++ to do that. Arguably Bjarne was "doing it right" -- actually changing the language. ---- HOWEVER I also had a recent experience that vividly shows the folly of ADTs even in C++, a language designed for them. In C++, unordered_set<void star>::insert() is shockingly slow -- slower than malloc(1) !!! And it is slow BY THE SPEC. Because of the abstract operations that the C++ standard requires (e.g. iterators and invalidation), you have to use a slow closed-addressing/linked list implementation that allocates for EVERY element !!! I hit this when working on the garbage collector for https://www.oilshell.org/ https://www.oilshell.org/ -- we're making a fork() friendly collector (no intrusive mark bits) that works with arbitrary addresses returned by malloc. It made some of our benchmarks 10x slower in TOTAL runtime! Best comment I found that explains it: https://old.reddit.com/r/programming/comments/5pwgtn/hash_map_benchmark_stdunordered_map_seems_very/ https://old.reddit.com/r/programming/comments/5pwgtn/hash_ma... Chrome message from 2017: https://groups.google.com/a/chromium.org/g/chromium-dev/c/rdxOHKzQmRY?pli=1 https://groups.google.com/a/chromium.org/g/chromium-dev/c/rd... So basically the hash table in C++ is a shockingly bad default, and it has to with a bad ADT design. Doing ADTs in C is even worse. Hanson's book is more like a thought experiment in merging two distinct lines of thinking -- not something you should actually use and base your code on!! ---- This would be a good blog post -- I would title it something like C Was Invented Before ADTs, and C++ Isn't Great Either
- belter 4y agoFor 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/0393979504 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 https://www.manning.com/books/modern-c and "Effective C" - https://nostarch.com/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-Kernighan/dp/0131103628 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++ :-)
- deleted 4y ago[deleted]