Sample Programs

The following sample programs (adapted from those provided by Jeff Ondich) will help you explore many useful features in C.

Study them! It is expected that you read them as the term progresses, just as you would the textbook or any other assigned reading.

Here are some things to keep in mind as you explore these programs:

  • Ask questions: You aren’t expected to understand everything the first time you read these sample programs. As you work through them, write down your questions and make sure to get them answered!

  • Experiment: The beauty of programming is that you can (usually) make changes and immediately see the impact when re-running your code. Play around with these samples to help you understand them! If you find yourself thinking I wonder what happens if I make this change…, just try it!

  • Build these programs: For most of these samples, you should use the following compilation approach:

    gcc -Wall -Werror -g -o hello hello.c
    

We will talk about the different parts of this command throughout the first few days of the term.

  • Pedagogical comments vs. production comments: These programs are written to have a lot of explanatory comments, because they are designed as pedagogical programs (to teach you things!), not as production programs. Production code (i.e., that intended for use by end users or even as homework submissions) should following the commenting advice in our style guide. You are welcome to borrow code from these samples, but delete these pedagogical comments before you are ready to submit.

Samples

Getting started in C, chars and strings, arrays, pointers:

Bits and bytes, integers:

Program representation and ISA:

Buffer overflow attacks:

  • attack.c - a program that is susceptible to buffer-overflow attacks

Process-related samples:

  • Makefile - a simple Makefile for all process-related samples