Let's take a look at a simple C program. Use Emacs to look at the
file ``sample1.c'', which is in your ``examples'' directory, or
view it directly.
This program is virtually identical to the one discussed in section
1.1 of the textbook, so I won't belabor the obvious. However, there
are a few points that I want to emphasize.
- A C program consists of one or more functions organized into one or
more files. For the time being we'll deal with programs, such as this
one, that are completely contained within one file.
- In every program, exactly one of the functions must be called ``main''.
(In this program there is only one function so, naturally, it is
called ``main''.) The main function is the one at which execution
begins when a program is compiled and run. Except for this, there is
no difference between the main function and any other function that
you might write. How does this differ from some other language you
might know?
Click here for an answer
- Every function and variable must be declared before it is used. This
program uses no variables, but it does use the function ``printf''
(which writes to the display) with no apparent declaration. What's
going on?
Click here for the answer
- If a program uses a function, it must also be implemented somewhere. Yet
no implementation of ``printf'' is visible. The implementation is not
to be found in ``stdio.h'' either. What's going on?
Click here for the answer
- In languages such as Pascal and Fortran, there is a distinction between
functions (which return results) and procedures or subroutines (which do
not). There are two functions in the sample program (``main'' and
``printf''). Neither one appears to return a value. What's going on?
Click here for the answer
Hamlet Project
hamlet@cs.utah.edu