Skip to main content

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 char arrays, the unsafe vs bounded functions, and buffer discipline
  • standard I/O: printf, scanf, fgets, file streams, and why gets was removed from C11
  • structs, unions, typedef, and the memory layout consequences of each
  • header organization and modular builds across multiple .c files
  • build systems: make, the compile/link cycle, and using gcc -Wall -Wextra -std=c11

What it deliberately does not try to finish here:

  • manual memory management with malloc/free and 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:

  1. What is the difference between a variable, an object in memory, and the name of a function in C?
  2. When you run gcc hello.c -o hello, name at least three distinct programs that run as part of that command.
  3. In int a[10]; int *p = a;, what exactly is a when you write a alone, and how is it different from p?
  4. If char s[] = "hello"; and you call strlen(s), what is the return value, and why is it not 6?
  5. 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 .o files?
  • 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

OrderConceptTypeFocus
1C Is a Portable AssemblerPRIMARYThe abstract machine C exposes: objects, memory, and undefined behavior
2The Translation PipelinePRIMARYPreprocessor, compiler, assembler, linker, and what each step produces
3The C Standards: K&R, C89, C99, C11SUPPORTINGWhat 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

OrderConceptTypeFocus
4Primitive Types, Integer Widths, and Implementation-Defined BehaviorPRIMARYWidths, signedness, integer promotion, and portability limits
5Operators, Precedence Traps, and Sequence PointsPRIMARYExpression value vs order of side effects, and undefined orderings
6Control Flow: if, switch, loops, and the for idiomSUPPORTINGStructured 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

OrderConceptTypeFocus
7Functions, Declarations vs Definitions, Header FilesPRIMARYPrototypes, definitions, and how a translation unit is assembled
8Scope, Linkage, and Storage ClassesPRIMARYauto, static, extern, register, and what the linker sees
9The Preprocessor: Macros, Include Guards, Conditional CompilationSUPPORTINGText 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

OrderConceptTypeFocus
10Arrays Decay to Pointers: the Foundational C SubtletyPRIMARYWhen a means "the array" and when it means "a pointer to its first element"
11C Strings: Null Terminators, strlen, strcpy vs strncpy, Buffer SafetyPRIMARYThe contract, the bugs, and the bounded alternatives
12Standard I/O: printf, scanf, fgets, and Why gets Was RemovedPRIMARYFormat 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

OrderConceptTypeFocus
13Structs, Unions, and typedefPRIMARYAggregate types, shared storage, and naming conventions
14Header Organization and Modular BuildsPRIMARYWhat goes in .h vs .c, include discipline, and the one-definition rule in C
15Build Systems: make Introduction and the Compile/Link CycleSUPPORTINGTargets, 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:

OrderPractice pathFocus
1C Compilation and Mental Model LabTranslation pipeline, abstract machine, and undefined behavior reading
2Types, Control Flow, and Functions WorkshopInteger width reasoning, precedence, functions, scope, linkage
3Arrays, Strings, and I/O ClinicArray decay, C-string safety, printf/scanf/fgets, struct layout
4Code KatasFluency 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:

  1. Describe the C abstract machine and name at least three classes of undefined behavior.
  2. Explain the four stages of the C translation pipeline and identify the artifact each stage produces.
  3. Distinguish K&R, C89, C99, and C11 well enough to read code from any of those eras.
  4. Reason about integer widths, signed/unsigned conversions, and implementation-defined behavior.
  5. Predict the value and evaluation order of a C expression and identify sequence-point violations.
  6. Write and read C control flow using structured if, switch, and loop idioms without accidental fall-through.
  7. Separate declarations from definitions, write correct header files, and explain what the linker sees.
  8. Use auto, static, extern, and (historically) register correctly, and describe linkage.
  9. Use the preprocessor safely: include guards, conditional compilation, and guarded macros.
  10. Explain when an array name decays to a pointer and when it does not, and write C-string code that stays within bounds.
  11. Use printf, scanf, and fgets correctly, and explain why gets was removed in C11.
  12. Design structs with clear invariants, use typedef judiciously, and describe the trade-offs of unions.
  13. 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, gcc output 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=c11 build
  • a C-string safety log: each unsafe strcpy/sprintf/gets pattern 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=c11 warned 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 struct with an explicit invariant and defend the choice of typedef or not
  • you can write a Makefile that 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 stuck means try the concept page, self-check, and drill first.
  • Optional deep dive means 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

DayWork
1Concepts 1-3 and a hand-drawn translation-pipeline diagram
2Concepts 4-5 and one precedence/sequence-point drill sheet
3Concepts 6-7 and one small multi-file compile exercise
4Concepts 8-9 and one macro-bug repro with a clean fix
5Concepts 10-11 and at least two C-string reimplementations
6Concepts 12-13 and one printf/fgets I/O drill
7Concepts 14-15 and one Makefile-driven project
8Practice pages 1-2
9Practice page 3 and targeted chunk reinforcement
10Practice page 4 (code katas), quiz, mistake-log cleanup

Reference

If you need exact links into the local chunked books, use Reference and Selective Reading.


Build Your Own X — elective

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.