I have this problem where if I read a book such as the K&R, I become fearful that I haven't learnt enough, until I've done all the exercises it asks of me; The K&R is a difficult book to many (C is a first language here), did you do and complete all exercises in the book? And if you didn't, are they all necessary in becoming competent in C ?
Doing all the exercises in K&R will not make you competent in C. Writing lots of code will. Doing the exercises will enable you to write lots of code. I think I did most of them.
C is a bit of an antiquated language today and I am not sure if becoming competent in it is a desirable way to spend one's time, but the K&R book has such a clear and concise way of introducing the reader into the concepts of (low-level) programming that there is definitely value in reading it today, even if we don't have to deal with register variables in today's interpreted languages anymore.
I can't speak specifically about the K&R book as I haven't had the time or need to learn C that well yet. However in other programming books, I try to do all of the exercises if I can.
It's really a matter of preference, but when I read a programming book, if I'm learning a totally new language concept (I started out in C++ and then learned PHP, for example) I'll take the time to learn the language by following the exercises to the best of my ability. I do this mainly because the differences between C++ and PHP are so major that I felt I needed to do them to make sure I understood the language.
Other books, I'll only go through the exercises mentally. I can usually understand code and mentally compile what will happen pretty well. If I can't do that, then I know that I need to actually try it out to make sure I understand it.
Of course practice of any kind is usually good anyway.
It's not necessary or practical to do every exercise. You will still be introduced to many C idioms and general techniques even if you just read the book. Many of the exercises get your brain working -- which is great -- but they are more often than not time-intensive, which is not so great when there are many, many exercises.
K&R was written in such a fabulous way that even if you never write a single line of code while reading, you will still come out with more knowledge than you started with.
I don't think I've done all of them even now, over twenty-five years later, but I'm pretty sure I could do any of them.
What's more, I did do some of the trickier exercises - like the bfree()
function that allows you to free a block of memory that was not originally allocated via malloc()
. I remain surprised, in some respects, that it hasn't made it into the main stream libraries, but it hasn't. My version dates from June 1990 (I checked the code) and that's nearly seven years after I started learning C. Of course, that code has to be part of a complete memory management package (but K&R show you a basic package); you can't just bolt it onto any old malloc()
library without know how the library handles memory.
I wouldn't say you have to actually do all the exercises. But you should spend enough time thinking about how you would do them to be moderately sure you could. And, it would be good practice - and practice is what makes you better. And it would reassure you that you have understood the language. (Note that there is The C Answer Book which contains explained answers to the exercises.)
The exercises are useful if you have no other means of practice. Such as in a programming class. But if you're out there in the world, especially if you get to use C normally, then your work (or hobby) is equally good as practice. And if you don't use it, is it necessary to learn everything there is to know about it?