28-05-2014, 02:24 PM
The C programming Language
The C programming.pdf (Size: 896.64 KB / Downloads: 19)
Introduction
C is a general-purpose programming language. It has been closely associated with the UNIX operating
system where it was developed, since both the system and most of the programs that run on it are written
in C. The language, however, is not tied to any one operating system or machine; and although it has
been called a ``system programming language'' because it is useful for writing compilers and operating
systems, it has been used equally well to write major programs in many different domains.
Many of the important ideas of C stem from the language BCPL, developed by Martin Richards. The
influence of BCPL on C proceeded indirectly through the language B, which was written by Ken
Thompson in 1970 for the first UNIX system on the DEC PDP-7.
BCPL and B are ``typeless'' languages. By contrast, C provides a variety of data types. The fundamental
types are characters, and integers and floating point numbers of several sizes. In addition, there is a
hierarchy of derived data types created with pointers, arrays, structures and unions. Expressions are
formed from operators and operands; any expression, including an assignment or a function call, can be a
statement. Pointers provide for machine-independent address arithmetic.
C provides the fundamental control-flow constructions required for well-structured programs: statement
grouping, decision making (if-else), selecting one of a set of possible values (switch), looping with
the termination test at the top (while, for) or at the bottom (do), and early loop exit (break).
A Tutorial Introduction
Let us begin with a quick introduction in C. Our aim is to show the essential elements of the language in real
programs, but without getting bogged down in details, rules, and exceptions. At this point, we are not trying to be
complete or even precise (save that the examples are meant to be correct). We want to get you as quickly as
possible to the point where you can write useful programs, and to do that we have to concentrate on the basics:
variables and constants, arithmetic, control flow, functions, and the rudiments of input and output. We are
intentionally leaving out of this chapter features of C that are important for writing bigger programs. These include
pointers, structures, most of C's rich set of operators, several control-flow statements, and the standard library.
This approach and its drawbacks. Most notable is that the complete story on any particular feature is not found
here, and the tutorial, by being brief, may also be misleading. And because the examples do not use the full power
of C, they are not as concise and elegant as they might be. We have tried to minimize these effects, but be warned.
Another drawback is that later chapters will necessarily repeat some of this chapter. We hope that the repetition
will help you more than it annoys.
In any case, experienced programmers should be able to extrapolate from the material in this chapter to their own
programming needs. Beginners should supplement it by writing small, similar programs of their own. Both groups
can use it as a framework on which to hang the more detailed descriptions that begin in Chapter 2.
Character Input and Output
We are going to consider a family of related programs for processing character data. You will find that many
programs are just expanded versions of the prototypes that we discuss here.
The model of input and output supported by the standard library is very simple. Text input or output, regardless of
where it originates or where it goes to, is dealt with as streams of characters. A text stream is a sequence of
characters divided into lines; each line consists of zero or more characters followed by a newline character. It is the
responsibility of the library to make each input or output stream confirm this model; the C programmer using the
library need not worry about how lines are represented outside the program.
The standard library provides several functions for reading or writing one character at a time, of which getchar
and putchar are the simplest. Each time it is called, getchar reads the next input character from a text stream
and returns that as its value.
Arrays
Let is write a program to count the number of occurrences of each digit, of white space characters (blank, tab,
newline), and of all other characters. This is artificial, but it permits us to illustrate several aspects of C in one
program.
There are twelve categories of input, so it is convenient to use an array to hold the number of occurrences of each
digit, rather than ten individual variables.
Functions
In C, a function is equivalent to a subroutine or function in Fortran, or a procedure or function in Pascal. A
function provides a convenient way to encapsulate some computation, which can then be used without worrying
about its implementation. With properly designed functions, it is possible to ignore how a job is done; knowing
what is done is sufficient. C makes the sue of functions easy, convinient and efficient; you will often see a short
function defined and called only once, just because it clarifies some piece of code.
So far we have used only functions like printf, getchar and putchar that have been provided for us; now
it's time to write a few of our own. Since C has no exponentiation operator like the * of Fortran, let us illustrate
the mechanics of function definition by writing a function power(m,n) to raise an integer m to a positive integer
power n. That is, the value of power(2,5) is 32. This function is not a practical exponentiation routine, since it
handles only positive powers of small integers, but it's good enough for illustration.(The standard library contains a
function pow(x,y) that computes xy.)
Arguments - Call by Value
One aspect of C functions may be unfamiliar to programmers who are used to some other languages, particulary
Fortran. In C, all function arguments are passed ``by value.'' This means that the called function is given the values
of its arguments in temporary variables rather than the originals. This leads to some different properties than are
seen with ``call by reference'' languages like Fortran or with var parameters in Pascal, in which the called routine
has access to the original argument, not a local copy.