Effective C, 2nd Edition

Book description

The latest release of the C programming language, C23, enhances the safety, security, and usability of the language. This second edition of Effective C has been thoroughly updated to cover C23, offering a modern introduction to C that will teach you best practices for writing professional, effective, and secure programs that solve real-world problems.

Effective C is a true product of the C community. Robert C. Seacord, a long-standing member of the C standards committee with over 40 years of programming experience, developed the book in collaboration with other C experts, such as Clangâ??s lead maintainer Aaron Ballman and C project editor JeanHeyd Meneide. Thanks to the efforts of this expert group, youâ??ll learn how to:

  • Develop professional C code that is fast, robust, and secure
  • Use objects, functions, and types effectively
  • Safely and correctly use integers and floating-point types
  • Manage dynamic memory allocation
  • Use strings and character types efficiently
  • Perform I/O operations using C standard streams and POSIX file descriptors
  • Make effective use of Câ??s preprocessor
  • Debug, test, and analyze C programs

The world runs on code written in C. Effective C will show you how to get the most out of the language and build robust programs that stand the test of time.

New to this edition: This edition has been extensively rewritten to align with modern C23 programming practices and leverage the latest C23 features.

Updated to cover C23

Publisher resources

View/Submit Errata

Table of contents

  1. Praise for Effective C
  2. Title Page
  3. Copyright
  4. Dedication
  5. About the Author, Contributor and Technical Reviewer
  6. Foreword to the Second Edition
  7. Foreword to the First Edition
  8. Acknowledgments
  9. Introduction
    1. A Brief History of C
    2. The C Standard
    3. The CERT C Coding Standard
    4. Common Weakness Enumeration
    5. Who This Book Is For
    6. What’s in This Book
  10. 1. Getting Started With C
    1. Developing Your First C Program
    2. Compiling and Running a Program
      1. Function Return Values
      2. Formatted Output
    3. Editors and Integrated Development Environments
    4. Compilers
      1. GNU Compiler Collection
      2. Clang
      3. Microsoft Visual Studio
    5. Portability
      1. Implementation-Defined Behavior
      2. Unspecified Behavior
      3. Undefined Behavior
      4. Locale-Specific Behavior and Common Extensions
    6. Summary
  11. 2. Objects, Functions, and Types
    1. Entities
    2. Declaring Variables
      1. Swapping Values, First Attempt
      2. Swapping Values, Second Attempt
    3. Object Types
      1. Boolean
      2. Character
      3. Arithmetic
      4. void
    4. Derived Types
      1. Function
      2. Pointer
      3. Array
      4. Structure
      5. Union
    5. Tags
    6. Type Qualifiers
      1. const
      2. volatile
      3. restrict
    7. Scope
    8. Storage Duration
    9. Storage Class
      1. static
      2. extern
      3. thread_local
      4. constexpr
      5. register
      6. typedef
      7. auto
    10. typeof Operators
    11. Alignment
    12. Variably Modified Types
    13. Attributes
    14. Summary
  12. 3. Arithmetic Types
    1. Integers
      1. Padding, Width, and Precision
      2. Integer Ranges
      3. Integer Declarations
      4. Unsigned Integers
      5. Signed Integers
      6. Bit-Precise Integer Types
      7. Integer Constants
    2. Floating-Point Representation
      1. Floating Types and Encodings
      2. C Floating-Point Model
      3. Floating-Point Arithmetic
      4. Floating-Point Values
      5. Floating Constants
    3. Arithmetic Conversion
      1. Integer Conversion Rank
      2. Integer Promotions
      3. Usual Arithmetic Conversions
      4. An Example of Implicit Conversion
      5. Safe Conversions
    4. Summary
  13. 4. Expressions and Operators
    1. Simple Assignment
    2. Evaluations
    3. Function Invocation
    4. Increment and Decrement Operators
    5. Operator Precedence and Associativity
    6. Order of Evaluation
      1. Unsequenced and Indeterminately Sequenced Evaluations
      2. Sequence Points
    7. sizeof Operator
    8. Arithmetic Operators
      1. Unary + and –
      2. Logical Negation
      3. Additive
      4. Multiplicative
    9. Bitwise Operators
      1. Complement
      2. Shift
      3. Bitwise AND
      4. Bitwise Exclusive OR
      5. Bitwise Inclusive OR
    10. Logical Operators
    11. Cast Operators
    12. Conditional Operator
    13. alignof Operator
    14. Relational Operators
    15. Compound Assignment Operators
    16. Comma Operator
    17. Pointer Arithmetic
    18. Summary
  14. 5. Control Flow
    1. Expression Statements
    2. Compound Statements
    3. Selection Statements
      1. if
      2. switch
    4. Iteration Statements
      1. while
      2. do…while
      3. for
    5. Jump Statements
      1. goto
      2. continue
      3. break
      4. return
    6. Summary
  15. 6. Dynamically Allocated Memory
    1. Storage Duration
      1. The Heap and Memory Managers
      2. When to Use Dynamically Allocated Memory
    2. Memory Management
      1. malloc
      2. aligned_alloc
      3. calloc
      4. realloc
      5. reallocarray
      6. free
      7. free_sized
      8. free_aligned_sized
    3. Memory States
    4. Flexible Array Members
    5. Other Dynamically Allocated Storage
      1. alloca
      2. Variable-Length Arrays
    6. Debugging Allocated Storage Problems
      1. dmalloc
      2. Safety-Critical Systems
    7. Summary
  16. 7. Characters and Strings
    1. Characters
      1. ASCII
      2. Unicode
      3. Source and Execution Character Sets
      4. Data Types
      5. Character Constants
      6. Escape Sequences
      7. Linux
      8. Windows
      9. Character Conversion
    2. Strings
    3. String Literals
    4. String-Handling Functions
      1. <string.h> and <wchar.h>
      2. Annex K Bounds-Checking Interfaces
      3. POSIX
      4. Microsoft
    5. Summary
  17. 8. Input/Output
    1. Standard I/O Streams
      1. Error and End-of-File Indicators
      2. Stream Buffering
      3. Predefined Streams
      4. Stream Orientation
      5. Text and Binary Streams
    2. Opening and Creating Files
      1. fopen
      2. open
    3. Closing Files
      1. fclose
      2. close
    4. Reading and Writing Characters and Lines
    5. Stream Flushing
    6. Setting the Position in a File
    7. Removing and Renaming Files
    8. Using Temporary Files
    9. Reading Formatted Text Streams
    10. Reading from and Writing to Binary Streams
    11. Endian
    12. Summary
  18. 9. Preprocessor
    1. The Compilation Process
    2. File Inclusion
    3. Conditional Inclusion
      1. Generating Diagnostics
      2. Using Header Guards
    4. Macro Definitions
      1. Macro Replacement
      2. Type-Generic Macros
      3. Embedded Binary Resources
      4. Predefined Macros
    5. Summary
  19. 10. Program Structure
    1. Principles of Componentization
      1. Coupling and Cohesion
      2. Code Reuse
      3. Data Abstractions
      4. Opaque Types
    2. Executables
    3. Linkage
    4. Structuring a Simple Program
    5. Building the Code
    6. Summary
  20. 11. Debugging, Testing, and Analysis
    1. Assertions
      1. Static Assertions
      2. Runtime Assertions
    2. Compiler Settings and Flags
      1. GCC and Clang Flags
      2. Visual C++ Options
    3. Debugging
    4. Unit Testing
    5. Static Analysis
    6. Dynamic Analysis
    7. AddressSanitizer
      1. Running the Tests
      2. Instrumenting the Code
      3. Running the Instrumented Tests
    8. Summary
    9. Future Directions
  21. Appendix: The Fifth Edition of the C Standard (C23)
    1. Attributes
    2. Keywords
    3. Integer Constant Expressions
    4. Enumeration Types
    5. Type Inference
    6. typeof Operators
      1. K&R C Functions
      2. Preprocessor
      3. Integer Types and Representations
    7. unreachable Function-Like Macro
    8. Bit and Byte Utilities
    9. IEEE Floating-Point Support
  22. References
  23. Index

Product information

  • Title: Effective C, 2nd Edition
  • Author(s): Robert C. Seacord
  • Release date: October 2024
  • Publisher(s): No Starch Press
  • ISBN: 9781718504127