Sample programs
- STUDY THESE PROGRAMS. Reading and understanding what's going on in these samples is part of your homework, and I will assume that you have done that work as the term progresses.
- Ask questions. You won't understand everything in these samples on first reading, so collect your questions and get them answered.
- Experiment. You won't understand everything in these samples on first reading, so play around with them. Some of your best learning will start with "I wonder what happens if I make this change...".
- Building these programs.
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
- Pedagogical comments vs. production comments. These programs have tons of explanatory comments, because they are intended as pedagogical programs, not production programs. Production code (i.e., code intended for use by end users or submission as homework) should follow the commenting advice in our style guide. You're welcome to borrow code from these samples, but delete my comments before you're done working.
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?)
- integers.c (writing integers to output in various ways)
- integers2.c (more integer stuff)
- 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)
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)
Threads
- pthreads_test.c (using POSIX Threads)
Network programming with sockets