CS Behind the Curtain (CS195 2003S)
Primary:
[Front Door]
[Current]
[Glance]
-
[Blurb]
[Disabilities]
[Honesty]
[Instructions]
[Links]
[Search]
[Syllabus]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
ECA:
[About]
[Grades]
[Quizzes]
[Submit Work]
[Change Password]
[Reset Password]
Misc:
[Walker/Fall 2001]
[SamR]
Summary: This lab reviews some mechanics related to the use of C in the MathLAN.
Citation: Much of this lab is closely based on chapter 1 of
Kernighan, Brian W. and Ritchie, Dennis M. (1988). The C Programming Language, Second Edition: ANSI C. Englewood Cliffs, NJ: Prentice-Hall. ISBN: 0-13-1110362-8.
Contents:
Create a new directory for this laboratory. Initially, this directory should be empty.
Here is a simple C program, taken from K&R.
#include <stdio.h> main() { printf("Hello, world!\n"); } /* main */
a. Save this code in a file named hello.c
b. Compile that file with
% gcc hello.c
c. Determine what files, if any, have been created in your lab directory.
d. If all went well, you should see an executable file. Execute (run) it by typing its name in a shell window.
a. The program above has a subtle error. Can you tell what it is?
b. Run splint
, the static C program checker, on
hello.c
, using
% splint hello.c
Note what errors it reports. Talk to me if you don't understand the error report.
c. Repair any errors reported by splint
.
a. Replace the world
in your program by whirled
. Do not
recompile.
b. Execute the command
% cc -o hello hello.c
c. What files, if any, have been added to your lab directory?
d. What happens if you execute those new files?
e. What happens if you execute a.out
?
f. What do your observations suggest about the purpose of the command in step b?
a. Eliminate the line in hello.c
that includes the
stdio.h
header.
b. What effect do you expect this change to have?
c. Try recompiling your program to verify your hypothesis.
d. What errors do you think splint
will report?
e. Try recompiling your program to verify your hypothesis.
f. What do your observations suggest?
On pages 7 and 8, Kernighan and Ritchie suggest many alternatives to
the printf
statement. Try the various alternatives to
see what happens.
a. Remove the file hello
.
b. Type make hello
. What happens?
c. Type make hello
again. What happens?
d. Change the text printed in hello.c
. Type
make hello
again. What happens?
e. Type make goodbye
. What happens?
f. Using the editor of your choice, create a file named Makefile
with the following lines
goodbye: hello.c $(CC) hello.c -o goodbye
g. Type make goodbye
. What happens?
h. Add the following line to the start of Makefile
.
CC=gcc
Make a change to hello.c
and type make hello
.
What, if anything, is different?
i. What do your observations suggest about make
and
your Makefile
?
Here's a slight variant of the program from section 1.2 of K&R.
#include <stdio.h> /* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300. */ main() { int fahr, celsius; int lower, upper, step; lower = 0; /* lower limit of temperature table */ upper = 300; /* upper limit of temperature table */ step = 20; /* step size */ fahr = lower; while (fahr != upper) { celsius = 5 * (fahr-32) / 9; printf("%d\t%d\n", fahr, celsius); fahr = fahr + step; } /* while (fahr != upper) return 0; } /* main */
a. Save this program in a file (say f2c.c
).
b. What syntax errors, if any, do you observe in this program?
c. Run splint
and fix any errors it reports. (Yes, that
should become a habit.)
d. Compile the program, run the program, and observe its output.
e. What do you expect to happen if you change the step to 9?
f. Rerun splint
, recompile the program, and run the program
to test your hypothesis.
g. Fix any errors revealed in your previous steps.
Kernighan and Ritchie suggest that you change the output line to read
printf("%3d%6d\n", fahr,celsius);
What effect does this change have?
a. Convert the declarations of fahr
and
celsius
to be float
rather than
int
. Do not make other changes.
b. What effect do you expect this change to make on your program?
c. Run splint
. Don't correct the errors it reports.
d. Compile and run your program to see the effect of these changes. What do you observe about the output.
e. Correct the errors splint
reported, recompile, and rerun
your program to see the effects.
f. Make any other changes you deem appropriate.
By default, the C compiler produces the file a.out
.
As you'll see in a subsequent exercise, there are also ways to name your
output file.
Primary:
[Front Door]
[Current]
[Glance]
-
[Blurb]
[Disabilities]
[Honesty]
[Instructions]
[Links]
[Search]
[Syllabus]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
ECA:
[About]
[Grades]
[Quizzes]
[Submit Work]
[Change Password]
[Reset Password]
Misc:
[Walker/Fall 2001]
[SamR]
Disclaimer:
I usually create these pages on the fly
, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Fri May 2 14:19:50 2003.
The source to the document was last modified on Wed Jan 29 14:04:06 2003.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS195/2003S/Labs/intro-c.html
.
You may wish to
validate this document's HTML
;
;
Check with Bobby