17-01-2013, 03:28 PM
The C Programming Language
The CProg.pdf (Size: 1.21 MB / Downloads: 22)
Preface to the first edition
C is a general-purpose programming language with features economy of expression, modern
flow control and data structures, and a rich set of operators. C is not a ``very high level''
language, nor a ``big'' one, and is not specialized to any particular area of application. But its
absence of restrictions and its generality make it more convenient and effective for many
tasks than supposedly more powerful languages.
C was originally designed for and implemented on the UNIX operating system on the DEC
PDP-11, by Dennis Ritchie. The operating system, the C compiler, and essentially all UNIX
applications programs (including all of the software used to prepare this book) are written in
C. Production compilers also exist for several other machines, including the IBM System/370,
the Honeywell 6000, and the Interdata 8/32. C is not tied to any particular hardware or
system, however, and it is easy to write programs that will run without change on any
machine that supports C.
This book is meant to help the reader learn how to program in C. It contains a tutorial
introduction to get new users started as soon as possible, separate chapters on each major
feature, and a reference manual. Most of the treatment is based on reading, writing and
revising examples, rather than on mere statements of rules. For the most part, the examples
are complete, real programs rather than isolated fragments. All examples have been tested
directly from the text, which is in machine-readable form. Besides showing how to make
effective use of the language, we have also tried where possible to illustrate useful algorithms
and principles of good style and sound design.
The book is not an introductory programming manual; it assumes some familiarity with basic
programming concepts like variables, assignment statements, loops, and functions.
Nonetheless, a novice programmer should be able to read along and pick up the language,
although access to more knowledgeable colleague will help.
In our experience, C has proven to be a pleasant, expressive and versatile language for a wide
variety of programs. It is easy to learn, and it wears well as on's experience with it grows. We
hope that this book will help you to use it well.
The thoughtful criticisms and suggestions of many friends and colleagues have added greatly
to this book and to our pleasure in writing it. In particular, Mike Bianchi, Jim Blue, Stu
Feldman, Doug McIlroy Bill Roome, Bob Rosin and Larry Rosler all read multiple volumes
with care. We are also indebted to Al Aho, Steve Bourne, Dan Dvorak, Chuck Haley, Debbie
Haley, Marion Harris, Rick Holt, Steve Johnson, John Mashey, Bob Mitze, Ralph Muha,
Peter Nelson, Elliot Pinson, Bill Plauger, Jerry Spivack, Ken Thompson, and Peter
Weinberger for helpful comments at various stages, and to Mile Lesk and Joe Ossanna for
invaluable assistance with typesetting.
Brian W. Kernighan
Dennis M. Ritchie
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 controlflow
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.
Getting Started
The only way to learn a new programming language is by writing programs in it. The first
program to write is the same for all languages:
Print the words
hello, world
This is a big hurdle; to leap over it you have to be able to create the program text somewhere,
compile it successfully, load it, run it, and find out where your output went. With these
mechanical details mastered, everything else is comparatively easy.
In C, the program to print ``hello, world'' is
#include <stdio.h>
main()
{
printf("hello, world\n");
}
Just how to run this program depends on the system you are using. As a specific example, on
the UNIX operating system you must create the program in a file whose name ends in ``.c'',
such as hello.c, then compile it with the command
cc hello.c
If you haven't botched anything, such as omitting a character or misspelling something, the
compilation will proceed silently, and make an executable file called a.out.
On other systems, the rules will be different; check with a local expert.
Now, for some explanations about the program itself. A C program, whatever its size, consists
of functions and variables. A function contains statements that specify the computing
operations to be done, and variables store values used during the computation. C functions are
like the subroutines and functions in Fortran or the procedures and functions of Pascal. Our
example is a function named main. Normally you are at liberty to give functions whatever
names you like, but ``main'' is special - your program begins executing at the beginning of
main. This means that every program must have a main somewhere.
main will usually call other functions to help perform its job, some that you wrote, and others
from libraries that are provided for you. The first line of the program,
#include <stdio.h>
tells the compiler to include information about the standard input/output library; the line
appears at the beginning of many C source files. The standard library is described in Chapter
7 and Appendix B.
One method of communicating data between functions is for the calling function to provide a
list of values, called arguments, to the function it calls. The parentheses after the function
name surround the argument list. In this example, main is defined to be a function that expects
no arguments, which is indicated by the empty list ( ).