This repository collects short C programs that accompany the lectures of the Programming 1 course. Its organization is based on bonigarcia/c-programming: each lecture is a folder, divided into topics, and each .c file is a standalone, commented program.
See the course overview for what you will learn, the learning outcomes, and where C is used, lecture by lecture.
Requirements: gcc and make (Linux, WSL, macOS, or GitHub Codespaces).
make # compile all examples
make clean # remove compiled files
# or compile a single file
gcc -Wall -g lecture01-intro/hello_world/hello.c -o hello
./helloFolders with their own Makefile contain multi-file programs (for example lecture01-intro/header_files); run make run inside that folder.
The handouts for the 4 lab sessions (plus the Lab 0 preparation lab) are in labs/:
- Lab 0 — Environment setup, Linux commands, SSH, Git/GitHub
- Lab 1 — C fundamentals
- Lab 2 — Arrays, strings, and debugging
- Lab 3 — Pointers and dynamic memory
- Lab 4 — Structs, function pointers, and multi-file programs
The repo includes a .devcontainer/ configuration for opening it in GitHub Codespaces (with gcc, gdb, make, and valgrind preinstalled).
| Topic | Examples |
|---|---|
| The "hello, world" program | hello.c |
| Functions (definition, calling, reuse) | print_hello.c |
| Header files, multi-file compilation, compile/link | header_files/ |
| Variables, while loops (temperature table) | fahr_celsius.c, fahr_celsius_float.c |
| Arithmetic operators | arithmetic.c |
Increment/decrement operators i++, ++i |
increment.c |
| Comments | comments.c |
| Exercises: add, factorial, Fibonacci (recursive and iterative) | add.c, factorial.c, fibonacci.c |
| Topic | Examples |
|---|---|
| Sizes of data types | sizeof_types.c |
| Declaring variables, constants, hexadecimal numbers | declare_assign.c |
| Type casting, loss of precision | casting.c, cast_in_function.c |
| Relational and logical operators | relational_logical.c |
| if / else if / else | if_else.c |
| for, while, do-while | loops.c |
The char type and the ASCII table |
char_ascii.c |
Exercises 1, 2: converting characters to numbers, atoi |
convert.c, my_atoi.c |
| Topic | Examples |
|---|---|
| switch, break, continue | switch.c, break_continue.c |
| 1D arrays, finding the max | array_1d.c |
| 2D arrays, max along an axis | array_2d.c |
| Exercises: inner product, matrix multiplication, convolution | inner_product.c, matrix_multiplication.c, convolution.c |
| Inserting and deleting array elements | array_insert_delete.c |
Declaring pointers, & and * |
pointer_basic.c |
| Pointers to arrays, array names are constant | pointer_to_array.c |
Moving pointers, the *(++p) vs. *(p++) quiz |
pointer_arithmetic.c |
| Call by value and call by reference | call_by_value_reference.c |
| Array pointers with functions (max, reversing an array, returning an array) | array_functions.c, even_odd_bit.c |
malloc, free, realloc, memory leaks |
malloc_free.c, realloc.c, memory_leak.c |
| Variable scope (global, local, heap) | scope.c |
Structs, pointers to structs, -> |
fraction.c, struct_pointer.c |
| Linked lists (access, insert, delete, free) | linked_list.c |
| Binary search trees | binary_tree.c |
| Topic | Examples |
|---|---|
| VS Code configuration: building, debugging with gdb | vscode/ |
| A program for practicing breakpoints | sample.c |
Allocating a 2D array with int ** |
alloc_2d.c |
Allocating an n-dimensional array recursively, returning void * |
alloc_nd.c |
Freeing linked lists and binary trees: see free_list in linked_list.c and free_tree in binary_tree.c.
| Topic | Examples |
|---|---|
Multi-dimensional array indexing (struct MultiArray, data + shape) |
multi_array.c |
Strings: char[] vs. char *, the '\0' character |
string_basics.c |
strlen, strcmp, strcat, strcpy, strstr |
string_functions.c |
| Implementing string functions yourself | my_string.c |
| Function pointers, arrays of function pointers | function_pointer.c |
Function pointers as parameters (reduce, qsort) |
reduce.c, qsort_compare.c |
typedef |
typedef.c |
#define: constants and macros |
define.c |
#ifdef: Debug / Release, per operating system |
debug_release.c, platform.c |
| Multiple files: prototypes and implementations, compile and link | multiple_files/ |
Calling functions from a dynamic library (dlopen/dlsym) |
dynamic_library/ |
# Debug and Release builds with #ifdef
gcc -DDEBUG lecture08-function-pointers/preprocessor/debug_release.c -o debug && ./debug
gcc lecture08-function-pointers/preprocessor/debug_release.c -o release && ./release
# Check for memory leaks
cd lecture03-array-pointer/dynamic_mem
gcc -g memory_leak.c -o memory_leak && valgrind --leak-check=full ./memory_leak
# Debug with gdb
gcc -g lecture07-review/multi_array/sample.c -o sample && gdb ./sample- Brian Kernighan, Dennis Ritchie — The C Programming Language
- Nick Parlante — Essential C
- Harvard CS50x and the cs50.dev environment
- bonigarcia/c-programming
- Practice: LeetCode (number and array topics)