                        C Tutorial:-Part 2
                 (c)Copyright 1996 Richard Harvey


    In part 1 of this tutorial I told you the key features of the C
Programming Language and promised to cover  them in more detail and 
what they mean.


* Operating System Portability

    Although C was developed on  the  UNIX operating system and was
originally used as its system  programming language, it soon spread 
onto other platforms. This was  due  to  two reasons. Firstly, many 
colleges and universities were  using  UNIX  so many people leaving 
college or graduating from  university  were  already familiar with 
both UNIX and C. Secondly,  as  mentioned  in the first chapter, it 
was developed by a programmer for use by programmers so many of the 
features found in C were  very  attractive  to programmers who were 
using different languages. Because of these two reasons C compilers 
started to appear on different computer systems.

    What this means to the programmer is that programs developed on
one computer can be  easily  ported  across  to another system with 
little or no modifications at all. The only problems what can arise 
are that companies sometimes  include  machine specific 'libraries' 
to enable the  programmer  to  make  use  of  things  such  as high 
resolution graphics, operating system calls or to allow the program 
to run under specific operating  systems such as Windows. Obviously 
programs written  using  such  libraries  cannot  easily  be ported 
across  to  another  computer  platform.  However  there  are  many 
programs that do  not  use  such  libraries  and  will  run  on any 
computer once it has been compiled for that computer.


* Highly  Efficient  Object  Code  /  Ease  of  Access  To Hardware 
Features

    These two  features  have  been  combined  because  the  way  C
accesses hardware that is  at  a  level  between Assembler and high 
level languages and is  one  of  the  reasons  that the object code 
produced is so efficient.

    C, in common with  most  high  level  languages,  is a compiled
language. What this means is that  the program code produced by the 
programmer, known as source code, is  stored  as a plain ASCII text 
file, the compiler  program  then  has  the  job  of converting, or 
compiling, this code into object,  or  machine specific, code which 
the computer can understand. As  practically  all computers use the 
ASCII system of representing  text,  it  easy  to transfer a source 
code program written on  something  like  an  IBM-PC  machine to an 
Atari TOS based machine such as an ST or Falcon.

    The computer cannot understand  the  source  code  but once the
compiler has converted into object code the computer is able to run 
it. If you were to look at  the  object it would not make any sense 
to you, at its most basic it is  just a series of 1's and 0's which 
the computer can understand.

    C combines both the control  and  data structures used in high-
level languages with the ability  to  address the machines hardware 
at a level more  akin  to  Assembler.  This  means  the object code 
produced is very efficient.


* Support Of Structured Programming

    This is a bit misleading  as  the  way  a program is structured
depends upon the person doing the programming. As C is a procedural 
language it  provides  inbuilt  modularity.  However  I  have  seen 
examples of  C  code  which,  although  they  work,  are  not  very 
structured. Once  again  it  is  mainly  up  to  the  programmer to 
structure his programs.

    Over the years certain  conventions  have  become  adopted in C
programming which the majority of programmers follow, these further 
enhance the structure within  C  programs.  If  you are someone who 
programs in another language, such  as  Basic, after learning how C 
programs are put together, you can probably apply this knowledge in 
making your programs more  structured.  Highly structured programs, 
in any language, are far more  easier  to understand and follow and 
therefore more easier to debug.


*Separate Compilation Of Modules

    All this really means is that  you  can build up C programs bit
by bit. As you  will  see  C  programs  can  become quite large and 
unwieldy, it is therefore far  more  easier and convenient to break 
down the program into sections. This sections can be programmed and 
compiled separately  from  the  main  program  and  then  a  linked 
together to produce the finished program.

    There are a number of  advantages  to  this, the most important
being that it makes debugging a  lot  easier.  If you wrote a large 
program all in one piece of  code  and  found  bugs when you ran it 
could take ages to find the bugs  and correct them. However, if you 
had smaller sections of the program you could compile and test them 
to ensure they were  working  correctly.  If  you  did find bugs at 
runtime it would be far more easier  to find and correct these bugs 
before you joined all the sections together.

    Another important aspect of separate compilation is that if you
