Sample programs
NOTE: I tend to way overcomment these programs, since they are intended as pedagogical programs, not production programs. You're welcome to borrow code from these samples, but keep the commenting advice in our style guide in mind for your own code.
For most of our programs, you should use the following compilation approach. We'll discuss all the parts of this command over the first few days of the term.
gcc -Wall -Werror -O0 -g -o hello hello.c
Samples
- hello.c (printf)
- character_counter.c (command-line arguments and reading from a file)
- strings.c (null-terminated character strings)
- pointers.c (starting to talk about pointers)
- arrays2d.c (2-dimensional arrays are weird, but interesting)
- sizes.c (sizeof & how many bytes do standard types require?)
- memory.c (*, &, ., and ->)
- hello.asm (printing a null-terminated string in x86_64 assembly language)
- bitwise.c (&, |, and >>)
- printing-bytes.c (printing one byte in hex is hard)
- smiley.c (storing UTF-8 by hand and printing it in a couple ways)
- environment.c (two ways to access environment variables in C)
- gdb_test.c (a recursive function to support gdb experimentation)
- charstar.c (exploring the many notations for pointers to chars)
Process-related samples for command-shell assignment
Race conditions
- race.c (shared memory and a simple race condition)
- race_with_lock.c (a failed attempt to protect shared memory with a shared lock)
- norace.c (protecting shared memory with a semaphore)
- pthreads_sample.c (using POSIX Threads)