Book Cover

Introduction to Scientific Programming
Computational Problem Solving Using:
Maple and C
Mathematica and C

Author:
Joseph L. Zachary
Online Resources:
Maple/C Version
Mathematica/C Version

Writing, Compiling, and Running C Programs Tutorial

In this laboratory you will learn the mechanics of writing, compiling, and running C programs. Since you don't know very much about C at the moment, we will work at a high level of detail. You will be using some example programs in this laboratory. You can use your Web browser to view or download them as your prefer.

When you are doing C programming, you will be using a Unix Shell window and an Emacs window. You should already have a Unix Shell window running, and you should start Emacs now. To do that, move the mouse cursor outside of any window, depress the middle mouse button, and select the emacs option.

A Bisection Program

Take a look at bisect1.c, which implements the bisection method for root finding. You should use your Web browser to copy it to your home directory, just as you have copied Maple worksheets in earlier lessons. Once you have copied the program, read it into Emacs. To do that,

The program is very similar to the Maple bisection procedure from Chapter 9 that we studied in class.

Take some time to study the program. The material bracketed between /* and */ pairs are called comments. Comments are inserted into programs for the benefit of human readers, just as the case in Maple worksheets.

Compiling and Running

Let's try compiling and running the program. The compiler is just an application provided via Unix, so you might think that we would compile the program in the UNIX Shell window. However, for several reasons it is convenient to run the compiler from within Emacs, so we'll compile programs that way.

To compile, pull down the Emacs Compile menu and select the Compile This File... option. When you do this, Emacs will display the command required to compile the program at the bottom of the window. It will be something like

gcc -Wall -g bisect1.c -o bisection -lm

Press the Return key. What happens?

Click here for the answer

Assuming that the program compiled with no error messages (as it should have) we can now run it. To do this, go to the Unix Shell window, ane make sure that you are connected to your home directory. If you are not, or if you aren't sure, you will need to do

cd

Next type the name of the compiled version of the file, which in this case is simply ``bisect1''. (The original program was ``bisect1.c''.) The program will ask you for a positive guess (2.5 will do) and a negative guess (1.5 will do). It will then compute and display the root.

Compilation Errors

As you know from your experience with Maple, it is all too easy to make syntactic mistakes when writing programs. When you make syntactic mistakes in a C program, the compiler will tell you about them in the Emacs compilation window.

A single mistake can cause the compiler to produce a great number of error messages. For example, deleting the closing brace of the implementation of cow results in

cd ~/examples
gcc -Wall -g bisect1.c -o bisect1 -lm
bisect1.c: In function `cow':
bisect1.c:29: parse error before `double'
bisect1.c:40: `avg' undeclared (first use this function)
bisect1.c:40: (Each undeclared identifier is reported only once
bisect1.c:40: for each function it appears in.)
bisect1.c:40: `pos' undeclared (first use this function)
bisect1.c:49: `neg' undeclared (first use this function)
bisect1.c:51: warning: implicit declaration of function `f'
bisect1.c: In function `main':
bisect1.c:80: warning: implicit declaration of function `bisection'

seven error messages. Because of this, it is a good policy to fix the first one and recompile before trying to fix any more.

Notice that each error message includes the name of the file and the number of the line where the error was detected. The place where the error is detected is not necessarily the placed where the error actually is. In the example above, the parse error detected on line 29 actually occurred on line 20, which is where we deleted the closing brace.

Emacs makes it easy for you to locate the lines where it has detected errors. If you pull down the Compile menu and select the Next Compilation Error option, Emacs will put the cursor at the beginning of line 29, which is where it detected the first error. Further uses of this option will take you to the subsequent errors.

Put the brace that you deleted back in and recompile to make sure that everything is back as it should be.

Let's experiment with introducing some deliberate errors into ``bisect1.c'' so that you can get used to the kinds of error messages that the compiler produces. Here are some things to try out; you can probably think of others.

Be sure to correct one error before you introduce a new one! Notice that you must save the buffer before you compile it. (It is not the contents of the buffer that is compiled, but the contents of the file that is displayed in the buffer. If you haven't saved the buffer, the two can be different.)

Changes to the Program

Even though you don't know very much about C at this point, it should still be relatively easy for you to make certain changes to our program. Try making the following changes. Compile and run your changed program to make sure that it still works.

Programs from Scratch

A good policy for writing a C program is to begin by making a copy of the program that you have previously written that is most similar to the one that you want to write, and then modifying the copy. Sometimes, however, you will want to write a program from scratch.

Suppose, for example, that you'd like to put a simple program into a file called hi.c. To do this, pretend that the file already exists and use Emacs to read it in:

Since the file doesn't exist, of course, the buffer will be empty. Beginning like this ensures that the menu of C compilation commands will be made available by Emacs.

Follow the directions above and then type (or cut and paste) this program into the empty buffer. The resulting program should be called hi.c and should be in your home directory.

#include <stdio.h>

/* This extremely simple program displays "Hello world". */

void main (void)
{
  printf("Hello world\n");
}

Then compile and run it as you learned above.