6 ms·
Ask HN: I use and love Python. What's the best book to learn C ?
So it's 2010. I know enough C to be able to read and patch small apps where I find bugs, but I'd like to sit down and properly learn the language, in a way that encourages neat, usable code for my colleagues, that meets whatever the current standards are for C and covers modern topics like threading. I'm planning on using this to develop Python modules or hack on small Unix tools.
I've previously read K and R and found it understandable but a little boring. I really like the Head First series (but alas they have no C book) or anything with lots of examples.
What's the best introduction to C book you'd recommend?
Edit: Thankyou to everyone who's responded.
- Kliment 17y agoI'd suggest you start dissecting small programs written in C. Those UNIX tools you were talking about? Grab some UNIX tools, see how they work. Dissect cat, grep, etc. It's the best way to learn.
- nailer 17y agoHmn, good point. I remember wanting to see how top got some of it's stats a while ago and wandering through the C source. Something simpler would be pretty quick. fetches the coreutils source Thanks.
- mdg 17y agoPlan 9 versions of these tools are straight forward: http://plan9.bell-labs.com/sources/plan9/sys/src/cmd/ http://plan9.bell-labs.com/sources/plan9/sys/src/cmd/
- deleted 17y ago[deleted]
- tptacek 17y agoI vividly remember C "clicking" for me with the "getpwnam()" function. Which is to say, go to /usr/share/man/man3, pick an interesting function, and write the (bad) 20-line program that makes it work.
- jshen 17y agoi don't know that I'd start or even look at grep ;)
- Kliment 17y agoIt sure ain't pretty, but it does teach you C. The source code is fairly well documented and does some complex stuff.
- jacquesm 17y agoGrep is actually a really good example. First program your own version of it, then look at the guts of grep and be amazed at how elegant that solution to the problem is. And if you come up with the same solution without peeking you will probably be able to name your own price at some point.
- bensummers 17y agoK&R is still the best book on C. Short, simple and accurate. C++, on the other hand, is a moving target. Difficult to know what to recommend, apart from suggesting you get something modern which concentrates on using the STL, and perhaps Boost.
- plinkplonk 17y agoFirst, K&R. Second , C interfaces and Implementations by David Hanson.
- prog 17y agoI found "The Complete C Reference" by Herbert Schildt to be quite good. Most of the language is covered in about 250 pages followed by standard library function, algorthims and data structures and ends in a good project "The Little C Interpreter". Its about 800 pages but then its a reference so you don't need to read it in one go :-)
- tptacek 17y agocomp.lang.c hated Schildt so much they made a FAQ entry for him (though I admit I've never read one of his books).
- 1331 17y agoI highly recommend _C: A Reference Manual_ by Harbinson and Steele. It has many tiny examples, but it is mainly used as a reference manual (as the title suggests). http://www.amazon.com/Reference-Manual-Samuel-P-Harbison/dp/013089592X http://www.amazon.com/Reference-Manual-Samuel-P-Harbison/dp/... A good book for learning C is _Mastering Algorithms with C_ by Loudon. The source code is over-commented and contains too much white space, which is a bit annoying, but the content is quite nice. http://www.amazon.com/Mastering-Algorithms-C-Kyle-Loudon/dp/1565924533 http://www.amazon.com/Mastering-Algorithms-C-Kyle-Loudon/dp/...
- andrewcooke 17y agonot for learning, but as a reference to keep on your desk when programming in c - harbison + steele http://www.careferencemanual.com/ http://www.careferencemanual.com/ this is also worth reading once you've got the basics down - http://www.amazon.com/Expert-Programming-Peter-van-Linden/dp/0131774298 http://www.amazon.com/Expert-Programming-Peter-van-Linden/dp...
- maw 17y agoI second the recommendation of van der Linden's book. It was through that book, along with lots of programming and experimentation, of course, that I finally understood C.
- tptacek 17y agoC Interfaces and Implementations by Hanson. It'll give you back most of the things you depend on in Python, like variable length strings and dictionaries. I'm teaching a 4-month weekend C class for Ruby programmers starting in April, and my syllabus is pretty much built around CII.
- telemachos 17y agoWhere are you giving the course? And who/how can sign up?
- mmelin 17y agoThat sounds awesome. I wish I was on the right side of the pond. Have you thought of putting some material online, or even taping the course? (of course, maybe you don't want to do that for the first iteration)
- tptacek 17y agoNo, I'm actually pretty skittish about publishing things these days; it's all risk and no reward. I've been writing C since '94 and I guarantee you there's some douchebag who will build an entire website about how little I know about C once the courseware hits Reddit. Pass. The structure I'm chasing is: * C Interfaces and Implementations. * Every month, a couple hours worth of casual exercises, spread over 2 weeks, along with background material and basic courseware (cheat sheets, notes on the book, etc). * "Tech support" style assistance for those exercises (ie, send me your code and I'll tell you what's wrong). * Once a month, several hours of lab-style class time. Each class presumes you did the exercises and, if you had trouble, that you chatted me up about them and we got through it. Each class is 2-3 small coding projects. All are security-focused, because that's why we're doing the class; so, for instance, in week 1 we'll probably write an XML fuzzer and a password cracker. * If you came to class prepared, I'm buying unlimited drinks and pork product at The Publican afterwards. I'm planning on the following arc for the class: * Month 1: Basics (Compiling/running, hello-world, malloc and free, the C type system, very basic pointers, using libraries). * Month 2: Network programming (Stevens-style socket programming, binary formats, I/O). * Month 3: Design (Library design, voidstar, function pointers, dlopen, object-oriented C). * Month 4: Secure programming in C (mostly, memory corruption and string handling). I guess I'm writing all this here not so much to sell a class that I'm not offering publicly so much as to advocate for the idea of doing things like this at your own company, and to solicit critiques. I've spent a long time giving lip service to training and growing a team, and giving up a couple weekends and plying team members with high-end whiskey seemed like a way of putting my money where my mouth was. (Also, this is the next thing Erin wanted me to teach her. So I'm pretty motivated.) (If you ping me offline, I'm happy to share stuff privately).
- pvg 17y agohttp://www.c-faq.com/resources/books.html http://www.c-faq.com/resources/books.html Take a look at the Amazon reviews of some of these, you'll probably find something to your liking. K&R is an important book to have on your bookshelf but the idea that it's the only or best book to learn C from is absurd. Another thing to do is scan the C FAQ and some of the online C tutorials just to get a feel for the language and where it fits in the language universe and to understand why a 'modern topic like threading' is not strictly a C topic. The C coding standard documents of various projects and organizations are also useful to give you an idea of what peoples' notions of sensible C look like - they can be surprisingly varied. Lastly, review known good code in the field you're interested in - in your case, this might be Python extensions. There aren't a lot of terribly good reasons to use C to 'hack on small Unix tools'. Your best path to learning the language is writing things that you actually need and where C is a good (or only) choice.
- kiwidrew 17y agoSlightly off-topic, but in case you haven't checked out Cython (www.cython.org) before, please do! It allows you to write Python code which is _translated_ into C code that uses the CPython API. This includes classes, list comprehensions, generator expressions... practically all Python code is valid Cython code. I'm honestly never going back to writing C again. Cython gives me all the expressiveness of Python combined with all the performance and close-to-the-metal-godlike-powers of C. I've been using it to implement high-performance graph traversal and routing algorithms and to interface with C/C++ libraries, and it's been an absolute amazing productivity boost. However, all of this comes with one caveat: you really need to have a good knowledge of C to understand Cython and what it's doing. It merely saves you from the tedium of hand-coding against the CPython API and all of the boilerplate code that's needed to get much of anything done in C.
- numeromancer 17y agoTry this: http://www.eskimo.com/~scs/cclass/cclass.html http://www.eskimo.com/~scs/cclass/cclass.html Once you can stand on your C legs, you should read the C-FAQ, here: http://www.c-faq.com/ http://www.c-faq.com/ and get a copy of the C99 standard. It's supposed to be here: http://www.dkuug.dk/JTC1/SC22/WG14/ http://www.dkuug.dk/JTC1/SC22/WG14/ but the site was unavailble when I tried to check it before writing this comment. Various C-related stuff that is good to read: http://www.lysator.liu.se/c/duffs-device.html http://www.lysator.liu.se/c/duffs-device.html http://www.lysator.liu.se/c/ten-commandments.html http://www.lysator.liu.se/c/ten-commandments.html http://www.lysator.liu.se/c/pikestyle.html http://www.lysator.liu.se/c/pikestyle.html Take this with a pinch of salt: http://www.seebs.net/faqs/c-iaq.html http://www.seebs.net/faqs/c-iaq.html
- benster222 17y agoK&R, the definitive reference and Expert C Programming:Deep C Secrets by Peter van der Linden Because the best way to understand a language (and its ups and downs) is from someone who understands that language's compiler.
- j_baker 17y agoI might get downvoted for this, but I'd recommend going with C++ before you get into C. You don't have to learn all of the crazy stuff like template metaprogramming, but C++ does have some tools to make C-like programming easier. Personally, I'd use C++ just for the string and vector classes. If you do want to go the C++ route, I hear good things about this book: http://www.amazon.com/Accelerated-Practical-Programming-Example-Depth/dp/020170353X http://www.amazon.com/Accelerated-Practical-Programming-Exam... Now that I think about it though, if you're still deadset on using C, the Python source code is actually a pretty easy read. You get two benefits for the price of one: you learn C and you get to learn more about the internals of Python. Look here: http://svn.python.org/view/python/trunk/ http://svn.python.org/view/python/trunk/
- bensummers 17y agoThat's fairly reasonable advice. C++ as a better C, using the STL to avoid having to reinvent data structures is a nicer language. However, you still want to understand pointers. There's a C++ style which avoids them, but understanding pointers is critical to understanding C and C++.
- tptacek 17y agoThe problem with this worldview is that if you don't want to reimplement data structures, you might as well stay in a modern language. Control over memory layouts (either for performance or for systems programming) is the reason to be in C/C++ in the first place.
- bcl 17y agoI won't downvote, but I will disagree. Anyone programming in C++ needs to know C first. The same goes for ObjC (which in my opinion is superior to C++). Build on a solid foundation before jumping for the shiny looking brass ring of object and garbage collection.
- scott_s 17y agoOne can use C++ as C with stricter type enforcement.
- bballant 17y agoI will second the vote for Mastering Algorithms in C. This book will bring you right into the details you want: http://www.amazon.com/Mastering-Algorithms-C-Kyle-Loudon/dp/1565924533 http://www.amazon.com/Mastering-Algorithms-C-Kyle-Loudon/dp/... Accelerated C++ was also mentioned and I just love this book. It brought me back to C++ after years of being away: http://www.acceleratedcpp.com/ http://www.acceleratedcpp.com/
- Apreche 17y agoPointers on C helped me really understand pointers like no other book was able to do. http://www.amazon.com/Pointers-C-Kenneth-Reek/dp/0673999866/ref=sr_1_1?ie=UTF8&s=books&qid=1269356298&sr=8-1 http://www.amazon.com/Pointers-C-Kenneth-Reek/dp/0673999866/...
- nfnaaron 17y agoFrom my C days, I second and third Harbison & Steele. I learned from K&R, but I learned better from H&S. From this list (can't say how current they are), http://www.c-faq.com/resources/sd19.html http://www.c-faq.com/resources/sd19.html I liked: - K&R and H&S (if you have to choose, H&S) - The Standard C Library, P.J.Plauger. - C Traps and Pitfalls, Andrew Koenig.
- gcv 17y agoAfter K&R, I highly recommend Peter van der Linden's Expert C Programming (http://www.amazon.com/Expert-Programming-Peter-van-Linden/dp/0131774298 http://www.amazon.com/Expert-Programming-Peter-van-Linden/dp...). It's fun to read (yes, really), and you'll understand the language better for it. C is fairly quirky, and this book helps navigate those quirks. For example, I bet most people who claim to "know" C can neither explain the difference between arrays and pointers, nor clarify where they cannot be used interchangeably. I was lucky to have read that book early on in my days of C, so I actually know the answer.
- Maro 17y agoA number of people have recommended David R. Hanson's C Interfaces and Impementations, so I'll mention his A Retargetable C Compiler: Design and Implementation. I own this book though I haven't read it, but it's about writing a C compiler in C. You should pretty much just go out and buy these two books because they'll eventually go out of print, and then you'll be screwed. CII used to be out of print and impossible to obtain until Addison-Wesley started printing it again a year ago.
- tptacek 17y agoYou should read it! It's fantastically written, and goes from a hand-written C lexer all the way to x86 code generation. Hanson's books are written in literate code style, and it's a shame that that style went out of fashion, because it is the answer to how to write code-heavy books.
- adrianscott 17y agoIf you really want to do Python modules, I can understand. In terms of hacking small Unix tools, have you considered other options like perl, bash, combo's of unix commands, etc.? I suggest that those options could be a lot more productive for your time investment than C. IMHO. Hope this helps.
- nailer 17y agoThe only time I'd bust out C is when there's no Python API (meaning there's probably no Perl API either). I generally avoid shell scripting these days as I find text processing a fragile way to build something useful.
- gte910h 17y agoThis post only contains non-aff links While I'm certainly good at C, and have been the guy people asked the C and C toolchain questions at a couple places I've worked, I'd say you're in for more than 1 book to grok the topics you've expressed interest in, and haven't met the single book that does all you want. 1: If you're a python programmer, I do suggest staying with pure C and AWAY from C++. You have little use case for it if you can mix python and C and it is literally pain incarnate while you do not know every little bit of it and/or work with people who do not know every little bit of it. 2: Secondly, a single book on C will not get you where you're going. You will need several. 3: I'd stick with K&R for reference, but then go to the following two books for threading and all the other stuff like networking: You need these two books to get down C and it's ecosystem (make, autotools, etc): Advanced Unix Programming by Rochkind (Really good explanation of signals especially): http://www.amazon.com/Advanced-UNIX-Programming-Marc-Rochkind/dp/0131411543/ref=sr_1_1?ie=UTF8&s=books&qid=1269362485&sr=8-1 http://www.amazon.com/Advanced-UNIX-Programming-Marc-Rochkin... Gnu/Linux Application Programming by M. Tim Jones (Recovers some of the same stuff as AUP, but does better with the toolchain): http://www.amazon.com/GNU-Linux-Application-Programming/dp/1584505680/ref=sr_1_1?ie=UTF8&s=books&qid=1269362580&sr=8-1 http://www.amazon.com/GNU-Linux-Application-Programming/dp/1... If you want to go more advanced, this is likely the correct book: Advanced Programming in the Unix Environment: http://www.amazon.com/Advanced-Programming-UNIX-Environment-2nd/dp/0201433079/ref=sr_1_2?ie=UTF8&s=books&qid=1269362485&sr=8-2 http://www.amazon.com/Advanced-Programming-UNIX-Environment-... Although if you're diving deep into networking, this is likely the correct book: Unix Networking Programming: Vol I by Stevens: http://www.amazon.com/Unix-Network-Programming-Sockets-Networking/dp/0131411551/ref=sr_1_1?ie=UTF8&s=books&qid=1269362819&sr=1-1 http://www.amazon.com/Unix-Network-Programming-Sockets-Netwo...