Chapter 1. Preface: C is punk rock
Is it really punk rock
Like the party line?
Wilco ``Too far apart''
C has only a handful of keywords and is a bit rough around the edges, and it rocks. You can do anything with it. Like the C, G, and D chords on a guitar, you can teach yourself in a couple of hours, and then spend the rest of your life getting better. The people who dont get it fear its power and think it too edgy to be safe. By all rankings, it is consistently the most popular language that doesnt have a corporation or foundation spending money to make sure youve heard of it.[]
Also, the language is about forty years old, which makes it middle-aged. It was written by a few guys basically working against managementthe perfect punk rock originsbut that was in the 1970s, and theres been a lot of time for the language to go mainstream.
What did people do when punk rock went mainstream? In the decades since its advent in the 1970s, punk certainly has come in from the fringes: The Clash, The Offspring, Green Day, and The Strokes sold millions of albums worldwide (to name just a few punk offshoots that came to mind), Billy Idol got the MTV generation to think of wearing studded clothing as normal, and I have heard lite instrumental versions of songs from the punk spinoff known as grunge at my local supermarket. The former lead singer of Sleater-Kinney now has a popular sketch comedy show ( Portlandia ) that frequently lampoons punk rockers.[] One reaction to the continuing evolution would be to take the hard line, and say that the original stuff was punk, and everything else is just easy punk pop for the masses. The traditionalists can still play their albums from the 70s, and if the grooves are worn out they can search for a digitally remastered edition to download. They can buy Ramones hoodies for their toddlers.
Outsiders dont get it. Some of them hear the word punk and picture something out of the 1970sa historic artifact about some kids that were, at the time, really doing something different. The traditionalist punks who still love and play their 1973 Iggy Pop LPs are having their fun, but they bolster the impression that punk is ossified and no longer relevant.
Getting back to the world of C, we have both the traditionalists, waving the banner of ANSI `89, and those who will rock out to whatever works and may not even realize that the code they are writing would not have compiled or run in the 1990s. Outsiders dont get the difference. They see still-in-print books from the 1980s and still-online tutorials from the 1990s, they hear from the hardcore traditionalists who insist on still writing like that today, and dont even know that the language and the rest of its users continue to evolve. Thats a shame, because theyre missing out on some great stuff.
This is a book about breaking tradition and keeping C punk rock. I really dont care to compare the code in this book to the original C specification in Kernighan & Ritchies 1978 book. My telephone has 512 megabytes of memory, so why are our C textbooks still spending pages upon pages covering techniques to shave kilobytes off of our executables? I am writing this on a bottom-of-the-line red netbook that can accommodate 3,200,000,000 instructions per second; what do I care about whether an operation requires comparing eight bits or sixteen? We should be writing code that we can write quickly and that is readable by our fellow humans. Were still writing in C, so our readable but imperfectly optimized code will still run an order of magnitude faster than if wed written comparable code in any of a number of alternative, bloated languages.
Q & A [Or, the parameters of the book]
Q: How is this C book different from all others?
A: C textbooks are a pretty uniform bunch (Ive read a lot of them, including ). Most were written before the C99 standard simplified many aspects of usage, and you can tell that some of those now in their N th edition just pasted in a few notes about updates rather than really rethinking how to use the language. They all mention that there might be libraries that you could use in writing your own code, but they predate the installation tools and ecosystem we have now that make using those libraries reliable and reasonably portable. Those textbooks are still valid and still have value, but modern C code just doesnt look like the code in those textbooks.
This book picks up where they left off, reconsidering the language and the ecosystem in which it lives. The storyline here is about using libraries that provide linked lists and XML parsers, not writing new ones from stratch. It is about writing code that is readable and function interfaces that are user-friendly.
Q: Who is this book for? Do I need to be a coding guru?
A: You have experience coding in any language, maybe Java or a scripting language like Perl. I dont have to sell you on why your code shouldnt just be one long routine with no subfunctions.
You have some knowledge of C, but dont worry if you dont know it too wellas Ill detail, theres a lot youre better off never learning. If you are a blank slate with respect to C syntax, it really is an aggressively simple language, and your search engine will point you to dozens of C tutorials online; if you have experience with another language, you should be able to get the basics in an hour or two.
I might as well point out to you that I have also written a textbook on statistical and scientific computing, Modeling with Data . Along with lots of details of dealing with numeric data and using statistical models for describing data, it has a standalone tutorial on C, which I naturally think overcomes many of the failings of older C tutorials.
Q: Im an application programmer, not a kernel hacker. Why should I use C instead of a quick-to-write scripting language like Python?
A: If you are an applications programmer, this book is for you. I read people asserting that C is a systems language, which impresses me as so un-punkwho are they to tell us what were allowed to write?
Statements in the way of our language is almost as fast as C, but is easier to write are so common that they are almost clich. Well, C is definitely as fast as C, and the purpose of this book is to show you that C is easier to write than the textbooks from decades passed imply that it is. You dont have to call malloc
and get elbow-deep in memory management half as often as the systems programmers of the 1990s did, we have facilities for easier string handling, and even the core syntax has evolved to make for more legible code.
I started writing C in earnest because I had to speed up a simulation in a scripting language, R. Like so many other scripting languages, R has a C interface and encourages the user to make use of it any time the host language is too slow. Eventually I had so many functions jumping out from the R script to C code that I just dropped the host language entirely. Next thing you know, Im writing a book on modern C technique.
Q: Its nice that application programmers coming from scripting languages will like this book, but I am a kernel hacker. I taught myself C in fifth grade and sometimes have dreams that correctly compile. What new material can there be for me?