About C
As a programming language, C is rather like Pascal or Fortran. Values are stored
in variables. Programs are structured by defining and calling functions. Program
flow is controlled using loops, if statements and function calls. Input and
output can be directed to the terminal or to files. Related data can be stored
together in arrays or structures.
Of the three languages, C allows the most precise control of input and output. C
is also rather more terse than Fortran or Pascal. This can result in short
efficient programs, where the programmer has made wise use of C's range of
powerful operators. It also allows the programmer to produce programs which are
impossible to understand.
Programmers who are familiar with the use of pointers (or indirect addressing,
to use the correct term) will welcome the ease of use compared with some other
languages. Undisciplined use of pointers can lead to errors which are very hard
to trace. This course only deals with the simplest applications of pointers.
It is hoped that newcomers will find C a useful and friendly language. Care must
be taken in using C. Many of the extra facilities which it offers can lead to
extra types of programming error. You will have to learn to deal with these to
successfully make the transition to being a C programmer.
C and UNIX
This course teaches C under the UNIX operating system. C programs will look
similar under any other system (such as VMS or DOS), some other features will
differ from system to system. In particular the method of compiling a program to
produce a file of runnable code will be different on each system.
The UNIX system is itself written in C. In fact C was invented specifically to
implement UNIX. All of the UNIX commands which you type, plus the other system
facilities such as password checking, lineprinter queues or magnetic tape
controllers are written in C.
In the course of the development of UNIX, hundreds of functions were written to
give access to various facets of the system. These functions are available to
the programmer in libraries. By writing in C and using the UNIX system
libraries, very powerful system programs can be created. These libraries are
less easy to access using other programming languages. C is therefore the
natural language for writing UNIX system programs.
Common C
Until recently there was one dominant form of the C language. This was the
native UNIX form, which for historical reasons is known as either Bell Labs C,
after the most popular compiler, or K. &R. C, after the authors of the most
popular textbook on the language. It is now often called "Classic C"
ANSI C
The American National Standards Institute defined a standard for C, eliminating
much uncertainty about the exact syntax of the language. This newcomer, called
ANSI C, proclaims itself the standard version of the language. As such it will
inevitably overtake, and eventually replace common C.
ANSI C does incorporate a few improvements over the old common C. The main
difference is in the grammar of the language. The form of function declarations
has been changed making them rather more like Pascal procedures.
This course introduces ANSI C since it is supported by the SUN workstation
compilers. Most C programming texts are now available in ANSI editions.
A Very Simple Program
This program which will print out the message This is a C program
#include <stdio.h>
main()
{
printf("This is a C program\n");
}
Though the program is very simple, a few points are worthy of note.
Every C program contains a function called main. This is the start point of the
program.
#include <stdio.h> allows the program to interact with the screen, keyboard and
filesystem of your computer. You will find it at the beginning of almost every C
program.
main() declares the start of the function, while the two curly brackets show the
start and finish of the function. Curly brackets in C are used to group
statements together as in a function, or in the body of a loop. Such a grouping
is known as a compound statement or a block.
printf("This is a C program\n");
prints the words on the screen. The text to be printed is enclosed in double
quotes. The \n at the end of the text tells the program to print a newline as
part of the output.
Most C programs are in lower case letters. You will usually find upper case
letters used in preprocessor definitions
The C Compiler (cc)
The simplest method is to type
cc testprog.c
This will try to compile testprog.c, and, if successful, will produce a runnable
file called a.out. If you want to give the runnable file a better name you can
type
cc testprog.c -o testprog
This will compile testprog.c, creating runnable file testprog.
Running the Program
To run a program under UNIX you simply type in the filename. So to run program
testprog, you would type
testprog
or if this fails to work, you could type
./testprog
You will see your prompt again after the program is done.