?? intro.html
字號:
<html><head><title>Introduction</title></head><body><hr><p align="center"><a href="preface1.html">Back to the Preface to the First Edition</a> -- <a href="kandr.html">Index</a> -- <a href="chapter1.html">Chapter 1</a><p><hr><h1>Introduction</h1>C is a general-purpose programming language. It has been closely associatedwith the UNIX operating system where it was developed, since both the systemand 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 ithas been called a ``system programming language'' because it is useful forwriting compilers and operating systems, it has been used equally well towrite major programs in many different domains.<p>Many of the important ideas of C stem from the language BCPL, developed byMartin Richards. The influence of BCPL on C proceeded indirectly through thelanguage B, which was written by Ken Thompson in 1970 for the first UNIXsystem on the DEC PDP-7.<p>BCPL and B are ``typeless'' languages. By contrast, C provides a variety ofdata types. The fundamental types are characters, and integers and floatingpoint numbers of several sizes. In addition, there is a hierarchy of deriveddata types created with pointers, arrays, structures and unions. Expressionsare formed from operators and operands; any expression, including anassignment or a function call, can be a statement. Pointers provide formachine-independent address arithmetic.<p>C provides the fundamental control-flow constructions required forwell-structured programs: statement grouping, decision making(<tt>if-else</tt>), selecting one of a set of possible values (<tt>switch</tt>),looping with the termination test at the top (<tt>while, for</tt>) or at thebottom (<tt>do</tt>), and early loop exit (<tt>break</tt>).<p>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 definitionsmay not be nested but variables may be declared in a block-structuredfashion. The functions of a C program may exist in separate source files thatare compiled separately. Variables may be internal to a function, externalbut known only within a single source file, or visible to the entire program.<p>A preprocessing step performs macro substitution on program text, inclusionof other source files, and conditional compilation.<p>C is a relatively ``low-level'' language. This characterization is notpejorative; it simply means that C deals with the same sort of objects thatmost computers do, namely characters, numbers, and addresses. These may becombined and moved about with the arithmetic and logical operators implementedby real machines.<p>C provides no operations to deal directly with composite objects such ascharacter strings, sets, lists or arrays. There are no operations thatmanipulate an entire array or string, although structures may be copied as aunit. The language does not define any storage allocation facility other thanstatic definition and the stack discipline provided by the local variables offunctions; there is no heap or garbage collection. Finally, C itself providesno input/output facilities; there are no READ or WRITE statements, and nobuilt-in file access methods. All of these higher-level mechanisms must beprovided by explicitly called functions. Most C implementations have includeda reasonably standard collection of such functions.<p>Similarly, C offers only straightforward, single-thread control flow: tests,loops, grouping, and subprograms, but not multiprogramming, paralleloperations, synchronization, or coroutines.<p>Although the absence of some of these features may seem like a gravedeficiency, (``You mean I have to call a function to compare two characterstrings?''), keeping the language down to modest size has real benefits.Since C is relatively small, it can be described in small space, and learnedquickly. A programmer can reasonably expect to know and understand and indeedregularly use the entire language.<p>For many years, the definition of C was the reference manual in the firstedition of <em>The C Programming Language</em>. In 1983, the American NationalStandards Institute (ANSI) established a committee to provide a modern,comprehensive definition of C. The resulting definition, the ANSI standard, or``ANSI C'', was completed in late 1988. Most of the features of the standardare already supported by modern compilers.<p>The standard is based on the original reference manual. The language isrelatively little changed; one of the goals of the standard was to make surethat most existing programs would remain valid, or, failing that, thatcompilers could produce warnings of new behavior.<p>For most programmers, the most important change is the new syntax fordeclaring and defining functions. A function declaration can now include adescription of the arguments of the function; the definition syntax changesto match. This extra information makes it much easier for compilers to detecterrors caused by mismatched arguments; in our experience, it is a veryuseful addition to the language.<p>There are other small-scale language changes. Structure assignment andenumerations, which had been widely available, are now officially part of thelanguage. Floating-point computations may now be done in single precision.The properties of arithmetic, especially for unsigned types, are clarified.The preprocessor is more elaborate. Most of these changes will have only minoreffects on most programmers.<p>A second significant contribution of the standard is the definition of alibrary to accompany C. It specifies functions for accessing the operatingsystem (for instance, to read and write files), formatted input and output,memory allocation, string manipulation, and the like. A collection ofstandard headers provides uniform access to declarations of functions in datatypes. Programs that use this library to interact with a host system areassured of compatible behavior. Most of the library is closely modeled onthe ``standard I/O library'' of the UNIX system. This library was describedin the first edition, and has been widely used on other systems as well.Again, most programmers will not see much change.<p>Because the data types and control structures provided by C are supporteddirectly by most computers, the run-time library required to implementself-contained programs is tiny. The standard library functions are onlycalled explicitly, so they can be avoided if they are not needed. Most can bewritten in C, and except for the operating system details they conceal, arethemselves portable.<p>Although C matches the capabilities of many computers, it is independent ofany particular machine architecture. With a little care it is easy to writeportable programs, that is, programs that can be run without change on avariety of hardware. The standard makes portability issues explicit, andprescribes a set of constants that characterize the machine on which theprogram is run.<p>C is not a strongly-typed language, but as it has evolved, its type-checkinghas been strengthened. The original definition of C frowned on, butpermitted, the interchange of pointers and integers; this has long since beeneliminated, and the standard now requires the proper declarations andexplicit conversions that had already been enforced by good compilers. Thenew function declarations are another step in this direction. Compilers willwarn of most type errors, and there is no automatic conversion ofincompatible data types. Nevertheless, C retains the basic philosophy thatprogrammers know what they are doing; it only requires that they state theirintentions explicitly.<p>C, like any other language, has its blemishes. Some of the operators have thewrong precedence; some parts of the syntax could be better. Nonetheless, Chas proven to ben an extremely effective and expressive language for a widevariety of programming applications.<p>The book is organized as follows. Chapter 1 is a tutorial on the central partof C. The purpose is to get the reader started as quickly as possible, sincewe believe strongly that the way to learn a new language is to write programsin it. The tutorial does assume a working knowledge of the basic elements ofprogramming; there is no explanation of computers, of compilation, nor of themeaning of an expression like <tt>n=n+1</tt>. Although we have tried wherepossible to show useful programming techniques, the book is not intended tobe a reference work on data structures and algorithms; when forced to makea choice, we have concentrated on the language.<p>Chapters 2 through 6 discuss various aspects of C in more detail, and rathermore formally, than does Chapter 1, although the emphasis is still onexamples of complete programs, rather than isolated fragments. Chapter 2deals with the basic data types, operators and expressions. Chapter 3 threatscontrol flow: <tt>if-else</tt>, <tt>switch</tt>, <tt>while</tt>, <tt>for</tt>,etc. Chapter 4 covers functions and program structure - external variables,scope rules, multiple source files, and so on - and also touches on thepreprocessor. Chapter 5 discusses pointers and address arithmetic. Chapter 6covers structures and unions.<p>Chapter 7 describes the standard library, which provides a common interfaceto the operating system. This library is defined by the ANSI standard and ismeant to be supported on all machines that support C, so programs that use itfor input, output, and other operating system access can be moved from onesystem to another without change.<p>Chapter 8 describes an interface between C programs and the UNIX operatingsystem, concentrating on input/output, the file system, and storageallocation. Although some of this chapter is specific to UNIX systems,programmers who use other systems should still find useful material here,including some insight into how one version of the standard library isimplemented, and suggestions on portability.<p>Appendix A contains a language reference manual. The official statement ofthe syntax and semantics of the C language is the ANSI standard itself. Thatdocument, however, is intended foremost for compiler writers. The referencemanual here conveys the definition of the language more concisely and withoutthe same legalistic style. Appendix B is a summary of the standard library,again for users rather than implementers. Appendix C is a short summary ofchanges from the original language. In cases of doubt, however, the standardand one's own compiler remain the final authorities on the language.<p><hr><p align="center"><a href="preface1.html">Back to the Preface to the First Edition</a> -- <a href="kandr.html">Index</a> -- <a href="chapter1.html">Chapter 1</a><p><hr></body></html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -