Hacker News new | comments | show | ask | jobs | submit login
C is 38 years old. K&R V2 is 22 years old. What's the best modern C book? (stackoverflow.com)
17 points by brianwillis 2622 days ago | hide | past | web | favorite | 28 comments

I've been recommending CII for years:


Past that, the trouble with C books is twofold: (a) do you really need that much book for such a small language? and (b) most of what you'd use C for (systems programming, codec design, bare-metal network programming, etc) is covered in domain-specific books that use... wait for it... C as their example language.

The problem with books that use C as their example language is that they don't cover the finer points of C that are going to fuck you in real life. What happens when you strncpy a string that's the same length as n? How should fatal errors be handled? Who is responsible for memory allocation and deallocation?

These are questions you need to know the answer to, but are unrelated to whatever the book is about.

The best way to learn C is to learn some other programming language, and then apply the common sense you learned back to C.

No, you won't ever really learn that stuff until you just do it. Reading code is part of writing code so you should read some actual C code in your domain before ever writing code.

K&R is a incredible book that every programmer should own. I honestly don't see why you would need any other book for C.

It's style isn't correct for modern C is a good reason why.

In general, categorizing someone's style preference as incorrect rarely leads to anything good.

And in this instance, considering how many people still use and prefer the style in question, if you're learning C there's a good chance you'll be working with others on existing code using that particular style.

While you're correct, people dislike being confronted and its often easier to just not bother doing so, they are actually doing it incorrectly at this point if they're writing straight out of K&R 2nd ed. The style of the book is actually in error, as in, will cause compiler errors and subtle bugs in modern systems.

They should know enough of what's gone on in the last 20 years of the C world to know K&R's second edition isn't the end all be all of C development.

If you insist on writing to C90 (and for many applications, there are very good reasons to do so), you need to restrict yourself to the intersection of valid C90 and valid C99. You need to do this for the same reasons you would be writing C90 today (well, other than you never bothered to learn C99 in the last decade)

K&R, 2nd Edition, does not observe this (as that's impossible for it to have done so, as it's so old). People who only read K&R, 2nd Ed, tend to not be cognizant of this most important of practices, which makes integration with their code and modern code a pain.

In addition to failing to do this, it uses variable names from an era where terminals were 16x80.

It uses increment operators in wholly inappropriate, precedence overeliant conditionals which would get you slapped silly the first time you cause a serious bug/heisenbug up in any C shop.

It introduces the bad string functions (K&R is often pointed to people who never learned to not use scanf, etc) without any discussion of why you shouldn't ever think of touching them except in very small edge cases and toy programs, nor does it give examples of how to safely use them if you don't have any substitutes available.

Past the actual deficiencies of the text, you still have other issues. At the end of K&R 2ndEd, You know nothing of dealing with characters and languages other than those that fit in ASCII, nothing of using modern threading/multiprocessing/parallelism, nothing of C99 (including why large variable span is bad), nothing of modern object oriented C (which has been in use since systems like X windows used it in the 80s), nothing of standard layouts of projects (or even C source files), nothing of safe string functions, nothing of the differences in variable sizes for different platforms, and in short, nothing of what's happened in 22 years worth of C betterment since this book came out.

The 2nd edition of the book was published in 1998. You need more than that to get a handle on the core of C today.

The end of 32bit ints and non ASCII languages alone mean the book needs more if you are going to use it. It's not enough on its own. If you find out a guy has learned C from nothing but it, it's a pretty reliable indicator "Nope, nope, no modern C experience" and you better make sure he's re trainable into accommodating how things are done today.

When you said style, I thought you meant where the braces go, as that's what a lot of people seem to complain about.

Now that you've elaborated, I'll certainly agree that it's not a tutorial for learning how to program, but I don't think it fails all that hard. And some of your criticism misses the mark too. None of the examples are substantive enough to warrant object orientation, there are no threads in C, etc. And ints that aren't 32 bits? When the book came out, tons of ints weren't 32 bits or even power of 2 sized. I never picked up that assumption from my copy. And I'm not aware of anything that won't actually compile.

