22-01-2013, 12:19 PM
The C programming Language
1The C programming.pdf (Size: 547.76 KB / Downloads: 21)
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).
Functions may return values of basic types, structures, unions, or pointers. Any function may be called
recursively. Local variables are typically ``automatic'', or created anew with each invocation. Function
definitions may not be nested but variables may be declared in a block−structured fashion. The functions of a
C program may exist in separate source files that are compiled separately. Variables may be internal to a
function, external but known only within a single source file, or visible to the entire program.
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.
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.
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.)