CSC 161 Grinnell College Spring, 2009
 
Imperative Problem Solving and Data Structures
 

Getting Started with C

Many commentaries and textbooks introduce the C programming language. Rather than duplicate those materials in laboratory exercises, specific readings are identified to guide the learning process.

Readings

Please read the following materials carefully:

Additional Notes

Moving from Scheme to C

The initial move from Scheme to C requires at least three major adjustments:

This lab focuses on writing and running a few simple C programs. The programs themselves will provide some introduction to C's syntax, semantics, and I/O library functions. However, when getting started, we must pay special attention to mechanics. After you gain some experience, these details will become familiar, and we can focus again on ideas, algorithms, and problem solving.

Compilation and Interpretation of Programs

As you may know, computers work with a number of different "languages". Each computer really understands only one language: its underlying machine language. We make computers understand another language by either

Already in this course, you have run Scheme programs, and these run using the second approach -- with a Scheme interpreter.

C, however, uses the first approach:

  1. You use an editor to write a program in C.

  2. You compile your C program to the computer's machine language.

  3. You run the machine language program.

The first of these steps requires use of a text editor, and you are strongly advised to use emacs or vim. (More about this shortly.) The second and third steps are performed by issuing commands within a terminal window.

Editing

When you used Dr. Scheme, the program editor (the Definitions Window) was integrated with the environment for running the program (the Interaction Window). Dr. Scheme also was helpful in formatting programs; when you entered code, Dr. Scheme indented code for readability, helped you check that parentheses matched, and otherwise aided program development.

When using C, the editing and running steps typically are separate, but we still want to utilize an editing environment that can help with formatting, checking parentheses, etc. Thus, we strongly advise that you use emacs or vim when writing C programs. Each of these editors requires some getting use to, but each has significant advantages over the long term. This lab focuses on emacs. A lab in the near future will introduce vim, and then you will have the choice of what editor you want to use.

You can open the emacs editor and begin editing in either of two ways:

Since we will be using the editor for writing C programs, set the following options in the "Options" menu.

When done, click the "Save Options" choice in the "Options" menu.

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

For more information on the emacs editor, see the Emacs Text Editor lab by Marge Coahran.

Compiling and Running C programs

After you have written a C program using a text editor, execution of the program involves two steps:


This document is available on the World Wide Web as

http://www.walker.cs.grinnell.edu/courses/161.sp09/readings/reading-intro-c.shtml

created 2 April 2008
last revised 18 January 2009
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.