wrote a piece of code which  you  find  you  use quite often, it is 
easier to then incorporate  into  other  programs.  You may write a 
piece of code which gets the current date from the system, provided 
your computer is equipped with  a  real  time clock, you could then 
incorporate this into any programs which need to access the current 
date. In certain case you may wish  to compile these pieces of code 
into libraries to keep the size of  the code down. ( Don't worry we 
will be covering libraries at a later date).


* Standard Library Functions

    These will be covered in more  detail  later on in this course.
The C programming  language  has  only  32  keywords  which  are as 
follows:-

        auto        double      int         struct
        break       else        long        switch
        case        num         register    typedef
        char        extern      return      union
        const       float       short       unsigned
        continue    for         signed      void
        default     goto        sizeof      volatile
        do          if          static      while

    Obviously you would not be able to do much in C with just these
few commands. However library functions  provide a way of expanding 
the C language by providing  library  functions. These are commands 
which are stored in libraries,  many  of  which are loaded when the 
compiler is compiling and linking the finished program. Programmers 
can create their own libraries  but  the  compiler  will need to be 
told to load these libraries for use in compilation.

    In C programs you will comes  across lines which look something
like this:-

    #include<stdio.h>

    This loads an ASCII text file called stdio.h and puts it at the
beginning of the C source code. The  .h means it is an header file, 
which simply means  it  goes  at  the  head  of  the program. These 
headers are used in  various  ways  but  the  main use is providing 
prototypes for library functions  and  defining  other commands for 
use in programming. The header  file  stdio.h  is probably the most 
common  and  provides  prototypes   and  definitions  for  standard 
Input/Output. There are many  others  which  we  will encounter and 
explain as we go along.


    Now that we've covered the main  features  of the C language we
will look at the example program I gave you in part 1.

    If you typed it in and compiled  it OK, when you ran you should
have seen the following on screen

HELLO WORLD!!!!

Press any key to continue

    Upon pressing any key on  the  keyboard  should have caused the
program to abort. I will now go through the program and explain how 
it works.

    The first line is just a comment line. In C anything between /*
and */ will be ignored on compilation. These are just comment lines 
and are useful in explaining what  various parts of a program does, 
this greatly aids debugging. Comments can be any length and can run 
over as many  lines  as  you  like.  However  be  careful,  in your 
comments if you use */ this  will  end the comment and the compiler 
assumed what follows is a  statement,  also  if  it runs over lines 
ensure that actual statements are not included in the comment.

    The next thing we come across is the line:-

    #include<stdio.h>

    As stated earlier this is an  header file which will be covered
in more detail later on in  this  series.  For  now all you need to 
know is that it provides prototypes for the commands 'printf()' and 
'getchar()' which are used in the program.

    The next thing we  come  across  is  vitally  important. At the
heart  of  C  programming  are   functions.  These  are  groups  of 
statements  which  carry  out  certain  tasks  as  defined  by  the 
programmer. Every C program  must  have  a function called 'main()' 
without which the program would not work as this is the entry point 
for the  program.  All  functions  have  a  name  and  a  group  of 
statements which are surrounded by the braces { and }.

    The line:-

    void main(void)

    means that the function will not return any values and does not
accept any values. This may sound confusing but in Part 4 I will be 
explaining functions in much  more  detail  and  this should become 
clear.

    The lines:-

    char ch;
    and
    ch = getchar();

    will be explained in more details in  part 2 but for now I want
to take a close look at the printf() statement.

    The printf()  statement  is  one  of  the  most  commonly  used
statements in C  programming.  Anything  between  the double quotes 
(i.e. ") will be printed to the screen but with some exceptions. In 
the program you will have noticed that the characters '\n\n' in the 
first printf statement were not printed.

    In the printf statement  the  back  slash character, '\', means
what follows is an escape sequence  and  is used for the formatting 
of the output produced by  the  printf statement. The following are 
all the valid escape sequences and what they do:-

    \\      prints a backslash
    \'      prints a single quote
    \"      prints a double quote
    \?      prints a question mark
    \a      sounds a bell
    \b      backspace
    \f      formfeed
    \n      newline
    \0      null character
    \r      carriage return
    \t      horizontal tab
    \v      vertical tab
    \xhhh   insert ASCII code hhh

    Using the above table see if you can figure out why the program
printed the output the way it did. Next time I will explain why and 
also look at the printf statement in a little more detail.
