book musings



missle defense



cultcha blog


scripts & programs

on the town



creds (PDF)



C programming

Eye candy

C Code Snippets:

Below are a set of simple programs that demonstrate some basic functionality of the C programming language. Each program provides a simple example of how a single feature of C works.

Each program can either be scanned or actually run. Click on the link of the .txt version to look at the program. The text in between /* and */ are the instrructional comments and not part of the working code itself.

To run a program (In Linux or Unix), download the .c file, and compile it atthe command line, i.e. "cc sample.c" or "gcc sample.c" depending on which compiler you use. This should give you a program called "a.out" which you can run by typing "./a.out" at the command prompt.

Keep in mind, nothing much happens when you actually run one of these programs. The idea is that if you want to tinker with the program itself, to explore how each feature works, then you have a working program to start with.

  • Functions: function.c, function.txt

  • Pointers: pointers.c, pointers.txt

  • How pointers work with arrays: PointerArray.c, PointerArray.txt

  • For Loop: ForLoop.c, ForLoop.txt

  • execs call (for executing Unix commands): execs.c, execs.txt

  • strcpy (for copying strings into an array): StringCopy.c, StringCopy.txt

  • Exit Handlers: ExitHandlers.c, ExitHandlers.txt

  • Ingesting arguments: argies.c, argies.txt

  • Storing arguments in an array: args.c, args.txt

  • Getting the process id: CLibCallgetpid.c, CLibCallgetpid.txt

  • Recasting data types: recast.c, recast.txt

  • Parsing a string (using strtok): strtok.c, strtok.txt

  • Forking a program: VFork.c, VFork.txt

  • Sending signals between a parent process and a child process using a POSIX signal: signals.c, signals.txt

  • Sending data from a parent process to a child process using a pipe: pipe.c, pipe.txt

  • Using two pipes: 2pipes.c, 2pipes.txt

  • Using a named pipe: NamedPipe.c, NamedPipe.txt