CS Behind the Curtain (CS195 2003S)

Laboratory: An Introduction to C

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.



Exercise 0: Preparation

Create a new directory for this laboratory. Initially, this directory should be empty.

Exercise 1: Your First C Program

Here is a simple C program, taken from K&R.

#include <stdio.h>

  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.

Exercise 2: (Sp)Lint

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.

Exercise 3: Naming Your Executable

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?

Exercise 4: Headers

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?

Exercise 5: Newlines

On pages 7 and 8, Kernighan and Ritchie suggest many alternatives to the printf statement. Try the various alternatives to see what happens.

Exercise 6: Make

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.


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?

Exercise 7: Temperature Conversion

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. */
  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.

Exercise 8: Formatting Output

Kernighan and Ritchie suggest that you change the output line to read

printf("%3d%6d\n", fahr,celsius);

What effect does this change have?

Exercise 9: Floats

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.


Notes on Exercise 1

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.



Wednesday, 29 January 2003 [Samuel A. Rebelsky]


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 ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu