

C is 38 years old. K&R V2 is 22 years old. What's the best modern C book? - brianwillis
http://stackoverflow.com/q/4170140/118

======
tptacek
I've been recommending CII for years:

<http://news.ycombinator.com/item?id=1245464>

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.

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

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

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

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

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

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

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

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

------
EatenByGrues
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/...](http://www.amazon.com/Reference-Manual-Samuel-P-
Harbison/dp/013089592X/ref=sr_1_1?ie=UTF8&qid=1289625427&sr=8-1)

Expert C [http://www.amazon.com/Expert-Programming-Peter-van-
Linden/dp...](http://www.amazon.com/Expert-Programming-Peter-van-
Linden/dp/0131774298/ref=sr_1_1?ie=UTF8&qid=1289625434&sr=8-1)

C Interfaces and Implementations [http://www.amazon.com/Interfaces-
Implementations-Techniques-...](http://www.amazon.com/Interfaces-
Implementations-Techniques-Creating-
Reusable/dp/0201498413/ref=sr_1_1?ie=UTF8&qid=1289625475&sr=8-1)

C Traps and Pitfalls [http://www.amazon.com/C-Traps-Pitfalls-Andrew-
Koenig/dp/0201...](http://www.amazon.com/C-Traps-Pitfalls-Andrew-
Koenig/dp/0201179288/ref=sr_1_1?ie=UTF8&s=books&qid=1289625457&sr=8-1)

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

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

------
Jach
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...](http://www.larryullman.com/books/c-programming-visual-quickstart-
guide/)

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.

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

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

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

------
azharcs
I recommend Programming in C by Stephen Kochan <http://amzn.to/cXnEPp>

I have also heard good things about Illustrating C

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

------
alnayyir
Working on it...

~~~
brianwillis
Really? You're writing a C book?

~~~
alnayyir
Yessir

<http://hackerne.ws/item?id=1873413>

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

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

