Intermediate C Programming, 2nd Edition

Book description

Revised for a new second edition, Intermediate C Programming provides a stepping-stone for intermediate-level students to go from writing short programs to writing real programs well. It shows students how to identify and eliminate bugs, write clean code, share code with others, and use standard Linux-based tools.

Table of contents

  1. Cover Page
  2. Half-Title Page
  3. Title Page
  4. Copyright Page
  5. Contents
  6. List of Figures
  7. List of Tables
  8. Foreword for the First Edition
  9. Recommendations for the First Edition
  10. Preface
  11. Acknowledgments
  12. Source Code
  13. Author and Artist
  14. I Storage: Memory and File
    1. 1 Program Execution
      1. 1.1 Compile
      2. 1.2 Redirect Output
      3. 1.3 Problems
        1. 1.3.1 Replace String
        2. 1.3.2 Keep Third Column
        3. 1.3.3 Find String in a File
        4. 1.3.4 Sort by a Column
        5. 1.3.5 gcc Warnings
        6. 1.3.6 du command
        7. 1.3.7 Show End of a File
    2. 2 Stack Memory
      1. 2.1 Values and Addresses
      2. 2.2 Stack
      3. 2.3 The Call Stack
        1. 2.3.1 The Return Location
        2. 2.3.2 Function Arguments
        3. 2.3.3 Local Variables
        4. 2.3.4 Value Address
        5. 2.3.5 Arrays
        6. 2.3.6 Retrieving Addresses
      4. 2.4 Visibility
      5. 2.5 Examine the Call Stack with DDD
      6. 2.6 Problems
        1. 2.6.1 Draw Call Stack I
        2. 2.6.2 Draw Call Stack II
        3. 2.6.3 Addresses
    3. 3 Prevent, Detect, and Remove Bugs
      1. 3.1 Rules in Software Development
      2. 3.2 Developing Software ≠ Coding
        1. 3.2.1 Before Coding
        2. 3.2.2 During Coding
        3. 3.2.3 After Coding
      3. 3.3 Post-Execution and Interactive Debugging
      4. 3.4 Separate Testing Code from Production Code
      5. 3.5 Use assert Correctly
      6. 3.6 How to Comment Code
    4. 4 Pointers
      1. 4.1 Scope
      2. 4.2 The Swap Function
      3. 4.3 Pointers
      4. 4.4 Self Test: Pointers
      5. 4.5 The Swap Function Revisited
      6. 4.6 Type Errors
      7. 4.7 Arrays and Pointers
      8. 4.8 Type Rules
      9. 4.9 Pointer Arithmetic
      10. 4.10 Sizes of Data Types
      11. 4.11 Problems
        1. 4.11.1 Swap Function 1
        2. 4.11.2 Swap Function 2
        3. 4.11.3 Swap Function 3
        4. 4.11.4 Swap Function 4
        5. 4.11.5 Swap Function 5
        6. 4.11.6 15,552 Variations
        7. 4.11.7 Pointer and Array
    5. 5 Writing and Testing Programs
      1. 5.1 Distinct Array Elements
        1. 5.1.1 main Function
        2. 5.1.2 areDistinct Function
        3. 5.1.3 Compiling and Linking
      2. 5.2 Build and Test Program with Multiple Files
        1. 5.2.1 make and Makefile
        2. 5.2.2 Test using make
        3. 5.2.3 Generate Test Cases
      3. 5.3 Detect Invalid Memory Access
      4. 5.4 Test Coverage
      5. 5.5 Limit Core Size
      6. 5.6 Remove Large Files
      7. 5.7 Problems
        1. 5.7.1 Array Index
        2. 5.7.2 gcov
        3. 5.7.3 Test Coverage
    6. 6 Strings
      1. 6.1 Array of Characters
      2. 6.2 String Functions in C
        1. 6.2.1 Copy: strcpy
        2. 6.2.2 Copy: strdup
        3. 6.2.3 Compare: strcmp
        4. 6.2.4 Find Substrings: strstr
        5. 6.2.5 Find Characters: strchr
      3. 6.3 Understand argv
      4. 6.4 Count Substrings
      5. 6.5 Problems
        1. 6.5.1 Compare Strings
        2. 6.5.2 Count Substring-1
        3. 6.5.3 Count Substring-2
    7. 7 Heap Memory
      1. 7.1 Allocate Memory using malloc
      2. 7.2 Stack and Heap Memory
      3. 7.3 Functions that Return a Heap Address
      4. 7.4 Two-Dimensional Arrays in C
      5. 7.5 Pointers and Function Arguments
      6. 7.6 Problems
        1. 7.6.1 free
        2. 7.6.2 malloc
        3. 7.6.3 Address of Stack Memory
    8. 8 Programming Problems Using Heap Memory
      1. 8.1 Sort an Array
        1. 8.1.1 Generate Test Input and Expected Output
        2. 8.1.2 Sort Integers
        3. 8.1.3 Makefile
        4. 8.1.4 Use valgrind to Detect Memory Leaks
        5. 8.1.5 Use gcc -fsanitize to Detect Memory Leaks
      2. 8.2 Sort Using qsort
        1. 8.2.1 qsort
        2. 8.2.2 The Comparison Function
        3. 8.2.3 Execution Examples
        4. 8.2.4 Sort Strings
      3. 8.3 Problems
        1. 8.3.1 Bubble Sort
        2. 8.3.2 Selection Sort
        3. 8.3.3 Comparison in qsort
    9. 9 Reading and Writing Files
      1. 9.1 Read from Files
      2. 9.2 Write to Files
      3. 9.3 Read and Write Strings
    10. 10 Programming Problems Using File
      1. 10.1 Sort a File of Integers
      2. 10.2 Count the Occurrences of Characters
      3. 10.3 Count the Occurrences of a Word
      4. 10.4 Problems
        1. 10.4.1 Determine Palindrome
        2. 10.4.2 Compare Two Files
        3. 10.4.3 fscanf and ftell
    11. 11 Array Index, Security, and Trends
      1. 11.1 Array Index
      2. 11.2 Modify Program Behavior
      3. 11.3 Trends in Programming Languages
    12. 12 Version Control
      1. 12.1 Version Control Overview
      2. 12.2 git and github.com
      3. 12.3 Create a Repository on github
      4. 12.4 Sample Repository
      5. 12.5 Use git and github.com
      6. 12.6 Problems
        1. 12.6.1 git commit and git push
        2. 12.6.2 Revert to an earlier version
        3. 12.6.3 When to commit?
        4. 12.6.4 When to clone?
        5. 12.6.5 Recover deleted file
  15. II Recursion
    1. 13 Concept
      1. 13.1 Select Balls with Restrictions
        1. 13.1.1 Balls of Two Colors
        2. 13.1.2 Balls of Three Colors
      2. 13.2 One-Way Streets
      3. 13.3 The Tower of Hanoi
      4. 13.4 Calculate Integer Partitions
        1. 13.4.1 Count the Number of “1”s
        2. 13.4.2 Odd Numbers Only
        3. 13.4.3 Increasing Values
        4. 13.4.4 Alternating Odd and Even Numbers
      5. 13.5 Problems
        1. 13.5.1 Parentheses
        2. 13.5.2 Ball Selection
        3. 13.5.3 Partition using Odd and Even Numbers
    2. 14 Recursive C Functions
      1. 14.1 Select Balls with Restrictions
      2. 14.2 One-Way Streets
      3. 14.3 The Tower of Hanoi
      4. 14.4 Integer Partition
      5. 14.5 Factorial
      6. 14.6 Fibonacci Numbers
      7. 14.7 Performance Profiling with gprof
      8. 14.8 Problems
        1. 14.8.1 Count Number of Calls: Original Definition
        2. 14.8.2 Count Number of Calls: Efficient Recursion
        3. 14.8.3 Parentheses
        4. 14.8.4 Fibonacci Numbers
    3. 15 Integer Partition
      1. 15.1 Print Partitions
      2. 15.2 Stack and Heap Memory
      3. 15.3 Trace Recursive Function Calls
      4. 15.4 Generate Partitions with Restrictions
        1. 15.4.1 Using Odd Numbers Only
        2. 15.4.2 Using Sequences of Increasing Numbers
        3. 15.4.3 Using Alternating Odd and Even Numbers
      5. 15.5 Problems
        1. 15.5.1 Even Numbers Only
        2. 15.5.2 Decreasing Values
        3. 15.5.3 Error in partition
        4. 15.5.4 Error in partition
        5. 15.5.5 Error in partition
    4. 16 Programming Problems Using Recursion
      1. 16.1 Binary Search
      2. 16.2 Quick Sort
      3. 16.3 Permutations and Combinations
      4. 16.4 Stack Sort
      5. 16.5 An Incorrect Recursive Function
      6. 16.6 Problems
        1. 16.6.1 Shuffle Cards - 1
        2. 16.6.2 Shuffle Cards - 2
        3. 16.6.3 Shuffle Cards - 3
  16. III Structure
    1. 17 Programmer-Defined Data Types
      1. 17.1 Struct and Object
      2. 17.2 Objects as Arguments
      3. 17.3 Objects and Pointers
      4. 17.4 Constructors and Destructors
      5. 17.5 Structures within Structures
      6. 17.6 Binary Files and Objects
      7. 17.7 Problems
        1. 17.7.1 Structure and Pointer
        2. 17.7.2 Structure and Attributes' Sizes
        3. 17.7.3 Alternative Ways to Assign Attributes
    2. 18 Programming Problems Using Structure
      1. 18.1 Sort a Person Database
      2. 18.2 Packing Decimal Digits
      3. 18.3 Binary File and Pointer
      4. 18.4 Problems
        1. 18.4.1 Number Systems
        2. 18.4.2 Structure with Pointer-1
        3. 18.4.3 Structure with Pointer-2
    3. 19 Linked Lists
      1. 19.1 Dynamic Data Structure
      2. 19.2 Linked Lists
      3. 19.3 Insert Data
      4. 19.4 Search a Linked List
      5. 19.5 Delete a Node from a Linked List
      6. 19.6 Print a Linked List
      7. 19.7 Destroy a Linked List
    4. 20 Programming Problems Using Linked List
      1. 20.1 Queues
      2. 20.2 Sort Numbers
      3. 20.3 Sparse Arrays
      4. 20.4 Reversing a Linked List
      5. 20.5 Doubly Linked List
    5. 21 Binary Search Trees
      1. 21.1 Binary Tree
      2. 21.2 Binary Search Tree
      3. 21.3 Insert Data into a Binary Search Tree
      4. 21.4 Search a Binary Search Tree
      5. 21.5 Print a Binary Tree
      6. 21.6 Delete from a Binary Search Tree
      7. 21.7 Destroy a Binary Tree
      8. 21.8 Count the Different Shapes of a Binary Tree
      9. 21.9 Problems
        1. 21.9.1 Count Nodes
        2. 21.9.2 Tree Height
        3. 21.9.3 Check Binary Search Tree
        4. 21.9.4 Ancestor-Offspring
        5. 21.9.5 Build Binary Tree
    6. 22 Parallel Programming Using Threads
      1. 22.1 Parallel Programming
      2. 22.2 POSIX Threads
      3. 22.3 Subset Sum
        1. 22.3.1 Sequential Solution
        2. 22.3.2 Multiple-Threads Solution
      4. 22.4 Interleave the Execution of Threads
      5. 22.5 Thread Synchronization
      6. 22.6 Amdahl's Law
        1. 22.6.1 Speedup vs. Efficiency
        2. 22.6.2 Understanding Amdahl's Law
        3. 22.6.3 The Limit's of Amdahl's Law
      7. 22.7 Problems
        1. 22.7.1 Interleaving of Threads
        2. 22.7.2 Interleaving of Threads
    7. 23 Unit Test
      1. 23.1 Google Test Framework
      2. 23.2 Rational Numbers
      3. 23.3 Rational Numbers and Operations
        1. 23.3.1 Create Ratoinal Object
        2. 23.3.2 Arithmetic Operations
      4. 23.4 Use and Test Rational Numbers
        1. 23.4.1 Use Rational Numbers
        2. 23.4.2 Test Rational Numbers
      5. 23.5 Testing Strategies
  17. IV Applications
    1. 24 Find the Exit of a Maze
      1. 24.1 Maze File Format
      2. 24.2 Strategy to Find Exit
      3. 24.3 Implement the Strategy
      4. 24.4 Problems
        1. 24.4.1 Mark of Bricks
        2. 24.4.2 Multiple Exits
        3. 24.4.3 Order of Directions
        4. 24.4.4 Change if Conditions
    2. 25 Sudoku
      1. 25.1 Sudoku Game
      2. 25.2 Recursive Solution
      3. 25.3 Implement the Solution
      4. 25.4 Design Decisions
      5. 25.5 Network Communication
        1. 25.5.1 Server
        2. 25.5.2 Client
      6. 25.6 Problems
        1. 25.6.1 Check Sudoku
        2. 25.6.2 Transfer Large File
    3. 26 Image Processing
      1. 26.1 Structure for Image
      2. 26.2 Image Pixels and Colors
      3. 26.3 Color Filter
      4. 26.4 Invert Colors
      5. 26.5 Detect Edges
      6. 26.6 Equalize Colors
      7. 26.7 main Function
      8. 26.8 Problems
        1. 26.8.1 Color Histogram
        2. 26.8.2 Color Space
        3. 26.8.3 Gradient
        4. 26.8.4 Vertical Mirror
    4. 27 Huffman Compression
      1. 27.1 Variable-Length Coding
      2. 27.2 Compress
        1. 27.2.1 Count and Sort Occurrences
        2. 27.2.2 Build the Code Tree
        3. 27.2.3 Create Code Book and Compress Data
        4. 27.2.4 Describe the Coding Tree
        5. 27.2.5 Use Bits
      3. 27.3 Decompress
      4. 27.4 Implementation
        1. 27.4.1 Test Case Generator
        2. 27.4.2 main Function
        3. 27.4.3 Makefile
        4. 27.4.4 Compression
        5. 27.4.5 Decompression
        6. 27.4.6 Compression Ratios
      5. 27.5 Problems
        1. 27.5.1 Compression Tree
        2. 27.5.2 Compressed File
        3. 27.5.3 Compressed File
        4. 27.5.4 One-Letter Input
        5. 27.5.5 Compressed File
        6. 27.5.6 Number of Leaf Nodes
        7. 27.5.7 Compression Ratio
        8. 27.5.8 Tree Description
        9. 27.5.9 Highest Possible Compression Ratio
        10. 27.5.10 Lowest Possible Compression Ratio
  18. Index
  19. Epilogue: The Computer Engineer as Tool-User

Product information

  • Title: Intermediate C Programming, 2nd Edition
  • Author(s): Yung-Hsiang Lu, George K. Thiruvathukal
  • Release date: February 2024
  • Publisher(s): CRC Press
  • ISBN: 9781003832744