Fluent C

Book description

Expert advice on C programming is hard to find. While much help is available for object-oriented programming languages, there's surprisingly little for the C language. With this hands-on guide, beginners and experienced C programmers alike will find guidance about design decisions, including how to apply them bit by bit to running code examples when building large-scale programs.

Christopher Preschern, a leading member of the design patterns community, answers questions such as how to structure C programs, cope with error handling, or design flexible interfaces. Whether you're looking for one particular pattern or an overview of design options for a specific topic, this book shows you how to implement hands-on design knowledge specifically for the C programming language.

You'll find design patterns for:

  • Error handling
  • Returning error information
  • Memory management
  • Returning data from C functions
  • Data lifetime and ownership
  • Flexible APIs
  • Flexible iterator interfaces
  • Organizing files in modular programs
  • Escaping #ifdef Hell

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. Why I Wrote This Book
    2. Patterns Basics
    3. How to Read This Book
    4. Overview of the Patterns
    5. Conventions Used in This Book
    6. Using Code Examples
    7. O’Reilly Online Learning
    8. How to Contact Us
    9. Acknowledgments
  2. I. C Patterns
  3. 1. Error Handling
    1. Running Example
    2. Function Split
    3. Guard Clause
    4. Samurai Principle
    5. Goto Error Handling
    6. Cleanup Record
    7. Object-Based Error Handling
    8. Summary
    9. Further Reading
    10. Outlook
  4. 2. Returning Error Information
    1. Running Example
    2. Return Status Codes
    3. Return Relevant Errors
    4. Special Return Values
    5. Log Errors
    6. Summary
    7. Further Reading
    8. Outlook
  5. 3. Memory Management
    1. Data Storage and Problems with Dynamic Memory
    2. Running Example
    3. Stack First
    4. Eternal Memory
    5. Lazy Cleanup
    6. Dedicated Ownership
    7. Allocation Wrapper
    8. Pointer Check
    9. Memory Pool
    10. Summary
    11. Further Reading
    12. Outlook
  6. 4. Returning Data from C Functions
    1. Running Example
    2. Return Value
    3. Out-Parameters
    4. Aggregate Instance
    5. Immutable Instance
    6. Caller-Owned Buffer
    7. Callee Allocates
    8. Summary
    9. Outlook
  7. 5. Data Lifetime and Ownership
    1. Stateless Software-Module
    2. Software-Module with Global State
    3. Caller-Owned Instance
    4. Shared Instance
    5. Summary
    6. Further Reading
    7. Outlook
  8. 6. Flexible APIs
    1. Header Files
    2. Handle
    3. Dynamic Interface
    4. Function Control
    5. Summary
    6. Further Reading
    7. Outlook
  9. 7. Flexible Iterator Interfaces
    1. Running Example
    2. Index Access
    3. Cursor Iterator
    4. Callback Iterator
    5. Summary
    6. Further Reading
    7. Outlook
  10. 8. Organizing Files in Modular Programs
    1. Running Example
    2. Include Guard
    3. Software-Module Directories
    4. Global Include Directory
    5. Self-Contained Component
    6. API Copy
    7. Summary
    8. Outlook
  11. 9. Escaping #ifdef Hell
    1. Running Example
    2. Avoid Variants
    3. Isolated Primitives
    4. Atomic Primitives
    5. Abstraction Layer
    6. Split Variant Implementations
    7. Summary
    8. Further Reading
    9. Outlook
  12. II. Pattern Stories
  13. 10. Implementing Logging Functionality
    1. The Pattern Story
      1. File Organization
      2. Central Logging Function
      3. Logging Source Filter
      4. Conditional Logging
      5. Multiple Logging Destinations
      6. File Logging
      7. Cross-Platform Files
      8. Using the Logger
    2. Summary
  14. 11. Building a User Management System
    1. The Pattern Story
      1. Data Organization
      2. File Organization
      3. Authentication: Error Handling
      4. Authentication: Error Logging
      5. Adding Users: Error Handling
      6. Iterating
      7. Using the User Management System
    2. Summary
  15. 12. Conclusion
    1. What You’ve Learned
    2. Further Reading
    3. Closing Remarks
  16. Index
  17. About the Author

Product information

  • Title: Fluent C
  • Author(s): Christopher Preschern
  • Release date: October 2022
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781492097334