Goal: Understand what C is and how it differs from JS/TS
- Compilation model (preprocess → compile → link)
main, headers, source files- Variables, types, operators
- Control flow (
if,for,while,switch) - Functions & function prototypes
- Stack vs heap (conceptual only)
stdio.h,printf,scanf- No runtime safety net
- No garbage collector
- No bounds checking
- No runtime type safety
- You control memory and lifetimes
- Write a program that prints command-line arguments (
argc,argv)✅ - Implement a calculator using
switch✅ - Write a function that swaps two integers (first attempt will fail—learn why) ✅
- Implement your own
strlen✅
Compile with:
gcc -Wall -Wextra -WerrorGoal: Become comfortable with pointers and memory ownership
- Addresses and
& - Pointers and
* - Pointer arithmetic
NULL- Arrays vs pointers
- Stack memory layout
- Passing by value vs by pointer
- Dereferencing uninitialized pointers
- Out-of-bounds access
- Returning pointers to stack memory
- Write a function that reverses an array in place✅
- Print memory addresses of array elements✅
- Implement
strcpyusing pointers✅ - Write a program that crashes, then explain why✅
Goal: Understand heap memory and manual lifecycle management
malloc,calloc,realloc,free- Ownership rules
- Memory leaks
- Double free
- Dangling pointers
- Heap vs stack lifetimes
valgrindAddressSanitizer
run with:
gcc -g -Wall -Wextra -Werror main.c -o main && valgrind --leak-check=full ./main- Allocate an array dynamically and fill it✅
- Implement a dynamic string type✅
- Write a program with a memory leak, then fix it✅
- Re-implement
strdup✅ - Re-implement
memcpy✅
Goal: Learn why C strings are dangerous
- Null-terminated strings
- Buffer overflow
strcpyvsstrncpyvssnprintf- Input handling (
fgetsvsgets) - Defensive coding
- Writing past buffer end
- Missing
\0 - Reading uninitialized memory
- Implement
strlensafely✅ - Write a safe input function using
fgets✅ - Implement string concatenation with size limits✅
- Write a program vulnerable to buffer overflow, then fix it
- Detect overflow using AddressSanitizer
Goal: Model complex data safely
struct,typedef- Padding & alignment
- Nested structs
- Enums and bit flags
- Ownership inside structs
- Define a
Userstruct with dynamically allocated fields✅ - Write create/destroy functions for a struct✅
- Pass structs by value vs pointer—measure difference✅
- Implement a linked list node✅
- Introduce and fix a dangling pointer bug✅
Goal: Work with the OS
FILE*,fopen,fread,fwrite- Binary vs text files
- Error handling with
errno - Resource leaks (files ≠ memory)
- Read a file line by line✅
- Write a binary file and read it back
- Implement a config file parser
- Handle all failure cases explicitly
- Intentionally forget
fcloseand detect it
Goal: Learn what C does not promise
- What UB actually is
- Strict aliasing
- Signed integer overflow
- Uninitialized reads
- Lifetime violations
- Write code that behaves differently with
-O0vs-O2 - Trigger signed overflow
- Use an uninitialized variable
- Cast incompatible pointer types
- Explain why behavior is undefined
Goal: Think like a systems programmer
- Custom allocators
- Memory pools
- Cache friendliness
- Alignment
restrict- Escape analysis
- Write a simple arena allocator
- Compare
mallocvs arena performance - Implement a fixed-size object pool
- Profile memory access patterns
- Remove heap allocations from hot paths
Goal: Understand shared memory dangers
pthread- Data races
- Mutexes
- Atomic operations
- False sharing
- Write a multithreaded counter (broken)
- Fix it with a mutex
- Fix it with atomics
- Cause a race condition intentionally
- Analyze race with ThreadSanitizer
Goal: Prove mastery
- Custom memory allocator
- HTTP server (no frameworks)
- JSON parser
mallocreplacement- Simple shell
-
Zero memory leaks
-
No undefined behavior
-
Compiles with:
-Wall -Wextra -Werror -fsanitize=address,undefined
gcc / clang
valgrind
AddressSanitizer
UndefinedBehaviorSanitizer
gdbFrom Node.js:
“The runtime protects me”
To C:
“The compiler trusts me—and will punish mistakes silently”
- Book: The C Programming Language – K&R
- Book: Expert C Programming – Peter van der Linden
- Book: Computer Systems: A Programmer’s Perspective
- Practice: LeetCode (but in C, memory-safe)