Hacker News new | past | comments | ask | show | jobs | submit login
Ask HN: Should I use K&R to learn C?
49 points by __revrs__ on April 7, 2015 | hide | past | favorite | 69 comments
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.

There 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.


If your goal in learning C is to write largish systems in it, then yes, "Learning C The Hard Way" is a good option. But the poster has said that s/he wants to learn for the purpose of reverse engineering. From this point of view, the main interest of C is just learning enough syntax to be able create functions and structs, and then compile them with gcc -S, so that you can see the assembly language that is generated by the compiler.

Personally I feel that pretty much any of the typically-cited C books are too heavy for this purpose. What you really want is a Learn C in 21 Days type of book, that will walk you through the build system as well as the basic of the C language. Even that may be too heavy - a well-written online tutorial would probably be a superior solution. Sadly, I haven't needed to look at "Intro to C" material for a very long time, so I can't really comment on what might be appropriate resources available these days.

Well if you want to start reverse engineering, I'd think you'd need a very deep understanding of C.

When reversing it doesn't help you if you know what a single instruction is doing. You need to be able to recompile the program in your head, and translate it back to high-level logic. You need far more knowledge of C than just the 'learn C in 21 days' stuff if you really want to start reverse engineering.

There was a very good critique of "earn C the Hard Way": http://hentenaar.com/dont-learn-c-the-wrong-way

I'm biased, I have used Zed's books many times, but I found that critique to be far more detailed than it was "good." I got the impression that the author perverted Zed's writing just to take shots at it. LearnCTheHardWay, like all of Zed's other books is of the "write the code first, try to figure out what's going on your own first, and then I'll explain it to you" style. I get that some people don't like that, and if you were just learning how to code, I could see how the C book would be particularly annoying, but he clearly states it's not for people new to code. Mr Hentenaar also can't seem to make up his mind as to how he would like a how-to book to be written. In the first couple sections he complains there isn't enough detail, and then as soon as printf() is introduced, he is whining about the fact that every line of code was explained. Further, as he complains that Zed does with K&R, Hentenaar takes a lot of LCTHW out of context to show why it is "wrong" (such as the "make is the new python" example). Maybe he's trying to be meta in the way he wrote the review, but I doubt it.

As a side note, it is (nearly) impossible for something that hasn't changed in almost 30 years (k&r) to keep up with something as dynamic as a program language. Even if the standard hadn't changed, the way a language is used is practically guaranteed to change over a period that long. Although, as somebody else said, the exercises in k&r are great and it covers a lot of interesting information, but does seem pretty light.

To criticize like Zed Shaw is easy, but create something which would be used around the world for so many years - a different story. That critique is just not respectful marketing to promote the books which isn't very good by itself.

I didn't know of the critique of K&R in LCTHW that got taken down. I'm surprised that the rabble-rousers weren't able to take it in context. When K&R was first authored, there were no "seasoned" C programmers from which to draw on for their experience in writing safe, secure C code!

Yes and no. Perhaps when the first edition was published that was true, but the second edition was published 11 years later.

When the second edition was published in 1988, there were seasoned C programmers, but security wasn't nearly the issue it is today. Since there was no World Wide Web yet, most computer systems weren't exposed to a global network full of people who wanted to crack code for profit. There was no e-commerce, no on-line banking, etc.

Was the Morris Worm, which brought down a huge percentage of Unix Servers in 1988, the result of C?

In any case, there was definitely a network that was being used by a lot of people by 1988.

I think you're overestimating the impact of the Morris Worm - there weren't all that many servers to bring down.

The first commercial ISP in the U.S. was founded in 1989.[1] Until then, only a few people in universities and the tech and defense industries had access to the internet. According to this chart[2], the number of internet hosts in 1992 is barely distinguishable from zero on today's scale.

[1] https://en.wikipedia.org/wiki/Internet_service_provider#Hist...

[2] https://en.wikipedia.org/wiki/File:Number_of_internet_hosts....

Then maybe it would be a good idea to go ahead and read K&R and then afterwards read Zed's book?

K&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.

Interesting, last time I asked on various C boards about learning resources, Zed's book got ripped apart.


The 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.

It is shocking to me that someone who doesn't even use C is handing out critiques of any book focused on C.

Yeah I am not picking sides here but they really sold me against your book on freenode. As a newbie I didn't know any better. Come to think of it, I was doing just fine following your book, until I went on #C at freenode.

The 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?

Also, 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.

I'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+989...

I will second Expert C Programming.

K&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!

I 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.

I might look into the K N King book, looks like another hn user is recommending it as well.

Seconding 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.

Yeah 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.

For 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.

If 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.

What you described is exactly how I want to learn C: together with ASM but wasn't sure how to go about it. But know you have gave me a general idea on how to go about it, thanks.

If you have any questions about that approach, feel free to email me.

A much more complete book than K&R is "C: A Reference Manual", which has much better explanations.

See it here: http://careferencemanual.com/, at its home page.

Or at Amazon here: 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.


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.

CAR 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.

Personally, 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

All the best,

> 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.

Actually, 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 :)

I'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.

Yes, 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/0321...

PS: Since you are interested in reverse engineering, after finishing K&R C I would recommend reading Computer Systems: A Programmer's Perspective.

35 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... . Also, I enjoy the author's entertaining style, and think it aids learning.

A 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"

hmm, 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?"

What 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"

For those of you reading this and feeling dumb for not knowing what K&R refers to (like me):


You 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.

Princeton'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

K&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.

I strongly recommend Learn C the Hard Way http://c.learncodethehardway.org/book/

You'll get a modern approach to C programming and it had great exercises.

I 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

Since 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.

I 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.

K&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-...

I 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.

I 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.

I 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.

Did a quick search and found it: 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).

It will give you the foundation to advance to modern day Intels. You won't last if you jump straight to x86_64

Should you read Shakespeare? Yes.

Should you read Shakespeare to learn English though?

I'm not sure if your use of iambic pentameter for that sentence was intentional, but if so, well done!

Thou shouldst.

Most 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!

You 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.


"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.

"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.

"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.

When I read it first time, it was ok to start to write C programs. When a read it again later I understood that it was much deeper and complete than it seemed first time.

Then, every time I learned something in programming using C (studying operating systems), I read K&R again and found that the answer was there even if I didn't see it before. So it was a good way for me to "validate" that I understood something new.

Note that I never used C at a very high level, and I don't know what is modern C programming, so it may not apply.

It's still a historical relic with little to no applicability today. K&R style C is awful by comparison with modern C.

If you want to learn C history, it's invaluable. If you want to learn C, it's only the first of many books you'll have to read.

Also try Pointers on C - Kenneth A. Reek

I highly recommend this one. Very clear for newbie.

There'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.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact