Elementary C Programming
Introduction
This laboratory exercise provides practice with basic elements of writing, editing, compiling, and running programs written in the C programming language. This will use the quarts.c program, complete with annotations.
Starting Emacs
-
Working within a terminal window, open
quarts.cin emacs.
Starting at your home directory, you could use these commands:cd csc161 cd labs emacs quarts.c &
-
Use the links for the reading (the quarts.c program) to open the program
quarts.cin your browser. Then copy and paste it into theemacswindow.
Compiling and Running
-
Compile and run the program in your terminal window by typing:
gcc -o quarts quarts.c ./quarts
-
Run the program several more times by typing just
./quarts. (You need not compile the program each time unless you have changedquarts.c.)
Experimenting with Compiling
-
Make the following typographical errors in
quarts.c, recompile, and observe what, if anything, happens.
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 runquarts?In working through the following, pay 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.
- Type a few characters into your program BEFORE any of the code.
- Type a few characters into your program AFTER the code.
- Type some extra words inside of your main method.
- Misspell your variables.
- Misspell your printed output.
- Misspell the name of your
main()method. - Misspell the name of the included library.
Writing Your Own Program
-
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 (i.e., 14.5 quarts total).
To organize this program, begin by declaring
pintsandgallonsas variables in addition toquartsandlitersin the existing program. Next, assign values to these variables, such as:gallons = 3; quarts = 2; pints = 1;In computing the total value of liters, one approach would be to compute the total number of quarts from
pints, quarts,andgallons(possibly using another variable, such astotal_quarts). From thistotal_quarts, you could compute the total number of liters.In computing the total number of quarts, you should use 4 quarts per gallon and 2 pints per quart. (Be sure that 3 gallons (given in the example above) translates to 12 quarts, not 3/4 or 0.75 quarts.)
You should reference the annotations on
quarts.cto properly print out your output. -
Although you can name this new program whatever you like, you should end the file name with
.cfor two reasons:-
You can identify the C programs quickly when you list files in your directory with the
lscommand. -
emacs recognizes the
.cextension as indicating a C program, and emacs adjusts its setting to aid your editing for that type of file.
-
Writing More C
-
Write a C program that uses a value for the radius of a circle and computes the circle's area and circumference.
Optional Activity: Experimenting with emacs
-
Experiment more with the emacs editor, following the reading on the Emacs text tditor.
