% -*- mode: LaTeX -*-
% Copyright 1995 Joseph L. Zachary.  All rights reserved.

\title{Writing, Compiling, and Running C Programs}

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.

\section{A Bisection Program}

\example{}{examples/bisect1.c}{bisect1.c}

Take a look at \hyperref{{\tt bisect1.c}}{}{}{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,

\begin{itemize}

\item pull down the Emacs {\tt File} menu,

\item select the {\tt Open File...} option,

\item type ``bisect1.c'' to the prompt at the bottom, and

\item press the Return (Enter) key.

\end{itemize}

\noindent
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
{\tt /*} and {\tt */} pairs are called {\em comments}.  Comments are
inserted into programs for the benefit of human readers, just as the
case in Maple worksheets.


\section{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 {\tt Compile} menu and select the {\tt
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

\begin{verbatim}
gcc -Wall -g bisect1.c -o bisection -lm
\end{verbatim}

Press the Return key.  What happens?

\begin{answer}{Click here for the answer}
The Emacs window splits into two pieces.  One piece still contains the
program, while the other piece is used to display the sequence of
commands required to compile the file.  It's not important at this
point to understand exactly what these commands do.  You need only
know that this is how to compile a file displayed in a buffer.  Emacs
will tell you when compilation is complete.
\end{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

\begin{verbatim}
cd
\end{verbatim}

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.

\section{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 {\tt cow} results in

\begin{verbatim}
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'
\end{verbatim}

\noindent
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 {\tt Compile} menu and select the {\tt
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.

\begin{itemize}

\item Delete a semicolon from the end of some statement.

\item Delete a brace

\item Remove one of the lines containing ``\#include''.

\item Delete a quote mark

\item Change the spelling of ``bisection''

\end{itemize}

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.)

\section{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.

\begin{itemize}

\item Change the prompts that are printed out when the program wants
the positive and negative guesses.

\item Change the {\tt main} function so that it does not call {\tt
bisection} unless the guesses are valid.  The following piece of code
does this.

\begin{verbatim}
  if ((cow(positive) > 0) && (cow(negative)) < 0) {
    root = bisection(cow, positive, negative);
    printf("The root is %g\n", root);
  }
  else {
    printf("One (or both) of the guesses was invalid\n");
  }
\end{verbatim}

\item Change the program so that the equation being solved is

\[ \sqrt{\tan(x)} = \cos(x) \]

Both {\tt tan} and {\tt sqrt} are defined in the math library that is
included at the beginning of the program.


\section{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 \texttt{hi.c}.  To do this, pretend that the file
already exists and use Emacs to read it in:

\begin{itemize}

\item pull down the Emacs {\tt File} menu,

\item select the {\tt Open File...} option,

\item type ``hi.c'' to the prompt at the bottom, and

\item press the Return (Enter) key.

\end{itemize}

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
\texttt{hi.c} and should be in your home directory.

\begin{verbatim}
#include <stdio.h>

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

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

Then compile and run it as you learned above.

\end{itemize}

