7 ms·
Ask HN: Should I use K&R to learn C?
Should I? Or would I be learning C the wrong way? This would be my second language and I am still a programming newbie. Learning C so I can reverse engineer.
- Oleg2tor 12y agoI highly recommend this one. Very clear for newbie.
- tambourine_man 12y agoShould you read Shakespeare? Yes.
- loumf 12y agoThere was a very good critique of K&R in Learn C the Hard Way that got taken down because of (IMO) undue backlash. The gist of it was that K&R ignores the safety problems in C and there is no discussion of the habits/constructs seasoned C programmers have developed to make sure their code is safe. Of course, many of those habits were developed after K&R, but that's why we need revisions and new sources. In its place, Zed Shaw (the author), updated Learn C the Hard Way to concentrate on using C safely -- it is an awesome way for a newbie to learn C. Even if you don't agree (and want to use K&R), read this alongside it. http://c.learncodethehardway.org/book/ http://c.learncodethehardway.org/book/
- __revrs__ 12y agoThen maybe it would be a good idea to go ahead and read K&R and then afterwards read Zed's book?
- loumf 12y agoK&R is a seminal work. I think it should at least be read because every C programmer has read it -- and so you are on common ground. Also, it is a better reference (has the grammar, precedence tables, etc.) To learn C -- I'd recommend LCTHW.
- fierycatnet 12y agoInteresting, last time I asked on various C boards about learning resources, Zed's book got ripped apart. http://www.iso-9899.info/wiki/Books http://www.iso-9899.info/wiki/Books
- FraaJad 12y agoThe critique of ZedShaw's book appears to be > Too many factual problems and a presentation that gets you to do things wrongly before being shown how to do it correctly, and not even always then. Eh?? Isn't that the whole point of "Learn __ the Hard Way"? ie., by making mistakes and learning from them?
- loumf 12y agoAlso, K&R does many things the wrong way for expediency. It's fine if you know that already, but newbies do not know that. The main critique of Zed seems to be that he didn't say it was impossible to protect a C function from the caller. So what? There is a big difference between his suggested style and the style you'd get from an uncritical reading of K&R, even though neither is 100% safe from the caller.
- zedshaw 12y agoThe man who moved my book into the "avoided" category admitted to me in an email that he did it just because of my K&R critique, not because of the book's quality, is not a C programmer, hasn't coded C professionally ever, and works doing PHP with almost no C experience.
- ewalk153 12y agoI strongly recommend Learn C the Hard Way http://c.learncodethehardway.org/book/ http://c.learncodethehardway.org/book/ You'll get a modern approach to C programming and it had great exercises.
- username3720 12y agoI'd strongly advise people to read K&R C as maybe their second or third book and mostly for historical purposes and not because it's particularly great for learning the language. The exercises are quite fun, however. You will be better served by reading a more recent (C99) and carefully considered book such as K.N. King's C Programming: A Modern Approach.
- pakled_engineer 12y agoPrinceton's CS50 course on edx.org covers C and recommends other good books like Hacker's Delight. This is also a good presentation for safer C programs https://www.bsdcan.org/2010/schedule/events/172.en.html https://www.bsdcan.org/2010/schedule/events/172.en.html
- bithush 12y agoK&R is a great book however I feel there are better books these days. My two personal recommendations are C Programming A Modern Approach by K N King (2nd edition) and Programming in C by Kochan. However the exercises in K&R are excellent so make sure you do them all. Actually the cost of K&R is justified just for the exercises IMHO. Also get Understanding and Using C Pointers by Richard Reese, it is a must have. Two other books I very much enjoyed are Head First C and 21st Century C (2nd edition). Head First has some nice "extra" bits like threads and networking in C. 21st Century C explains sensible, modern ways to do things in C and, more importantly, things you should NEVER do in C!
- __revrs__ 12y agoI might look into the K N King book, looks like another hn user is recommending it as well.
- kjs3 12y agoSeconding Understanding and Using C Pointers by Richard Reese and 21st Century C, but more for after you've gone through a more basic resource. I haven't looked at Kim King's book, but if it's anywhere near as good as his intro to Modula-2, it will be excellent to learn C from.
- bithush 12y agoYeah 21CC and U&UCP are for once you are comfortable with the language not for beginners. U&UCP will be a book to keep by for many years as well as it is pretty much the gospel for pointers IMHO.
- 0xCMP 12y agoI second the Programming in C by Kochan. I initially learned using the KNR but starting using this book after doing CS50 which is actually another great way to get introduced to C if you have the time to watch the lectures because they give you some training wheels that allow you to taste "doing things" with C before you know everything. There is something to be said for having that feeling of doing something cool once to motivate you to learn and get that feeling again.
- tokenrove 12y agoIf you're serious about reverse engineering, you might consider learning assembly language and C side-by-side. Learn an assembly language (with the Art of Assembly Language Programming or similar) while working through K&R (or another introduction to C), but compiling everything with gcc -S and reading the assembly output (initially, compile with -O0; optimization will obscure the output too much). This would be slow going, but you'd really solidify the connection between the low-level and the higher level, which is presumably what you are really looking for when you say you want to learn C. (A lot of things you're going to reverse engineer will have to be reverse engineered at the assembly level, not the C level.) K&R would be fine for this purpose, I think. As a more general introduction to C, I think it's still great, but really needs to be paired with something more modern if you're planning to actually develop applications in C.
- jibsen 12y agoI think it is hard to find a more well-written and concise introduction to a programming language than K&R. That being said, the example code in the book is sometimes terse at the expense of readability (and security). I would try to pick up a more modern, and defensive, coding style from the web.
- halosghost 12y agoPersonally, I have found most C books to be colossal failures. K&R is a great text that would be good for you to read eventually (if only to see how far C has come), but to learn how to do modern programming, K&R probably is not where you should go. Additionally, LCTHW let me down significantly (regardless of what you think of the author). If I could make two recommendations to you, the first would be that reading a text book will never make you good at C programming; like many other languages (or perhaps programming in-general with the exclusion of highly-academic languages), creating your own personal projects and learning as you go with a mentor to guide you is probably the single best way to become proficient. Second, eventually, you should read the current Spec (or at least, the latest draft and all its TC). Before getting to that, if you are convinced that a text book would be helpful, go look at http://icube-icps.unistra.fr/img_auth.php/d/db/ModernC.pdf http://icube-icps.unistra.fr/img_auth.php/d/db/ModernC.pdf All the best,
- nothrabannosir 12y ago> with a mentor to guide you I wholeheartedly agree. Not to say they're mutually exclusive, but nothing comes close in terms of education / time density to an experienced programmer telling you what you did wrong and why.
- halosghost 12y agoActually, that's why I happily offer to help those who are interested in learning C to come and ping me with code snippets and to even assign them exercises and walk through them step by step with people. The world needs more competent C programmers, and if I can help with that at all, I'd like to :)
- protomyth 12y agoK&R (draft ANSI edition) was my first C book and still sits on my shelf. I learned C code from it and it was my goto reference for many years. There is a fair bit of nostalgia for that book. But, at this point in my life, I believe the K&R book is a good read if you are into the history of programming languages or going to write a book about your own language and want to avoid the bloat common in computer books. It is not a great modern C book and has not been updated in years. I wish this wasn't true, but pointing new programmers to that book in anything other than a historical context is just not serving them well. It is really too outdated and does not cover the language as it is used or safe programming in the language. I'm not sure what the replacement is these days.
- zumtar 12y agoYou should definitely have a copy of K&R on your desk. A book I read alongside K&R was "Illustrating C" by Donald G. Alcock, even though it is out of print and quite hard to find I'd recommend hunting a copy down as it explains the concepts visually and for me, it was the only book that made things fall into place. http://www.amazon.com/Illustrating-C-Ansi-Iso-Version/dp/0521468213 http://www.amazon.com/Illustrating-C-Ansi-Iso-Version/dp/052...
- protomyth 12y ago"You should definitely have a copy of K&R on your desk." I ask honestly, why? Other than nostalgia and as a signal to others what does it bring the modern C programmer? I have it on my shelf, but it is part of my history.
- zumtar 12y ago"I ask honestly, why?" As you say yourself, it is part of your history, you've probably forgotten which concepts that K&R taught you and the OP is trying to learn C from scratch. The OP mentions that they are learning C to help them with their reverse engineering skills, so learning about the older ways that C was programmed is also invaluable as those insecure programming techniques are still (unfortunately) widely used today and if OP wants to exploit such bugs, then all information is useful! You mention nostalgia, and yes that is certainly one of the reasons as in my opinion the preface, the preface to the second edition and the introduction in the K&R book are worth the cost alone. Besides, the book is so thin and flexible and makes a perfect ultra-portable, high-definition, long-life, skills acquisition device that works well on bus journeys, flights etc. ;) I would say owning it is better than not.
- protomyth 12y ago"the OP is trying to learn C from scratch" This weighed heaviest in my mind. K&R is a poor book to learn modern C. Its like saying a person starting out with Fortran today should learn from a book written for FORTRAN 77. It might be ok later once better habits have set in, but for someone who didn't live through that era, it just seems like a waste of time and money for learning modern C. I am still convinced its a signal to others and an aspiration for what we want out of language authors, but it just isn't up to date for the task of being the first C book someone reads.
- alfiedotwtf 12y agoI would recommend "A Reference Manual" by Harbison and Steele jr over K&R... But to be honest, if you want to do reverse engineering, I would instead pick up a copy of "80386 Programmer's Manual" from Intel as you'll be living in assembly.
- __revrs__ 12y agoDid a quick search and found it: http://www.logix.cz/michal/doc/i386/ http://www.logix.cz/michal/doc/i386/ I must ask though and I don't mean this in a challenging way but isn't this teaching ASM for an old cpu? I'm largely ignorant of ASM so feel free to correct me. I just don't see the point of learning asm for that (As I believe that ASM changes from cpu to cpu).
- alfiedotwtf 12y agoIt will give you the foundation to advance to modern day Intels. You won't last if you jump straight to x86_64
- joshuapants 12y agoSince you describe yourself as a programming newbie, I think you should take a look at Head First C. I'm not the hugest fan of the Learn __ the Hard Way approach anymore, and I think the Head First series does a good job of teaching you in an interesting and easy to remember way. I can't speak to what you need for reverse engineering, so I'll let others say whether it's appropriate for that.
- cicero 12y agoI started with K&R as a college senior, 31 years ago. I found it tough going, but rewarding. It's a good book, but as others have said, you don't want to miss out on things that have been learned about C since it was written, including safety issues.
- agentultra 12y agoI'd say that K&R C is a historical relic and not the best way to learn the language anymore. I have the ANSI second edition. It's way outdated. If you have some programming language experience already I'd suggest going with 21st Century C by Ben Klemens. There's a decent refresher on the language in the appendix and most of the book spends time talking about all of the things most text books skip over: tooling, debugging, the environment, useful libraries, etc. It also covers C99/C11 which is where anyone new to C should be starting these days, IMO. You might also want to pick up Expert C Programming by Peter Van Der Linden. It's also starting to show its age but is useful for a deep, historical dive into why and how C evolved the way it has. It will keep you from yelling, "why on Earth would you ever do it that way!?" when you get a little frustrated with C's quirks. There are reasons most of the time (even if they're not always good ones in retrospect). Also there's nothing quite like ordering yourself a copy of the specification. It's not insurmountable and easy to chew through a section here and there as you go along. Happy hacking! UPDATE: You can wait to purchase a copy of the ISO ANSI C specification as it can be quite expensive... you'll find it on http://webstore.ansi.org/RecordDetail.aspx?sku=ISO%2fIEC+9899%3a2011 http://webstore.ansi.org/RecordDetail.aspx?sku=ISO%2fIEC+989...
- billrobertson42 12y agoI will second Expert C Programming.
- barbs 12y agoFor those of you reading this and feeling dumb for not knowing what K&R refers to (like me): http://en.wikipedia.org/wiki/The_C_Programming_Language http://en.wikipedia.org/wiki/The_C_Programming_Language
- Elrac 12y ago35 years ago, when I was told my 2nd freshman course in programming would be in Pascal, I bought the _Pascal User's Manual and Report_, read through it in one sitting, managed to remember most of it (Pascal is a small language) and was able to program in it - at a beginner level, of course. K&R is roughly the C equivalent. If you want the language and nothing but, then it should do you nicely. My personal favorite, because it explains better (so I think) and dives into a little more depth where K&R leaves you to extrapolate on your own, is Peter van der Linden's "Expert C Programming" (Deep C Secrets): http://www.amazon.com/Expert-Programming-Peter-van-Linden/dp/0131774298 http://www.amazon.com/Expert-Programming-Peter-van-Linden/dp... . Also, I enjoy the author's entertaining style, and think it aids learning.
- kasperset 12y agoAlso try Pointers on C - Kenneth A. Reek
- VLM 12y agoA small detail that has been missed by most responses to the question is OP isn't asking how to learn how to write the best possible 2015 C code for 2015 C tasks, but asked for reverse engineering previously written code (perhaps written VERY long ago). Depending on the age and quality of the code OP is reverse engineering, learning a very modern style of coding might somewhat oppose OPs goal. I'd advise OP to rephrase the question to something like "I really want to learn how unixv6 works as a retro computing hobby project and I want to know what books the authors of unixv6 originally read to learn C". Or "I want to eventually be able to reverse engineer stereotypical 90s/00s microcontroller code so I want to know what textbooks an average EE used to learn C in that era"
- __revrs__ 12y agohmm, I think my question is fine how it is. I want to learn C, I am asking HN their opinion of a book I am thinking about. If you want specifics, I would revise my question to something like "I really want to learn how to reverse engineer malware so I'm starting this goal by learning C, is K&R a good resource to learn C from?"
- VLM 12y agoWhat kind of C? 1980s K+R or 2015 or ... Its like the difference between "Programming Perl" 1st edition manual and 2014 edition of "Modern Perl"
- yiyus 12y agoYou are not going to learn C with one book. K&R is one you should know, but it is not everything you will need. I think it still is a nice book for beginners, but not the most pragmatic option. You may prefer to start with something more up to date. But if you really want to learn C, read it at some point. As a side note, even if C was already a dead language, I would still recommend to read the book just as an example of good writing.
- ebbv 12y agoThere's a lot of great book recommendations in this thread already, so I will just add that learning C so that you can reverse engineer anything more than trivial programs is kind of like memorizing a parts manual so you can reverse engineer your car. Yeah you'll be able to identify all the parts and in theory what they do, but you won't understand the why of things. There's really no substitute for experience in understanding why something is designed the way it is.
- brudgers 12y agoI wouldn't recommend K & R to a beginner. To a first approximation C is portable assembly language and Kernigan and Ritchie wrote for an audience approximating their coworkers at Bell Labs - people with experience in Assembly language. A book I would recommend, particularly if Zed Shaw's Learn the Hard Way does not appeal to you is Head First C.[1] The underlying pedagogy of programmed instruction is similar. The exercises are more diverse, the tone is lighter, and the overall presentation more approachable. Good luck. [1]: http://shop.oreilly.com/product/0636920015482.do http://shop.oreilly.com/product/0636920015482.do
- zedshaw 12y agoFor reverse engineering? You should use every single thing you can get your hands on that helps you learn. The K and R are the two people who wrote C. Of course you should read their if you want to learn C. Then read, and WRITE the code in, everything else you can get your hands on to learn a programming language. It may only take one book, it may take 10, but that's how you do it. Then after that go learn Assembler, or even better, implement a compiler. You'd probably learn a ton of the things you need to know for reverse engineering code from just making one simple compiler.
- o_syn 12y agoYes, I would highly recommend reading it, with the exception of Chapter 8. Some of the other people have said things like: "The gist of it was that K&R ignores the safety problems in C and there is no discussion of the habits/constructs seasoned C programmers have developed to make sure their code is safe. Of course, many of those habits were developed after K&R, but that's why we need revisions and new sources." This is indeed extremely important, but I also think in an introductory book these details will be very overwhelming. Safety in C is a very complicated topic, and indeed entire books have been written about it[1]. I find these "critiques" rather misguided. After all, we all know Newton's laws are an inaccurate picture of reality, but one would hardly start teaching quantum physics straightaway. C is a hard language with a lot of bizarre edge cases, and K&R C does an excellent job of presenting the language without all these pesky details. The second criticism seems to be that it is outdated. This is true, but you can catch up with coding conventions and best practices very quickly. To summarize, K&R C is excellent for introducing people to C. I would suggest completely ignoring safety issues et cetera at the start and just have fun solving the exercises! --- [1] Secure Coding in C and C++ is a really good book on this topic. http://www.amazon.com/Secure-Coding-Robert-C-Seacord/dp/0321335724 http://www.amazon.com/Secure-Coding-Robert-C-Seacord/dp/0321... PS: Since you are interested in reverse engineering, after finishing K&R C I would recommend reading Computer Systems: A Programmer's Perspective.
- hungnv 12y agoMost people - who know C or any programming language do not ask for a book to read, just pick one, read it carefully, do something based on it, make mistakes, learn from them. Time went by, write a book!
- LocalMan 12y agoA much more complete book than K&R is "C: A Reference Manual", which has much better explanations. See it here: http://careferencemanual.com/ http://careferencemanual.com/, at its home page. Or at Amazon here: http://www.amazon.com/Reference-Manual-5th-Edition/dp/013089592X http://www.amazon.com/Reference-Manual-5th-Edition/dp/013089.... Turn to this book when you have questions. The authors are Harbison and Steele. If you become a C programmer, you will want and need this book. So you may as well get a copy now. Another (more advanced) book is The C Puzzle Book. http://www.amazon.com/The-Puzzle-Book-Alan-Feuer/dp/0201604612 http://www.amazon.com/The-Puzzle-Book-Alan-Feuer/dp/02016046.... It's older and I'm not sure it's totally up-to-date with the latest version of C. But it's still very valuable. If you finish this book, you will be a C ninja, and it isn't too hard. K&R is outdated, in my opinion, because it is too skimpy.
- sireat 12y agoCAR is a great reference book and something that I will use when I am writing embedded C even today. But it is still a reference book not a get started with C guide.
- contiver 12y agoI feel K&R is an important classic from which one can still take a lot of valuable jewels in a pretty concise way. If possible, complement it with other books like Peter van der Linden's Expert C programming (although I don't like his opposition to static linking). If possible, check the suckless.org projects, they tend to have pretty idiomatic and clean source codes.
- joeld42 12y agoK&R is more of a reference, I'd suggest this instead: "C Interfaces and Implementations" by David R. Hanson http://www.amazon.com/Interfaces-Implementations-Techniques-Creating-Reusable/dp/0201498413 http://www.amazon.com/Interfaces-Implementations-Techniques-...