21st Century C

Book description

Throw out your old ideas of C, and relearn a programming language that’s substantially outgrown its origins. With 21st Century C, you’ll discover up-to-date techniques that are absent from every other C text available. C isn’t just the foundation of modern programming languages, it is a modern language, ideal for writing efficient, state-of-the-art applications. Learn to dump old habits that made sense on mainframes, and pick up the tools you need to use this evolved and aggressively simple language. No matter what programming language you currently champion, you’ll agree that C rocks.

  • 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 which older C concepts should be downplayed or deprecated
  • Explore problematic C concepts that are too useful to throw out
  • Solve C’s string-building problems with C-standard and POSIX-standard functions
  • Use modern syntactic features for functions that take structured inputs
  • Build high-level object-based libraries and programs
  • Apply existing C libraries for doing advanced math, talking to Internet servers, and running databases

Publisher resources

View/Submit Errata

Table of contents

  1. A Note Regarding Supplemental Files
  2. Preface
    1. C Is Punk Rock
    2. Q & A (Or, the Parameters of the Book)
    3. Standards: So Many to Choose From
      1. The POSIX Standard
    4. Some Logistics
      1. Conventions Used in This Book
      2. Using Code Examples
      3. Safari® Books Online
      4. How to Contact Us
      5. Acknowledgments
    5. Content Updates
      1. June 14, 2013
  3. I. The Environment
    1. 1. Set Yourself Up for Easy Compilation
      1. Use a Package Manager
      2. Compiling C with Windows
        1. POSIX for Windows
        2. Compiling C with POSIX
        3. Compiling C Without POSIX
      3. Which Way to the Library?
        1. A Few of My Favorite Flags
        2. Paths
        3. Runtime Linking
      4. Using Makefiles
        1. Setting Variables
        2. The Rules
      5. Using Libraries from Source
      6. Using Libraries from Source (Even if Your Sysadmin Doesn’t Want You To)
      7. Compiling C Programs via Here Document
        1. Include Header Files from the Command Line
        2. The Unified Header
        3. Here Documents
        4. Compiling from stdin
    2. 2. Debug, Test, Document
      1. Using a Debugger
        1. GDB Variables
        2. Print Your Structures
      2. Using Valgrind to Check for Errors
      3. Unit Testing
        1. Using a Program as a Library
        2. Coverage
      4. Interweaving Documentation
        1. Doxygen
          1. The narrative
        2. Literate Code with CWEB
      5. Error Checking
        1. What Is the User’s Involvement in the Error?
        2. The Context in Which the User Is Working
        3. How Should the Error Indication Be Returned?
    3. 3. Packaging Your Project
      1. The Shell
        1. Replacing Shell Commands with Their Outputs
        2. Use the Shell’s for Loops to Operate on a Set of Files
        3. Test for Files
        4. fc
      2. Makefiles vs. Shell Scripts
      3. Packaging Your Code with Autotools
        1. An Autotools Demo
        2. Describing the Makefile with Makefile.am
          1. Form variables
          2. Content variables
          3. Adding testing
          4. Adding makefile bits
        3. The configure Script
          1. More Bits of Shell
    4. 4. Version Control
      1. Changes via diff
      2. Git’s Objects
        1. The Stash
      3. Trees and Their Branches
        1. Merging
        2. The Rebase
      4. Remote Repositories
    5. 5. Playing Nice with Others
      1. The Process
        1. Writing to Be Read by Nonnatives
        2. The Wrapper Function
        3. Smuggling Data Structures Across the Border
        4. Linking
      2. Python Host
        1. Compiling and Linking
        2. The Conditional Subdirectory for Automake
        3. Distutils Backed with Autotools
  4. II. The Language
    1. 6. Your Pal the Pointer
      1. Automatic, Static, and Manual Memory
      2. Persistent State Variables
      3. Pointers Without malloc
        1. Structures Get Copied, Arrays Get Aliased
        2. malloc and Memory-Twiddling
        3. The Fault Is in Our Stars
        4. All the Pointer Arithmetic You Need to Know
          1. Typedef as a teaching tool
    2. 7. C Syntax You Can Ignore
      1. Don’t Bother Explicitly Returning from main
      2. Let Declarations Flow
        1. Set Array Size at Runtime
      3. Cast Less
      4. Enums and Strings
      5. Labels, gotos, switches, and breaks
        1. goto Considered
        2. switch
      6. Deprecate Float
      7. Comparing Unsigned Integers
    3. 8. Obstacles and Opportunity
      1. Cultivate Robust and Flourishing Macros
        1. Preprocessor Tricks
      2. Linkage with static and extern
        1. Externally Linked Variables in Header Files
      3. The const Keyword
        1. Noun-Adjective Form
        2. Tension
        3. Depth
        4. The char const ** Issue
    4. 9. Text
      1. Making String Handling Less Painful with asprintf
        1. Security
        2. Constant Strings
        3. Extending Strings with asprintf
      2. A Pæan to strtok
      3. Unicode
        1. The Encoding for C Code
        2. Unicode Libraries
        3. The Sample Code
    5. 10. Better Structures
      1. Compound Literals
        1. Initialization via Compound Literals
      2. Variadic Macros
      3. Safely Terminated Lists
      4. Foreach
      5. Vectorize a Function
      6. Designated Initializers
      7. Initialize Arrays and Structs with Zeros
      8. Typedefs Save the Day
        1. A Style Note
      9. Return Multiple Items from a Function
        1. Reporting Errors
      10. Flexible Function Inputs
        1. Declare Your Function as printf-Style
        2. Optional and Named Arguments
        3. Polishing a Dull Function
      11. The Void Pointer and the Structures It Points To
        1. Functions with Generic Inputs
        2. Generic Structures
    6. 11. Object-Oriented Programming in C
      1. What You Don’t Get (and Why You Won’t Miss It)
        1. Scope
          1. Private struct elements
        2. Overloaded with Operator Overloading
          1. _Generic
      2. Extending Structures and Dictionaries
        1. Extending a Structure
          1. C, with fewer seams
        2. Implementing a Dictionary
        3. Base Your Code on Pointers to Objects
      3. Functions in Your Structs
      4. Count References
        1. Example: A Substring Object
        2. An Agent-Based Model of Group Formation
    7. 12. Libraries
      1. GLib
      2. POSIX
        1. Using mmap for Gigantic Data Sets
        2. Easy Threading with Pthreads
          1. The pthreads checklist
          2. Protect threaded resources with mutexes
          3. The example
          4. _Thread_local and static variables
      3. The GNU Scientific Library
      4. SQLite
        1. The Queries
      5. libxml and cURL
  5. Epilogue
  6. Glossary
  7. Bibliography
  8. Index
  9. About the Author
  10. Colophon
  11. Copyright

Product information

  • Title: 21st Century C
  • Author(s): Ben Klemens
  • Release date: October 2012
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781449327149