11-10-2012, 01:48 PM
C Language Tutorial
c tutorial.pdf (Size: 311.7 KB / Downloads: 18)
Introduction
TheC programming language was originally developed by Dennis Ritchie of Bell Laboratories,
and was designed to run on a PDP-11 with a UNIX operating system. Although it was originally
intended to run under UNIX, there was a great interest in running it on the IBM PC and compatibles,
and other systems. C is excellent for actually writing system level programs, and the
entire Applix 1616/OS operating system is written in C (except for a few assembler routines).
It is an excellent language for this environment because of the simplicity of expression, the
compactness of the code, and the wide range of applicability.
It is not a good "beginning" language because it is somewhat cryptic in nature. It allows the
programmer a wide range of operations from high level down to a very low level approaching
the level of assembly language. There seems to be no limit to the flexibility available. One
experienced C programmer made the statement, "You can program anything in C", and the
statement is well supported by my own experience with the language. Along with the resulting
freedom however, you take on a great deal of responsibility. It is very easy to write a program
that destroys itself due to the silly little errors that, say, a Pascal compiler will flag and call a
fatal error. In C, you are very much on your own, as you will soon find.
Since C is not a beginners language, I will assume you are not a beginning programmer, and I
will not attempt to bore you by defining a constant and a variable. You will be expected to
know these basic concepts. You will, however, not be expected to know anything of the C
programming language. I will begin with the highest level of C programming, including the
usually intimidating concepts of pointers, structures, and dynamic allocation. To fully understand
these concepts, it will take a good bit of time and work on your part, because they not
particularly easy to grasp, but they are very powerful tools. Enough said about that, you will
see their power when we get there, just don’t allow yourself to worry about them yet.
Programming in C is a tremendous asset in those areas where you may want to use Assembly
Language, but would rather keep it a simple to write and easy to maintain program. It has been
said that a program written in C will pay a premium of a 50 to 100% increase in runtime, because
no language is as compact or fast as Assembly Language. However, the time saved in coding
can be tremendous, making it the most desirable language for many programming chores. In
addition, since most programs spend 90 percent of their operating time in only 10 percent or
less of the code, it is possible to write a program in C, then rewrite a small portion of the code
in Assembly Language and approach the execution speed of the same program if it were written
entirely in Assembly Language.
Getting Started
This tutorial can be read simply as a text, however it is intended to be interactive. That is, you
should be compiling, modifying and using the programs that are presented herein.
All the programs have been tested using the HiTech C compiler, and we assume that you have
a copy of this. In addition, you should have a copy of various updates and header files for the
C compiler, which appear on Applix User disks.
You can use either the builtin Applix 1616/OS editor edit, or the $30 Dr Doc editor in
non-document mode. Dr Doc is somewhat more powerful, however as it loads from disk, it is
slightly slower to get started. The source code has been edited to suit a tab setting of 5, so invoke
your editor with tabs set to a spacing of 5. For example, edit sourcecode.c 5 would let
you edit a file called sourcecode.c.
Before you can really use C, there are certain equipment requirements that must be met. You
must have a disk co-processor card, and at least one disk drive. If your drives are smaller than
800k, you will probably require two disk drives. We assume you either have 1616/OS Version
4 multitasking, or else have an assign MRD available on your boot disk.
You should make use of the xpath, and the assign commands to set up your boot disk in a
form suitable for use with C. This should be done in the autoexec.shell file on your boot
disk, as set out below.
What About The Underline?
Even though the underline can be used as part of a variable name, it seems to be used very little
by experienced C programmers. It adds greatly to the readability of a program to use descriptive
names for variables and it would be to your advantage to do so. Pascal programmers tend to
use long descriptive names, but most C programmers tend to use short cryptic names. Most of
the example programs in this tutorial use very short names for this reason.
How This Tutorial Is Written
Any computer program has two entities to consider, the data, and the program. They are highly
dependent on one another and careful planning of both will lead to a well planned and well
written program. Unfortunately, it is not possible to study either completely without a good
working knowledge of the other. For this reason, this tutorial will jump back and forth between
teaching methods of program writing and methods of data definition. Simply follow along and
you will have a good understanding of both. Keep in mind that, even though it seems expedient
to sometimes jump right into the program coding, time spent planning the data structures will
be well spent and the final program will reflect the original planning.
How Do We Print Numbers
To keep our promises, let’s return to the "printf" statements for a definition of how they work.
Notice that they are all identical and that they all begin just like the "printf" statements we have
seen before. The first difference occurs when we come to the % character. This is a special
character that signals the output routine to stop copying characters to the output and do something
different, namely output a variable. The % sign is used to signal the start of many different
types of variables, but we will restrict ourselves to only one for this example. The character
following the %sign is a "d", which signals the output routine to get a decimal value and output
it. Where the decimal value comes from will be covered shortly. After the "d", we find the
familiar \n, which is a signal to return the video "carriage", and the closing quotation mark.
All of the characters between the quotation marks define the pattern of data to be output by this
statement, and after the pattern, there is a comma followed by the variable name "index". This
is where the "printf" statement gets the decimal value which it will output because of the "%d"
we saw earlier. We could add more "%d" output field descriptors within the brackets and more
variables following the description to cause more data to be printed with one statement. Keep
in mind however, that it is important that the number of field descriptors and the number of
variable definitions must be the same or the runtime system will get confused and probably quit
with a runtime error.
Another Poor Programming Example
Recalling uglyform.c from the last chapter, you saw a very poorly formatted program. If
you load and display dumbconv.c you will have an example of poor formatting which is much
closer to what you will actually find in practice. This is the same program as tempconv.c
with the comments removed and the variable names changed to remove the descriptive aspect
of the names. Although this program does exactly the same as the last one, it is much more
difficult to read and understand. You should begin to develop good programming practices
now.