Lab: Elementary C Programming

This laboratory exercise provides practice with basic elements of writing, editing, compiling, and running programs written in the C programming language.

A. Basic C Program Development

Getting Started

Create a directory called elementary-c to hold your work for this lab under ~/csc161/labs/ in your home directory. You should be able to do this entirely from the terminal.

Next, download a copy of the file quarts.c and save it to that directory.

Now, starting from your terminal, run this command to move to the elementary-c directory an open Visual Studio Code there:

$ cd ~/csc161/labs/elementary-c
$ code .

Once VSCode starts up you should see a file browser on the left, and quarts.c should be listed. Double-click quarts.c in VSCode’s file browser to open it.

Compiling and Running

You will still compile and run this program in the terminal. You can use the terminal you already have opened, or you can open a terminal within VSCode by going to the New Terminal option under the Terminal menu.

Compile and run the program in your terminal window by typing:

$ clang -o quarts quarts.c
./quarts

Run the program several more times by typing just ./quarts. You only need to re-run clang—that is, to recompile quarts.c—when you edit the program’s source code.

Experimenting with Compiling

Make the following typographical errors in quarts.c, recompile, and observe what, if anything, happens. (Note: in many cases, you will get an error of some kind.)

In each case, check whether the program compiles, and whether the program runs. If the program does not compile, what happens if you try to run ./quarts?

In working through the following, pay close attention to the error messages that result. What do they say? These will be helpful cues to you later as you try to find the underlying cause of errors the compiler reports to you.

  1. Type a few characters into your program BEFORE any of the code.
  2. Type a few characters into your program AFTER the code.
  3. Type some extra words inside of your main method.
  4. Misspell your variables.
  5. Misspell your printed output.
  6. Misspell the name of your main() function.
  7. Misspell the filename stdio.h on the line that begins with #include.

You will almost certainly make all of these errors on accident at some point in this course. You might not remember all of the error messages for each, but it can help to remember that even small errors can sometimes produce long, cryptic error messages.

Writing Your Own Program

In this exercise, you will write a C program that uses values for pints, quarts, and gallons and determines the corresponding number of liters. For example, your program might compute the number of liters corresponding to 3 gallons, 2 quarts, and 1 pint (13.72209446 liters).

Create a new file called liters.c. You can do this inside VSCode using the file browser, or by running code liters.c in VSCode’s integrated terminal. You should start with a copy of the quarts.c file.

To organize this program, begin by declaring pints and gallons as variables in addition to quarts and liters in the existing program. Assign values to these variables. For example:

int gallons = 3;
int quarts = 2;
int pints = 1;

To compute the total number of liters, convert the values of quarts, pints, and gallons to liters, then add them together. You can create additional variables to hold these intermediate values, or you can compute everything together. If you choose to save intermediate results (e.g. the quarts value converted to liters) make sure you think carefully about what type of variable you need to use.

In case you don’t know the conversions off the top of your head, one gallon is four quarts, a pint is half a quart, and the conversion from quarts to liters is in the quarts.c file already.

Your program should print the values of gallons, quarts, and pints, as well as the equivalent number of liters for the three together. As you write your code, try to match the commenting and indentation style of the quarts.c program. We will discuss good commenting and formatting in a later class.

You will need to save your file, then use clang to compile the program. You should practice saving and compiling frequently; the compiler will catch many errors, and the sooner you find them the easier they are to fix. To do this effectively, you’ll need to make sure your program is in a compile-able state even when it is incomplete. For this exercise, that likely just means including the closing curly brace (}) at the end of main.

Exercises

  1. Complete your implementation in liters.c and test it on a few different values.
  2. Write a second program, circle.c, that computes a circle’s area and circumference from its radius.