|CSC 161||Grinnell College||Spring, 2010|
|Imperative Problem Solving and Data Structures|
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.
Please read the following materials carefully:
Walker, An Introduction to C Through Annotated Examples, Program 1 (quarts-1.c), pages 1-3.
King, Chapters 1-2, pages 1-32.
Marge Coahran, the Emacs Text Editor.
The initial move from Scheme to C requires at least three major adjustments:
The language syntax and semantics of C are dramatically different;
Most C programs explicitly draw upon an extensive standard library for input, output, and other tasks; and
C programs must first be translated to a machine language before they can be run.
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.
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
writing programs that translate the other language to the computer's machine language (compilation)
writing programs that read programs written in the other language and then directly executing those programs (interpretation)
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:
You use an editor to write a program in C.
You compile your C program to the computer's machine language.
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.
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:
Click on the icon of a emu (the animal head) at the bottom of your screen. Then use the "File" menu at the top to "Open" a file. (Emacs does not distinguish between new and existing files; to start a new file, just type in the desired file name.)
Open a terminal window, move the desired directory, and type
emacs file.c &
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.
After you have written a C program using a text editor, execution of the program involves two steps:
To compile C programs, move to a terminal window and write
gcc -o file file.c
That is, given a C program in file file.c, the gcc compiler produces a machine-language version in a program called file, where file.c is the file name for your C program.
To run the program, simply type
in the terminal window.
This document is available on the World Wide Web as
created 2 April 2008
last revised 9 January 2010
|For more information, please contact Henry M. Walker at firstname.lastname@example.org.|