skip to main content
Skip header Section
21st Century C: C Tips from the New SchoolNovember 2012
Publisher:
  • O'Reilly Media, Inc.
ISBN:978-1-4493-2714-9
Published:05 November 2012
Pages:
296
Skip Bibliometrics Section
Bibliometrics
Skip Abstract Section
Abstract

Q&A with "21st Century C" author Ben Klemens "C has no corporation or foundation pushing it, no trademarked logos, no ad budget, and no designers on perpetual book tour. It's just a simple, fast language." O'Reilly Media: What made you write the book? Ben Klemens: I kept having the same conversation, with the same disconnect. I would talk about how I was having an easy time writing code in C, and the person I was talking to would tell me that doing so is impossible, because everything needs to be written from scratch and there is endless declaration and memory management cruft obscuring the real flow of the code. Finally, I got a friend to concede that maybe I wasn't hallucinating, and maybe I really was writing C code in reasonable time and with reasonable clarity, and he asked me, "OK, so can you refer me to a textbook that teaches C with a modern style?" And finally, I understood the disconnect, because I couldn't find such a text. Everything I read was at least a decade old and had a section teaching you how to re-implement linked lists yet again. The C standard was revised in 1999, allowing for a much more flowing writing style, yet these books still presented code with a halting and obtuse style. ORM: Why is your book especially important now? BK: I should've written it five years ago. ORM: What is on the horizon for your readers? BK: I have it easy here, because C is still immensely common. You can find measures that say that it is the most popular computing language, and some that rank it at maybe No. 2 or No. 3. What makes this amazing about our 40-year-old friend is that it has no corporation or foundation pushing it, no trademarked logos, no ad budget, and no designers on perpetual book tour. It's just a simple, fast language. And it's not going away very quickly. I have no idea what we'll be writing 50 years from now, but I'm pretty confident that in five or 10 years, we'll still be writing a lot of C code. Also, C is still the lingua franca of computing. So many languages have a back door that lets you write some code in C, so even people who long ago decided to avoid C eventually find themselves writing little snippets in it. By the way, I'm obviously a satisfied user, but I come neither to praise or to bury C. The language has warts, many of which I point out in the book along the way to pointing out its better features. ORM: What is the single most important take-away from your book? BK: Use libraries. If you don't know how to link to a binary tree library, you are doomed to write your own binary tree implementation. I think a lot of the people who think everything in C has to be written from scratch have just never learned how to reliably link their code to the wealth of existing libraries. ORM: Who is your intended audience? BK: I have two. People who only know a smattering of C, which they picked up on the street. People who learned C from a more traditional textbook, and know that they need to update from how C was written in the 1980s. Probably included here are CS majors who were traumatized by their college C textbook and decided they hate C, that it is an unmerciful disaster, and so begins a downward spiral where they avoid writing in C, so when they do have some C-based problem it is a melancholy burden, so after getting that done they avoid writing any more C for as long as possible and thus their skills get rustier. This book can help to break the cycle.

Contributors

Recommendations

Reviews

Edgar R. Chavez

Whether you love C, hate C, or have no opinion, you cannot ignore C. A great deal of code is written in C. It is at the core of Unix and the Internet, and practically every computer now in use has a compiler for it. Many languages can link to it, and it is widely used to get close to the hardware. Although a number of newer languages are derived from it, C is likely to survive for the foreseeable future. Thankfully, recent updates have served to make it a much better language to work with. If C has a problem, it may have more to do with an outdated perception of what coding in C used to be like than what it can actually be like today. That is precisely one of the premises of this book, and the author aims to rectify the situation. The book is intended for programmers who know only a smattering of C or who learned the language from a more traditional textbook. I would add that it could also be useful for programmers who are not using the new features added to the language by the C99 and C11 updates. Coding in modern C involves three components: creating an environment that includes tools for compilation, linking, testing, debugging, documentation, version control, and distribution; using modern C features and idioms; and using appropriate libraries. The author lays out his recommendations and explains in considerable detail how they can be put to work. The book is organized into two parts: Part 1, "The Environment," contains chapters 1 through 5, and Part 2, "The Language," contains chapters 6 through 12. The book concludes with an epilogue, a glossary, a bibliography, and a 13-page index. Three chapters are noteworthy. Chapter 5, "Playing Nice with Others," describes the details of interfaces between Python and C. Chapter 11, "Object-Oriented Programming in C," is best summarized by a quotation from page 200: "To develop control structures and build functions that make use of [object-oriented programming (OOP) features], C syntax easily provides the 10 percent of the edifice that creates 90 percent of the structure." Chapter 12, "Libraries," offers examples that use GLib, POSIX features of the standard C library (including Pthreads), the GNU Scientific Library, SQLite, libxml, and cURL. I liked the author's recommendations about setting up an environment. While recognizing that many people prefer to work within the comforting confines of an integrated development environment (IDE), the book recommends setting up a Unix-based environment (even allowing for the idiosyncrasies of a major player, Microsoft, who likes to do things its own way) to keep control of your own destiny. Some of the recommendations stand out, such as using make files rather than shell scripts, using Git for version control, using Doxygen and CWEB for documentation, and using autotools for code packaging and distribution. In his review of C, the author covers a great deal of ground, though not every feature and nuance of the language. He has strong opinions, which he lays out in detail. Among the features he favors are macros with a variable number of arguments and structures with designated initializers. On the other hand, he considers functions with a variable number of inputs to be broken, and carefully explains their limitations and proper use. He explains how to avoid memory allocations. He also mentions features that should be considered obsolete or deprecated, and reviews pointers—for many, a treacherous, swampy bog—in detail so that they can be used effectively while avoiding needless and dangerous overuse. This book exceeded my expectations. The writing is clear and upbeat, not ponderous or patronizing. While I did not agree with every word and every recommendation, I found plenty of material to consider and a lot more to learn than I anticipated. The author clearly knows his stuff and has carefully studied the language definition; his opinions matter. At times, I wished his examples were more extensive or his explanations more detailed. On the other hand, a couple of times, while conceding his point, I decided to ignore this or that recommendation. Occasionally, his sample code used one trick too many and would be clearer if it didn't try to be quite so concise. This book deserves very close scrutiny, ideally by running, modifying, or extending every example. Whether you are new to C or a longtime user, you will find plenty of opportunities to learn and to improve your knowledge of the language. Online Computing Reviews Service

Access critical reviews of Computing literature here

Become a reviewer for Computing Reviews.