The dangers of using bad functions is real, but the alternatives don't exist in the standard. It's unfair to complain that a book that describes the C standard only describes what's in the standard. C99 didn't change all that much about the language.

>It's unfair to complain that a book that describes the C standard only describes what's in the standard.

It describes more than what's in the standard (namely, non-standard functions you could use to implement the standard). Other books describe the problem, and what you should do instead today (the entire point of this discussion is finding better substitutes to this very old book). My frustration comes from people hired to do C who think that book is cannon, when it's really more like "what was current in the early 90s".

I do get why you were originally reacting though. If I thought someone was just objecting to the brace style of the code I'd find that questionable as well.

Another thing which makes me really question the value of a C90 only book is that when confronted with things C90 doesn't do well (say, fixed sized types), you'll reach for C90 based solutions of the 90's to solve that problems (say, company defined type sizes and variable naming standards based on size), rather than the C99 solutions to those same problems, aka, the standards based solution to the problems.

I'd still think about getting K&R for perhaps the 4th book on C for a person (after something like Advanced Programming in the Unix Environment or _insert C language on platform of their choice_). Just not #1 anymore, and probably not #2.

Do you have any recommendations?

No. You should read K&R and then read the spec.

It appears to be out of print.

EDIT: Or maybe not. It's just surprisingly expensive (~$60) and Amazon apparently doesn't stock it themselves.

It's expensive because people keep buying (and keeping) it.

I may be using the word 'modern' a bit liberally here, but these are all more recent than K&R at least. 'C A Reference Manual' I think is what a lot of people really want out of a C book and 'Expert C'/'C Traps and Pitfalls' both help with all of the less intuitive parts of the language that you don't really get out of K&R.

C A Reference Manual http://www.amazon.com/Reference-Manual-Samuel-P-Harbison/dp/...

Expert C http://www.amazon.com/Expert-Programming-Peter-van-Linden/dp...

C Interfaces and Implementations http://www.amazon.com/Interfaces-Implementations-Techniques-...

C Traps and Pitfalls http://www.amazon.com/C-Traps-Pitfalls-Andrew-Koenig/dp/0201...

One I really liked is _C Programming: A Modern Approach_ by K.N. King.

I personally learned C from that very book, and still own a well-worn copy of it.

I always like Larry Ullman's books, especially his PHP and MySQL one. When I was learning C, his book was really helpful: http://www.larryullman.com/books/c-programming-visual-quicks...

C as a language is pretty simple to learn. Using C in practice is where things can be tricky when you need to dance with the compiler and work on low level systems where a datasheet is always open, and if you haven't had your "Pointer Enlightenment" you're toast. Domain-specific books are key there and just continual practice.

K&R. It's like the C language itself: it just can't be improved upon. It was already meant for our time, when it was written.

Well, it doesn't cover the niceties of C99. While I own it, and it'd be the first C book i'd recommend, it could perfectly have a chapter explaining some changes in C since it was written.

The book I have is "C A reference Manual" by Harbison and Steele. The advice I got when I had to buy one or other for a course I was doing that it covered much the same stuff, just assumed you had some background in programming.

I recommend Programming in C by Stephen Kochan http://amzn.to/cXnEPp

I have also heard good things about Illustrating C

K & R, V2. Mandatory reading for anyone who programs, IMHO. Still modern at 22.

Working on it...

Really? You're writing a C book?

Very cool. My first reaction was that there wouldn't be much of a market for this, but after reading through the thread you linked to it's clear there is.

Looking forward to seeing the final product.

It's not about market, although I'll be selling a physical copy. The digital will be free, as per Zed's example.

I care deeply about programming, and about preventing software from turning into a mass cargo-cult profession. I want more systems-knowledgeable people out there who understand how things are and could be built.

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | DMCA | Apply to YC | Contact