Module 1: C Programming Fundamentals
Primary text: The C Programming Language (Kernighan & Ritchie, 2nd edition)
Selective support: Computer Organization and Design (Patterson & Hennessy) for the translation pipeline, Code (Petzold) for the mental model of machines executing instructions, and a small pass of SICP ideas where abstraction thinking sharpens C design
This guide is the primary teacher. You do not need to read the source books front-to-back to complete this module. You do need to become operationally strong at the C mental model, types and operators, functions and linkage, arrays and pointers at the language level, C strings, standard I/O, structs, and the compile/link build cycle.
Scope of This Module
This module is not a tour of the C standard library. It is where C becomes a predictable language you can compile, link, and reason about.
What it covers in depth:
- the mental model of C: memory, objects, undefined behavior, and why C is "a portable assembler"
- the translation pipeline: preprocessor, compiler, assembler, linker, and what each step produces
- the C standards: K&R C, C89, C99, C11 - what each added and why it matters
- primitive types, integer widths, conversions, and implementation-defined behavior
- operators, precedence, and the difference between expression value and sequence of effects
- control flow:
if,switch,while,for,do/while,break,continue,goto - functions: declarations vs definitions, prototypes, header files, and the compilation unit
- scope, linkage, and storage classes:
auto,static,extern,register - the preprocessor:
#include,#define, conditional compilation, include guards - arrays and their decay to pointers in value contexts
- C strings as null-terminated
chararrays, the unsafe vs bounded functions, and buffer discipline - standard I/O:
printf,scanf,fgets, file streams, and whygetswas removed from C11 - structs, unions,
typedef, and the memory layout consequences of each - header organization and modular builds across multiple
.cfiles - build systems:
make, the compile/link cycle, and usinggcc -Wall -Wextra -std=c11
What it deliberately does not try to finish here:
- manual memory management with
malloc/freeand pointer arithmetic for data structures (that is Module 2) - deep machine representation of floats, endianness, and alignment (Module 2 and Module 3)
- OS-level systems programming: processes, signals, filesystems beyond stdio (Module 4)
- C++, Rust, or any comparison with other systems languages
This is the foundation that the rest of Semester 4 is built on.
Before You Start
Answer these closed-book before starting the main path:
- What is the difference between a variable, an object in memory, and the name of a function in C?
- When you run
gcc hello.c -o hello, name at least three distinct programs that run as part of that command. - In
int a[10]; int *p = a;, what exactly isawhen you writeaalone, and how is it different fromp? - If
char s[] = "hello";and you callstrlen(s), what is the return value, and why is it not6? - Why is
printf("%d", 3.14);wrong, and what category of error is it?
Diagnostic Interpretation
4-5 solid answers
- You are ready for the full path.
2-3 solid answers
- Continue, but expect extra time in Cluster 1 (mental model) and Cluster 4 (arrays/strings/I/O).
0-1 solid answers
- Revisit Semester 3 notes on how a higher-level language is compiled and executed. C will feel alien without that backdrop.
What This Module Is For
C is the lingua franca of systems work. Later modules and jobs repeatedly ask:
- what does this source line actually do at the machine level?
- what does the linker see when it combines my
.ofiles? - which bug is mine, which is the compiler's, and which is undefined behavior?
- what is a pointer really pointing at, and who owns that memory?
- how do I write a C program I can confidently ship and maintain?
This module builds the fluency needed for:
- memory, pointers, and machine representation (Module 2)
- computer organization and architecture (Module 3)
- systems-level programming, processes, and I/O (Module 4)
- abstraction and interpretation (Module 5), which assumes you can read C comfortably
- every later career encounter with embedded, OS, database, or performance-critical code
You are learning to write C that you can explain line by line, not C that "just seems to work."
Concept Map
How To Use This Module
Work in order. The later clusters only make sense if the earlier mental models are stable.
Cluster 1: The C Mental Model
| Order | Concept | Type | Focus |
|---|---|---|---|
| 1 | C Is a Portable Assembler | PRIMARY | The abstract machine C exposes: objects, memory, and undefined behavior |
| 2 | The Translation Pipeline | PRIMARY | Preprocessor, compiler, assembler, linker, and what each step produces |
| 3 | The C Standards: K&R, C89, C99, C11 | SUPPORTING | What each standard changed and how to read code from different eras |
Cluster mastery check: Can you describe what happens between hello.c on disk and a process executing main, naming each transformation?
Cluster 2: Types, Expressions, and Control Flow
| Order | Concept | Type | Focus |
|---|---|---|---|
| 4 | Primitive Types, Integer Widths, and Implementation-Defined Behavior | PRIMARY | Widths, signedness, integer promotion, and portability limits |
| 5 | Operators, Precedence Traps, and Sequence Points | PRIMARY | Expression value vs order of side effects, and undefined orderings |
| 6 | Control Flow: if, switch, loops, and the for idiom | SUPPORTING | Structured control, fall-through, and loop design discipline |
Cluster mastery check: Can you predict a C expression's value and say which subexpressions have a defined evaluation order?
Cluster 3: Functions, Scope, and Storage Classes
| Order | Concept | Type | Focus |
|---|---|---|---|
| 7 | Functions, Declarations vs Definitions, Header Files | PRIMARY | Prototypes, definitions, and how a translation unit is assembled |
| 8 | Scope, Linkage, and Storage Classes | PRIMARY | auto, static, extern, register, and what the linker sees |
| 9 | The Preprocessor: Macros, Include Guards, Conditional Compilation | SUPPORTING | Text substitution before compilation and its well-known hazards |
Cluster mastery check: Given a name in a C file, can you say where it is visible, when it has storage, and what symbol the linker resolves for it?
Cluster 4: Arrays, Strings, and I/O
| Order | Concept | Type | Focus |
|---|---|---|---|
| 10 | Arrays Decay to Pointers: the Foundational C Subtlety | PRIMARY | When a means "the array" and when it means "a pointer to its first element" |
| 11 | C Strings: Null Terminators, strlen, strcpy vs strncpy, Buffer Safety | PRIMARY | The contract, the bugs, and the bounded alternatives |
| 12 | Standard I/O: printf, scanf, fgets, and Why gets Was Removed | PRIMARY | Format strings, buffered streams, and safe input |
Cluster mastery check: Can you reason about a C string operation without writing past its buffer, and defend your reasoning from the array-pointer conversion rules?
Cluster 5: Structuring C Programs
| Order | Concept | Type | Focus |
|---|---|---|---|
| 13 | Structs, Unions, and typedef | PRIMARY | Aggregate types, shared storage, and naming conventions |
| 14 | Header Organization and Modular Builds | PRIMARY | What goes in .h vs .c, include discipline, and the one-definition rule in C |
| 15 | Build Systems: make Introduction and the Compile/Link Cycle | SUPPORTING | Targets, prerequisites, recipes, and the incremental build |
Cluster mastery check: Can you lay out a small multi-file C project with clean headers, a Makefile, and a clean gcc -Wall -Wextra -std=c11 build?
Then work these practice pages:
| Order | Practice path | Focus |
|---|---|---|
| 1 | C Compilation and Mental Model Lab | Translation pipeline, abstract machine, and undefined behavior reading |
| 2 | Types, Control Flow, and Functions Workshop | Integer width reasoning, precedence, functions, scope, linkage |
| 3 | Arrays, Strings, and I/O Clinic | Array decay, C-string safety, printf/scanf/fgets, struct layout |
| 4 | Code Katas | Fluency drills: strlen, strdup, dynamic array, mini grep, bounded buffer, small make project |
Use Module Quiz after the concept and practice path. Use Reference and Selective Reading and Learning Resources only for targeted reinforcement.
Learning Objectives
By the end of this module you should be able to:
- Describe the C abstract machine and name at least three classes of undefined behavior.
- Explain the four stages of the C translation pipeline and identify the artifact each stage produces.
- Distinguish K&R, C89, C99, and C11 well enough to read code from any of those eras.
- Reason about integer widths, signed/unsigned conversions, and implementation-defined behavior.
- Predict the value and evaluation order of a C expression and identify sequence-point violations.
- Write and read C control flow using structured
if,switch, and loop idioms without accidental fall-through. - Separate declarations from definitions, write correct header files, and explain what the linker sees.
- Use
auto,static,extern, and (historically)registercorrectly, and describe linkage. - Use the preprocessor safely: include guards, conditional compilation, and guarded macros.
- Explain when an array name decays to a pointer and when it does not, and write C-string code that stays within bounds.
- Use
printf,scanf, andfgetscorrectly, and explain whygetswas removed in C11. - Design structs with clear invariants, use
typedefjudiciously, and describe the trade-offs of unions. - Split a small C program across multiple translation units with clean headers and a
Makefile.
Outputs
- a C mental-model notebook with at least 10 annotated reading exercises on undefined behavior, integer promotion, and array decay
- a translation-pipeline diagram you drew yourself, with
gcc -E,gcc -S,gcc -c,gccoutput files named - a portability sheet describing integer width behavior on at least two compilers or targets
- at least 8 precedence and sequence-point drills with written justifications
- a multi-file C project with public headers, private headers, a
Makefile, and a clean-Wall -Wextra -std=c11build - a C-string safety log: each unsafe
strcpy/sprintf/getspattern you see in exercises with a bounded rewrite - a structs and unions sketchbook with at least 5 designed aggregates and explicit invariants
- a mistake log with at least 12 real C errors (
missing prototype,off-by-one in a C string,format string mismatch,non-static file-scope definition in a header,macro without parentheses, etc.) - a short memo connecting this module's habits to the pointer, memory, and systems modules that follow
Completion Standard
You have completed Module 1 when all of these are true:
- you can compile, link, and run a small multi-file C program from the command line without guessing
- you can read C code and point to the object, its lifetime, and its linkage
- you can explain what
-Wall -Wextra -std=c11warned about before you fixed it - you can say when an array name is the array and when it is a pointer
- you can write C-string code that never writes past its buffer
- you can design a
structwith an explicit invariant and defend the choice oftypedefor not - you can write a
Makefilethat only rebuilds what changed and explain each rule
If your code compiles but you cannot explain each warning the compiler would have printed with stricter flags, the module is not complete.
Reading Policy
- Concept pages are the main path.
- Local book chunks are selective reinforcement, not a second syllabus.
Read only if stuckmeans try the concept page, self-check, and drill first.Optional deep divemeans additional nuance, not required progression.- Because this is the foundation for the rest of Semester 4, written explanations and working code are both required.
Suggested Weekly Flow
| Day | Work |
|---|---|
| 1 | Concepts 1-3 and a hand-drawn translation-pipeline diagram |
| 2 | Concepts 4-5 and one precedence/sequence-point drill sheet |
| 3 | Concepts 6-7 and one small multi-file compile exercise |
| 4 | Concepts 8-9 and one macro-bug repro with a clean fix |
| 5 | Concepts 10-11 and at least two C-string reimplementations |
| 6 | Concepts 12-13 and one printf/fgets I/O drill |
| 7 | Concepts 14-15 and one Makefile-driven project |
| 8 | Practice pages 1-2 |
| 9 | Practice page 3 and targeted chunk reinforcement |
| 10 | Practice page 4 (code katas), quiz, mistake-log cleanup |
Reference
If you need exact links into the local chunked books, use Reference and Selective Reading.
Two excellent first projects in C: the Text Editor (kilo) tutorial and the Shell tutorial. Both finish in a weekend, both produce something visible. See Build Your Own X overview.
Rich Learning Pages
Worked Examples | Guided Labs | Case Studies | Mistake Clinic | Reading Guide | Capstone Thread
Model Artifact Calibration
For debugging evidence, compare your investigation notes to the C debugging transcript model artifact.