Copyright Page
- Joint Authorship
- Copyright Declaration
- Permitted Uses
Introduction: What This Book Will Make Clear
- What This Book Is Not
- What This Book Is
- What We Will Cover
- How to Read This Book
- The Authors’ Perspective
- A Note on Language
- A Note on AI and Learning
- What You Will Become
- If You Cannot Afford This Book
Author Interview: Meet the Three Voices Behind This Book
A Small Theater: The First Meeting
Chapter 1: How Does a Computer Actually Work?
- 1.1 The Skeleton: CPU, Memory, Storage, Motherboard
- 1.2 L’s “Terminology Breakdown Corner”
- 1.3 Hands-On: Look at Your Own Computer
1.1 Information Literacy: What Did You Actually Learn?
- The Three-Sentence Rule
- The “Why Should I Care?” Section
- Quick Quiz: Can You Pass the “Grandma Test”?
- A Real Problem: Can You Spot the Mistake?
- One More: The “Price” Question
- The Takeaway: What You Should Remember from This Chapter
- Onward to Chapter 2
1.2 The Value Cube: How to Learn Anything (Including Code)
- The Psychologist’s Question
- What This Means for Learning Code
- The Grandma Test (Revisited)
- The “Why Do I Need to Know This?” Moment
- A Real Example: Learning Pointers
- The “Second Brain” Method
- The One Question to Ask Yourself
- Practice: Five Exercises to Train Your Learning Method
- Onward to Chapter 2
Chapter 2: First Contact — The C Language
- 2.1 Why C? A Brief History
- 2.2 Where Does C Fit in the Language Family?
- 2.3 Your First C Program
- 2.4 Line-by-Line Breakdown
- 2.5 Variables: Putting Things in Memory
- 2.6 Printing Variables: printf with Placeholders
- 2.7 Getting Input: scanf
- 2.8 Doing Something: A Simple Calculation
- 2.8.1 Break It on Purpose
- 2.9 What You’ve Learned in This Chapter
- 2.10 Practice: You Build Something
- 2.11 T’s Information Literacy Summary
- 2.12 Value Cube: Understanding C — Its History, Power, and Limits
Chapter 3: Variables, Memory, and Writing Safe Code
- 3.1 Memory Is Just a Big List of Numbers
- 3.2 Every Variable Has Three Properties
- 3.3 How Much Memory Does Each Type Use?
- 3.4 Memory Layout: How Variables Are Arranged
- 3.5 The Stack: Where Local Variables Live
- 3.6 What Are Reserved Words (Keywords)?
- 3.7 What Are Header Files?
- 3.8 Creating Your Own Header File
- 3.9 <> vs ““ — What’s the Difference?
- 3.10 Naming Conventions: How to Write Readable Code
- 3.11 Memory Safety: How Variables Affect Security
- 3.12 The Golden Rules of Safe Variable Usage
- L’s Quick Interruption
- Practice: Explore Your Own Memory
- T’s Information Literacy Summary
- Chapter 3 Checklist
- 3.12 T’s Information Literacy Summary
- 3.13 Value Cube: The Story of Variables — From Math to Memory to Security
Chapter 4: Pointers — The Address of Everything (Expanded Edition)
- Part 1: What Are Variables, Pointers, and Functions?
- Part 2: Why Do We Need Pointers?
- Part 3: The Two Operators You Must Know
- Part 4: Declaring and Using Pointers
- Part 5: Pointers and Functions — Passing by Reference
- Part 6: Swapping Two Numbers
- Part 7: Pointers and Arrays
- Part 8: Pointers to Different Types
- Part 9: NULL Pointers
- Part 10: Common Mistakes with Pointers
- Part 11: Pointers in Real-World Scenarios
- Part 12: L’s Quick Interruption
- Practice: Write Your Own Pointer Programs
- T’s Information Literacy Summary
- Chapter 4 Checklist
4.1 Information Literacy: What Did You Actually Learn?
- Core Concepts in One Sentence Each
- Small Experiments to Try
- So Why Should You Care?
- Common Mistakes and How to Avoid Them
- Quick Check: Can You Answer These?
- The Big Picture: How This Chapter Connects to Everything Else
- Checklist
- Onward
Chapter 4: Pointers — The Address of Everything
- Chapter Introduction
- Prologue: The Sticky Note with the Room Number
- 4.1 Three Core Concepts
- 4.2 Why Do We Need Pointers?
- 4.3 The Two Operators: & and *
- 4.4 Declaration, Assignment, Reading, and Modifying via Pointer
- 4.5 Pointers and Functions: Why C Is “Pass-by-Value”
- 4.6 Classic Example: Swapping Two Numbers with Pointers
- 4.7 Pointers and Arrays
- 4.8 Pointers to Different Types
- 4.9 NULL: A Sticky Note That Says “No Address”
- 4.10 The Five Most Common Pointer Mistakes
- 4.11 Pointers in Real-World Scenarios
- 4.12 Quick Reference Table
- Chapter Checklist
- Practice Exercises
- 5.2 Value Cube: The Warehouse That Kept Growing
Chapter 5: The Warehouse That Grows — Dynamic Memory Allocation
- 5.1 The Stack vs The Heap
- 5.2 malloc — Allocating Memory on the Heap
- 5.3 Using Allocated Memory
- 5.4 free — Returning Memory to the System
- 5.5 Allocating Memory for Arrays
- 5.6 calloc — Allocate and Zero
- 5.7 realloc — Resizing Memory
- 5.8 Common Dynamic Memory Mistakes
- 5.9 Real-World Scenario: Reading Unknown Input
- 5.10 L’s Quick Interruption
- Practice: Dynamic Memory Exercises
- 5.11 T’s Information Literacy Summary
- 5.1 Information Literacy: What Did You Actually Learn?
- Core Concepts in One Sentence Each
- Small Experiments to Try
- What Happens When You Forget to Free?
- Quick Check: Can You Answer These?
- Before You Move On
- Chapter 6: The Sentence That Wasn’t a Sentence
Chapter 6: Strings in C — The Sentence That Wasn’t a Sentence
- 6.1 What Is a String in C?
- 6.2 Declaring Strings
- 6.3 The ‘\0’ Invisible Dot
- 6.4 String Length: strlen
- 6.5 Copying Strings: strcpy and strncpy
- 6.6 Concatenating Strings: strcat and strncat
- 6.7 Comparing Strings: strcmp and strncmp
- 6.8 Reading Strings from the User
- 6.9 Arrays of Strings
- 6.10 Finding Characters in Strings: strchr and strrchr
- 6.11 Finding Substrings: strstr
- 6.12 Common String Mistakes (Detailed)
- 6.13 Real-World Scenario: Processing User Input
- 6.14 Putting It All Together: A String Reversal Program
- L’s Quick Interruption
- Practice: String Exercises
- 6.1 Information Literacy: What Did You Actually Learn?
- Core Concepts in One Sentence Each
- Small Experiments to Try
- What Happens When You Forget the
'\0'? - Quick Check: Can You Answer These?
- Before You Move On
- Chapter 7: The Box That Wanted to Hold More
Chapter 7: The Box That Wanted to Hold More — Structures in C
- 7.1 What Is a Structure?
- 7.2 Declaring and Using Structures
- 7.3 Initializing Structures
- 7.4 Copying Structures
- 7.5 Arrays of Structures
- 7.6 Pointers to Structures
- 7.7 Passing Structures to Functions
- 7.8 Nested Structures
- 7.9 typedef — Creating Type Aliases
- 7.10 Self-Referential Structures
- 7.11 Common Structure Mistakes
- 7.12 Real-World Scenario: Student Database
- 7.13 L’s Quick Interruption
- Practice: Structure Exercises
- 7.1 Information Literacy: What Did You Actually Learn?
- Core Concepts in One Sentence Each
- Small Experiments to Try
- What Happens When You Pass a Large Structure by Value?
- Quick Check: Can You Answer These?
- Before You Move On
- Chapter 8: The Notebook That Never Lost a Page
- 8.1 What Is a File?
- 8.2 Opening and Closing Files
- 8.3 Writing to a Text File
- 8.4 Reading from a Text File
- 8.5 Appending to a File
- 8.6 Reading and Writing Binary Files
- 8.7 Checking for Errors
- 8.8 Common File Mistakes
- 8.9 Real-World Scenario: Student Database with Files
- 8.10 L’s Quick Interruption
- Practice: File Exercises
- 8.11 Information Literacy: What Did You Actually Learn?
Chapter 8: The Notebook That Never Lost a Page — File I/O in C
- 8.1 What Is a File?
- 8.2 Opening a File:
fopen - 8.3 Closing a File:
fclose - 8.4 Writing to a Text File:
fprintf - 8.5 Reading from a Text File:
fscanf - 8.6 Reading a Whole Line:
fgets - 8.7 Writing a Whole Line:
fputs - 8.8 Reading/Writing Binary Files:
freadandfwrite - 8.9 File Position:
ftellandfseek - 8.10 Common File I/O Mistakes
- 8.11 Putting It All Together: A Simple File Database
- 8.12 L’s Quick Interruption
- Practice: File I/O Exercises
- 8.1 Information Literacy: What Did You Actually Learn?
- Core Concepts in One Sentence Each
- Small Experiments to Try
- What Happens When You Forget to Close a File?
- What Happens When You Open a File That Doesn’t Exist?
- Quick Check: Can You Answer These?
- Before You Move On
- Chapter 9: The Recipe That Wrote Itself
Chapter 9: The Recipe That Wrote Itself — Preprocessor and Macros
- 9.1 The Compilation Process
- 9.2
#include— Copying Files - 9.3
#define— Text Substitution - 9.4 Why the Parentheses?
- 9.5 The Dark Side of Macros
- 9.6 Conditional Compilation:
#ifdef,#ifndef,#endif - 9.7 Header Guards: The
#ifndefPattern - 9.8 Common Macros You Already Use
- 9.9 Putting It All Together: A Debug System
- 9.10 L’s Quick Interruption
- Practice: Preprocessor Exercises
- 9.1 Information Literacy: What Did You Actually Learn?
- Core Concepts in One Sentence Each
- Small Experiments to Try
- What Happens When You Forget a Header Guard?
- Quick Check: Can You Answer These?
- Before You Move On
- Chapter 10: The Book That Became Too Big
Chapter 10: The Book That Became Too Big — Modular Programming
- 10.1 Why Split Code Into Multiple Files?
- 10.2 Header Files (.h) and Source Files (.c)
- 10.3 The Compilation Process for Multiple Files
- 10.4
#include— Connecting Files - 10.5 Scope: Who Can See What?
- 10.6
staticandextern - 10.7 A Real Example: A Calculator Library
- 10.8 Common Modular Programming Patterns
- 10.9 Common Mistakes
- 10.10 L’s Quick Interruption
- Practice: Modular Programming Exercises
- 10.1 Information Literacy: What Did You Actually Learn?
- Core Concepts in One Sentence Each
- Small Experiments to Try
- What Happens When You Forget to Add
math.cto the Compilation? - What Happens When You Include a Header in Another Header?
- Real-World Tip: Where Do Header Guards Go?
- What Happens When You Put a Definition in a Header?
- The Two Types of Linking
- The Makefile: Automating Compilation
- Quick Check: Can You Answer These?
- Before You Move On
- Chapter 11: The Mirror That Reflected Itself
Chapter 11: The Mirror That Reflected Itself — Recursion and Function Pointers
- Part 1: Recursion
- 11.1 What Is Recursion?
- 11.2 How Recursion Works: The Stack
- 11.3 Classic Recursion: Factorial
- 11.4 Classic Recursion: Fibonacci
- 11.5 Recursion vs Iteration
- 11.6 Common Recursion Mistakes
- Part 2: Function Pointers
- 11.7 What Is a Function Pointer?
- 11.8 Why Use Function Pointers?
- 11.9
qsort— The Classic Function Pointer Example - 11.10 Function Pointer Syntax
- 11.11 Putting It All Together: Recursion + Function Pointers
- 11.12 L’s Quick Interruption
- Practice: Recursion and Function Pointer Exercises
- 11.1 Information Literacy: What Did You Actually Learn?
- Core Concepts in One Sentence Each
- Small Experiments to Try
- Recursive vs Iterative: Which One to Choose?
- What Happens When You Forget the Base Case?
- Why Is Fibonacci Recursion So Slow?
- Quick Check: Can You Answer These?
- Before You Move On
- Chapter 12: The Room That Became a Study
Chapter 12: The Room That Became a Study — Building a Complete Project
- 12.1 Before We Write Code: The Project Lifecycle
- 12.2 Requirements: What Should the Program Do?
- 12.3 Design: What Are the Modules?
- 12.4 The Header File: student.h
- 12.5 The Implementation: student.c
- 12.6 The Storage Module: storage.h
- 12.7 The Storage Implementation: storage.c
- 12.8 The User Interface: main.c
- 12.9 Compiling the Project
- 12.10 What You Just Built
- 12.11 What You Learned from This Project
- 12.12 L’s Final Thought
- Practice: Extend the Project
- 12.1 Information Literacy: The End of One Journey — And the Beginning of Another
- Before You Move On
- 12.2 Value Cube: The Engineer’s Mindset
- Chapter 13: The Packed Suitcase
Chapter 13: The Packed Suitcase — Bitwise Operations and Memory Optimization
- 13.1 What Is a Bit?
- 13.2 Binary Number System
- 13.3 Hexadecimal (Base 16)
- 13.4 The Bitwise Operators
- 13.5 Bitwise AND (&)
- 13.6 Bitwise OR (|)
- 13.7 Bitwise XOR (^)
- 13.8 Bitwise NOT (~)
- 13.9 Left Shift (<<)
- 13.10 Right Shift (>>)
- 13.11 Bitmasks: Using Bits as Flags
- 13.12 Real-World Example: Packing Colors in 32 Bits
- 13.13 Packing Data: Saving Memory with Bitfields
- 13.14 Memory Alignment and Padding
- 13.15 Common Bitwise Use Cases
- 13.16 L’s Quick Interruption
- 13.1 Information Literacy: Why Bits Matter — And Where They Show Up
- Before You Move On
- 13.2 Value Cube: The Engineer’s Judgment — How to Tell Good Code from Bad Code
- Chapter 14: The Map That Led to More Maps
Chapter 14: The Map That Led to More Maps — Advanced Pointers
- 14.1 Pointers to Pointers (Double Pointers)
- 14.2 Why Do We Need Double Pointers?
- 14.3 Arrays of Pointers
- 14.4 Function Pointers — Real-World Use Cases
- 14.5 Function Pointers as “Maps to Code”
- 14.6 Complex Pointer Declarations
- 14.7 Using typedef with Function Pointers
- 14.8 Common Mistakes with Advanced Pointers
- 14.9 L’s Quick Interruption
- Practice: Advanced Pointer Exercises
- 14.1 Information Literacy: Reading Code Like a Pro
- Chapter 15: The City of Memory
Chapter 15: The City of Memory — Program Memory Layout
- 15.1 The Five Memory Regions
- 15.2 Text (Code) Segment
- 15.3 Data Segment (Initialized Data)
- 15.4 BSS Segment (Uninitialized Data)
- 15.5 The Heap
- 15.6 The Stack
- 15.7 Visual Memory Layout (Addresses)
- 15.8 Stack vs Heap: Detailed Comparison
- 15.9 Real-World Example: Variable Locations
- 15.10 Common Memory Errors
- 15.11 L’s Quick Interruption
- Practice: Memory Layout Exercises
- 15.1 Information Literacy: Seeing Memory with Your Own Eyes
- Chapter 16: The Toolbox That Never Stops Growing
Chapter 16: The Toolbox That Never Stops Growing — Libraries in C
- 16.1 What Is a Library?
- 16.2 Why Use Libraries?
- 16.3 How Static Libraries Work
- 16.4 Creating a Static Library
- 16.5 How Dynamic Libraries Work
- 16.6 Creating a Dynamic Library (Linux/Unix)
- 16.7 Static vs Dynamic Libraries — When to Use Which
- 16.8 Using Existing Libraries
- 16.9 Practical Example: A Simple Math Library
- 16.10 Library Documentation
- 16.11 Using Third-Party Libraries
- 16.12 L’s Quick Interruption
- Practice: Library Exercises
- 16.1 Information Literacy: Static vs Dynamic Libraries — A Practical Guide
- Chapter 17: The Assembly Line
Chapter 17: The Assembly Line — How Your Code Becomes a Program
- 17.1 The Four Stages of Compilation
- 17.2 Stage 1: Preprocessing
- 17.3 Stage 2: Compilation
- 17.4 Stage 3: Assembly
- 17.5 Stage 4: Linking
- 17.6 The C Preprocessor — Deeper Dive
- 17.7 Conditional Compilation — Advanced Patterns
- 17.8 The Compiler Flags You Should Know
- 17.9 Real-World Example: Building a Multi-file Project
- 17.10 L’s Quick Interruption
- Practice: Preprocessor and Compilation Exercises
- Chapter 18: The Detective and The Watchmaker
Chapter 18: The Detective and The Watchmaker — Debugging and Profiling
- 18.1 What Is Debugging?
- 18.2 The Simplest Debugging Tool: printf
- 18.3 GDB — The GNU Debugger
- 18.4 Common Debugging Techniques
- 18.5 Valgrind — Finding Memory Bugs
- 18.6 What Is Profiling?
- 18.7 Profiling with gprof
- 18.8 The Optimization Process
- 18.9 Common Optimization Techniques
- 18.10 Common Debugging and Profiling Mistakes
- 18.11 L’s Quick Interruption
- Practice: Debugging and Profiling Exercises
- 18.1 Information Literacy: Debugging and Profiling in Practice
- Chapter 19: The Recipe That Cooked Itself
Chapter 19: The Recipe That Cooked Itself — Makefiles and Build Automation
- 19.1 What Is a Makefile?
- 19.2 A Simple Makefile
- 19.3 Multiple Targets
- 19.4 Multi-File Projects
- 19.5 Makefile Variables
- 19.6 Pattern Rules
- 19.7 Phony Targets
- 19.8 A Complete Makefile
- 19.9 Using the Complete Makefile
- 19.10 Common Makefile Mistakes
- 19.11 L’s Quick Interruption
- Practice: Makefile Exercises
- 19.1 Information Literacy: Make and Build Tools — A Complete Guide
- Chapter 20: The Library of Everything
Chapter 20: The Library of Everything — Version Control and Project Management
- 20.1 What Is Version Control?
- 20.2 Git — The Most Popular Version Control System
- 20.3 Getting Started with Git
- 20.4 A Typical Git Workflow
- 20.5 Branching
- 20.6 Merging
- 20.7 Working with Remote Repositories
- 20.8 The Four Stages of Git
- 20.9 Common Git Commands
- 20.10 The .gitignore File
- 20.11 Real-World Workflow Example
- 20.12 Project Management Beyond Git
- 20.13 L’s Quick Interruption
- Practice: Git Exercises
- 20.1 Information Literacy: Version Control — The Backbone of Modern Development
- Special Issue 1: Windows — The Operating System You Probably Already Use
- 1.1 What Is Windows?
- 1.2 Windows Architecture: A Bird’s-Eye View
- 1.3 Windows and C: A Love Story
- 1.4 Common Windows Headers and What They’re For
- 1.5 Building Windows C Programs
- 1.6 Windows Security: How the OS Protects Itself
- 1.7 C Language Security: Why C Is Powerful — And Dangerous
- 1.8 Windows Security Features for Developers
- 1.9 Common C Errors on Windows and How to Fix Them
- 1.10 Windows Command-Line Basics Every C Programmer Should Know
- 1.11 Windows vs Linux: What C Programmers Need to Know
- 1.12 L’s Final Thought
- Quick Check: Can You Answer These?
- Special Issue 2: Linux — The Operating System That Runs the World
- 1.1 What Is Linux?
- 1.2 Linux Philosophy: The Unix Way
- 1.3 Linux Distributions
- 1.4 Linux and C: A Deep Connection
- 1.5 The Linux Filesystem Hierarchy
- 1.6 Linux Command Basics Every C Programmer Should Know
- 1.7 Compiling C on Linux
- 1.8 Common Linux C Errors and Fixes
- 1.9 Debugging on Linux
- 1.10 Linux vs Windows: C Programmer’s View
- 1.11 L’s Final Thought
- Quick Check: Can You Answer These?
- Special Issue 3: Android — The Operating System That Lives in Your Pocket
- 1.1 What Is Android?
- 1.2 Android Architecture: A Layered View
- 1.3 Android and C: The Native Connection
- 1.4 The NDK: Writing C for Android Apps
- 1.5 JNI: Java Native Interface
- 1.6 Common Android C Headers and Libraries
- 1.7 Logging in Android C Code
- 1.8 Android vs Linux: What C Programmers Need to Know
- 1.9 Common Android C Errors and Fixes
- 1.10 The APK Structure
- 1.11 Building Android C Code: CMake
- 1.12 L’s Final Thought
- Quick Check: Can You Answer These?
- Special Issue 4: macOS — The Operating System That Hides Power Behind Elegance
- 1.1 What Is macOS?
- 1.2 macOS Architecture: The Two Layers
- 1.3 macOS and C: The Connection
- 1.4 The XNU Kernel: Two Worlds in One
- 1.5 macOS for C Programmers: What You Need to Know
- 1.6 macOS vs Linux: What C Programmers Need to Know
- 1.7 Installing C Tools on macOS
- 1.8 Common macOS C Errors and Fixes
- 1.9 Debugging on macOS
- 1.10 macOS C Libraries You Should Know
- 1.11 macOS vs Windows vs Linux
- 1.12 L’s Final Thought
- Quick Check: Can You Answer These?
- Side Story: The Gates of Code
- Chapter 21: The Key That Opened the Mountain
- Chapter 21: The Life of a Program — From Source to Process
- Practice: Process Exercises
- Chapter 21: The Key That Opened the Mountain
- Chapter 21: The Life of a Program — From Source to Process
- Practice: Process Exercises