2n d Ed ■■ Set up a C programming environment with shell facilities, makefiles, text editors, debuggers, and memory checkers ■■ Use Autotools, C’s de facto cross-platform package manager ■■ Learn about the problematic C concepts too useful to discard ■■ Solve C’s string-building problems with C-standard functions ■■ Use modern syntactic features for functions that take structured inputs ■■ Build high-level, object-based libraries and programs ■■ Perform advanced math, talk to internet servers, and run databases with existing C libraries and cc? Does C memory management still plague you as it did in the '90s? Ben Klemens capably addresses these and other common problems, demonstrating how tools have emerged to make C programming easier— helping you debug, track down memory leaks, organize the compilation process, and manage source code versions ” —Dave Kitabjian Director of Software Development, NetCarrier Telecom This edition also includes new material on concurrent threads, virtual tables, C99 numeric types, and other features C Twitter: @oreillymedia facebook.com/oreilly Klemens US $49.99 21st Century C TIPS FROM THE NEW SCHOOL Ben Klemens has written statistical analyses and computationally intensive models for the Brookings Institution, World Bank, National Institute of Mental Health, and the US government He has also worked with Brookings and the Free Software Foundation to ensure that authors retain the right to use the software they write He currently leads a statistical computing group in the research division of the United States Census Bureau PROGR AMMING/C on C isn’t just the foundation of modern programming languages; it is a modern language, ideal for writing efficient, state-of-the-art applications Get past idioms that made sense on mainframes and learn the tools you need to work with this evolved and aggressively simple language No matter what programming language you currently favor, you’ll quickly see that 21st century C rocks your C development “Isenvironment limited to vi SECOND EDITION 21st Century C Throw out your old ideas about C and get to know a programming language that has substantially outgrown its origins With this revised edition of 21st Century C, you’ll discover up-to-date techniques missing from other C tutorials, whether you’re new to the language or just getting reacquainted iti 21st Century C CAN $52.99 ISBN: 978-1-491-90389-6 Ben Klemens www.it-ebooks.info 2n d Ed ■■ Set up a C programming environment with shell facilities, makefiles, text editors, debuggers, and memory checkers ■■ Use Autotools, C’s de facto cross-platform package manager ■■ Learn about the problematic C concepts too useful to discard ■■ Solve C’s string-building problems with C-standard functions ■■ Use modern syntactic features for functions that take structured inputs ■■ Build high-level, object-based libraries and programs ■■ Perform advanced math, talk to internet servers, and run databases with existing C libraries and cc? Does C memory management still plague you as it did in the '90s? Ben Klemens capably addresses these and other common problems, demonstrating how tools have emerged to make C programming easier— helping you debug, track down memory leaks, organize the compilation process, and manage source code versions ” —Dave Kitabjian Director of Software Development, NetCarrier Telecom This edition also includes new material on concurrent threads, virtual tables, C99 numeric types, and other features C Twitter: @oreillymedia facebook.com/oreilly Klemens US $49.99 21st Century C TIPS FROM THE NEW SCHOOL Ben Klemens has written statistical analyses and computationally intensive models for the Brookings Institution, World Bank, National Institute of Mental Health, and the US government He has also worked with Brookings and the Free Software Foundation to ensure that authors retain the right to use the software they write He currently leads a statistical computing group in the research division of the United States Census Bureau PROGR AMMING/C on C isn’t just the foundation of modern programming languages; it is a modern language, ideal for writing efficient, state-of-the-art applications Get past idioms that made sense on mainframes and learn the tools you need to work with this evolved and aggressively simple language No matter what programming language you currently favor, you’ll quickly see that 21st century C rocks your C development “Isenvironment limited to vi SECOND EDITION 21st Century C Throw out your old ideas about C and get to know a programming language that has substantially outgrown its origins With this revised edition of 21st Century C, you’ll discover up-to-date techniques missing from other C tutorials, whether you’re new to the language or just getting reacquainted iti 21st Century C CAN $52.99 ISBN: 978-1-491-90389-6 Ben Klemens www.it-ebooks.info SECOND EDITION 21st Century C Ben Klemens www.it-ebooks.info 21st Century C, Second Edition by Ben Klemens Copyright © 2015 Ben Klemens All rights reserved Printed in the United States of America Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472 O’Reilly books may be purchased for educational, business, or sales promotional use Online editions are also available for most titles (http://www.safaribooksonline.com) For more information, contact our cor‐ porate/institutional sales department: 800-998-9938 or corporate@oreilly.com Editors: Rachel Roumeliotis and Allyson MacDonald Production Editor: Nicole Shelby Copyeditor: Becca Freed Proofreader: Amanda Kersey September 2014: Indexer: Judy McConville Interior Designer: David Futato Cover Designer: Karen Montgomery Illustrator: Rebecca Demarest Second Edition Revision History for the Second Edition 2014-09-24: First Release See http://oreilly.com/catalog/errata.csp?isbn=9781491903896 for release details The O’Reilly logo is a registered trademark of O’Reilly Media, Inc 21st Century C, the cover image, and related trade dress are trademarks of O’Reilly Media, Inc Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trade‐ mark claim, the designations have been printed in caps or initial caps While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work Use of the information and instructions contained in this work is at your own risk If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights 978-1-491-90389-6 [LSI] www.it-ebooks.info Table of Contents Preface ix Part I The Environment Set Yourself Up for Easy Compilation Use a Package Manager Compiling C with Windows POSIX for Windows Compiling C with POSIX Compiling C Without POSIX Which Way to the Library? A Few of My Favorite Flags Paths Runtime Linking Using Makefiles Setting Variables The Rules Using Libraries from Source Using Libraries from Source (Even if Your Sysadmin Doesn’t Want You To) Compiling C Programs via Here Document Include Header Files from the Command Line The Unified Header Here Documents Compiling from stdin 4 10 11 14 15 15 18 22 23 25 25 26 27 28 Debug, Test, Document 31 Using a Debugger 31 iii www.it-ebooks.info A Debugging Detective Story GDB Variables Print Your Structures Using Valgrind to Check for Errors Unit Testing Using a Program as a Library Coverage Error Checking What is the User’s Involvement in the Error? The Context in Which the User is Working How Should the Error Indication Be Returned? Interweaving Documentation Doxygen Literate Code with CWEB 34 43 44 48 50 53 54 55 56 57 59 59 60 61 Packaging Your Project 65 The Shell Replacing Shell Commands with Their Outputs Use the Shell’s for Loops to Operate on a Set of Files Test for Files fc Makefiles vs Shell Scripts Packaging Your Code with Autotools An Autotools Demo Describing the Makefile with Makefile.am The configure Script 66 67 68 70 73 75 77 79 82 87 Version Control 93 Changes via diff Git’s Objects The Stash Trees and Their Branches Merging The Rebase Remote Repositories 94 95 99 100 101 103 104 Playing Nice with Others 107 Dynamic Loading The Limits of Dynamic Loading The Process Writing to Be Read by Nonnatives The Wrapper Function iv | 107 110 110 111 111 Table of Contents www.it-ebooks.info Smuggling Data Structures Across the Border Linking Python Host Compiling and Linking The Conditional Subdirectory for Automake Distutils Backed with Autotools Part II 112 114 114 116 116 118 The Language Your Pal the Pointer 123 Automatic, Static, and Manual Memory Persistent State Variables Pointers Without malloc Structures Get Copied, Arrays Get Aliased malloc and Memory-Twiddling The Fault Is in Our Stars All the Pointer Arithmetic You Need to Know Typedef as a teaching tool 123 128 129 131 134 135 136 139 Inessential C Syntax that Textbooks Spend a Lot of Time Covering 141 Don’t Bother Explicitly Returning from main Let Declarations Flow Set Array Size at Runtime Cast Less Enums and Strings Labels, gotos, switches, and breaks goto Considered switch Deprecate Float Comparing Unsigned Integers Safely Parse Strings to Numbers 141 142 144 145 147 148 149 152 153 156 156 Important C Syntax that Textbooks Often Do Not Cover 161 Cultivate Robust and Flourishing Macros The Preprocessor Test Macros Header Guards Linkage with static and extern Externally Linked Variables in Header Files The const Keyword Noun-Adjective Form 161 166 170 172 174 175 177 178 Table of Contents www.it-ebooks.info | v Tension Depth The char const ** Issue 179 179 180 Easier Text Handling 185 Making String Handling Less Painful with asprintf Security Constant Strings Extending Strings with asprintf A Pæan to strtok Unicode The Encoding for C Code Unicode Libraries The Sample Code 185 188 189 191 192 197 199 200 201 10 Better Structures 205 Compound Literals Initialization via Compound Literals Variadic Macros Safely Terminated Lists Multiple Lists Foreach Vectorize a Function Designated Initializers Initialize Arrays and Structs with Zeros Typedefs Save the Day A Style Note Return Multiple Items from a Function Reporting Errors Flexible Function Inputs Declare Your Function as printf-Style Optional and Named Arguments Polishing a Dull Function The Void Pointer and the Structures It Points To Functions with Generic Inputs Generic Structures 206 207 208 209 210 212 212 214 216 217 218 220 221 223 224 226 228 234 234 239 11 Object-Oriented Programming in C 245 Extending Structures and Dictionaries Implementing a Dictionary C, with fewer seams Functions in Your Structs vi | 247 249 253 258 Table of Contents www.it-ebooks.info Vtables Scope Private Struct Elements Overload _Generic Count References Example: A Substring Object Example: An Agent-Based Model of Group Formation Conclusion 262 267 268 270 271 274 275 279 286 12 Parallel Threads 289 The Environment The Ingredients OpenMP Compiling OpenMP, pthreads, and C atoms Interference Map-reduce Multiple Tasks Thread Local Localizing Nonstatic Variables Shared Resources Atoms Pthreads C atoms Atomic structs 290 291 292 294 295 296 297 298 300 300 305 308 312 315 13 Libraries 321 GLib POSIX Parsing Regular Expressions Using mmap for Gigantic Data Sets The GNU Scientific Library SQLite The Queries libxml and cURL 321 322 322 327 330 332 334 335 Epilogue 341 A C 101 343 Table of Contents www.it-ebooks.info | vii Glossary 363 References 367 Index 371 viii | Table of Contents www.it-ebooks.info Harbison, S P and G L Steele Jr (1991) C: A Reference Manual (3rd ed.) Prentice Hall Kernighan, B W and D M Ritchie (1978) The C Programming Language (1st ed.) Prentice Hall Kernighan, B W and D M Ritchie (1988) The C Programming Language (2nd ed.) Prentice Hall Klemens, B (2008) Modeling with Data: Tools and Techniques for Statistical Comput‐ ing Princeton University Press Kochan, S G (2004) Programming in C (3rd ed.) Sams van der Linden, P (1994) Expert C Programming: Deep C Secrets Prentice Hall Meyers, S (2000, February) How non-member functions improve encapsulation C/C++ Users Journal Meyers, S (2005) Effective C++: 55 Specific Ways to Improve Your Programs and Designs (3rd ed.) Addison-Wesley Professional Nabokov, V (1962) Pale Fire G P Putnams’s Sons Norman, D A (2002) The Design of Everyday Things Basic Books Oliveira, S and D E Stewart (2006) Writing Scientific Software: A Guide to Good Style Cambridge University Press Oram, A and Talbott, T (1991) Managing Projects with Make O’Reilly Media Oualline, S (1997) Practical C Programming (3rd ed.) O’Reilly Media Page, A., K Johnston, and B Rollison (2008) How We Test Software at Microsoft Microsoft Press Perry, G (1994) Absolute Beginner’s Guide to C (2nd ed.) Sams Prata, S (2004) The Waite Group’s C Primer Plus (5th ed.) Waite Group Press Press, W H., B P Flannery, S A Teukolsky, and W T Vetterling (1988) Numerical Recipes in C: The Art of Scientific Computing Cambridge University Press Press, W H., B P Flannery, S A Teukolsky, and W T Vetterling (1992) Numerical Recipes in C: The Art of Scientific Computing (2nd ed.) Cambridge University Press Prinz, P and T Crawford (2005) C in a Nutshell O’Reilly Media Spolsky, J (2008) More Joel on Software: Further Thoughts on Diverse and Occasion‐ ally Related Matters That Will Prove of Interest to Software Developers, Designers, and to Those Who, Whether by Good Fortune or Ill Luck, Work with Them in Some Capacity Apress 368 | Bibliography www.it-ebooks.info Stallman, R M., R Pesch, and S Shebs (2002) Debugging with GDB: The GNU Source-Level Debugger Free Software Foundation Stroustrup, B (1986) The C++ Programming Language Addison-Wesley Ullman, L and M Liyanage (2004) C Programming Peachpit Press Bibliography www.it-ebooks.info | 369 www.it-ebooks.info Index Symbols # (octothorp), 166 #define, 294 #endif, 173 #if 0, 173 #ifdef, 173 #ifndef, 173 #include, #pragma, 294 #pragma once, 173 $*, 18 $[...]... gcc, clang, and icc (Intel C Compiler) • -std=gnu11 is clang- and gcc-specific, and specifies that the compiler should allow code conforming to the C1 1 and POSIX standards (plus some GNU exten‐ sions) As of this writing, clang will default to using the C9 9 standard, and gcc the C8 9 standard If your copy of gcc, clang, or icc predates C1 1, use -std=gnu99 to get it to use C9 9 The POSIX standard specifies... ISO C9 9 The C standard underwent a major revision a decade later Additions were made for numeric and scientific computing, with a standard type for complex numbers and some type-generic functions A few conveniences from C+ + got lifted, including one-line comments (which originally came from one of C s predecessor languages, BCPL) and being able to declare variables at the head of for loops Using structures... Logistics The Second Edition I used to be a cynic and think that people just wrote second editions to disrupt all the people selling used copies of the first edition But this second edition actually could not have happened without the first being published, and could not have happened sooner (and most of the book’s readers are reading electronic copies anyway) The big addition from the first edition. .. GNU Compiler Collection (gcc) on your POSIX box—even BSD uses it Again, the gcc defines a de facto standard that extends C and POSIX in a few ways, and I will be explicit when making use of those extensions Legally, the BSD license is slightly more permissive than the GNU license Because some parties are deeply concerned with the political and business implications of the licenses, one can typically... specifies that c9 9 be present on your system, so the compiler-agnostic version of the above line for compiling C9 9 code would be: c9 9 erf .c -o erf -lm -g -Wall -O3 In the following makefiles, I get this effect by setting the variable CC =c9 9 Depending on the vintage of your Mac, c9 9 may be a specially hacked version of gcc, which is probably not what you want If you have a version of c9 9 that halts... with POSIX Microsoft provides a C+ + compiler, in the form of Visual Studio, which has a C8 9 compatibility mode (commonly referred to as ANSI C, even though C1 1 is the cur‐ rent ANSI standard) This is the only means of compiling C code currently provided by Microsoft Many representatives from the company have made it clear that any‐ thing beyond support for a few C9 9 features (let alone C1 1 support)... keeping C punk rock I don’t care to com‐ pare the code in this book to the original C specification in Kernighan & Ritchie’s 1978 book My telephone has 512 MB of memory, so why are our C textbooks still spending pages upon pages covering techniques to shave kilobytes off of our executa‐ bles? I am writing this on a bottom-of-the-line red netbook that can accommodate 3,200,000,000 instructions per second; ... I discuss various environment variables that affect compilation, including paths for searching for files That’s not just for POSIX: Windows has envi‐ ronment variables as well, which you can find in the system settings segment of the control panel Cygwin is much more usable if you add its bin directory (probably c: \cygwin\bin) to the Windows PATH Now you can get to compiling C code Compiling C with... follow what’s going on, then change this to -O0 This will be a common tweak in the CFLAGS variable, later This works for gcc, clang, and icc • -Wall adds compiler warnings This works for gcc, clang, and icc For icc, you might prefer -w1, which displays the compiler’s warnings, but not its remarks 10 | Chapter 1: Set Yourself Up for Easy Compilation www.it-ebooks.info ... ple, both the gcc and the BSD’s clang are top-notch C compilers The authors from both camps closely watch and learn from each other’s work, so we can expect that the differences that currently exist will tend to even out over time The Legal Sidebar US law no longer has a registration system for copyright: with few exceptions, as soon as anybody writes something down, it is copyrighted Of course, distribution ... specifies a default of CC =c9 9, but current editions of GNU make set CC=cc, which is typically a link to gcc In the minimal makefile at the head of this segment, $(CC) is explicitly set to c9 9,... will be a common tweak in the CFLAGS variable, later This works for gcc, clang, and icc • -Wall adds compiler warnings This works for gcc, clang, and icc For icc, you might prefer -w1, which displays... language you currently favor, you’ll quickly see that 21st century C rocks your C development “Isenvironment limited to vi SECOND EDITION 21st Century C Throw out your old ideas about C and get