C How to Program, Fifth Edition

Book description

The Deitels' groundbreaking How to Program series offers unparalleled breadth and depth of object-oriented programming concepts and intermediate-level topics for further study. Using the Deitels’ signature “Live-Code™ Approach,” this complete, authoritative introduction to C programming offers strong treatment of structured algorithm and program development in ANSI/ISO C with 150 working C programs. Includes rich, 300-page treatment of object-oriented programming in C++ that helps students interpret the code more effectively.

Table of contents

  1. Copyright
    1. Dedication
  2. Deitel® Series Page
    1. How to Program Series
    2. Simply Series
    3. Also Available
  3. Preface
    1. Before You Begin
      1. Installing the Microsoft Visual C++ 2005 Express Edition Software
      2. C How to Program, 5/e Code Examples
      3. Additional Software Downloads
      4. Additional C and C++ Compilers and IDEs
    2. Features of C How to Program, Fifth Edition
      1. Interior Design
      2. Syntax Shading
      3. Code Highlighting
      4. Game Programming with the Allegro C Game Programming Library
      5. Sorting: A Deeper Look
      6. Introduction to C99
      7. Updated C++ Treatment
    3. Teaching Approach
      1. Live-Code Approach
      2. World Wide Web Access
      3. Objectives
      4. Quotations
      5. Outline
      6. Sections
      7. Thousands of Lines of Syntax-Highlighted Code in Hundreds of Example Programs (with Outputs)
      8. Illustrations/Figures
      9. Programming Tips
      10. Summary Bullets
      11. Terminology
      12. Self-Review Exercises and Answers
      13. Exercises
      14. Thousands of Index Entries
      15. “Double Indexing” of C Live-Code Examples
    4. Software Included with C How to Program, 5/e
      1. DIVE-INTO® Series Tutorials for Popular C and C++ Environments
      2. Additional C and C++ Compilers and IDEs
    5. Teaching Resources for C How to Program, 5/e
    6. Deitel® Buzz Online Free E-mail Newsletter
    7. What’s New at Deitel
      1. Free Content Initiative
      2. Resource Centers and the Deitel Internet Business Initiative
    8. Acknowledgments
      1. C How to Program, 4/e Post-Publication Reviewers
      2. C How to Program, 5/e Reviewers
      3. Allegro Reviewers
      4. C99 Reviewers
    9. About the Authors
    10. About Deitel & Associates, Inc.
  4. 1. Introduction to Computers, the Internet and the Web
    1. 1.1. Introduction
    2. 1.2. What Is a Computer?
    3. 1.3. Computer Organization
    4. 1.4. Early Operating Systems
    5. 1.5. Personal, Distributed and Client/Server Computing
    6. 1.6. Machine Languages, Assembly Languages and High-Level Languages
    7. 1.7. Fortran, COBOL, Pascal and Ada
    8. 1.8. History of C
    9. 1.9. C Standard Library
    10. 1.10. C++
    11. 1.11. Java
    12. 1.12. BASIC, Visual Basic, Visual C++, Visual C# and .NET
    13. 1.13. Key Software Trend: Object Technology
    14. 1.14. Typical C Program Development Environment
      1. Phase 1: Creating a Program
      2. Phases 2 and 3: Preprocessing and Compiling a C Program
      3. Phase 4: Linking
      4. Phase 5: Loading
      5. Phase 6: Execution
      6. Problems That May Occur at Execution Time
    15. 1.15. Hardware Trends
    16. 1.16. History of the Internet
    17. 1.17. History of the World Wide Web
    18. 1.18. Notes About C and This Book
    19. 1.19. Web Resources
      1. C Standard and Working Group
      2. Free C/C++ Compilers and Development Tools
    20. Summary
      1. Section 1.1 Introduction
      2. Section 1.2 What Is a Computer?
      3. Section 1.3 Computer Organization
      4. Section 1.4 Early Operating Systems
      5. Section 1.5 Personal, Distributed and Client/Server Computing
      6. Section 1.6 Machine Languages, Assembly Languages and High-Level Languages
      7. Section 1.7 Fortran, COBOL, Pascal and Ada
      8. Section 1.8 History of C
      9. Section 1.9 C Standard Library
      10. Section 1.10 C++
      11. Section 1.11 Java
      12. Section 1.12 BASIC, Visual Basic, Visual C++, Visual C# and .NET
      13. Section 1.13 Key Software Trend: Object Technology
      14. Section 1.14 Typical C Program Development Environment
      15. Section 1.15 Hardware Trends
      16. Section 1.16 History of the Internet
      17. Section 1.17 History of the World Wide Web
    21. Terminology
    22. Self-Review Exercises
    23. Answers to Self-Review Exercises
    24. Exercises
  5. 2. Introduction to C Programming
    1. 2.1. Introduction
    2. 2.2. A Simple C Program: Printing a Line of Text
    3. 2.3. Another Simple C Program: Adding Two Integers
    4. 2.4. Memory Concepts
    5. 2.5. Arithmetic in C
      1. Arithmetic Expressions in Straight-Line Form
      2. Parentheses for Grouping Subexpressions
      3. Rules of Operator Precedence
      4. Sample Algebraic and C Expressions
      5. Evaluation of a Second-Degree Polynomial
    6. 2.6. Decision Making: Equality and Relational Operators
    7. Summary
      1. Section 2.1 Introduction
      2. Section 2.2 A Simple C Program: Printing a Line of Text
      3. Section 2.3 Another Simple C Program: Adding Two Integers
      4. Section 2.4 Memory Concepts
      5. Section 2.5 Arithmetic in C
      6. Section 2.6 Decision Making: Equality and Relational Operators
    8. Terminology
    9. Self-Review Exercises
    10. Answers to Self-Review Exercises
    11. Exercises
  6. 3. Structured Program Development in C
    1. 3.1. Introduction
    2. 3.2. Algorithms
    3. 3.3. Pseudocode
    4. 3.4. Control Structures
    5. 3.5. The if Selection Statement
    6. 3.6. The if...else Selection Statement
    7. 3.7. The while Repetition Statement
    8. 3.8. Formulating Algorithms Case Study 1: Counter-Controlled Repetition
    9. 3.9. Formulating Algorithms with Top-Down, Stepwise Refinement Case Study 2: Sentinel-Controlled Repetition
    10. 3.10. Formulating Algorithms with Top-Down, Stepwise Refinement Case Study 3: Nested Control Structures
    11. 3.11. Assignment Operators
    12. 3.12. Increment and Decrement Operators
    13. Summary
      1. Section 3.1 Introduction
      2. Section 3.2 Algorithms
      3. Section 3.3 Pseudocode
      4. Section 3.4 Control Structures
      5. Section 3.5 The if Selection Statement
      6. Section 3.6 The if...else Selection Statement
      7. Section 3.7 The while Repetition Statement
      8. Section 3.8 Formulating Algorithms Case Study 1: Counter-Controlled Repetition
      9. Section 3.9 Formulating Algorithms with Top-Down, Stepwise Refinement Case Study 2: Sentinel-Controlled Repetition
      10. Section 3.11 Assignment Operators
      11. Section 3.12 Increment and Decrement Operators
    14. Terminology
    15. Self-Review Exercises
    16. Answers to Self-Review Exercises
    17. Exercises
      1. For Exercises 3.17 to 3.21, perform each of these steps
  7. 4. C Program Control
    1. 4.1. Introduction
    2. 4.2. Repetition Essentials
    3. 4.3. Counter-Controlled Repetition
    4. 4.4. for Repetition Statement
    5. 4.5. for Statement: Notes and Observations
    6. 4.6. Examples Using the for Statement
    7. 4.7. switch Multiple-Selection Statement
    8. 4.8. do...while Repetition Statement
    9. 4.9. break and continue Statements
    10. 4.10. Logical Operators
    11. 4.11. Confusing Equality (==) and Assignment (=) Operators
    12. 4.12. Structured Programming Summary
    13. Summary
      1. Section 4.2 Repetition Essentials
      2. Section 4.3 Counter-Controlled Repetition
      3. Section 4.4 for Repetition Statement
      4. Section 4.5 for Statement: Notes and Observations
      5. Section 4.6 Examples Using the for Statement
      6. Section 4.7 switch Multiple-Selection Statement
      7. Section 4.8 do...while Repetition Statement
      8. Section 4.9 break and continue Statements
      9. Section 4.10 Logical Operators
      10. Section 4.11 Confusing Equality (==) and Assignment (=) Operators
    14. Terminology
    15. Self-Review Exercises
    16. Answers to Self-Review Exercises
    17. Exercises
  8. 5. C Functions
    1. 5.1. Introduction
    2. 5.2. Program Modules in C
    3. 5.3. Math Library Functions
    4. 5.4. Functions
    5. 5.5. Function Definitions
    6. 5.6. Function Prototypes
    7. 5.7. Function Call Stack and Activation Records
    8. 5.8. Headers
    9. 5.9. Calling Functions: Call-by-Value and Call-by-Reference
    10. 5.10. Random Number Generation
    11. 5.11. Example: A Game of Chance
    12. 5.12. Storage Classes
    13. 5.13. Scope Rules
    14. 5.14. Recursion
    15. 5.15. Example Using Recursion: Fibonacci Series
    16. 5.16. Recursion vs. Iteration
    17. Summary
      1. Section 5.1 Introduction
      2. Section 5.2 Program Modules in C
      3. Section 5.3 Math Library Functions
      4. Section 5.4 Functions
      5. Section 5.5 Function Definitions
      6. Section 5.6 Function Prototypes
      7. Section 5.7 Function Call Stack and Activation Records
      8. Section 5.8 Headers
      9. Section 5.9 Calling Functions: Call-by-Value and Call-by-Reference
      10. Section 5.10 Random Number Generation
      11. Section 5.11 Example: A Game of Chance
      12. Section 5.12 Storage Classes
      13. Section 5.13 Scope Rules
      14. Section 5.14 Recursion
      15. Section 5.16 Recursion vs. Iteration
    18. Terminology
    19. Self-Review Exercises
    20. Answers to Self-Review Exercises
    21. Exercises
  9. 6. C Arrays
    1. 6.1. Introduction
    2. 6.2. Arrays
    3. 6.3. Defining Arrays
    4. 6.4. Array Examples
      1. Defining an Array and Using a Loop to Initialize the Array’s Elements
      2. Initializing an Array in a Definition with an Initializer List
      3. Specifying an Array’s Size with a Symbolic Constant and Initializing Array Elements with Calculations
      4. Summing the Elements of an Array
      5. Using Arrays to Summarize Survey Results
      6. Graphing Array Element Values with Histograms
      7. Rolling a Die 6000 Times and Summarizing the Results in an Array
      8. Using Character Arrays to Store and Manipulate Strings
      9. Static Local Arrays and Automatic Local Arrays
    5. 6.5. Passing Arrays to Functions
    6. 6.6. Sorting Arrays
    7. 6.7. Case Study: Computing Mean, Median and Mode Using Arrays
    8. 6.8. Searching Arrays
      1. Searching an Array with Linear Search
      2. Searching an Array with Binary Search
    9. 6.9. Multiple-Subscripted Arrays
    10. Summary
      1. Section 6.1 Introduction
      2. Section 6.2 Arrays
      3. Section 6.3 Defining Arrays
      4. Section 6.4 Array Examples
      5. Section 6.5 Passing Arrays to Functions
      6. Section 6.6 Sorting Arrays
      7. Section 6.7 Case Study: Computing Mean, Median and Mode Using Arrays
      8. Section 6.8 Searching Arrays
      9. Section 6.9 Multiple-Subscripted Arrays
    11. Terminology
    12. Self-Review Exercises
    13. Answers to Self-Review Exercises
    14. Exercises
    15. Recursion Exercises
    16. Special Section: Sudoku
  10. 7. C Pointers
    1. 7.1. Introduction
    2. 7.2. Pointer Variable Definitions and Initialization
    3. 7.3. Pointer Operators
    4. 7.4. Passing Arguments to Functions by Reference
    5. 7.5. Using the const Qualifier with Pointers
      1. Converting a String to Uppercase Using a Non-Constant Pointer to Non-Constant Data
      2. Printing a String One Character at a Time Using a Non-Constant Pointer to Constant Data
      3. Attempting to Modify a Constant Pointer to Non-Constant Data
      4. Attempting to Modify a Constant Pointer to Constant Data
    6. 7.6. Bubble Sort Using Call-by-Reference
    7. 7.7. sizeof Operator
    8. 7.8. Pointer Expressions and Pointer Arithmetic
    9. 7.9. Relationship between Pointers and Arrays
    10. 7.10. Arrays of Pointers
    11. 7.11. Case Study: Card Shuffling and Dealing Simulation
    12. 7.12. Pointers to Functions
      1. Using Function Pointers to Create a Menu-Driven System
    13. Summary
      1. Section 7.2 Pointer Variable Definitions and Initialization
      2. Section 7.3 Pointer Operators
      3. Section 7.4 Passing Arguments to Functions by Reference
      4. Section 7.5 Using the const Qualifier with Pointers
      5. Section 7.7 sizeof Operator
      6. Section 7.8 Pointer Expressions and Pointer Arithmetic
      7. Section 7.9 Relationship between Pointers and Arrays
      8. Section 7.10 Arrays of Pointers
      9. Section 7.12 Pointers to Functions
    14. Terminology
    15. Self-Review Exercises
    16. Answers to Self-Review Exercises
    17. Exercises
    18. Special Section: Building Your Own Computer
  11. 8. C Characters and Strings
    1. 8.1. Introduction
    2. 8.2. Fundamentals of Strings and Characters
    3. 8.3. Character-Handling Library
    4. 8.4. String-Conversion Functions
    5. 8.5. Standard Input/Output Library Functions
    6. 8.6. String-Manipulation Functions of the String-Handling Library
    7. 8.7. Comparison Functions of the String-Handling Library
    8. 8.8. Search Functions of the String-Handling Library
    9. 8.9. Memory Functions of the String-Handling Library
    10. 8.10. Other Functions of the String-Handling Library
    11. Summary
      1. Section 8.2 Fundamentals of Strings and Characters
      2. Section 8.3 Character-Handling Library
      3. Section 8.4 String-Conversion Functions
      4. Section 8.5 Standard Input/Output Library Functions
      5. Section 8.6 String-Manipulation Functions of the String-Handling Library
      6. Section 8.7 Comparison Functions of the String-Handling Library
      7. Section 8.8 Search Functions of the String-Handling Library
      8. Section 8.9 Memory Functions of the String-Handling Library
      9. Section 8.10 Other Functions of the String-Handling Library
    12. Terminology
    13. Self-Review Exercises
    14. Answers to Self-Review Exercises
    15. Exercises
    16. Special Section: Advanced String-Manipulation Exercises
    17. A Challenging String-Manipulation Project
  12. 9. C Formatted Input/Output
    1. 9.1. Introduction
    2. 9.2. Streams
    3. 9.3. Formatting Output with printf
    4. 9.4. Printing Integers
    5. 9.5. Printing Floating-Point Numbers
    6. 9.6. Printing Strings and Characters
    7. 9.7. Other Conversion Specifiers
    8. 9.8. Printing with Field Widths and Precision
    9. 9.9. Using Flags in the printf Format Control String
    10. 9.10. Printing Literals and Escape Sequences
    11. 9.11. Reading Formatted Input with scanf
    12. Summary
      1. Section 9.2 Streams
      2. Section 9.3 Formatting Output with printf
      3. Section 9.4 Printing Integers
      4. Section 9.5 Printing Floating-Point Numbers
      5. Section 9.6 Printing Strings and Characters
      6. Section 9.7 Other Conversion Specifiers
      7. Section 9.8 Printing with Field Widths and Precision
      8. Section 9.9 Using Flags in the printf Format Control String
      9. Section 9.10 Printing Literals and Escape Sequences
      10. Section 9.11 Formatting Input with scanf
    13. Terminology
    14. Self-Review Exercises
    15. Answers to Self-Review Exercises
    16. Exercises
  13. 10. C Structures, Unions, Bit Manipulations and Enumerations
    1. 10.1. Introduction
    2. 10.2. Structure Definitions
    3. 10.3. Initializing Structures
    4. 10.4. Accessing Members of Structures
    5. 10.5. Using Structures with Functions
    6. 10.6. typedef
    7. 10.7. Example: High-Performance Card Shuffling and Dealing Simulation
    8. 10.8. Unions
    9. 10.9. Bitwise Operators
      1. Displaying an Unsigned Integer in Bits
      2. Making Function displayBits More Scalable and Portable
      3. Using the Bitwise AND, Inclusive OR, Exclusive OR and Complement Operators
      4. Using the Bitwise Left- and Right-Shift Operators
      5. Bitwise Assignment Operators
    10. 10.10. Bit Fields
    11. 10.11. Enumeration Constants
    12. Summary
      1. Section 10.1 Introduction
      2. Section 10.2 Structure Definitions
      3. Section 10.3 Initializing Structures
      4. Section 10.4 Accessing Members of Structures
      5. Section 10.5 Using Structures with Functions
      6. Section 10.6 typedef
      7. Section 10.8 Unions
      8. Section 10.9 Bitwise Operators
      9. Section 10.10 Bit Fields
      10. Section 10.11 Enumeration Constants
    13. Terminology
    14. Self-Review Exercises
    15. Answers to Self-Review Exercises
    16. Exercises
  14. 11. C File Processing
    1. 11.1. Introduction
    2. 11.2. Data Hierarchy
    3. 11.3. Files and Streams
    4. 11.4. Creating a Sequential-Access File
    5. 11.5. Reading Data from a Sequential-Access File
    6. 11.6. Random-Access Files
    7. 11.7. Creating a Random-Access File
    8. 11.8. Writing Data Randomly to a Random-Access File
    9. 11.9. Reading Data from a Random-Access File
    10. 11.10. Case Study: Transaction-Processing Program
    11. Summary
      1. Section 11.1 Introduction
      2. Section 11.2 Data Hierarchy
      3. Section 11.3 Files and Streams
      4. Section 11.4 Creating a Sequential-Access File
      5. Section 11.5 Reading Data from a Sequential-Access File
      6. Section 11.6 Random-Access Files
      7. Section 11.7 Creating a Random-Access File
      8. Section 11.8 Writing Data Randomly to a Random-Access File
      9. Section 11.9 Reading Data from a Random-Access File
    12. Terminology
    13. Self-Review Exercises
    14. Answers to Self-Review Exercises
    15. Exercises
  15. 12. C Data Structures
    1. 12.1. Introduction
    2. 12.2. Self-Referential Structures
    3. 12.3. Dynamic Memory Allocation
    4. 12.4. Linked Lists
      1. Function insert
      2. Function delete
      3. Function printList
    5. 12.5. Stacks
      1. Function push
      2. Function pop
      3. Applications of Stacks
    6. 12.6. Queues
      1. Function enqueue
      2. Function dequeue
    7. 12.7. Trees
    8. Summary
      1. Section 12.1 Introduction
      2. Section 12.2 Self-Referential Structures
      3. Section 12.3 Dynamic Memory Allocation
      4. Section 12.4 Linked Lists
      5. Section 12.5 Stacks
      6. Section 12.6 Queues
      7. Section 12.7 Trees
    9. Terminology
    10. Self-Review Exercises
    11. Answers to Self-Review Exercises
    12. Exercises
    13. Special Section: Building Your Own Compiler
      1. First Pass
      2. Second Pass
      3. A Complete Example
      4. Step-by-Step View of the Compilation Process
  16. 13. C Preprocessor
    1. 13.1. Introduction
    2. 13.2. #include Preprocessor Directive
    3. 13.3. #define Preprocessor Directive: Symbolic Constants
    4. 13.4. #define Preprocessor Directive: Macros
    5. 13.5. Conditional Compilation
    6. 13.6. #error and #pragma Preprocessor Directives
    7. 13.7. # and ## Operators
    8. 13.8. Line Numbers
    9. 13.9. Predefined Symbolic Constants
    10. 13.10. Assertions
    11. Summary
      1. Section 13.1 Introduction
      2. Section 13.2 #include Preprocessor Directive
      3. Section 13.3 #define Preprocessor Directive: Symbolic Constants
      4. Section 13.4 #define Preprocessor Directive: Macros
      5. Section 13.5 Conditional Compilation
      6. Section 13.6 #error and #pragma Preprocessor Directives
      7. Section 13.7 # and ## Operators
      8. Section 13.8 Line Numbers
      9. Section 13.9 Predefined Symbolic Constants
      10. Section 13.10 Assertions
    12. Terminology
    13. Self-Review Exercises
    14. Answers to Self-Review Exercises
    15. Exercises
  17. 14. Other C Topics
    1. 14.1. Introduction
    2. 14.2. Redirecting Input/Output on Linux/UNIX and Windows Systems
    3. 14.3. Variable-Length Argument Lists
    4. 14.4. Using Command-Line Arguments
    5. 14.5. Notes on Compiling Multiple-Source-File Programs
    6. 14.6. Program Termination with exit and atexit
    7. 14.7. volatile Type Qualifier
    8. 14.8. Suffixes for Integer and Floating-Point Constants
    9. 14.9. More on Files
    10. 14.10. Signal Handling
    11. 14.11. Dynamic Memory Allocation: Functions calloc and realloc
    12. 14.12. Unconditional Branching with goto
    13. Summary
      1. Section 14.2 Redirecting Input/Output on Linux/UNIX and Windows Systems
      2. Section 14.3 Variable-Length Argument Lists
      3. Section 14.4 Using Command-Line Arguments
      4. Section 14.5 Notes on Compiling Multiple-Source-File Programs
      5. Section 14.6 Program Termination with exit and atexit
      6. Section 14.7 volatile Type Qualifier
      7. Section 14.8 Suffixes for Integer and Floating-Point Constants
      8. Section 14.9 More on Files
      9. Section 14.10 Signal Handling
      10. Section 14.11 Dynamic Memory Allocation: Functions calloc and realloc
      11. Section 14.12 Unconditional Branching with goto
    14. Terminology
    15. Self-Review Exercise
    16. Answers to Self-Review Exercise
    17. Exercises
  18. 15. Game Programming with the Allegro C Library
    1. 15.1. Introduction
    2. 15.2. Installing Allegro
    3. 15.3. A Simple Allegro Program
    4. 15.4. Simple Graphics: Importing Bitmaps and Blitting
    5. 15.5. Animation with Double Buffering
      1. Double Buffering for Smooth Animation
    6. 15.6. Importing and Playing Sounds
    7. 15.7. Keyboard Input
    8. 15.8. Fonts and Displaying Text
    9. 15.9. Implementing the Game of Pong
    10. 15.10. Timers in Allegro
    11. 15.11. The Grabber and Allegro Datafiles
    12. 15.12. Other Allegro Capabilities
    13. 15.13. Allegro Internet and Web Resources
      1. Books
      2. Groups
      3. Forums
      4. Blog postings
    14. Summary
      1. Section 15.3 A Simple Allegro Program
      2. Section 15.4 Simple Graphics: Importing Bitmaps and Blitting
      3. Section 15.5 Animation with Double Buffering
      4. Section 15.6 Importing and Playing Sounds
      5. Section 15.7 Keyboard Input
      6. Section 15.8 Fonts and Displaying Text
      7. Section 15.10 Timers in Allegro
      8. Section 15.11 The Grabber and Allegro Datafiles
    15. Terminology
    16. Self-Review Exercises
    17. Answers to Self-Review Exercises
    18. Exercises
  19. 16. Sorting: A Deeper Look
    1. 16.1. Introduction
    2. 16.2. Big O Notation
    3. 16.3. Selection Sort
      1. Efficiency of Selection Sort
    4. 16.4. Insertion Sort
      1. Efficiency of Insertion Sort
    5. 16.5. Merge Sort
      1. Efficiency of Merge Sort
    6. Summary
      1. Section 16.1 Introduction
      2. Section 16.2 Big O Notation
      3. Section 16.3 Selection Sort
      4. Section 16.4 Insertion Sort
      5. Section 16.5 Merge Sort
    7. Terminology
    8. Self-Review Exercises
    9. Answers to Self-Review Exercises
    10. Exercises
  20. 17. Introduction to C99
    1. 17.1. Introduction
    2. 17.2. Support for C99
    3. 17.3. New C99 Headers
    4. 17.4. // Comments
    5. 17.5. Mixing Declarations and Executable Code
    6. 17.6. Declaring a Variable in a for Statement Header
    7. 17.7. Designated Initializers and Compound Literals
    8. 17.8. Type bool
    9. 17.9. Implicit int in Function Declarations
    10. 17.10. Complex Numbers
    11. 17.11. Variable-Length Arrays
    12. 17.12. Other C99 Features
      1. Extended Identifiers
      2. The restrict Keyword
      3. Reliable Integer Division
      4. Flexible Array Members
      5. long long int Type
      6. Type Generic Math
      7. Inline Functions
      8. Return Without Expression
      9. The snprinf Function: Helping Avoid Hacker Attacks
    13. 17.13. Internet and Web Resources
    14. Summary
      1. Section 17.1 Introduction
      2. Section 17.2 Support for C99
      3. Section 17.4 // Comments
      4. Section 17.5 Mixing Declarations and Executable Code
      5. Section 17.6 Declaring a Variable in a for Statement Header
      6. Section 17.7 Designated Initializers and Compound Literals
      7. Section 17.8 Type bool
      8. Section 17.9 Implicit int Function Declarations
      9. Section 17.10 Complex Numbers
      10. Section 17.11 Variable-Length Arrays
      11. Section 17.12 Other C99 Features
    15. Terminology
    16. Self-Review Exercises
    17. Answers to Self-Review Exercises
    18. Exercises
  21. 18. C++ as a Better C; Introducing Object Technology
    1. 18.1. Introduction
    2. 18.2. C++
    3. 18.3. A Simple Program: Adding Two Integers
    4. 18.4. C++ Standard Library
    5. 18.5. Header Files
    6. 18.6. Inline Functions
    7. 18.7. References and Reference Parameters
      1. Reference Parameters
      2. Passing Arguments by Value and by Reference
      3. References as Aliases within a Function
      4. Returning a Reference from a Function
      5. Error Messages for Uninitialized References
    8. 18.8. Empty Parameter Lists
    9. 18.9. Default Arguments
    10. 18.10. Unary Scope Resolution Operator
    11. 18.11. Function Overloading
      1. Overloaded square Functions
      2. How the Compiler Differentiates Overloaded Functions
      3. Overloaded Operators
    12. 18.12. Function Templates
    13. 18.13. Introduction to Object Technology and the UML
      1. Basic Object Technology Concepts
      2. Classes, Data Members and Member Functions
      3. Introduction to Object-Oriented Analysis and Design (OOAD)
      4. History of the UML
      5. What Is the UML?
      6. Internet and Web UML Resources
      7. Recommended Readings
    14. 18.14. Wrap-Up
    15. Summary
      1. Section 18.2 C++
      2. Section 18.3 A Simple Program: Adding Two Integers
      3. Section 18.4 C++ Standard Library
      4. Section 18.5 Header Files
      5. Section 18.6 Inline Functions
      6. Section 18.7 References and Reference Parameters
      7. Section 18.8 Empty Parameter Lists
      8. Section 18.9 Default Arguments
      9. Section 18.10 Unary Scope Resolution Operator
      10. Section 18.11 Function Overloading
      11. Section 18.12 Function Templates
      12. Section 18.13 Introduction to Object Technology and the UML
    16. Terminology
    17. Self-Review Exercises
    18. Answers to Self-Review Exercises
    19. Exercises
  22. 19. Introduction to Classes and Objects
    1. 19.1. Introduction
    2. 19.2. Classes, Objects, Member Functions and Data Members
    3. 19.3. Overview of the Chapter Examples
    4. 19.4. Defining a Class with a Member Function
      1. Class GradeBook
      2. Testing Class GradeBook
      3. UML Class Diagram for Class GradeBook
    5. 19.5. Defining a Member Function with a Parameter
      1. Defining and Testing Class GradeBook
      2. More on Arguments and Parameters
      3. Updated UML Class Diagram for Class GradeBook
    6. 19.6. Data Members, set Functions and get Functions
      1. GradeBook Class with a Data Member, a set Function and a get Function
      2. Access Specifiers public and private
      3. Member Functions setCourseName and getCourseName
      4. Member Function displayMessage
      5. Testing Class GradeBook
      6. Software Engineering with set and get Functions
      7. GradeBook’s UML Class Diagram with a Data Member and set and get Functions
    7. 19.7. Initializing Objects with Constructors
      1. Defining a Constructor
      2. Testing Class GradeBook
      3. Two Ways to Provide a Default Constructor for a Class
      4. Adding the Constructor to Class GradeBook’s UML Class Diagram
    8. 19.8. Placing a Class in a Separate File for Reusability
      1. Header Files
      2. Including a Header File That Contains a User-Defined Class
      3. How Header Files Are Located
      4. Additional Software Engineering Issues
    9. 19.9. Separating Interface from Implementation
      1. Interface of a Class
      2. Separating the Interface from the Implementation
      3. GradeBook.h: Defining a Class’s Interface with Function Prototypes
      4. GradeBook.cpp: Defining Member Functions in a Separate Source-Code File
      5. Testing Class GradeBook
      6. The Compilation and Linking Process
    10. 19.10. Validating Data with set Functions
      1. GradeBook Class Definition
      2. Validating the Course Name with GradeBook Member Function setCourseName
      3. Testing Class GradeBook
      4. Additional Notes on set Functions
    11. 19.11. Wrap-Up
    12. Summary
      1. Section 19.2 Classes, Objects, Member Functions and Data Members
      2. Section 19.4 Defining a Class with a Member Function
      3. Section 19.5 Defining a Member Function with a Parameter
      4. Section 19.6 Data Members, set Functions and get Functions
      5. Section 19.7 Initializing Objects with Constructors
      6. Section 19.8 Placing a Class in a Separate File for Reusability
      7. Section 19.9 Separating Interface from Implementation
      8. Section 19.10 Validating Data with set Functions
    13. Terminology
    14. Self-Review Exercises
    15. Answers to Self-Review Exercises
    16. Exercises
  23. 20. Classes: A Deeper Look, Part 1
    1. 20.1. Introduction
    2. 20.2. Time Class Case Study
      1. Time Class Definition
      2. Time Class Member Functions
      3. Defining Member Functions Outside the Class Definition; Class Scope
      4. Member Functions vs. Global Functions
      5. Using Class Time
      6. Looking Ahead to Composition and Inheritance
      7. Object Size
    3. 20.3. Class Scope and Accessing Class Members
    4. 20.4. Separating Interface from Implementation
    5. 20.5. Access Functions and Utility Functions
    6. 20.6. Time Class Case Study: Constructors with Default Arguments
      1. Notes Regarding Class Time’s set and get Functions and Constructor
    7. 20.7. Destructors
    8. 20.8. When Constructors and Destructors Are Called
    9. 20.9. Time Class Case Study: A Subtle Trap—Returning a Reference to a private Data Member
    10. 20.10. Default Memberwise Assignment
    11. 20.11. Software Reusability
    12. 20.12. Wrap-Up
    13. Summary
      1. Section 20.2 Time Class Case Study
      2. Section 20.3 Class Scope and Accessing Class Members
      3. Section 20.4 Separating Interface from Implementation
      4. Section 20.5 Access Functions and Utility Functions
      5. Section 20.6 Time Class Case Study: Constructors with Default Arguments
      6. Section 20.7 Destructors
      7. Section 20.8 When Constructors and Destructors Are Called
      8. Section 20.9 Time Class Case Study: A Subtle Trap—Returning a Reference to a private Data Member
      9. Section 20.8 When Constructors and Destructors Are Called
      10. Section 20.11 Software Reusability
    14. Terminology
    15. Self-Review Exercises
    16. Answers to Self-Review Exercises
    17. Exercises
  24. 21. Classes: A Deeper Look, Part 2
    1. 21.1. Introduction
    2. 21.2. const (Constant) Objects and const Member Functions
      1. Defining and Using const Member Functions
      2. Initializing a const Data Member with a Member Initializer
      3. Erroneously Attempting to Initialize a const Data Member with an Assignment
    3. 21.3. Composition: Objects as Members of Classes
    4. 21.4. friend Functions and friend Classes
      1. Modifying a Class’s private Data with a Friend Function
      2. Erroneously Attempting to Modify a private Member with a Non-friend Function
    5. 21.5. Using the this Pointer
      1. Implicitly and Explicitly Using the this Pointer to Access an Object’s Data Members
      2. Using the this Pointer to Enable Cascaded Function Calls
    6. 21.6. Dynamic Memory Management with Operators new and delete
    7. 21.7. static Class Members
    8. 21.8. Data Abstraction and Information Hiding
      1. 21.8.1. Example: Array Abstract Data Type
      2. 21.8.2. Example: String Abstract Data Type
      3. 21.8.3. Example: Queue Abstract Data Type
    9. 21.9. Container Classes and Iterators
    10. 21.10. Proxy Classes
    11. 21.11. Wrap-Up
    12. Summary
      1. Section 21.2 const (Constant) Objects and const Member Functions
      2. Section 21.3 Composition: Objects as Members of Classes
      3. Section 21.4 friend Functions and friend Classes
      4. Section 21.5 Using the this Pointer
      5. Section 21.6 Dynamic Memory Management with Operators new and delete
      6. Section 21.7 static Class Members
      7. Section 21.8 Data Abstraction and Information Hiding
      8. Section 21.9 Container Classes and Iterators
      9. Section 21.10 Proxy Classes
    13. Terminology
    14. Self-Review Exercises
    15. Answers to Self-Review Exercises
    16. Exercises
  25. 22. Operator Overloading
    1. 22.1. Introduction
    2. 22.2. Fundamentals of Operator Overloading
    3. 22.3. Restrictions on Operator Overloading
      1. Precedence, Associativity and Number of Operands
      2. Creating New Operators
      3. Operators for Fundamental Types
      4. Related Operators
    4. 22.4. Operator Functions as Class Members vs. Global Functions
      1. Operators That Must Be Overloaded as Member Functions
      2. Operators as Member Functions and Global Functions
      3. Why Overloaded Stream Insertion and Stream Extraction Operators Are Overloaded as Global Functions
      4. Commutative Operators
    5. 22.5. Overloading Stream Insertion and Stream Extraction Operators
    6. 22.6. Overloading Unary Operators
    7. 22.7. Overloading Binary Operators
    8. 22.8. Case Study: Array Class
      1. Creating Arrays, Outputting Their Size and Displaying Their Contents
      2. Using the Overloaded Stream Insertion Operator to Fill an Array
      3. Using the Overloaded Inequality Operator
      4. Initializing a New Array with a Copy of an Existing Array’s Contents
      5. Using the Overloaded Assignment Operator
      6. Using the Overloaded Equality Operator
      7. Using the Overloaded Subscript Operator
      8. Array Class Definition
      9. Overloading the Stream Insertion and Stream Extraction Operators as friends
      10. Array Default Constructor
      11. Array Copy Constructor
      12. Array Destructor
      13. getSize Member Function
      14. Overloaded Assignment Operator
      15. Overloaded Equality and Inequality Operators
      16. Overloaded Subscript Operators
    9. 22.9. Converting between Types
    10. 22.10. Overloading ++ and −−
      1. Overloading the Prefix Increment Operator
      2. Overloading the Postfix Increment Operator
    11. 22.11. explicit Constructors
      1. Accidentally Using a Single-Argument Constructor as a Conversion Constructor
      2. Preventing Accidental Use of a Single-Argument Constructor as a Conversion Constructor
    12. 22.12. Wrap-Up
    13. Summary
      1. Section 22.2 Fundamentals of Operator Overloading
      2. Section 22.3 Restrictions on Operator Overloading
      3. Section 22.4 Operator Functions as Class Members vs. Global Functions
      4. Section 22.5 Overloading Stream Insertion and Stream Extraction Operators
      5. Section 22.6 Overloading Unary Operators
      6. Section 22.7 Overloading Binary Operators
      7. Section 22.8 Case Study: Array Class
      8. Section 22.9 Converting between Types
      9. Section 22.10 Overloading ++ and −−
      10. Section 22.11 explicit Constructors
    14. Terminology
    15. Self-Review Exercises
    16. Answers to Self-Review Exercises
    17. Exercises
  26. 23. Object-Oriented Programming: Inheritance
    1. 23.1. Introduction
    2. 23.2. Base Classes and Derived Classes
    3. 23.3. protected Members
    4. 23.4. Relationship between Base Classes and Derived Classes
      1. 23.4.1. Creating and Using a CommissionEmployee Class
      2. 23.4.2. Creating a BasePlusCommissionEmployee Class Without Using Inheritance
        1. Defining Class BasePlusCommissionEmployee
        2. Testing Class BasePlusCommissionEmployee
        3. Exploring the Similarities Between Class BasePlusCommissionEmployee and Class CommissionEmployee
      3. 23.4.3. Creating a CommissionEmployee–BasePlusCommissionEmployee Inheritance Hierarchy
        1. Including the Base Class Header File in the Derived Class Header File with #include
        2. Linking Process in an Inheritance Hierarchy
      4. 23.4.4. CommissionEmployee–BasePlusCommissionEmployee Inheritance Hierarchy Using protected Data
        1. Defining Base Class CommissionEmployee with protected Data
        2. Modifying Derived Class BasePlusCommissionEmployee
        3. Testing the Modified BasePlusCommissionEmployee Class
        4. Notes on Using protected Data
      5. 23.4.5. CommissionEmployee–BasePlusCommissionEmployee Inheritance Hierarchy Using private Data
    5. 23.5. Constructors and Destructors in Derived Classes
    6. 23.6. public, protected and private Inheritance
    7. 23.7. Software Engineering with Inheritance
    8. 23.8. Wrap-Up
    9. Summary
      1. Section 23.1 Introduction
      2. Section 23.2 Base Classes and Derived Classes
      3. Section 23.3 protected Members
      4. Section 23.4 Relationship between Base Classes and Derived Classes
      5. Section 23.5 Constructors and Destructors in Derived Classes
      6. Section 23.6 public, protected and private Inheritance
    10. Terminology
    11. Self-Review Exercises
    12. Answers to Self-Review Exercises
    13. Exercises
  27. 24. Object-Oriented Programming: Polymorphism
    1. 24.1. Introduction
    2. 24.2. Polymorphism Examples
    3. 24.3. Relationships Among Objects in an Inheritance Hierarchy
      1. 24.3.1. Invoking Base-Class Functions from Derived-Class Objects
      2. 24.3.2. Aiming Derived-Class Pointers at Base-Class Objects
      3. 24.3.3. Derived-Class Member-Function Calls via Base-Class Pointers
      4. 24.3.4. Virtual Functions
      5. 24.3.5. Summary of the Allowed Assignments Between Base-Class and Derived-Class Objects and Pointers
    4. 24.4. Type Fields and switch Statements
    5. 24.5. Abstract Classes and Pure virtual Functions
    6. 24.6. Case Study: Payroll System Using Polymorphism
      1. 24.6.1. Creating Abstract Base Class Employee
      2. 24.6.2. Creating Concrete Derived Class SalariedEmployee
      3. 24.6.3. Creating Concrete Derived Class HourlyEmployee
      4. 24.6.4. Creating Concrete Derived Class CommissionEmployee
      5. 24.6.5. Creating Indirect Concrete Derived Class BasePlusCommissionEmployee
      6. 24.6.6. Demonstrating Polymorphic Processing
    7. 24.7. (Optional) Polymorphism, Virtual Functions and Dynamic Binding “Under the Hood”
    8. 24.8. Case Study: Payroll System Using Polymorphism and Runtime Type Information with Downcasting, dynamic_cast, typeid and type_info
    9. 24.9. Virtual Destructors
    10. 24.10. Wrap-Up
    11. Summary
      1. Section 24.1 Introduction
      2. Section 24.3 Relationships Among Objects in an Inheritance Hierarchy
      3. Section 24.5 Abstract Classes and Pure virtual Functions
      4. Section 24.7 (Optional) Polymorphism, Virtual Functions and Dynamic Binding “Under the Hood”
      5. Section 24.8 Case Study: Payroll System Using Polymorphism and Runtime Type Information with Downcasting, dynamic_cast, typeid and type_info
      6. Section 24.9 Virtual Destructors
    12. Terminology
    13. Self-Review Exercises
    14. Answers to Self-Review Exercises
    15. Exercises
  28. 25. Templates
    1. 25.1. Introduction
    2. 25.2. Function Templates
      1. Example: Function Template printArray
    3. 25.3. Overloading Function Templates
    4. 25.4. Class Templates
      1. Creating Class Template Stack< T >
      2. Creating a Driver to Test Class Template Stack< T >
      3. Creating Function Templates to Test Class Template Stack< T >
    5. 25.5. Nontype Parameters and Default Types for Class Templates
    6. 25.6. Notes on Templates and Inheritance
    7. 25.7. Notes on Templates and Friends
    8. 25.8. Notes on Templates and static Members
    9. 25.9. Wrap-Up
    10. Summary
      1. Section 25.1 Introduction
      2. Section 25.2 Function Templates
      3. Section 25.3 Overloading Function Templates
      4. Section 25.4 Class Templates
      5. Section 25.5 Nontype Parameters and Default Types for Class Templates
      6. Section 25.6 Notes on Templates and Inheritance
      7. Section 25.7 Notes on Templates and Friends
      8. Section 25.8 Notes on Templates and static Members
    11. Terminology
    12. Self-Review Exercises
    13. Answers to Self-Review Exercises
    14. Exercises
  29. 26. Stream Input/Output
    1. 26.1. Introduction
    2. 26.2. Streams
      1. 26.2.1. Classic Streams vs. Standard Streams
      2. 26.2.2. iostream Library Header Files
      3. 26.2.3. Stream Input/Output Classes and Objects
        1. Stream-I/O Template Hierarchy and Operator Overloading
        2. Standard Stream Objects cin, cout, cerr and clog
        3. File-Processing Templates
    3. 26.3. Stream Output
      1. 26.3.1. Output of char * Variables
      2. 26.3.2. Character Output Using Member Function put
    4. 26.4. Stream Input
      1. 26.4.1. get and getline Member Functions
        1. Using Member Functions eof, get and put
        2. Comparing cin and cin.get
        3. Using Member Function getline
      2. 26.4.2. istream Member Functions peek, putback and ignore
      3. 26.4.3. Type-Safe I/O
    5. 26.5. Unformatted I/O Using read, write and gcount
    6. 26.6. Introduction to Stream Manipulators
      1. 26.6.1. Integral Stream Base: dec, oct, hex and setbase
      2. 26.6.2. Floating-Point Precision (precision, setprecision)
      3. 26.6.3. Field Width (width, setw)
      4. 26.6.4. User-Defined Output Stream Manipulators
    7. 26.7. Stream Format States and Stream Manipulators
      1. 26.7.1. Trailing Zeros and Decimal Points (showpoint)
      2. 26.7.2. Justification (left, right and internal)
      3. 26.7.3. Padding (fill, setfill)
      4. 26.7.4. Integral Stream Base (dec, oct, hex, showbase)
      5. 26.7.5. Floating-Point Numbers; Scientific and Fixed Notation (scientific, fixed)
      6. 26.7.6. Uppercase/Lowercase Control (uppercase)
      7. 26.7.7. Specifying Boolean Format (boolalpha)
      8. 26.7.8. Setting and Resetting the Format State via Member Function flags
    8. 26.8. Stream Error States
    9. 26.9. Tying an Output Stream to an Input Stream
    10. 26.10. Wrap-Up
    11. Summary
      1. Section 26.1 Introduction
      2. Section 26.2 Streams
      3. Section 26.3 Stream Output
      4. Section 26.4 Stream Input
      5. Section 26.5 Unformatted I/O Using read, write and gcount
      6. Section 26.6 Introduction to Stream Manipulators
      7. Section 26.7 Stream Format States and Stream Manipulators
      8. Section 26.8 Stream Error States
      9. Section 26.9 Tying an Output Stream to an Input Stream
    12. Terminology
    13. Self-Review Exercises
    14. Answers to Self-Review Exercises
    15. Exercises
  30. 27. Exception Handling
    1. 27.1. Introduction
    2. 27.2. Exception-Handling Overview
    3. 27.3. Example: Handling an Attempt to Divide by Zero
      1. Defining an Exception Class to Represent the Type of Problem That Might Occur
      2. Demonstrating Exception Handling
      3. Enclosing Code in a try Block
      4. Defining a catch Handler to Process a DivideByZeroException
      5. Termination Model of Exception Handling
      6. Flow of Program Control When the User Enters a Nonzero Denominator
      7. Flow of Program Control When the User Enters a Denominator of Zero
    4. 27.4. When to Use Exception Handling
    5. 27.5. Rethrowing an Exception
    6. 27.6. Exception Specifications
    7. 27.7. Processing Unexpected Exceptions
    8. 27.8. Stack Unwinding
    9. 27.9. Constructors, Destructors and Exception Handling
    10. 27.10. Exceptions and Inheritance
    11. 27.11. Processing new Failures
      1. new Returning 0 on Failure
      2. new Throwing bad_alloc on Failure
      3. Handling new Failures Using Function set_new_handler
    12. 27.12. Class auto_ptr and Dynamic Memory Allocation
    13. 27.13. Standard Library Exception Hierarchy
    14. 27.14. Other Error-Handling Techniques
    15. 27.15. Wrap-Up
    16. Summary
      1. Section 27.1 Introduction
      2. Section 27.2 Exception-Handling Overview
      3. Section 27.3 Example: Handling an Attempt to Divide by Zero
      4. Section 27.4 When to Use Exception Handling
      5. Section 27.5 Rethrowing an Exception
      6. Section 27.6 Exception Specifications
      7. Section 27.7 Processing Unexpected Exceptions
      8. Section 27.8 Stack Unwinding
      9. Section 27.9 Constructors, Destructors and Exception Handling
      10. Section 27.10 Exceptions and Inheritance
      11. Section 27.11 Processing new Failures
      12. Section 27.12 Class auto_ptr and Dynamic Memory Allocation
      13. Section 27.13 Standard Library Exception Hierarchy
    17. Terminology
    18. Self-Review Exercises
    19. Answers to Self-Review Exercises
    20. Exercises
  31. A. Internet and Web Resources
    1. A.1. Free C/C++ Compilers and Development Tools
    2. A.2. C Resource Sites
    3. A.3. C99
    4. A.4. C Projects, Freeware and Shareware
    5. A.5. C Source Code
    6. A.6. C Articles and Whitepapers
    7. A.7. C Tutorials and Webcasts
    8. A.8. GNOME and GLib
    9. A.9. SWIG
    10. A.10. Objective-C
    11. A.11. C Sample Chapters and eBooks
    12. A.12. C Wikis
    13. A.13. C FAQs
    14. A.14. C Newsgroups
    15. A.15. C Blogs
    16. A.16. C Downloads from Download.com
    17. A.17. C Game Programming
    18. A.18. Allegro Game Programming Resources
    19. A.19. Jobs for C Programmers
    20. A.20. Deitel C Training
  32. B. Operator Precedence Charts
  33. C. ASCII Character Set
  34. D. Number Systems
    1. D.1. Introduction
    2. D.2. Abbreviating Binary Numbers as Octal and Hexadecimal Numbers
    3. D.3. Converting Octal and Hexadecimal Numbers to Binary Numbers
    4. D.4. Converting from Binary, Octal or Hexadecimal to Decimal
    5. D.5. Converting from Decimal to Binary, Octal or Hexadecimal
    6. D.6. Negative Binary Numbers: Two’s Complement Notation
    7. Summary
    8. Terminology
    9. Self-Review Exercises
    10. Answers to Self-Review Exercises
    11. Exercises
  35. E. Game Programming: Solving Sudoku
    1. E.1. Introduction
    2. E.2. Deitel Sudoku Resource Center
    3. E.3. Solution Strategies
      1. Singletons
      2. Doubles
      3. Triples
      4. Other Sudoku Solution Strategies
    4. E.4. Programming Sudoku Puzzle Solvers
      1. Programming a Solution for “Easy” Sudokus
      2. Programming a Solution for Harder Sudokus
    5. E.5. Generating New Sudoku Puzzles
      1. Brute Force Approaches
      2. An Exhaustive Brute Force Approach
      3. Brute Force Approach with Randomly Selected Row Permutations
      4. Heuristic Solution Strategies
      5. A “Keep Your Options Open” Sudoku Solving Heuristic
      6. Lookahead Heuristic
      7. Forming Sudoku Puzzles with Empty Cells
      8. A Programming Challenge
    6. E.6. Conclusion

Product information

  • Title: C How to Program, Fifth Edition
  • Author(s): P. J. Deitel - Deitel & Associates, Inc., H. M. Deitel - Deitel & Associates, Inc.
  • Release date: August 2006
  • Publisher(s): Pearson
  • ISBN: 9780136085881