CSC 161 Grinnell College Fall, 2013
 
Imperative Problem Solving and Data Structures
 
 

Laboratory Exercise on 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.

Introduction to the Emacs Editor

Open a terminal window and move to your C subdirectory for this lab.

  1. Start emacs with the command:

       emacs quarts.c &
    
  2. Use the links for the reading to open the program quarts.c in your browser. Then copy and paste it into the emacs window.

Compiling and Running

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

       gcc -o quarts  quarts.c
       ./quarts
    
  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 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 run quarts?

    • 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

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

    You should reference the annotations on quarts.c to properly print out your output.

  2. Although you can name this new program whatever you like, you should end the file name with .c for two 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.

Modifying emacs

  1. By default, the emacs editor is configured, so that you encounter a split screen each time you open the editor. If you find this feature annoying, you can change this default in either of two ways:
    • When you open emacs, look at the bottom of the "Welcome to GNU Emacs" window. Click the box that says "Never show it [this startup screen] again."
    • Within emacs,
      • click the "Options" tab at the top of the window, select "Customize Emacs" and then "Specific Option".
      • The phrase "Customize variable:" will appear in the little window at the bottom. Type inhibit-startup-screen
      • A new window will appear, which contains the line
        Inhbit Startup Screen: Toggle off (nil)
        Click the Toggle button, and then the Save for future sessions button above.
    With either choice, emacs should start with just one window (no split screen) in the future.
  2. Refine your emacs environment to help support C programming.

    • In the "Options" menu, set the following options.

      • Click the box for "Paren Match Highlighting (Show Paren mode)"
      • Click the box for "Case-Insensitive Search"
    • Enable "Syntax Highlighting (Global Font Lock mode)" as follows:

      • Press the escape key ("Esc") [this is considered a "meta" key; nothing will appear to happen until you finish the next step]
      • Type x (for execute) [M-x will appear in the small window at the bottom]
      • Type global-font-lock-mode (and return) in the bottom window
    • When done, click the "Save Options" choice in the "Options" menu.

    Reminder: Since emacs is a very powerful editor, sometimes you will hit an erroneous key, emacs will do something unexpected, and then you will wonder what is happening. In such cases, the keystroke combination <ctrl>-g will stop any editing process within emacs!

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.

For those with extra time: Experimenting with emacs

  1. Experiment with the emacs editor, following the lab on the Emacs Text Editor by Marge Coahran.

Feedback Welcome

Development of laboratory exercises is an iterative process. Prof. Walker welcomes your feedback! Feel free to talk to him during class or stop by his office.