You are previewing C in a Nutshell, 2nd Edition.
O'Reilly logo
C in a Nutshell, 2nd Edition

Book Description

The new edition of this classic O’Reilly reference provides clear, detailed explanations of every feature in the C language and runtime library, including multithreading, type-generic macros, and library functions that are new in the 2011 C standard (C11). If you want to understand the effects of an unfamiliar function, and how the standard library requires it to behave, you’ll find it here, along with a typical example. Ideal for experienced C and C++ programmers, this book also includes popular tools in the GNU software collection.

Table of Contents

  1. Preface
    1. How This Book Is Organized
      1. Part I
      2. Part II
      3. Part III
    2. Further Reading
    3. Conventions Used in This Book
    4. Using Code Examples
    5. Safari® Books Online
    6. How to Contact Us
    7. Acknowledgments
      1. Peter
      2. Tony
  2. I. Language
  3. 1. Language Basics
    1. Characteristics of C
    2. The Structure of C Programs
    3. Source Files
    4. Comments
    5. Character Sets
      1. Wide Characters and Multibyte Characters
      2. Universal Character Names
      3. Digraphs and Trigraphs
    6. Identifiers
      1. Identifier Name Spaces
      2. Identifier Scope
    7. How the C Compiler Works
      1. The C Compiler’s Translation Phases
      2. Tokens
  4. 2. Types
    1. Typology
    2. Integer Types
      1. Integer Types Defined in Standard Headers
    3. Floating-Point Types
    4. Complex Floating-Point Types
    5. Enumerated Types
    6. The Type void
      1. void in Function Declarations
      2. Expressions of Type void
      3. Pointers to void
    7. The Alignment of Objects in Memory
  5. 3. Literals
    1. Integer Constants
    2. Floating-Point Constants
      1. Decimal Floating-Point Constants
      2. Hexadecimal Floating-Point Constants
    3. Character Constants
      1. Types and Values of Character Constants
      2. Escape Sequences
    4. String Literals
  6. 4. Type Conversions
    1. Conversion of Arithmetic Types
      1. Hierarchy of Types
      2. Integer Promotion
      3. Usual Arithmetic Conversions
      4. Other Implicit Type Conversions
      5. The Results of Arithmetic Type Conversions
    2. Conversion of Nonarithmetic Types
      1. Array and Function Designators
      2. Explicit Pointer Conversions
      3. Implicit Pointer Conversions
      4. Conversions Between Pointer and Integer Types
  7. 5. Expressions and Operators
    1. How Expressions Are Evaluated
      1. Generic Selections (C11)
      2. Lvalues
      3. Side Effects and Sequence Points
      4. Operator Precedence and Associativity
    2. Operators in Detail
      1. Arithmetic Operators
      2. Assignment Operators
      3. Increment and Decrement Operators
      4. Comparative Operators
      5. Logical Operators
      6. Bitwise Operators
      7. Memory Addressing Operators
      8. Other Operators
    3. Constant Expressions
      1. Integer Constant Expressions
      2. Other Constant Expressions
  8. 6. Statements
    1. Expression Statements
    2. Block Statements
    3. Loops
      1. while Statements
      2. for Statements
      3. do…while Statements
      4. Nested Loops
    4. Selection Statements
      1. if Statements
      2. switch Statements
    5. Unconditional Jumps
      1. The break Statement
      2. The continue Statement
      3. The goto Statement
      4. The return Statement
  9. 7. Functions
    1. Function Definitions
      1. Functions and Storage Class Specifiers
      2. K&R-Style Function Definitions
      3. Function Parameters
      4. Arrays as Function Parameters
      5. The main() Function
    2. Function Declarations
      1. Declaring Optional Parameters
      2. Declaring Variable-Length Array Parameters
    3. How Functions Are Executed
    4. Pointers as Arguments and Return Values
    5. Inline Functions
    6. Non-Returning Functions
    7. Recursive Functions
    8. Variable Numbers of Arguments
  10. 8. Arrays
    1. Defining Arrays
      1. Fixed-Length Arrays
      2. Variable-Length Arrays
    2. Accessing Array Elements
    3. Initializing Arrays
      1. Writing Initialization Lists
      2. Initializing Specific Elements
    4. Strings
    5. Multidimensional Arrays
      1. Matrices
      2. Declaring Multidimensional Arrays
      3. Initializing Multidimensional Arrays
    6. Arrays as Arguments of Functions
  11. 9. Pointers
    1. Declaring Pointers
      1. Null Pointers
      2. void Pointers
      3. Initializing Pointers
    2. Operations with Pointers
      1. Using Pointers to Read and Modify Objects
      2. Modifying and Comparing Pointers
    3. Pointers and Type Qualifiers
      1. Constant Pointers and Pointers to Constant Objects
      2. Restricted Pointers
    4. Pointers to Arrays and Arrays of Pointers
      1. Array Pointers
      2. Pointer Arrays
    5. Pointers to Functions
  12. 10. Structures, Unions, and Bit-Fields
    1. Structures
      1. Defining Structure Types
      2. Structure Objects and typedef Names
      3. Incomplete Structure Types
      4. Accessing Structure Members
      5. Initializing Structures
      6. Initializing Specific Members
      7. Structure Members in Memory
      8. Flexible Structure Members
      9. Pointers as Structure Members
    2. Unions
      1. Defining Union Types
      2. Initializing Unions
    3. Anonymous Structures and Unions
    4. Bit-Fields
  13. 11. Declarations
    1. Object and Function Declarations
      1. Examples
      2. Storage Class Specifiers
      3. Type Qualifiers
      4. Declarations and Definitions
      5. Complex Declarators
    2. Type Names
    3. typedef Declarations
    4. _Static_assert Declarations
    5. Linkage of Identifiers
      1. External Linkage
      2. Internal Linkage
      3. No Linkage
    6. Storage Duration of Objects
      1. Static Storage Duration
      2. Thread Storage Duration
      3. Automatic Storage Duration
    7. Initialization
      1. Implicit Initialization
      2. Explicit Initialization
  14. 12. Dynamic Memory Management
    1. Allocating Memory Dynamically
    2. Characteristics of Allocated Memory
    3. Resizing and Releasing Memory
    4. An All-Purpose Binary Tree
    5. Characteristics
    6. Implementation
      1. Generating an Empty Tree
      2. Inserting New Data
      3. Finding Data in the Tree
      4. Removing Data from the Tree
      5. Traversing a Tree
      6. A Sample Application
  15. 13. Input and Output
    1. Streams
      1. Text Streams
      2. Binary Streams
    2. Files
      1. File Position
      2. Buffers
      3. The Standard Streams
    3. Opening and Closing Files
      1. Opening a File
      2. Access Modes
      3. Closing a File
    4. Reading and Writing
      1. Byte-Oriented and Wide-Oriented Streams
      2. Error Handling
      3. Unformatted I/O
      4. Formatted Output
      5. Formatted Input
    5. Random File Access
      1. Obtaining the Current File Position
      2. Setting the File Access Position
  16. 14. Multithreading
    1. Threads
      1. Creating Threads
      2. Other Thread Functions
    2. Accessing Shared Data
      1. Mutual Exclusion
      2. Atomic Objects
      3. Atomic Operations
      4. Memory Ordering
      5. Fences
    3. Communication Between Threads: Condition Variables
    4. Thread-Local Objects and Thread-Specific Storage
      1. Using Thread-Local Objects
      2. Using Thread-Specific Storage
  17. 15. Preprocessing Directives
    1. Inserting the Contents of Header Files
      1. How the Preprocessor Finds Header Files
      2. Nested #include Directives
    2. Defining and Using Macros
      1. Macros Without Parameters
      2. Macros with Parameters
      3. Using Macros Within Macros
      4. Macro Scope and Redefinition
    3. Type-generic Macros
    4. Conditional Compiling
      1. The #if and #elif Directives
      2. The defined Operator
      3. The #ifdef and #ifndef Directives
    5. Defining Line Numbers
    6. Generating Error Messages
    7. The #pragma Directive
    8. The _Pragma Operator
    9. Predefined Macros
      1. Conditionally Defined Macros
  18. II. Standard Library
  19. 16. The Standard Headers
    1. Using the Standard Headers
      1. Execution Environments
      2. Function and Macro Calls
      3. Reserved Identifiers
    2. Functions with Bounds-Checking
      1. Availability
      2. Runtime Constraints
    3. Contents of the Standard Headers
      1. assert.h
      2. complex.h
      3. ctype.h
      4. errno.h
      5. fenv.h
      6. float.h
      7. inttypes.h
      8. iso646.h
      9. limits.h
      10. locale.h
      11. math.h
      12. setjmp.h
      13. signal.h
      14. stdalign.h
      15. stdarg.h
      16. stdatomic.h
      17. stdbool.h
      18. stddef.h
      19. stdint.h
      20. stdio.h
      21. stdlib.h
      22. stdnoreturn.h
      23. string.h
      24. tgmath.h
      25. threads.h
      26. time.h
      27. uchar.h
      28. wchar.h
      29. wctype.h
  20. 17. Functions at a Glance
    1. Input and Output
    2. Mathematical Functions
      1. Mathematical Functions for Integer Types
      2. Floating-Point Functions
      3. Function-Like Macros
      4. Pragmas for Arithmetic Operations
      5. The Floating-Point Environment
      6. Error Handling
    3. Character Classification and Conversion
      1. Character Classification
      2. Case Mapping
    4. String Processing
    5. Multibyte Characters
    6. Converting Between Numbers and Strings
    7. Searching and Sorting
    8. Memory Block Handling
    9. Dynamic Memory Management
    10. Date and Time
    11. Process Control
      1. Communication with the Operating System
      2. Signals
    12. Internationalization
    13. Nonlocal Jumps
    14. Multithreading (C11)
      1. Thread Functions
      2. Atomic Operations
    15. Debugging
    16. Error Messages
  21. 18. Standard Library Functions
  22. III. Basic Tools
  23. 19. Compiling with GCC
    1. The GNU Compiler Collection
    2. Obtaining and Installing GCC
    3. Compiling C Programs with GCC
      1. Step by Step
      2. Multiple Input Files
      3. Dynamic Linking and Shared Object Files
      4. Freestanding Programs
    4. C Dialects
    5. Compiler Warnings
    6. Optimization
      1. The -O Levels
      2. The -f Flags
      3. Floating-Point Optimization
      4. Architecture-Specific Optimization
      5. Why Not Optimize?
    7. Debugging
    8. Profiling
    9. Option and Environment Variable Summary
      1. Command-Line Options
      2. Environment Variables
  24. 20. Using make to Build C Programs
    1. Targets, Prerequisites, and Commands
    2. The Makefile
    3. Rules
      1. The Command Script
      2. Pattern Rules
      3. Suffix Rules
      4. Built-In Rules
      5. Implicit Rule Chains
      6. Double-Colon Rules
    4. Comments
    5. Variables
      1. Assignment Operators
      2. Variables and Whitespace
      3. Target-Specific Variable Assignments
      4. The Automatic Variables
      5. Other Built-In Variables
      6. Environment Variables
    6. Phony Targets
    7. Other Target Attributes
    8. Macros
    9. Functions
      1. Built-In Functions
      2. User-Defined Functions
    10. Directives
      1. Conditionals
      2. Includes
      3. Other Directives
    11. Running make
      1. Generating Header Dependencies
      2. Recursive make Commands
      3. Command-Line Options
      4. Special Targets Used as Runtime Options
      5. GCC Options for Generating Makefile Rules
  25. 21. Debugging C Programs with GDB
    1. Installing GDB
    2. A Sample Debugging Session
      1. Symbol Information
      2. Finding a Bug
    3. Starting GDB
      1. Command-Line Arguments
      2. Command-Line Options
      3. Initialization Files
    4. Using GDB Commands
      1. Command Completion
      2. Displaying Help for Commands
      3. Status Information
      4. Running a Program in the Debugger
      5. Displaying Source Code
      6. Working with Breakpoints
      7. Resuming Execution After a Break
      8. Analyzing the Stack
      9. Displaying Data
      10. Watchpoints: Observing Operations on Variables
    5. Analyzing Core Files in GDB
  26. 22. Using an IDE with C
    1. IDEs for C
    2. The Eclipse IDE for C/C++
      1. Installing Eclipse CDT
      2. Running Eclipse
      3. Perspectives and Views
    3. Developing a C Program with Eclipse
      1. Creating a New C Project
      2. Editing
      3. Compiling and Running a Program
      4. Project Properties
    4. Debugging a C Program in Eclipse
      1. Starting the Debugger
      2. Setting Breakpoints
      3. Controlling Program Execution in the Debugger
    5. Further Information on Eclipse
  27. Index