You are previewing 21st Century C, 2nd Edition.
O'Reilly logo
21st Century C, 2nd Edition

Book Description

Throw out your old ideas of C, and relearn a programming language that’s 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. 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.

Table of Contents

  1. 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. The Second Edition
      2. Conventions Used in This Book
      3. Using Code Examples
      4. Safari® Books Online
      5. How to Contact Us
      6. Acknowledgments
  2. I. The Environment
  3. 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
  4. 2. Debug, Test, Document
    1. Using a Debugger
      1. A Debugging Detective Story
      2. GDB Variables
      3. Print Your Structures
    2. Using Valgrind to Check for Errors
    3. Unit Testing
      1. Using a Program as a Library
      2. Coverage
    4. 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?
    5. Interweaving Documentation
      1. Doxygen
      2. Literate Code with CWEB
  5. 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
      3. The configure Script
  6. 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
  7. 5. Playing Nice with Others
    1. Dynamic Loading
      1. The Limits of Dynamic Loading
    2. The Process
      1. Writing to Be Read by Nonnatives
      2. The Wrapper Function
      3. Smuggling Data Structures Across the Border
      4. Linking
    3. Python Host
      1. Compiling and Linking
      2. The Conditional Subdirectory for Automake
      3. Distutils Backed with Autotools
  8. II. The Language
  9. 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
      5. Typedef as a teaching tool
  10. 7. Inessential C Syntax that Textbooks Spend a Lot of Time Covering
    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
    8. Safely Parse Strings to Numbers
  11. 8. Important C Syntax that Textbooks Often Do Not Cover
    1. Cultivate Robust and Flourishing Macros
      1. The Preprocessor
      2. Test Macros
      3. Header Guards
    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
  12. 9. Easier Text Handling
    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
  13. 10. Better Structures
    1. Compound Literals
      1. Initialization via Compound Literals
    2. Variadic Macros
    3. Safely Terminated Lists
    4. Multiple Lists
    5. Foreach
    6. Vectorize a Function
    7. Designated Initializers
    8. Initialize Arrays and Structs with Zeros
    9. Typedefs Save the Day
      1. A Style Note
    10. Return Multiple Items from a Function
      1. Reporting Errors
    11. Flexible Function Inputs
      1. Declare Your Function as printf-Style
      2. Optional and Named Arguments
      3. Polishing a Dull Function
    12. The Void Pointer and the Structures It Points To
      1. Functions with Generic Inputs
      2. Generic Structures
  14. 11. Object-Oriented Programming in C
    1. Extending Structures and Dictionaries
      1. Implementing a Dictionary
      2. C, with fewer seams
    2. Functions in Your Structs
      1. Vtables
    3. Scope
      1. Private Struct Elements
    4. Overload
      1. _Generic
    5. Count References
      1. Example: A Substring Object
      2. Example: An Agent-Based Model of Group Formation
      3. Conclusion
  15. 12. Parallel Threads
    1. The Environment
      1. The Ingredients
    2. OpenMP
      1. Compiling OpenMP, pthreads, and C atoms
      2. Interference
      3. Map-reduce
      4. Multiple Tasks
    3. Thread Local
      1. Localizing Nonstatic Variables
    4. Shared Resources
      1. Atoms
    5. Pthreads
    6. C atoms
      1. Atomic structs
  16. 13. Libraries
    1. GLib
    2. POSIX
      1. Parsing Regular Expressions
      2. Using mmap for Gigantic Data Sets
    3. The GNU Scientific Library
    4. SQLite
      1. The Queries
    5. libxml and cURL
  17. Epilogue
  18. A. C 101
    1. The Structure
      1. C requires a compilation step, which consists of running a <span xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" class="keep-together">single command</span>
      2. There’s a standard library, and it’s part of your operating system
      3. There’s a preprocessor
      4. There are two types of comment
      5. There is no print keyword
    2. Variable Declarations
      1. Variables have to be declared
      2. Even functions have to be declared or defined
      3. Basic types can be aggregated into arrays and structs
      4. New structure types can be defined
      5. You can find out how much space a type takes
      6. There is no special string type
    3. Expressions
      1. The scoping rules for C are very simple
      2. The main function is special
      3. Most of what a C program actually does is evaluate expressions
      4. Functions are evaluated using copies of the inputs
      5. Expressions are delimited by semicolons
      6. There are many shortcuts for incrementing or scaling a variable
      7. C has an expansive definition of truth
      8. Dividing two integers always produces an integer
      9. C has a trinary conditional operator
      10. Branching and looping expressions are not very different from any other language
      11. The for loop is just a compact version of the while loop
    4. Pointers
      1. You can directly request a block of memory
      2. Arrays are just blocks of memory; any block of memory can be used like an array
      3. A pointer to a scalar is really just a one-item array
      4. There is a special notation for elements of pointed-to structs
      5. Pointers let you modify function inputs
      6. Everything is somewhere, so everything can be pointed to
  19. Glossary
  20. References
  21. Index