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

Laboratory Exercise on Elementary C Programming

Goals

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

Getting Started

Reading for this lab includes Walker, An Introduction to C Through Annotated Examples, Program 1 (quarts-1.c), pages 1-3. This link includes both commentary and a link to the directory of all of these C programs available on the World Wide Web. You will want to use this list of programs for this and subsequent labs.

  1. Introduction to the Emacs Editor

    Open a terminal window and move to your C subdirectory for this lab. Then use the emacs editor to create the quarts-1.c program discussed in the reading.

    1. Start emacs with the command:

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

  2. Compiling and Running

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

       gcc -o quarts-1  quarts-1.c
       ./quarts-1
    

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

  3. Experimenting with Compiling

    Make a few typographical errors in quarts-1.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-1?

  4. Writing Your Own Program

    Write a C program that uses values for pints, quarts, and gallons and determines the corresponding number of liters.

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

  5. Modifying emacs

    Refine your emacs environment as suggested in the reading for today's lab. That is, set the following options in the "Options" menu.

    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!

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

  7. For those with extra time: Experimenting with emacs

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

Reminder: Complete Evaluation Form

When you have finished this lab, be sure to fill out its evaluation form in the "Lab Evaluation" section for CSC 161 on Pioneer Web.


This document is available on the World Wide Web as

     http://www.cs.grinnell.edu/~walker/courses/161.sp10/lab-intro-c.html

created 7 April 2008 by Henry M. Walker
last full revision 18 July 2011 by Dilan Ustek and April O'Neill
minor editing 24 August 2011 by Henry M. Walker
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.