ebook img

The C Programming Language - Draft Version 1 PDF

152 Pages·1977·5.497 MB·
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview The C Programming Language - Draft Version 1

L)/0/4 ‘- t ‘*¢< '2‘ » 4* THE W C PROGRAMMING LANGUAGE Brian W. Kernighan Dennis M. Ritchie Bell TelephoneLaboratories, Incorporated MurrayHill, NewJersey DRAFT VERSION1. Comments on this book are solicited. 1977. Allrights reserved. @ ‘M PREFACE - 0 -: C is a general-purpose programming language which features economy of expression, modern control ow 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 par- ticular area of application. But its absence of restrictions and its generality make it more convenient and e'ective for many tasks than supposedly more powerful languages. A This book aims to teach how to program in C. Most ofthe treatment is based on reading, writing and revising examples, rather than on bald statements of rules. For the most part, the examples are complete, real programs, rather than articial ones, although they are of necessity not very big. Besides howing how to make effective use ofthe language, we have also tried where possible to show useful algo- rithms and principles ofgood style and sound design. The book is not an introductory programming manual; it assumes some fami- liarity with basic programming concepts like variables, assignment statements, loops, and subroutines. Nonetheless, an absolutely novice programmer should be able to read along and pick up the language, although access to a more knowledgeable col- league will help. C was originally designed for and implemented on the UNIX operating system on the DEC PDP-ll; the operating system, the compiler, and essentially all UNIX applications programs are written in C. Production compilers also exist for the IBM System/370, the Honeywell 6000, and the Interdata 8-32, with preliminary versions on several other machines. C is not tied to any particular system and it is readily possible to write portable C programs. Since C is an evolving language that exists on a variety of systems, some of the material in this book may be incorrect for a particular system. We have tried to steer clear of such problems, and to warn of the inevitable difculties. When in doubt, however, we have generally chosen to describe the situation on UNIX, since that is the environment ofthe majority ofC programmers. 1 CHAPTER 1: A TUTORIAL INTRODUCTION Let us begin with a quick introduction to C. Our aim is to show the essential elements" ofthe language in real programs, but without getting bogged down in details, formal 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 ow, functions, and the rudiments of input and output. We are quite intentionally leaving out of this chapter features of C which are ofvital importance for writing bigger programs. These include pointers, structures, most of C’s rich set of operators, several control ow statements, and myriad details. This approach has its drawbacks, of course. Most notable is that the complete story on any particular language feature is not found in a single place, and the tutorial, by being brief, may also mislead. We have tried to minimize this effect, 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 descrip- tions that begin in Chapter 2. 1.1 Getting Started The only way to really learn a new programming language is by writing programs in it. And the rst program is the same for all languages: Print upon the normal output the words hello, world This is the basic hurdle; to leap over it you have to be able to create the pro- gram text somewhere, compile it successfully, load it, run it, and nd out where your output went. With these mechanical details mastered, everything else is comparatively easy. 1 2 THECPROGRAMMINGLANGUAGE CHAPTER l In C, the program to print “hello, world” is malnl ) l prlntf("helIo, world\n"); I Just how to run this program depends on the system you are using. As an specic example, on Unix you must create the source program on a le whose name ends in “.0”, such as heIlo.c, then compile it with the cc com- mand ccheIlo.c If you haven’t botched anything, such as omitting a character or misspelling something, the compilation will proceed silently, and make an executable le called a.out. Running that by the command 0.01!! will produce hello, world as its output. On other systems, the rules will be different; check with a local expert. I-I: Exercise Run this program on your system. Experiment with leaving out parts ofthe program, to see what error messages you get. E1 Now for some explanations about the program itself. A C program, whatever its size, consists ofone or more “functions” which specify the actual computing operations that are to be done. C functions are similar to the func- tions and subroutines of a Fortran program or the procedures of PL/I, Pascal, etc. main is such a function. Normally you are at liberty to give functions whatever names you like, but main is a special name — your program begins executing at the beginning ofmaln. This means thatevery program musthave a maln somewhere. mbln will usually invoke other functions to perform its job, some coming from the same program, and others from libraries of previ- ously written functions. One method of communicating data between functions is by arguments. The parentheses following the function name surround the argument list; here maln is a function of no arguments, indicated by ( ). The braces [ enclose 1 the statements that make up the function. A function is invoked by naming it, followed by a parenthesized list of arguments. There is no CALL statement as there is in Fortran or PL/I. The parentheses must be present even ifthere are no arguments. The line that says prlntf("hello, world\n"); is a function call, which calls a function named prlntf, with the argument "hello, worId\n". prlntf is a library function which prints output on the CHAPTER l ATUTORIALINTRODUCTION 3 terminal (unless some other destination is specied). In this case it prints the string ofcharacters that make up its argument. A sequence of characters enclosed in the double quotes "..." is called a character string. For the moment our only use of character strings will be as arguments for printf andother functions. A string may contain any number of any characters. The sequence \n in the string is C shorthand for the newline character, which when printed advances the terminal to the next line. If you leave out the \n (a worthwhile experiment), you will nd that your output is not ter- minated properly by a line feed. The only way to get a newline character into the prlntf»argument is with \n; ifyou try something like prlntf("hello, world "); the C compiler will print unfriendly diagnostics about missing quotes. prlntf never supplies a newline automatically, so multiple calls may be used to build up an output line in stages. Our rst program couldjust as well have been written maIn( ) { printf("hello, "); prlntf("world"): printf("\n"); to produce an identical output. Notice that \n represents only a single character. There are several other “escape sequences” like \n for representing hard-to-get or invisible characters, such as \t for tab, \b for backspace, \" for the quote, and \\ for the backslash itself Exercise I-2w: - triment to nd out what happens when prlntf’s argument ‘ stri 1 - - -- ere X is some character not listed above. El 1.2 Variables and Arithmetic The next program prints the following table of fahrenheit temperatures and their centigrade equivalents, using the formula c==(5/9_)(f—32). 0 -17.8 20 -6.7 40 4.4 60 15.6 80 26.7 100 37.8 120 48.9 140 60.0 160 71.1 180 82.2 4 THECPROGRAMMINGLANGUAGE CHAPTER 1 200 93.3 220 104.4 240 115.6 260 126.7 280 137.8 300 148.9 Here is the program itself. /-prlnt fahrenheit-centigrade table tori = 0, 20,300 =-/ maln( ) l Int lower, upper, step; float fahr, cent; lower = 0; /~ lower limit of temperature table -/ upper = 300; /# upper limit =-/ step = 20; /~ step size -/ fahr = lower; while (iahr <--= upper) l cent 5.0/9.0 * (fahr—32); printi("%4.0i %6.1f\n", fahr, cent); fahr — iahr + step; I l The rst two lines /~ print fahrenhelt—centigrade table tori == O, 20, 300 ~/ are a’comment, which in this case explains briey what the program does. Any characters between /* and ~/ are ignored; they may beused freely to make a program easier to understand. ln C, allvariables must be declared before use, usually at the beginning of the function before any executable statements. If you forget a declaration, you will get a diagnostic from the compiler. A declaration consists of a type and a list ofvariables which have that type, as in int lower, upper, step; float fahr, cent; The type int implies that the variables listed are integers; float stands foroat- ingpoint, i.e., numbers which may have a fractional part. The precision ofboth int and float depends on the particular machine you are using; on the PDP-11, for instance, an int is a 16 bit number, that is one which lies between —32768 and +32767. A float number is a 32 bit quantity, which amounts to about seven signicant digits, with exponents between -38 and +38. CHAPTER 1 ATUTORIALINTRODUCTION 5 C provides several other basic data types besides int and float, the most common ofwhich are char character — a single byte long long integer . double double precision oating point There are also arrays and structures of these basic types, pointers to them, and functions that return them, all ofwhich we will meet in due course. The executable statements in the temperature conversion program begin with the assignments - lower 0; upper-=- 300; step 20; etc., which set the variables to their starting values. Individual statements are terminated by semicolons; no semicolon follows a brace. To produce a table of many lines, we need a loop; this is the function of the while statement whlle (fahr <= upper) { } . The condition in parentheses is tested. Ifit is true, the body ofthe loop (all of the statements enclosed in the braces { and I) is executed. Then the condition is re-tested, and if true, the body is executed again. Eventually when the test becomes false (fahr > upper) the loop ends, and execution continues at the statement that follows the loop. There are no further statements in this pro- gram, so it terminates. The body ofa while can be a single C statement, like whlle (...) printt(...); or one or more statements enclosed in braces, as in the temperature converter. The statements controlled by the whlle are indented by one tab stop so you can see at a glance what the scope of the while is, that is, what statements are inside the loop. The indentation emphasizes the logical structure of the program. Although C is quite permissive about statement positioning, proper indentation and use of white space are critical in making programs easy for people to read. The position of the braces is less important; we have chosen one of several popular styles. Pick a style that suits you, then use it con- sistently. Most of the work gets done in the body of the loop. The centigrade temperature is computed and assigned to cent by the statement 6 THECPROGRAMMINGLANGUAGE CHAPTER l cent -= 5.0/9.0 s (tahr—32); - The reason for using 5.0/9.0 instead of the simpler looking 5/9 is that in C, as in many other languages, integer division truncates, so any fractional part is dis- carded. Thus 5/9 is zero and of course so would be all the temperatures. A decimal point in a constant indicates that it is oating point, so 5.0/9.0 is 0.555..., as we want. Why not use 32.0 instead of 32? Since fahr is a float, 32 is automati- cally converted to float (to 32.0) before the subtraction can be done. As a matter of style, it’s wise to write oating point constants with explicit decimal points even when they have integral values; it emphasizes their oating point nature for human readers, and ensures that the compiler will see things your way too. This example shows a bit more of how printf works. prlntt is actually a general-purpose format conversion function, which we’ll describe completely in Chapter 7. Its rst argument is a string of characters to be printed, with each % sign indicating where one ofthe other (second, third, ...) arguments is to be substituted, and what form it is to be printed in. In particular, %4.0f says that a oating point number is to be printed in a space at least fourcharacters wide, with no digits after the decimal point. %6.1f describes another number to occupy six spaces, with 1 digit after the decimal point. (printt also recognizes %d for decimal integer, %O for octal, %x for hexadecimal, %C for character, %s for character string, and %% for % itself.) Each % construction in the rst argument of printf is paired with its corresponding second, third, etc., argument; they must line up properly by number and type, or you’ll get meaningless answers. By the way, prlntf is notpart ofthe C language; there is no input or out- put dened in C itself. There is nothing magic about prlntf; it is just a useful function which is part of the standard library of routines that are normally accessible to C programs. In order to concentrate on C itself, we won’t talk much about I/O until Chapter 7. In particular, we will defer formatted input until then. Exercise 1-3: Modify the temperature conversion program to print a head- ing above the table. U Exercise 1-4: Write the program which does the corresponding centigrade to fahrenheit table. El Some Variations As you might expect, there are plenty of different ways to write a pro- gram; let’s try a variation on the temperature converter. CHAPTER l ATUTORIALINTRODUCTION 7 maIn( ) /- fahrenheit—centlgrade table -/ { int fahr; for (fahr — 0; fahr <== 300; fahr -.fahr + 20) printf("%4d %6.1f\n", fahr, 5.0/9.0 (fahr—32)); 1- I This producesthe same answers, but it certainly looks different. One major change is the elimination ofmost ofthe variables; only fahr remains, as an lnt. The lower and upper limits and the step size appear only as constants in the for statement, itself a new construction, and the expression that used to produce cent now appears as the third argument of prlntl instead of a separate assign- ment statement. Thislast change is an instance ofa quite general rule in C — in any con- text where it is permissible to use the value ofa variable ofsome type, you can use an expression of that type. Since the third argument of printf has to be a oating point value, any oating point expression can occur there. The for itself is a loop, a generalization of the whlle. If you compare it to the earlier while, its operation should be clear. The rst part fahr = O is done once, before the loop proper is entered. The condition fahr <= 300 is evaluated, and if true, the body of the loop (here a single printf) is exe- cuted. Then the re-initialization step - fahr fahr + 20 is done, and the condition re-evaluated. The loop terminates when the condi- tion becomes false. As with the while, -the body of the loop can be a single statement, or a group of statements enclosed in braces. The initialization and re-initialization parts can be any single expression. The choice between while and for is arbitrary, based on what seems clearest. The for is usually appropriate for loops when the loop initialization and re-initialization are single statements and logically related. Symbolic Constants A nal observation before we leave temperature conversion forever. It’s bad practice to bury “magic numbers” like 300 and 20 in a program; they con- vey no information to someone who might have to read the program later, and they are hard to change in a systematic way. Fortunately, C provides a way to avoid such magic numbers. With the #dene construction, you can dene a symbolic name with a particular value at the beginning of a program, then use the name whenever the value is needed. The compiler replaces all occurrences ofthe name by the value. 8 THECPROGRAMMING LANGUAGE CHAPTER 1 #dene LOWER 0 #define UPPER 300 #define STEP 20 main( /- fahrenheitlcentigrade table -/ ) l int fahr; for (fahr = LOWER; fahr <'- UPPER; fahr = fahr + STEP) printf("%4d %6.1f\n", fahr, 5.0/9.0 - (fahr-32)); } The replacement for the name can actually be any text at all; it is not limited to numbers. Notice that there is no semicolon at the end of a denition. Since the whole line after the dened name is substituted, there would be too many semicolons in the for. A Exercise I-5: Verify that the construction #define LOWER 0; causes an error. El V 1.3 A Collection of Useful Programs We are now going to consider a family of related programs for doing simple operations on character data. You will nd that many programs arejust expanded versions ofthe prototypes that we write here. The standard library provides functions for reading and writing a charac- ter at a time. getchar( ) fetches the next input charactereach time it is called, and returns that character as its value. That is, after c = getcharl ) then c contains the next character of input. The characters normally come from the terminal, but that need not concern us right now. (More on that topic in Chapter 7.) The function putchar(c) is the complement ofgetchar: putchar(c). prints the character c on some output medium, again usually the terminal. As with printf, there is nothing special about getchar and putchar. They are not part ofthe C language, but they are universally available. File Copying e Given getchar and putchar, you can write a surprising amount of use- ful code without knowing anything more about I/0. Thesimplest example is a program which copies its input to its output a character at a time. In outline,

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.