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.

Basic C Program Development

Starting Emacs

  1. Working within a terminal window, open a program file called quarts.c in emacs.
    Starting at your home directory, you could use these commands:
    cd csc161
    cd labs
    emacs quarts.c &
    Troubleshooting: If your emacs editor opens with a split screen, and if you find the split screen annoying, you might want to switch to one window. Within emacs, hold down the control and x keys at the same time, release them and then type a 1 (one) to change to one window.

  2. Paste the contents of quarts.c from the reading into the emacs window you opened from the terminal.

Compiling and Running

  1. Compile and run the program in your terminal window by typing:
    clang -o quarts  quarts.c
    ./quarts

    In this example, clang is the name of the compiler program that you want to use to translate your file into a machine-executable one.

    The next token in the line is -o which tells the compiler that you want to create a machine-executable output file with a specific name, which is the next token in the command line: quarts. This becomes the name of the program, which you will type in order to run it. The final token in the line is the name of the file you created, which is used as the source of the compilation/translation process. In this example, typing the command to compile a single file into an executable program is pretty straight-forward. But when we are compiling multiple files (such as when we create programs to run the robot), we will use scripts to automate what can be a rather tedious and error-prone process (see step 8 below).

  2. Run the program several more times by typing just ./quarts. (You need not compile the program each time unless you have changed quarts.c.)

Experimenting with Compiling

  1. 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.

    • 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 header file.

Writing Your Own Program

  1. 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 (i.e., 14.5 quarts total).

    1. From the terminal command line, create a new program source file using emacs <filename.c>

      Note: Although you can name this new program whatever you like, you should end the file name with .c for several reasons:

      • You can identify the C programs quickly when you list files in your directory with the ls command.
      • emacs recognizes the .c extension as indicating a C program, and emacs adjusts its setting to aid your editing for that type of file.
      • The build tool make also recognizes the .c extension and uses this information to properly compile your program.
    2. To organize this program, begin by declaring pints and gallons as variables in addition to quarts and liters in 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, and gallons (possibly using another variable, such as total_quarts). From this total_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.) To convert quarts to liters, use the formula: 1 quart = 0.95 liters.

      You should refer to the annotations on quarts.c page for the code to print your output properly.

    Remember that you will need to save your file (e.g., using the C-x C-s command sequence in emacs or by using the icons or menus in the upper ribbon of the emacs program) and compile your program (using clang) before you can run it.

Writing More C

  1. Write a C program that uses a value for the radius of a circle and computes the circle's area and circumference.

Extra Time?

  1. Experiment more with the emacs editor, following the reading on the Emacs text editor. Find and try out at least one command from the GNU Emacs Quick Refence card that was not discussed in the reading.