06-02-2013, 12:55 PM
The C Book
1The C Book.pdf (Size: 3.13 MB / Downloads: 21)
The form of a C program
If you're used to the block-structured form of, say, Pascal, then at the outer level the
layout of a C program may surprise you. If your experience lies in the FORTRAN
camp you will find it closer to what you already know, but the inner level will look
quite different. C has borrowed shamelessly from both kinds of language, and from
a lot of other places too. The input from so many varied sources has spawned a
language a bit like a cross-bred terrier: inelegant in places, but a tenacious brute
that the family is fond of. Biologists refer to this phenomenon as ‘hybrid vigour’.
They might also draw your attention to the ‘chimera’, an artificial crossbreed of
creatures such as a sheep and a goat. If it gives wool and milk, fine, but it might
equally well just bleat and stink!
At the coarsest level, an obvious feature is the multi-file structure of a program. The
language permits separate compilation, where the parts of a complete program can
be kept in one or more source files and compiled independently of each other. The
idea is that the compilation process will produce files which can then be linked
together using whatever link editor or loader that your system provides. The block
structure of the Algol-like languages makes this harder by insisting that the whole
program comes in one chunk, although there are usually ways of getting around it.
Functions
A C program is built up from a collection of items such as functions and what we
could loosely call global variables. All of these things are given names at the point
where they are defined in the program; the way that the names are used to access
those items from a given place in the program is governed by rules. The rules are
described in the Standard using the term linkage. For the moment we only need to
concern ourselves with external linkage and no linkage. Items with external linkage
are those that are accessible throughout the program (library functions are a good
example); items with no linkage are also widely used but their accessibility is much
more restricted. Variables used inside functions are usually ‘local’ to the function;
they have no linkage. Although this book avoids the use of complicated terms like
those where it can, sometimes there isn't a plainer way of saying things. Linkage is
a term that you are going to become familiar with later. The only external linkage
that we will see for a while will be when we are using functions.
What was in it
Even such a small example has introduced a lot of C. Among other things, it
contained two functions, a #include ‘statement’, and some comment. Since
comment is the easiest bit to handle, let's look at that first.
Layout and comment
The layout of a C program is not very important to the compiler, although for
readability it is important to use this freedom to carry extra information for the
human reader. C allows you to put space, tab or newline characters practically
anywhere in the program without any special effect on the meaning of the program.
All of those three characters are the same as far as the compiler is concerned and
are called collectively white space, because they just move the printing position
without causing any ‘visible’ printing on an output device. White space can occur
practically anywhere in a program except in the middle of identifiers, strings, or
character constants. An identifier is simply the name of a function or some other
object; strings and character constants will be discussed later—don't worry about
them for the moment