Example C Programs
I've written a bunch of short C programs to demonstrate how C programs
interface with and use the Unix operating system. Here are links to
them, in approximately the order in which I'll discuss them:
- cswap.c, which does the same job as the shell
script swap: it takes two
command-line arguments and prints them in reverse order.
- mycat.c, which does the same job as the "cat"
command, at least in a simple form: with no arguments, it copies stdin
to stdout, and with one argument, it copies the specified file to
- mycat2.c, a more general version that allows
more than one file. It's also restructured to use a "dumpfile" function
rather than doing the copying in-line.
- evalenv.c, which takes the name of an
environment variable and prints out its value, if any.
- myset.c, which sets the value of an
environment variable. Not a particularly useful program, since
environment variables changed in a subprocess don't affect their values
in the parent process.
- wsp.buggy.c. Several years ago I
assigned people to write a program which would take an environment
variable name and a filename, evaluate the environment variable as a
colon-delimited list of directories (like PATH), and look for the
specified filename in each of those directories. This is a
BUGGY version of that program; I intentionally
introduced some bugs to give people practice using gdb.
- clongword.c, which finds the longest word
- clongword2.c, which also finds the longest
word in stdin, but it uses scanf rather than reading character by
- clongword3.c, which does the exact same
thing, but is a little more "bulletproof" in that it refuses to read a
longer word than it has space for. Demonstrates use of sprintf.
You are visitor number
to this and related
pages since Feb. 8,
Thu Oct 24 11:49:41 EDT 1996
Stephen Bloch / firstname.lastname@example.org