This laboratory exercise provides practice with basic elements of writing, editing, compiling, and running programs written in the C programming language.
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 these command to move to the elementary-c directory and open the quarts.c file using emacs:
$ cd ~/csc161/labs/elementary-c
$ emacs quarts.c &
You will still compile and run this program in the terminal.
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.
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.
main() function.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.
Switch drivers 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).
First we will create a new file called liters.c. There are several ways this can be done.
emacs liters.c & in the terminal. This creates the file in your current working directory and opens it with emacs.emacs open, you can use keystroke C-x C-f. At the bottom of the emacs window there will be a prompt Find file:. Type the name of the file you want to create and press return.emacs open, you can use the menu to create a new file.After creating your new file, copy in the contents of the quarts.c file to start with. Make sure to update the list of authors.
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.
switch drivers before each exercise
liters.c and test it on a few different values.circle.c, that computes a circle’s area and circumference from its radius.