Skip to content

The C programming language by Kernighan and Ritchie – First impression

I’m halfway through the well-known book “The C Programming Language” by Kernighan and Ritchie (I will just refer to it as K&R). In this post, I will share my opinion about it.

An unintended read

I had bookmarked K&R as one of the books I wanted to read but I wasn’t planning on reading it anytime soon. Particularly because I didn’t want to embark in learning a new language before learning Python in greater detail.

After finishing SICP, I wanted to continue with the next book proposed in the online curriculum of teach yourself CS (you can see other options here). I was very excited about it, having been reading SICP already for a few months.

The C programming language K&R book cover
The C Programming Language book cover

After rushing a bit through the last part of SICP, I was finally ready to start reading Computer Systems: A Programmer’s Perspective (CS:APP). To increase my excitement, I had even read on reddit about other people praising it.

The day came and as I was reading CS:APP preface, I came upon some bad news. The book was written in C and assumed readers were already familiar with it. Initially, I thought that I could learn C as I read the book. After all, I had learned a bit of C when I took CS50 on edX . But the preface also mentioned the classic K&R and recommended it for those who wanted to learn C.

That’s when I decided to follow CS:APP’s advice. I decided to get familiar enough with C by studying K&R before reading it.

K&R: a book for beginners?

Most reviews I’ve read about K&R don’t recommend it for people that are just starting to learn C. While I’m a beginner in programming, I had the chance of learning the basics of C in CS50. The very first chapter of K&R is an overview of C that doesn’t care about introducing the details. In this sense, I’d say K&R is better suited for people that already have some experience with C or that are familiar with other programming languages. For example, CS:APP mentioned that previous experience with Java was sufficient to start reading it without priorly learning C.

With that being said, I consider K&R a great book. I will explain my reasons now.

An excellent book

I’m glad to say that the reputation of K&R is well deserved. The fact that it was written more than 30 years ago (it was published in 1988) doesn’t make it outdated. If anything, it has proved that K&R has passed the test of time, which is very rare in the rapidly changing world of computer science. On to what makes it so good:

Concise

The main portion of the book is less than 200 pages (the rest are appendices with manuals and reference material). In its preface, the authors mention that C isn’t a big language and as such, a long book wouldn’t serve it well. In that sense, the book is intentionally short. The same spirit of simplicity and conciseness that you can see in C, is present in the book. This is greatly appreciated, almost every sentence serves a purpose.

Masterful

Maybe the most important traits you look for in a programming book is that its author is an experienced programmer. In K&R this couldn’t be more the case. One of its authors, Dennis Ritchie, is the very creator of C. The other one, Brian Kernighan, actively participated in the development of UNIX which revolves around C programming (many command line tools are written in C).

The mastery of both authors in C is made clear throughout the book. The functioning of the C language is explained with clarity and detail for every line of code presented. For example, here I first learned that statements such as:

a = b = c = 1;

Are evaluated from right to left, and that the result of each assignment pair is the type and value of the left-hand operand. In that sense, the latter statement is evaluated as:

a = (b = (c = 1));

Besides a complete understanding of how C handles expressions, the authors offer almost an insight into the logic behind C. That is, not only how things were done, but also why. For example, I always found the pointer notation to be kind of arcane. K&R mentioned that the declaration of a pointer associated with a type, as in int *ip, was intended as a mnemonic. Showing that the pointer *ip could take the place of any variable of the same type:

int x, *ip;

x = 3 * 5;
x += 2;
/* equivalent statements using pointer *ip */    
*ip = 3 * 5;  
*ip += 2;

Exercise progression

There are two things that I value about K&R exercises: 1) there aren’t many of them and 2) they are built upon the programs presented in the book.

The first point is related to the conciseness of the book. While obviously more practice through more exercises means more learning, in K&R there’s more care about quality than quantity. After solving the few exercises presented, I feel that I have enough understanding to proceed on to the next subject and also to use it on more elaborate programs.

Secondly, I appreciate that many of the programs presented in the book are related to real UNIX command tools. For example, in the very first chapter, a simplified implementation of the command tool to count words (wc) is presented. Besides that, many of the exercises are presented as improvements over a program presented in the book. Like handling exceptions or extending its functionality. This process isn’t only enjoyable, but also puts into practice a very important concept in programming: iteration. By this, I refer to the practice of continuous improvement and building of complexity starting with simpler cases.

In conclusion

I haven’t finished K&R yet to offer a complete review, but I’m already quite convinced of its great value. I highly recommend it to learn C (algthough not for complete beginners). Besides all the positive aspects which I mentioned in this post, it’s simply just well written. Unlike most introductory books to a programming language, which tend to be full of syntax rules, K&R is fun to read and immediately shows its applications in interesting programs.

Published inArticles

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *