%!PS-Adobe-3.0 %%Creator: groff version 1.16.1 %%CreationDate: Sun Jul 7 03:00:59 2002 %%DocumentNeededResources: font Times-Bold %%+ font Times-Roman %%+ font Times-Italic %%+ font Courier %%+ font Symbol %%DocumentSuppliedResources: procset grops 1.16 1 %%Pages: 25 %%PageOrder: Ascend %%Orientation: Portrait %%EndComments %%BeginProlog %%BeginResource: procset grops 1.16 1 /setpacking where{ pop currentpacking true setpacking }if /grops 120 dict dup begin /SC 32 def /A/show load def /B{0 SC 3 -1 roll widthshow}bind def /C{0 exch ashow}bind def /D{0 exch 0 SC 5 2 roll awidthshow}bind def /E{0 rmoveto show}bind def /F{0 rmoveto 0 SC 3 -1 roll widthshow}bind def /G{0 rmoveto 0 exch ashow}bind def /H{0 rmoveto 0 exch 0 SC 5 2 roll awidthshow}bind def /I{0 exch rmoveto show}bind def /J{0 exch rmoveto 0 SC 3 -1 roll widthshow}bind def /K{0 exch rmoveto 0 exch ashow}bind def /L{0 exch rmoveto 0 exch 0 SC 5 2 roll awidthshow}bind def /M{rmoveto show}bind def /N{rmoveto 0 SC 3 -1 roll widthshow}bind def /O{rmoveto 0 exch ashow}bind def /P{rmoveto 0 exch 0 SC 5 2 roll awidthshow}bind def /Q{moveto show}bind def /R{moveto 0 SC 3 -1 roll widthshow}bind def /S{moveto 0 exch ashow}bind def /T{moveto 0 exch 0 SC 5 2 roll awidthshow}bind def /SF{ findfont exch [exch dup 0 exch 0 exch neg 0 0]makefont dup setfont [exch/setfont cvx]cvx bind def }bind def /MF{ findfont [5 2 roll 0 3 1 roll neg 0 0]makefont dup setfont [exch/setfont cvx]cvx bind def }bind def /level0 0 def /RES 0 def /PL 0 def /LS 0 def /MANUAL{ statusdict begin/manualfeed true store end }bind def /PLG{ gsave newpath clippath pathbbox grestore exch pop add exch pop }bind def /BP{ /level0 save def 1 setlinecap 1 setlinejoin 72 RES div dup scale LS{ 90 rotate }{ 0 PL translate }ifelse 1 -1 scale }bind def /EP{ level0 restore showpage }bind def /DA{ newpath arcn stroke }bind def /SN{ transform .25 sub exch .25 sub exch round .25 add exch round .25 add exch itransform }bind def /DL{ SN moveto SN lineto stroke }bind def /DC{ newpath 0 360 arc closepath }bind def /TM matrix def /DE{ TM currentmatrix pop translate scale newpath 0 0 .5 0 360 arc closepath TM setmatrix }bind def /RC/rcurveto load def /RL/rlineto load def /ST/stroke load def /MT/moveto load def /CL/closepath load def /FL{ currentgray exch setgray fill setgray }bind def /BL/fill load def /LW/setlinewidth load def /RE{ findfont dup maxlength 1 index/FontName known not{1 add}if dict begin { 1 index/FID ne{def}{pop pop}ifelse }forall /Encoding exch def dup/FontName exch def currentdict end definefont pop }bind def /DEFS 0 def /EBEGIN{ moveto DEFS begin }bind def /EEND/end load def /CNT 0 def /level1 0 def /PBEGIN{ /level1 save def translate div 3 1 roll div exch scale neg exch neg exch translate 0 setgray 0 setlinecap 1 setlinewidth 0 setlinejoin 10 setmiterlimit []0 setdash /setstrokeadjust where{ pop false setstrokeadjust }if /setoverprint where{ pop false setoverprint }if newpath /CNT countdictstack def userdict begin /showpage{}def }bind def /PEND{ clear countdictstack CNT sub{end}repeat level1 restore }bind def end def /setpacking where{ pop setpacking }if %%EndResource %%IncludeResource: font Times-Bold %%IncludeResource: font Times-Roman %%IncludeResource: font Times-Italic %%IncludeResource: font Courier %%IncludeResource: font Symbol grops begin/DEFS 1 dict def DEFS begin/u{.001 mul}bind def end/RES 72 def/PL 792 def/LS false def/ENC0[/asciicircum/asciitilde/Scaron/Zcaron /scaron/zcaron/Ydieresis/trademark/quotesingle/.notdef/.notdef/.notdef /.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef /.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef /.notdef/.notdef/space/exclam/quotedbl/numbersign/dollar/percent /ampersand/quoteright/parenleft/parenright/asterisk/plus/comma/hyphen /period/slash/zero/one/two/three/four/five/six/seven/eight/nine/colon /semicolon/less/equal/greater/question/at/A/B/C/D/E/F/G/H/I/J/K/L/M/N/O /P/Q/R/S/T/U/V/W/X/Y/Z/bracketleft/backslash/bracketright/circumflex /underscore/quoteleft/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y /z/braceleft/bar/braceright/tilde/.notdef/quotesinglbase/guillemotleft /guillemotright/bullet/florin/fraction/perthousand/dagger/daggerdbl /endash/emdash/ff/fi/fl/ffi/ffl/dotlessi/dotlessj/grave/hungarumlaut /dotaccent/breve/caron/ring/ogonek/quotedblleft/quotedblright/oe/lslash /quotedblbase/OE/Lslash/.notdef/exclamdown/cent/sterling/currency/yen /brokenbar/section/dieresis/copyright/ordfeminine/guilsinglleft /logicalnot/minus/registered/macron/degree/plusminus/twosuperior /threesuperior/acute/mu/paragraph/periodcentered/cedilla/onesuperior /ordmasculine/guilsinglright/onequarter/onehalf/threequarters /questiondown/Agrave/Aacute/Acircumflex/Atilde/Adieresis/Aring/AE /Ccedilla/Egrave/Eacute/Ecircumflex/Edieresis/Igrave/Iacute/Icircumflex /Idieresis/Eth/Ntilde/Ograve/Oacute/Ocircumflex/Otilde/Odieresis /multiply/Oslash/Ugrave/Uacute/Ucircumflex/Udieresis/Yacute/Thorn /germandbls/agrave/aacute/acircumflex/atilde/adieresis/aring/ae/ccedilla /egrave/eacute/ecircumflex/edieresis/igrave/iacute/icircumflex/idieresis /eth/ntilde/ograve/oacute/ocircumflex/otilde/odieresis/divide/oslash /ugrave/uacute/ucircumflex/udieresis/yacute/thorn/ydieresis]def /Courier@0 ENC0/Courier RE/Times-Italic@0 ENC0/Times-Italic RE /Times-Roman@0 ENC0/Times-Roman RE/Times-Bold@0 ENC0/Times-Bold RE %%EndProlog %%Page: 1 1 %%BeginPageSetup BP %%EndPageSetup /F0 12/Times-Bold@0 SF(Pr)206.742 192 Q(ogramming in C \255 A T)-.216 E (utorial)-1.104 E/F1 10/Times-Roman@0 SF(Brian W)247.9 216 Q 2.5(.K)-.92 G(ernighan)-2.75 E/F2 10/Times-Italic@0 SF(Bell Labor)214.245 240 Q (atories, Murr)-.15 E(ay Hill, N. J)-.15 E(.)-.25 E/F3 10/Times-Bold@0 SF 2.5(1. Intr)72 319.2 R(oduction)-.18 E F1 3.174(Ci)97 334.8 S 3.174 (sac)-3.174 G .674(omputer language a)-3.174 F -.25(va)-.2 G .674 (ilable on the).25 F/F4 8/Times-Roman@0 SF(GCOS)3.174 E F1(and)3.174 E F4(UNIX)3.174 E F1 .673(operating systems at Murray Hill and \(in)3.174 F .633(preliminary form\) on OS/360 at Holmdel.)72 346.8 R 3.133(Cl) 5.633 G .634 (ets you write your programs clearly and simply \255 it has de-)-3.133 F .752(cent control \215o)72 358.8 R 3.251(wf)-.25 G .751 (acilities so your code can be read straight do)-3.351 F .751 (wn the page, without labels or GO)-.25 F -.18(TO)-.4 G -.55('s).18 G 3.251(;i).55 G(t)-3.251 E .579(lets you write code that is compact with\ out being too cryptic; it encourages modularity and good program)72 370.8 R(or)72 382.8 Q -.05(ga)-.18 G(nization; and it pro).05 E (vides good data-structuring f)-.15 E(acilities.)-.1 E .1 (This memorandum is a tutorial to mak)97 398.4 R 2.599(el)-.1 G .099 (earning C as painless as possible.)-2.599 F .099 (The \214rst part concentrates)5.099 F .271(on the central features of \ C; the second part discusses those parts of the language which are usef\ ul \(usually)72 410.4 R .251(for getting more ef)72 422.4 R .251 (\214cient and smaller code\) b)-.25 F .251 (ut which are not necessary for the ne)-.2 F 2.751(wu)-.25 G(ser)-2.751 E 5.251(.T)-.55 G .251(his is)-5.251 F F2(not)2.751 E F1 2.751(ar)2.751 G(ef-)-2.751 E .57(erence manual.)72 434.4 R .57 (Details and special cases will be skipped ruthlessly)5.57 F 3.07(,a) -.65 G .57(nd no attempt will be made to co)-3.07 F -.15(ve)-.15 G(r).15 E -2.15 -.25(ev e)72 446.4 T .735(ry language feature.).25 F .734 (The order of presentation is hopefully pedagogical instead of logical.) 5.735 F .734(Users who)5.734 F -.1(wo)72 458.4 S .078(uld lik).1 F 2.579 (et)-.1 G .079(he full story should consult the)-2.579 F F2 2.579(CR) 2.579 G(efer)-2.579 E .079(ence Manual)-.37 F F1 .079 (by D. M. Ritchie [1], which should be read)2.579 F .305(for details an) 72 470.4 R(yw)-.15 E(ay)-.1 E 5.305(.R)-.65 G .305 (untime support is described in [2] and [3]; you will ha)-5.305 F .605 -.15(ve t)-.2 H 2.805(or).15 G .305(ead one of these to learn)-2.805 F (ho)72 482.4 Q 2.5(wt)-.25 G 2.5(oc)-2.5 G(ompile and run a C program.) -2.5 E 2.024 -.8(We w)97 498 T .424(ill assume that you are f).8 F .424 (amiliar with the mysteries of creating \214les, te)-.1 F .425 (xt editing, and the lik)-.15 F 2.925(ei)-.1 G(n)-2.925 E (the operating system you run on, and that you ha)72 510 Q .3 -.15(ve p) -.2 H(rogrammed in some language before.).15 E F3 2.5(2. A)72 534 R (Simple C Pr)2.5 E(ogram)-.18 E/F5 10/Courier@0 SF 1.5(main\( \) {)108 554.6 R 1.5(printf\("hello, world"\);)144 566.6 R(})108 578.6 Q F1 2.719 (ACp)97 599.2 S .218(rogram consists of one or more)-2.719 F F2 (functions,)2.718 E F1 .218 (which are similar to the functions and subroutines of)2.718 F 3.254(aF) 72 611.2 S .754 (ortran program or the procedures of PL/I, and perhaps some e)-3.404 F .754(xternal data de\214nitions.)-.15 F F5(main)5.754 E F1 .755 (is such a)3.254 F .061(function, and in f)72 623.2 R .061 (act all C programs must ha)-.1 F .361 -.15(ve a)-.2 H F5(main)2.711 E /F6 16/Courier@0 SF(.)A F1(Ex)5.06 E .06(ecution of the program be)-.15 F .06(gins at the \214rst state-)-.15 F .252(ment of)72 635.2 R F5(main) 2.752 E F6(.)A F5(main)5.252 E F1 .252(will usually in)2.752 F -.2(vo) -.4 G .452 -.1(ke o).2 H .253 (ther functions to perform its job, some coming from the same).1 F (program, and others from libraries.)72 647.2 Q .717 (One method of communicating data between functions is by ar)97 662.8 R 3.216(guments. The)-.18 F .716(parentheses follo)3.216 F(wing)-.25 E .92 (the function name surround the ar)72 674.8 R .92(gument list; here)-.18 F F5(main)3.42 E F1 .92(is a function of no ar)3.42 F .92 (guments, indicated by \()-.18 F(\).)2.5 E .889 (The {} enclose the statements of the function.)72 686.8 R(Indi)5.889 E .889(vidual statements end with a semicolon b)-.25 F .888(ut are other) -.2 F(-)-.2 E(wise free-format.)72 698.8 Q F5(printf)97 714.4 Q F1 .551 (is a library function which will format and print output on the termin\ al \(unless some other)3.05 F(destination is speci\214ed\).)72 726.4 Q (In this)5 E(case it prints)5 E EP %%Page: 2 2 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF 2.5(CT)72 36 S 182.23(utorial -)-2.95 F 2.5(2-) 2.5 G/F1 10/Courier@0 SF 1.5(hello, world)108 89 R F0 2.708(Af)72 106 S .208(unction is in)-2.708 F -.2(vo)-.4 G -.1(ke).2 G 2.708(db).1 G 2.708 (yn)-2.708 G .208(aming it, follo)-2.708 F .208(wed by a list of ar)-.25 F .208(guments in parentheses.)-.18 F .208(There is no)5.208 F/F2 8 /Times-Roman@0 SF(CALL)2.708 E F0(state-)2.708 E(ment as in F)72 118 Q (ortran or)-.15 E F2(PL/I.)2.5 E/F3 10/Times-Bold@0 SF 2.5(3. A)72 142 R -.75(Wo)2.5 G(rking C Pr).75 E(ogram; V)-.18 E(ariables; T)-.92 E (ypes and T)-.74 E(ype Declarations)-.74 E F0(Here')97 157.6 Q 2.5(sab) -.55 G(igger program that adds three inte)-2.5 E (gers and prints their sum.)-.15 E F1 1.5(main\( \) {)108 174.6 R 1.5 (int a, b, c, sum;)144 186.6 R 7.5(a=1)144 198.6 S 15(;b)-7.5 G 7.5(=2) -7.5 G 15(;c)-7.5 G 7.5(=3)-7.5 G(;)-7.5 E 1.5(sum = a + b + c;)144 210.6 R 1.5(printf\("sum is %d", sum\);)144 222.6 R(})108 234.6 Q F0 1.621 (Arithmetic and the assignment statements are much the same as in F)97 255.2 R 1.621(ortran \(e)-.15 F 1.621(xcept for the semi-)-.15 F .09 (colons\) or)72 267.2 R F2(PL/I.)2.59 E F0 .09 (The format of C programs is quite free.)5.09 F 1.69 -.8(We c)5.09 H .09 (an put se).8 F -.15(ve)-.25 G .089(ral statements on a line if we w).15 F(ant,)-.1 E .589(or we can split a statement among se)72 279.2 R -.15 (ve)-.25 G .59 (ral lines if it seems desirable. The split may be between an).15 F 3.09 (yo)-.15 G 3.09(ft)-3.09 G(he)-3.09 E .599(operators or v)72 291.2 R .599(ariables, b)-.25 F(ut)-.2 E/F4 10/Times-Italic@0 SF(not)3.099 E F0 .599(in the middle of a name or operator)3.099 F 5.598(.A)-.55 G 3.098 (sam)-5.598 G .598(atter of style, spaces, tabs, and)-3.098 F(ne)72 303.2 Q(wlines should be used freely to enhance readability)-.25 E(.) -.65 E 2.5(Ch)97 318.8 S(as four fundamental)-2.5 E F4(types)2.5 E F0 (of v)2.5 E(ariables:)-.25 E F1(int)108 336.8 Q F0(inte)18 E (ger \(PDP-11: 16 bits; H6070: 36 bits; IBM360: 32 bits\))-.15 E F1 (char)108 348.8 Q F0 (one byte character \(PDP-11, IBM360: 8 bits; H6070: 9 bits\))12 E F1 (float)108 360.8 Q F0(single-precision \215oating point)6 E F1(double) 108 372.8 Q F0(double-precision \215oating point)36 E .696 (There are also)72 390.8 R F4(arr)3.196 E(ays)-.15 E F0(and)3.196 E F4 (structur)3.196 E(es)-.37 E F0 .696(of these basic types,)3.196 F F4 (pointer)3.197 E(s)-.1 E F0 .697(to them and)3.197 F F4(functions)3.197 E F0 .697(that return them,)3.197 F(all of which we will meet shortly)72 402.8 Q(.)-.65 E F4(All)97 418.4 Q F0 -.25(va)3.598 G 1.097(riables in \ a C program must be declared, although this can sometimes be done impli\ citly by).25 F(conte)72 430.4 Q 2.5(xt. Declarations)-.15 F (must precede e)2.5 E -.15(xe)-.15 G(cutable statements.).15 E (The declaration)5 E F1 1.5(int a, b, c, sum;)108 447.4 R F0(declares)72 464.4 Q F1 -3.5(a, b, c,)2.5 F F0(and)2.5 E F1(sum)2.5 E F0(to be inte) 2.5 E(gers.)-.15 E -1.11(Va)97 480 S .46(riable names ha)1.11 F .76 -.15 (ve o)-.2 H .4 LW 414.485 482.5 409.485 482.5 DL .46 (ne to eight characters, chosen from A-Z, a-z, 0-9, and)188.28 480 R 2.961(,a)7.961 G .461(nd start with a non-)-2.961 F 4.159 (digit. Stylistically)72 492 R 4.159(,i)-.65 G(t')-4.159 E 4.159(sm)-.55 G 1.659(uch better to use only a single case and gi)-4.159 F 1.958 -.15 (ve f)-.25 H 1.658(unctions and e).15 F 1.658(xternal v)-.15 F(ariables) -.25 E .243(names that are unique in the \214rst six characters.)72 504 R .243(\(Function and e)5.243 F .243(xternal v)-.15 F .243 (ariable names are used by v)-.25 F(arious)-.25 E 1.077(assemblers, som\ e of which are limited in the size and case of identi\214ers the)72 516 R 3.577(yc)-.15 G 1.076(an handle.\))-3.577 F(Furthermore,)6.076 E -.1 (ke)72 528 S(yw)-.05 E (ords and library functions may only be recognized in one case.)-.1 E F3 2.5(4. Constants)72 552 R F0 1.842 -.8(We h)97 567.6 T -2.25 -.2(av e).8 H .242(already seen decimal inte)2.942 F .242(ger constants in the pre) -.15 F .243(vious e)-.25 F .243(xample _ 1, 2, and 3.)-.15 F .243 (Since C is of-)5.243 F 1.359(ten used for system programming and bit-m\ anipulation, octal numbers are an important part of the lan-)72 579.6 R 2.799(guage. In)72 591.6 R .299(C, an)2.799 F 2.799(yn)-.15 G .299 (umber that be)-2.799 F .299(gins with 0 \(zero!\))-.15 F .299 (is an octal inte)5.299 F .299(ger \(and hence can')-.15 F 2.799(th)-.18 G -2.25 -.2(av e)-2.799 H(an)2.999 E 2.799(y8)-.15 G 1.399 -.55('s o) -2.799 H 2.8(r9).55 G -.55('s)-2.8 G(in it\).)72 603.6 Q (Thus 0777 is an octal constant, with decimal v)5 E(alue 511.)-.25 E 3.108(A`)97 619.2 S(`character')-3.848 E 3.108('i)-.74 G 3.108(so)-3.108 G .608(ne byte \(an inherently machine-dependent concept\).)-3.108 F .607(Most often this is e)5.608 F(xpressed)-.15 E .259(as a)72 631.2 R F4 -.15(ch)2.759 G(ar).15 E .259(acter constant,)-.15 F F0 .259 (which is one character enclosed in single quotes.)2.759 F(Ho)5.26 E(we) -.25 E -.15(ve)-.25 G 1.06 -.4(r, i).15 H 2.76(tm).4 G .26(ay be an) -2.76 F 2.76(yq)-.15 G(uantity)-2.76 E(that \214ts in a byte, as in)72 643.2 Q F1(flags)2.5 E F0(belo)2.5 E(w:)-.25 E F1 1.5 (char quest, newline, flags;)108 660.2 R 1.5(quest =)108 672.2 R/F5 10 /Symbol SF7.5 E F1(?)A F5A F1(;)A 1.5(newline =)108 684.2 R F5 7.5 E F1(\\n)A F5A F1(;)A 1.5(flags = 077;)108 696.2 R EP %%Page: 3 3 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF 2.5(CT)72 36 S 182.23(utorial -)-2.95 F 2.5(3-) 2.5 G .625(The sequence `\\n' is C notation for `)97 87.6 R(`ne)-.74 E .625(wline character')-.25 F .624 (', which, when printed, skips the terminal to)-.74 F .704(the be)72 99.6 R .704(ginning of the ne)-.15 F .704(xt line.)-.15 F .704 (Notice that `\\n' represents only a single character)5.704 F 5.704(.T) -.55 G .704(here are se)-5.704 F -.15(ve)-.25 G .705(ral other).15 F -.74(``)72 111.6 S(escapes').74 E 5.033('l)-.74 G(ik)-5.033 E 5.033(e`) -.1 G 5.033(\\n' for)-5.033 F 2.533(representing hard-to-get or in)5.033 F 2.532(visible characters, such as `\\t' for tab, `\\b' for)-.4 F(back\ space, `\\0' for end of \214le, and `\\\\' for the backslash itself.)72 123.6 Q/F1 10/Courier@0 SF(float)97 139.2 Q F0(and)2.5 E F1(double)2.5 E F0(constants are discussed in section 26.)2.5 E/F2 10/Times-Bold@0 SF 2.5(5. Simple)72 163.2 R(I/O _ getchar)2.5 E 2.5(,p)-.92 G(utchar)-2.5 E 2.5(,p)-.92 G(rintf)-2.5 E F1 1.5(main\( \) {)108 183.8 R 1.5(char c;) 144 195.8 R 7.5(c=g)144 207.8 S 1.5(etchar\( \);)-7.5 F(putchar\(c\);) 144 219.8 Q(})108 231.8 Q(getchar)97 252.4 Q F0(and)3.251 E F1(putchar) 3.251 E F0 .751(are the basic I/O library functions in C.)3.251 F F1 (getchar)5.752 E F0 .752(fetches one character)3.252 F .516(from the st\ andard input \(usually the terminal\) each time it is called, and retur\ ns that character as the v)72 264.4 R(alue)-.25 E .045(of the function.) 72 276.4 R .045(When it reaches the end of whate)5.045 F -.15(ve)-.25 G 2.545<728c>.15 G .046 (le it is reading, thereafter it returns the character rep-)-2.545 F (resented by `\\0' \(ascii)72 288.4 Q/F3 8/Times-Roman@0 SF(NUL,)2.5 E F0(which has v)2.5 E(alue zero\).)-.25 E 1.6 -.8(We w)5 H(ill see ho).8 E 2.5(wt)-.25 G 2.5(ou)-2.5 G(se this v)-2.5 E(ery shortly)-.15 E(.)-.65 E F1(putchar)97 304 Q F0 .437(puts one character out on the standard ou\ tput \(usually the terminal\) each time it is called.)2.938 F .685 (So the program abo)72 316 R .985 -.15(ve r)-.15 H .685 (eads one character and writes it back out.).15 F .686 (By itself, this isn')5.686 F 3.186(tv)-.18 G .686(ery interesting, b) -3.336 F(ut)-.2 E(observ)72 328 Q 3.401(et)-.15 G .901(hat if we put a \ loop around this, and add a test for end of \214le, we ha)-3.401 F 1.2 -.15(ve a c)-.2 H .9(omplete program for).15 F(cop)72 340 Q (ying one \214le to another)-.1 E(.)-.55 E F1(printf)97 355.6 Q F0 .141 (is a more complicated function for producing formatted output.)2.641 F 1.742 -.8(We w)5.141 H .142(ill talk about only the).8 F .216 (simplest use of it.)72 367.6 R(Basically)5.216 E(,)-.65 E F1(printf) 2.716 E F0 .216(uses its \214rst ar)2.716 F .216 (gument as formatting information, and an)-.18 F 2.716(ys)-.15 G (uccessi)-2.716 E -.15(ve)-.25 G(ar)72 379.6 Q(guments as v)-.18 E (ariables to be output.)-.25 E(Thus)5 E F1 1.5 (printf \("hello, world\\n"\);)108 396.6 R F0 .259 (is the simplest use _ the string `)72 413.6 R .259(`hello, w)-.74 F (orld\\n')-.1 E 2.759('i)-.74 G 2.759(sp)-2.759 G .26(rinted out.)-2.759 F .26(No formatting information, no v)5.26 F .26(ariables, so)-.25 F .678(the string is dumped out v)72 425.6 R 3.178(erbatim. The)-.15 F(ne) 3.178 E .677(wline is necessary to put this out on a line by itself.) -.25 F .677(\(The con-)5.677 F(struction)72 437.6 Q F1 1.5 ("hello, world\\n")108 454.6 R F0(is really an array of)72 471.6 Q F1 (chars)2.5 E/F4 16/Courier@0 SF(.)A F0(More about this shortly)5 E(.\)) -.65 E(More complicated, if)97 487.2 Q F1(sum)2.5 E F0(is 6,)2.5 E F1 1.5(printf \("sum is %d\\n", sum\);)108 504.2 R F0(prints)72 521.2 Q F1 1.5(sum is 6)108 538.2 R F0 -.4(Wi)72 555.2 S .234(thin the \214rst ar) .4 F .234(gument of)-.18 F F1(printf,)2.734 E F0 .234(the characters `) 2.734 F(`%d')-.74 E 2.734('s)-.74 G .235(ignify that the ne)-2.734 F .235(xt ar)-.15 F .235(gument in the ar)-.18 F(gument)-.18 E (list is to be printed as a base 10 number)72 567.2 Q(.)-.55 E .158 (Other useful formatting commands are `)97 582.8 R(`%c')-.74 E 2.658('t) -.74 G 2.658(op)-2.658 G .158(rint out a single character)-2.658 F 2.657 (,`)-.4 G(`%s')-3.397 E 2.657('t)-.74 G 2.657(op)-2.657 G .157 (rint out an en-)-2.657 F(tire string, and `)72 594.8 Q(`%o')-.74 E 2.5 ('t)-.74 G 2.5(op)-2.5 G (rint a number as octal instead of decimal \(no leading zero\).)-2.5 E -.15(Fo)5 G 2.5(re).15 G(xample,)-2.65 E F1 7.5(n=5)108 611.8 S(11;)-7.5 E 1.5(printf \("What is the value of %d in octal?", n\);)108 623.8 R 1.5 (printf \(")108 635.8 R 1.5 (%s! %d decimal is %o octal\\n", "Right", n, n\);)15 F F0(prints)72 652.8 Q F1 2.375(What is the value of 511 in octal?)108 669.8 R 2.375 (Right! 511 decimal is 777)15.875 F(octal)108 681.8 Q F0 1.514 (Notice that there is no ne)72 698.8 R 1.514 (wline at the end of the \214rst output line.)-.25 F(Successi)6.514 E 1.814 -.15(ve c)-.25 H 1.514(alls to).15 F F1(printf)4.013 E F0 (\(and/or)4.013 E F1(putchar,)72 710.8 Q F0 .912 (for that matter\) simply put out characters.)3.411 F .912(No ne)5.912 F .912(wlines are printed unless you ask for them.)-.25 F(Similarly)72 722.8 Q 2.688(,o)-.65 G 2.688(ni)-2.688 G .188 (nput, characters are read one at a time as you ask for them.)-2.688 F .188(Each line is generally terminated)5.188 F EP %%Page: 4 4 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF 2.5(CT)72 36 S 182.23(utorial -)-2.95 F 2.5(4-) 2.5 G(by a ne)72 84 Q(wline \(\\n\), b)-.25 E (ut there is otherwise no concept of record.)-.2 E/F1 10/Times-Bold@0 SF 2.5(6. If;)72 108 R -.18(re)2.5 G (lational operators; compound statements).18 E F0 (The basic conditional-testing statement in C is the)97 123.6 Q/F2 10 /Courier@0 SF(if)2.5 E F0(statement:)2.5 E F2 7.5(c=g)108 140.6 S 1.5 (etchar\( \);)-7.5 F 1.5(if\( c)108 152.6 R F0(==)7.5 E/F3 10/Symbol SF 7.5 E F2(?)A F3A F2(\))7.5 E 1.5 (printf\("why did you type a question mark?\\n"\);)144 164.6 R F0 (The simplest form of)72 181.6 Q F2(if)2.5 E F0(is)2.5 E F2 1.5 (if \(expression\) statement)108 198.6 R F0 .735 (The condition to be tested is an)97 219.2 R 3.236(ye)-.15 G .736 (xpression enclosed in parentheses.)-3.386 F .736(It is follo)5.736 F .736(wed by a statement.)-.25 F 1.316(The e)72 231.2 R 1.316 (xpression is e)-.15 F -.25(va)-.25 G 1.316(luated, and if its v).25 F 1.316(alue is non-zero, the statement is e)-.25 F -.15(xe)-.15 G 3.815 (cuted. There').15 F 3.815(sa)-.55 G 3.815(no)-3.815 G(ptional)-3.815 E F2(else)72 243.2 Q F0(clause, to be described soon.)2.5 E (The character sequence `==')97 258.8 Q (is one of the relational operators in C; here is the complete set:)5 E (==)108 275.8 Q F2 1.5(equal to \()24.72 F/F4 16/Courier@0 SF(.)A F2(EQ) A F4(.)A F2 1.5(to Fortraners\))7.5 F 18(!= not)108 287.8 R 1.5 (equal to)7.5 F 30(>g)108 299.8 S 1.5(reater than)-30 F 30(= greater)108 323.8 R 1.5(than or equal to)7.5 F 18(<= less)108 335.8 R 1.5(than or equal to)7.5 F F0 1.426(The v)97 356.4 R 1.426(alue of)-.25 F F2 -2.074 (``expression relation expression'')3.926 F F0 1.426 (is 1 if the relation is true, and 0 if)3.926 F -.1(fa)72 368.4 S 2.581 (lse. Don').1 F 2.581(tf)-.18 G(or)-2.581 E .08(get that the equality t\ est is `=='; a single `=' causes an assignment, not a test, and in)-.18 F -.25(va)-.4 G(riably).25 E(leads to disaster)72 380.4 Q(.)-.55 E -.7 (Te)97 396 S .583(sts can be combined with the operators).7 F F2(`&&') 3.084 E/F5 8/Times-Roman@0 SF(\(AND\),)3.084 E .4 LW 330.788 389.25 330.788 396 DL 334.454 389.25 334.454 396 DL F2 5.666(`')323.788 396 S F5(\(OR\),)-2.582 E F0(and)3.084 E F2(`!')3.084 E F5(\(NO)5.584 E(T\).) -.32 E F0 -.15(Fo)5.584 G 3.084(re).15 G .584(xample, we)-3.234 F (can test whether a character is blank or tab or ne)72 408 Q(wline with) -.25 E F2 1.5(if\( c)108 425 R F0(==)A 171.72 418.25 171.72 425 DL 175.386 418.25 175.386 425 DL F3 7.5150.78 425 S F2(c)13.166 E F0 (==)A F3A F2(\\t)A 226.606 418.25 226.606 425 DL 230.272 418.25 230.272 425 DL F3215.636 425 Q F2(c)20.666 E F0(==)A F3A F2(\\n) A F3A F2(\))7.5 E F4(...)7.5 E F0 2.5(Cg)72 442 S(uarantees that) -2.5 E F2(`&&')2.5 E F0(and)2.5 E 194.37 435.25 194.37 442 DL 198.036 435.25 198.036 442 DL F2 5.666(`')187.37 442 S F0(are e)-3.166 E -.25 (va)-.25 G (luated left to right _ we shall soon see cases where this matters.).25 E .158(One of the nice things about C is that the)97 457.6 R F2 (statement)2.658 E F0 .158(part of an)2.658 F F2(if)2.658 E F0 .158 (can be made arbitrarily compli-)2.658 F .107 (cated by enclosing a set of statements in {}.)72 469.6 R .108 (As a simple e)5.108 F .108(xample, suppose we w)-.15 F .108 (ant to ensure that)-.1 F F2(a)2.608 E F0 .108(is big-)2.608 F .575 (ger than)72 481.6 R F2(b,)3.075 E F0 .575(as part of a sort routine.) 3.075 F .575(The interchange of)5.575 F F2(a)3.075 E F0(and)3.075 E F2 (b)3.075 E F0(tak)3.075 E .574(es three statements in C, grouped to-)-.1 F(gether by {}:)72 493.6 Q F2 1.5(if \(a < b\) {)108 510.6 R 7.5(t=a)144 522.6 S(;)-7.5 E 7.5(a=b)144 534.6 S(;)-7.5 E 7.5(b=t)144 546.6 S(;)-7.5 E(})108 558.6 Q F0 .626(As a general rule in C, an)97 579.2 R .626 (ywhere you can use a simple statement, you can use an)-.15 F 3.127(yc) -.15 G .627(ompound state-)-3.127 F .063(ment, which is just a number o\ f simple or compound ones enclosed in {}.)72 591.2 R .063 (There is no semicolon after the })5.063 F(of a compound statement, b)72 603.2 Q(ut there)-.2 E/F6 10/Times-Italic@0 SF(is)2.5 E F0 2.5(as)2.5 G (emicolon after the last non-compound statement inside the {}.)-2.5 E 1.001(The ability to replace single statements by comple)97 618.8 R 3.501(xo)-.15 G 1.001(nes at will is one feature that mak)-3.501 F 1.002 (es C much)-.1 F .36(more pleasant to use than F)72 630.8 R 2.86 (ortran. Logic)-.15 F(\(lik)2.86 E 2.86(et)-.1 G .36(he e)-2.86 F .36 (xchange in the pre)-.15 F .36(vious e)-.25 F .36(xample\) which w)-.15 F .36(ould require)-.1 F(se)72 642.8 Q -.15(ve)-.25 G 1.312(ral GO).15 F -.18(TO)-.4 G 2.412 -.55('s a).18 H 1.312(nd labels in F).55 F 1.312 (ortran can and should be done in C without an)-.15 F 2.613 -.65(y, u) -.15 H 1.313(sing compound state-).65 F(ments.)72 654.8 Q F1 2.5 (7. While)72 678.8 R(Statement; Assignment within an Expr)2.5 E (ession; Null Statement)-.18 E F0 .169 (The basic looping mechanism in C is the)97 694.4 R F2(while)2.668 E F0 2.668(statement. Here')2.668 F 2.668(sap)-.55 G .168 (rogram that copies its input to)-2.668 F (its output a character at a time.)72 706.4 Q (Remember that `\\0' marks the end of \214le.)5 E F2 1.5(main\( \) {)108 723.4 R EP %%Page: 5 5 %%BeginPageSetup BP %%EndPageSetup /F0 10/Courier@0 SF 7.5(CT)72 36 S(utorial)-7.5 E/F1 10/Symbol SF(-) 156.51 E F0(5)7.5 E F1(-)7.5 E F0 1.5(char c;)144 84 R 1.5 (while\( \(c=getchar\( \)\) !=)144 96 R F17.5 E F0(\\0)A F1A F0 (\))7.5 E(putchar\(c\);)180 108 Q(})108 120 Q/F2 10/Times-Roman@0 SF (The)72 137 Q F0(while)2.5 E F2 (statement is a loop, whose general form is)2.5 E F0 1.5 (while \(expression\) statement)108 154 R F2(Its meaning is)72 171 Q F0 1.5(\(a\) evaluate the expression)108 188 R 1.5 (\(b\) if its value is true \(i)108 200 R/F3 16/Courier@0 SF(.)A F0(e)A F3(.)A F0 7.5(,n)C 1.5(ot zero\))-7.5 F 1.5 (do the statement, and go back to \(a\))180 212 R F2 .716(Because the e) 72 229 R .717(xpression is tested before the statement is e)-.15 F -.15 (xe)-.15 G .717(cuted, the statement part can be e).15 F -.15(xe)-.15 G .717(cuted zero).15 F .352(times, which is often desirable.)72 241 R .351(As in the)5.351 F F0(if)2.851 E F2 .351(statement, the e)2.851 F .351(xpression and the statement can both be arbi-)-.15 F .15 (trarily complicated, although we ha)72 253 R -.15(ve)-.2 G(n').15 E 2.65(ts)-.18 G .15(een that yet.)-2.65 F .15(Our e)5.15 F .15 (xample gets the character)-.15 F 2.65(,a)-.4 G .15(ssigns it to)-2.65 F F0(c,)2.65 E F2(and)2.65 E .028(then tests if it')72 265 R 2.528(sa`) -.55 G(\\0')-2.528 E 2.528('. If)-.74 F .027 (it is not a `\\0', the statement part of the)2.527 F F0(while)2.527 E F2 .027(is e)2.527 F -.15(xe)-.15 G .027(cuted, printing the character) .15 F(.)-.55 E(The)72 277 Q F0(while)3.585 E F2 1.086(then repeats.) 3.585 F 1.086(When the input character is \214nally a `\\0', the)6.086 F F0(while)3.586 E F2 1.086(terminates, and so does)3.586 F F0(main)72 289 Q F3(.)A F2(Notice that we used an assignment statement)97 304.6 Q F0 7.5(c=g)108 321.6 S 1.5(etchar\( \))-7.5 F F2 .611(within an e)72 338.6 R 3.111(xpression. This)-.15 F .611 (is a handy notational shortcut which often produces clearer code.)3.111 F .61(\(In f)5.61 F .61(act it is)-.1 F .133(often the only w)72 350.6 R .133(ay to write the code cleanly)-.1 F 5.133(.A)-.65 G 2.633(sa)-5.133 G 2.633(ne)-2.633 G -.15(xe)-2.783 G .134 (rcise, re-write the \214le-cop).15 F 2.634(yw)-.1 G .134 (ithout using an assign-)-2.634 F .105(ment inside an e)72 362.6 R 2.604 (xpression.\) It)-.15 F -.1(wo)2.604 G .104 (rks because an assignment statement has a v).1 F .104(alue, just as an) -.25 F 2.604(yo)-.15 G .104(ther e)-2.604 F(xpres-)-.15 E .571 (sion does.)72 374.6 R .571(Its v)5.571 F .571(alue is the v)-.25 F .571 (alue of the right hand side.)-.25 F .571 (This also implies that we can use multiple assign-)5.571 F(ments lik)72 386.6 Q(e)-.1 E F0 7.5(x=y=z=0)108 403.6 S(;)-7.5 E F2(Ev)72 420.6 Q (aluation goes from right to left.)-.25 E .198(By the w)97 436.2 R(ay) -.1 E 2.698(,t)-.65 G .198(he e)-2.698 F .198(xtra parentheses in the a\ ssignment statement within the conditional were really nec-)-.15 F (essary: if we had said)72 448.2 Q F0 7.5(c=g)108 465.2 S 1.5 (etchar\( \) !=)-7.5 F F17.5 E F0(\\0)A F1A F0(c)72 482.2 Q F2 -.1(wo)2.978 G .479 (uld be set to 0 or 1 depending on whether the character fetched w).1 F .479(as an end of \214le or not.)-.1 F .479(This is be-)5.479 F 1.113 (cause in the absence of parentheses the assignment operator `=' is e)72 494.2 R -.25(va)-.25 G 1.113(luated after the relational operator).25 F 2.5(`!='. When)72 506.2 R(in doubt, or e)2.5 E -.15(ve)-.25 G 2.5(ni).15 G 2.5(fn)-2.5 G(ot, parenthesize.)-2.5 E(Since)97 521.8 Q F0 (putchar\(c\))3.43 E F2(returns)3.43 E F0(c)3.43 E F2 .93 (as its function v)3.43 F .931(alue, we could also cop)-.25 F 3.431(yt) -.1 G .931(he input to the output by)-3.431 F(nesting the calls to)72 533.8 Q F0(getchar)2.5 E F2(and)2.5 E F0(putchar:)2.5 E 1.5(main\( \) {) 108 550.8 R 1.5(while\( putchar\(getchar\( \)\) !=)144 562.8 R F17.5 E F0(\\0)A F1A F0 7.5(\);)7.5 G(})108 574.8 Q F2 .733 (What statement is being repeated?)72 591.8 R .733(None, or technically) 5.733 F 3.233(,t)-.65 G(he)-3.233 E/F4 10/Times-Italic@0 SF(null)3.232 E F2 .732(statement, because all the w)3.232 F .732(ork is really)-.1 F .46(done within the test part of the)72 603.8 R F0(while)2.96 E F3(.)A F2 .46(This v)5.46 F .46(ersion is slightly dif)-.15 F .46 (ferent from the pre)-.25 F .46(vious one, because)-.25 F (the \214nal `\\0' is copied to the output before we decide to stop.)72 615.8 Q/F5 10/Times-Bold@0 SF 2.5(8. Arithmetic)72 639.8 R F2 .172 (The arithmetic operators are the usual `+', `)97 655.4 R F1(-)A F2 .172 (', `*', and `/' \(truncating inte)B .172(ger di)-.15 F .171 (vision if the operands)-.25 F(are both)72 667.4 Q F0(int\),)2.5 E F2 (and the remainder or mod operator `%':)2.5 E F0 7.5(x=a)108 684.4 S (%b;)-7.5 E F2(sets)72 701.4 Q F0(x)2.686 E F2 .186 (to the remainder after)2.686 F F0(a)2.686 E F2 .186(is di)2.686 F .186 (vided by)-.25 F F0(b)2.686 E F2(\(i.e.,)2.686 E F0 2.686(am)2.686 G -3.314(od b\))-2.686 F F3(.)A F2 .186 (The results are machine dependent unless)5.186 F EP %%Page: 6 6 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF 2.5(CT)72 36 S 182.23(utorial -)-2.95 F 2.5(6-) 2.5 G/F1 10/Courier@0 SF(a)72 84 Q F0(and)2.5 E F1(b)2.5 E F0 (are both positi)2.5 E -.15(ve)-.25 G(.).15 E .505(In arithmetic,)97 99.6 R F1(char)3.005 E F0 -.25(va)3.005 G .505 (riables can usually be treated lik).25 F(e)-.1 E F1(int)3.005 E F0 -.25 (va)3.005 G 3.005(riables. Arithmetic).25 F .505(on characters is)3.005 F(quite le)72 111.6 Q -.05(ga)-.15 G(l, and often mak).05 E(es sense:) -.1 E F1 7.5(c=c+)108 128.6 S/F2 10/Symbol SFA F1(A)A F2 7.5 C F1(a)-7.5 E F2A F1(;)A F0(con)72 145.6 Q -.15(ve)-.4 G 1.159 (rts a single lo).15 F 1.159(wer case ascii character stored in)-.25 F F1(c)3.659 E F0 1.159(to upper case, making use of the f)3.659 F 1.159 (act that corre-)-.1 F .916(sponding ascii letters are a \214x)72 157.6 R .916(ed distance apart.)-.15 F .916(The rule go)5.916 F -.15(ve)-.15 G .916(rning this arithmetic is that all).15 F F1(chars)3.415 E F0(are) 3.415 E(con)72 169.6 Q -.15(ve)-.4 G .088(rted to).15 F F1(int)2.588 E F0 .089(before the arithmetic is done.)2.588 F(Be)5.089 E -.1(wa)-.25 G .089(re that con).1 F -.15(ve)-.4 G .089(rsion may in).15 F -.2(vo)-.4 G (lv).2 E 2.589(es)-.15 G(ign-e)-2.589 E .089(xtension _ if the)-.15 F .47(leftmost bit of a character is 1, the resulting inte)72 181.6 R .47 (ger might be ne)-.15 F -.05(ga)-.15 G(ti).05 E -.15(ve)-.25 G 5.47(.\() .15 G .47(This doesn')-5.47 F 2.97(th)-.18 G .47(appen with genuine) -2.97 F(characters on an)72 193.6 Q 2.5(yc)-.15 G(urrent machine.\))-2.5 E(So to con)97 209.2 Q -.15(ve)-.4 G(rt a \214le into lo).15 E (wer case:)-.25 E F1 1.5(main\( \) {)108 226.2 R 1.5(char c;)144 238.2 R 1.5(while\( \(c=getchar\( \)\) !=)144 250.2 R F27.5 E F1(\\0)A F2 A F1(\))7.5 E(if\()180 262.2 Q F27.5 E F1(A)A F2A F1 1.5 (<=c && c<=)B F2A F1(Z)A F2A F1(\))7.5 E(putchar\(c+)216 274.2 Q F2A F1(a)A F2A F1(A)A F2A F1(\);)A(else)180 286.2 Q (putchar\(c\);)216 298.2 Q(})108 310.2 Q F0 .637(Characters ha)72 327.2 R .937 -.15(ve d)-.2 H(if).15 E .637(ferent sizes on dif)-.25 F .637 (ferent machines.)-.25 F(Further)5.637 E 3.137(,t)-.4 G .637(his code w) -3.137 F(on')-.1 E 3.138(tw)-.18 G .638(ork on an IBM machine,)-3.238 F (because the letters in the ebcdic alphabet are not contiguous.)72 339.2 Q/F3 10/Times-Bold@0 SF 2.5(9. Else)72 363.2 R(Clause; Conditional Expr) 2.5 E(essions)-.18 E F0 1.6 -.8(We j)97 378.8 T(ust used an).8 E F1 (else)2.5 E F0(after an)2.5 E F1(if)2.5 E/F4 16/Courier@0 SF(.)A F0 (The most general form of)5 E F1(if)2.5 E F0(is)2.5 E F1 1.5 (if \(expression\) statement1 else statement2)108 395.8 R F0(the)72 412.8 Q F1(else)2.5 E F0(part is optional, b)2.5 E(ut often useful.)-.2 E(The canonical e)5 E(xample sets)-.15 E F1(x)2.5 E F0 (to the minimum of)2.5 E F1(a)2.5 E F0(and)2.5 E F1(b:)2.5 E 1.5 (if \(a < b\))108 429.8 R 7.5(x=a)144 441.8 S(;)-7.5 E(else)108 453.8 Q 7.5(x=b)144 465.8 S(;)-7.5 E F0(Observ)72 482.8 Q 2.5(et)-.15 G (hat there')-2.5 E 2.5(sas)-.55 G(emicolon after)-2.5 E F1(x=a)2.5 E F4 (.)A F0 2.65(Cp)97 498.4 S(ro)-2.65 E .15 (vides an alternate form of conditional which is often more concise.) -.15 F .15(It is called the `)5.15 F(`conditional)-.74 E -.15(ex)72 510.4 S(pression').15 E 2.822('b)-.74 G .323 (ecause it is a conditional which actually has a v)-2.822 F .323 (alue and can be used an)-.25 F .323(ywhere an e)-.15 F(xpression)-.15 E 2.5(can. The)72 522.4 R -.25(va)2.5 G(lue of).25 E F1 1.5(a7.5 E F1(\\0)A F3A F1(\))7.5 E 1.5(if\( \()180 322.2 R F3A F1(A)A F3A F1 1.5(<=c && c<=)B F3A F1(Z)A F3A .4 LW 310.88 315.45 310.88 322.2 DL 314.546 315.45 314.546 322.2 DL F1 20.666(\)\() 296.38 322.2 S F3-20.666 E F1(a)A F3A F1 1.5(<=c && c<=)B F3 A F1(z)A F3A F1 7.5(\)\))C F0(++)7.5 E F1(let;)A 1.5(else if\()180 334.2 R F37.5 E F1(0)A F3A F1 1.5(<=c && c<=)B F3A F1(9)A F3 A F1(\))7.5 E F0(++)15 E F1(dig;)A(else)180 346.2 Q F0(++)15 E F1 (other;)A 1.5 (printf\("%d letters, %d digits, %d others\\n", let, dig, other\);)144 358.2 R(})108 370.2 Q F0(The `++' operator means `)72 387.2 Q (`increment by 1')-.74 E('; we will get to it in the ne)-.74 E (xt section.)-.15 E/F4 10/Times-Bold@0 SF 2.5(10. Incr)72 411.2 R (ement and Decr)-.18 E(ement Operators)-.18 E F0 .928 (In addition to the usual `)97 426.8 R F3(-)A F0 .928(', C also has tw)B 3.427(oo)-.1 G .927 (ther interesting unary operators, `++' \(increment\) and)-3.427 F(`)72 438.8 Q F3(--)A F0 2.5('\()C 2.5(decrement\). Suppose)-2.5 F(we w)2.5 E (ant to count the lines in a \214le.)-.1 E F1 1.5(main\( \) {)108 455.8 R 1.5(int c,n;)144 467.8 R 7.5(n=0)144 479.8 S(;)-7.5 E 1.5 (while\( \(c=getchar\( \)\) !=)144 491.8 R F37.5 E F1(\\0)A F3A F1(\))7.5 E 1.5(if\( c)180 503.8 R F0(==)7.5 E F37.5 E F1(\\n)A F3 A F1(\))7.5 E F0(++)216 515.8 Q F1(n;)A 1.5 (printf\("%d lines\\n", n\);)144 527.8 R(})108 539.8 Q F0(++)72 556.8 Q F1(n)A F0 .99(is equi)3.49 F -.25(va)-.25 G .99(lent to).25 F F1(n=n+1) 3.49 E F0 -.2(bu)3.49 G 3.49(tc).2 G(learer)-3.49 E 3.49(,p)-.4 G .99 (articularly when)-3.49 F F1(n)3.49 E F0 .99(is a complicated e)3.49 F 3.49(xpression. `++')-.15 F .99(and `)3.49 F F3(--)A F0(')A (can be applied only to)72 568.8 Q F1(int's)2.5 E F0(and)2.5 E F1 (char's)2.5 E F0(\(and)2.5 E F1(pointers)2.5 E F0(which we ha)2.5 E -.15 (ve)-.2 G(n').15 E 2.5(tg)-.18 G(ot to yet\).)-2.5 E .76 (The unusual feature of `++' and `)97 584.4 R F3(--)A F0 3.26('i)C 3.26 (st)-3.26 G .76(hat the)-3.26 F 3.259(yc)-.15 G .759 (an be used either before or after a v)-3.259 F 3.259(ariable. The)-.25 F -.25(va)72 596.4 S .376(lue of ++).25 F F1(k)A F0 .376(is the v)2.876 F .377(alue of)-.25 F F1(k)2.877 E/F5 10/Times-Italic@0 SF(after)2.877 E F0 .377(it has been incremented.)2.877 F .377(The v)5.377 F .377 (alue of)-.25 F F1(k)2.877 E F0 .377(++ is)B F1(k)2.877 E F5(befor)2.877 E(e)-.37 E F0 .377(it is increment-)2.877 F 2.5(ed. Suppose)72 608.4 R F1(k)2.5 E F0(is 5.)2.5 E(Then)5 E F1 7.5(x=)108 625.4 S F0(++)A F1(k;)A F0(increments)72 642.4 Q F1(k)2.5 E F0(to 6 and then sets)2.5 E F1(x)2.5 E F0(to the resulting v)2.5 E(alue, i.e., to 6.)-.25 E(But)5 E F1 7.5 (x=k)108 659.4 S F0(++)-7.5 E F1(;)A F0 .7(\214rst sets)72 676.4 R F1(x) 3.2 E F0 .699(to to 5, and)3.199 F F5(then)3.199 E F0(increments)3.199 E F1(k)3.199 E F0 .699(to 6.)3.199 F .699(The incrementing ef)5.699 F .699 (fect of ++)-.25 F F1(k)A F0(and)3.199 E F1(k)3.199 E F0 .699 (++ is the same, b)B(ut)-.2 E(their v)72 688.4 Q(alues are respecti)-.25 E -.15(ve)-.25 G(ly 5 and 6.).15 E 1.6 -.8(We s)5 H(hall soon see e).8 E (xamples where both of these uses are important.)-.15 E EP %%Page: 8 8 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF 2.5(CT)72 36 S 182.23(utorial -)-2.95 F 2.5(8-) 2.5 G/F1 10/Times-Bold@0 SF 2.5(11. Arrays)72 84 R F0 .06(In C, as in F) 97 99.6 R .06(ortran or PL/I, it is possible to mak)-.15 F 2.56(ea)-.1 G .06(rrays whose elements are basic types.)-2.56 F .06(Thus we can)5.06 F (mak)72 111.6 Q 2.5(ea)-.1 G 2.5(na)-2.5 G(rray of 10 inte)-2.5 E (gers with the declaration)-.15 E/F2 10/Courier@0 SF 1.5(int x[10];)108 128.6 R F0 .814(The square brack)72 145.6 R .814(ets mean)-.1 F/F3 10 /Times-Italic@0 SF(subscripting;)3.314 E F0 .814 (parentheses are used only for function references.)3.314 F .813 (Array inde)5.813 F -.15(xe)-.15 G(s).15 E(be)72 157.6 Q(gin at)-.15 E F3(zer)2.5 E(o,)-.45 E F0(so the elements of)2.5 E F2(x)2.5 E F0(are)2.5 E F2 1.5(x[0], x[1], x[2],)108 174.6 R/F4 16/Courier@0 SF(...)7.5 E F2 7.5(,x)C([9])-7.5 E F0(If an array has)72 191.6 Q F2(n)2.5 E F0 (elements, the lar)2.5 E(gest subscript is)-.18 E F2(n)2.5 E/F5 10 /Symbol SF(-)A F2(1)A F4(.)A F0 .425(Multiple-dimension arrays are pro) 97 207.2 R .426(vided, though not much used abo)-.15 F .726 -.15(ve t) -.15 H .626 -.1(wo d).15 H 2.926(imensions. The).1 F(declara-)2.926 E (tion and use look lik)72 219.2 Q(e)-.1 E F2 1.5(int name[10] [20];)108 236.2 R 7.5(n=n)108 248.2 S 1.5(ame[i+j] [1] + name[k] [2];)-7.5 F F0 .213(Subscripts can be arbitrary inte)72 265.2 R .212(ger e)-.15 F 2.712 (xpressions. Multi-dimension)-.15 F .212(arrays are stored by ro)2.712 F 2.712(w\()-.25 G .212(opposite to F)-2.712 F(or)-.15 E(-)-.2 E (tran\), so the rightmost subscript v)72 277.2 Q(aries f)-.25 E(astest;) -.1 E F2(name)2.5 E F0(has 10 ro)2.5 E(ws and 20 columns.)-.25 E .131 (Here is a program which reads a line, stores it in a b)97 292.8 R(uf) -.2 E(fer)-.25 E 2.631(,a)-.4 G .131(nd prints its length \(e)-2.631 F .131(xcluding the ne)-.15 F(wline)-.25 E(at the end\).)72 304.8 Q F2 1.5 (main\( \) {)108 321.8 R 1.5(int n, c;)144 333.8 R 1.5(char line[100];) 144 345.8 R 7.5(n=0)144 357.8 S(;)-7.5 E 1.5 (while\( \(c=getchar\( \)\) !=)144 369.8 R F57.5 E F2(\\n)A F5A F2 7.5(\){)7.5 G 1.5(if\( n < 100 \))180 381.8 R 1.5(line[n] = c;)216 393.8 R(n)180 405.8 Q F0(++)A F2(;)A(})144 417.8 Q 1.5 (printf\("length = %d\\n", n\);)144 429.8 R(})108 441.8 Q F0 .768 (As a more complicated problem, suppose we w)97 462.4 R .767 (ant to print the count for each line in the input, still)-.1 F (storing the \214rst 100 characters of each line.)72 474.4 Q -.35(Tr)5 G 2.5(yi).35 G 2.5(ta)-2.5 G 2.5(sa)-2.5 G 2.5(ne)-2.5 G -.15(xe)-2.65 G (rcise before looking at the solution:).15 E F2 1.5(main\( \) {)108 491.4 R 1.5(int n, c; char line[100];)144 503.4 R 7.5(n=0)144 515.4 S(;) -7.5 E 1.5(while\( \(c=getchar\( \)\) !=)144 527.4 R F57.5 E F2(\\0) A F5A F2(\))7.5 E 1.5(if\( c)180 539.4 R F0(==)7.5 E F57.5 E F2 (\\n)A F5A F2 7.5(\){)7.5 G 1.5(printf\("%d0, n\);)216 551.4 R 7.5 (n=0)216 563.4 S(;)-7.5 E(})180 575.4 Q 1.5(else {)180 587.4 R 1.5 (if\( n < 100 \) line[n] = c;)216 599.4 R(n)216 611.4 Q F0(++)A F2(;)A (})180 623.4 Q(})108 635.4 Q F1 2.5(12. Character)72 664.4 R (Arrays; Strings)2.5 E F0 -1.25 -.7(Te x)97 680 T 3.507(ti).7 G 3.507 (su)-3.507 G 1.007(sually k)-3.507 F 1.007 (ept as an array of characters, as we did with)-.1 F F2 -2.492(line[ ]) 3.508 F F0 1.008(in the e)3.508 F 1.008(xample abo)-.15 F -.15(ve)-.15 G 6.008(.B).15 G(y)-6.008 E(con)72 692 Q -.15(ve)-.4 G .101(ntion in C, t\ he last character in a character array should be a `\\0' because most p\ rograms that manipu-).15 F .936(late character arrays e)72 704 R .936 (xpect it.)-.15 F -.15(Fo)5.936 G 3.436(re).15 G(xample,)-3.586 E F2 (printf)3.437 E F0 .937 (uses the `\\0' to detect the end of a character array)3.437 F EP %%Page: 9 9 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF 2.5(CT)72 36 S 182.23(utorial -)-2.95 F 2.5(9-) 2.5 G(when printing it out with a `%s'.)72 84 Q 1.6 -.8(We c)97 99.6 T (an cop).8 E 2.5(yac)-.1 G(haracter array)-2.5 E/F1 10/Courier@0 SF(s) 2.5 E F0(into another)2.5 E F1(t)2.5 E F0(lik)2.5 E 2.5(et)-.1 G(his:) -2.5 E F1 7.5(i=0)144 116.6 S(;)-7.5 E 1.5(while\( \(t[i]=s[i]\) !=)144 128.6 R/F2 10/Symbol SF7.5 E F1(\\0)A F2A F1(\))7.5 E(i)180 140.6 Q F0(++)A F1(;)A F0 .054(Most of the time we ha)97 161.2 R .354 -.15(ve t)-.2 H 2.554(op).15 G .054(ut in our o)-2.554 F .054 (wn `\\0' at the end of a string; if we w)-.25 F .054 (ant to print the line with)-.1 F F1(printf,)72 173.2 Q F0(it')2.5 E 2.5 (sn)-.55 G(ecessary)-2.5 E 5(.T)-.65 G (his code prints the character count before the line:)-5 E F1 1.5 (main\( \) {)108 190.2 R 1.5(int n;)144 202.2 R 1.5(char line[100];)144 214.2 R 7.5(n=0)144 226.2 S(;)-7.5 E 1.5(while\( \(line[n)144 238.2 R F0 (++)A F1 1.5(]=getchar\( \)\) !=)B F27.5 E F1(\\n)A F2A F1(\);) 7.5 E 1.5(line[n] =)144 250.2 R F27.5 E F1(\\0)A F2A F1(;)A 1.5 (printf\("%d:\\t%s", n, line\);)144 262.2 R(})108 274.2 Q F0 .393 (Here we increment)72 291.2 R F1(n)2.893 E F0 .393 (in the subscript itself, b)2.893 F .393(ut only after the pre)-.2 F .394(vious v)-.25 F .394(alue has been used.)-.25 F .394(The character) 5.394 F(is read, placed in)72 303.2 Q F1(line[n],)2.5 E F0 (and only then)2.5 E F1(n)2.5 E F0(is incremented.)2.5 E .16(There is o\ ne place and one place only where C puts in the `\\0' at the end of a c\ haracter array for you,)97 318.8 R(and that is in the construction)72 330.8 Q F1 1.5("stuff between double quotes")108 347.8 R F0 1.277 (The compiler puts a `\\0' at the end automatically)72 364.8 R 6.277(.T) -.65 G -.15(ex)-6.977 G 3.777(te).15 G 1.277 (nclosed in double quotes is called a)-3.777 F/F3 10/Times-Italic@0 SF (string;)3.778 E F0(its)3.778 E(properties are precisely those of an \(\ initialized\) array of characters.)72 376.8 Q/F4 10/Times-Bold@0 SF 2.5 (13. F)72 400.8 R(or Statement)-.25 E F0(The)97 416.4 Q F1(for)2.657 E F0 .157(statement is a some)2.657 F .157(what generalized)-.25 F F1 (while)2.657 E F0 .156 (that lets us put the initialization and increment)2.657 F (parts of a loop into a single statement along with the test.)72 428.4 Q (The general form of the)5 E F1(for)2.5 E F0(is)2.5 E F1 1.5 (for\( initialization; expression; increment \))108 445.4 R(statement) 144 457.4 Q F0(The meaning is e)72 474.4 Q(xactly)-.15 E F1 (initialization;)144 491.4 Q 1.5(while\( expression \) {)144 503.4 R (statement)180 515.4 Q(increment;)180 527.4 Q(})144 539.4 Q F0 (Thus, the follo)72 556.4 Q(wing code does the same array cop)-.25 E 2.5 (ya)-.1 G 2.5(st)-2.5 G(he e)-2.5 E(xample in the pre)-.15 E (vious section:)-.25 E F1 1.5(for\( i=0; \(t[i]=s[i]\) !=)144 573.4 R F2 7.5 E F1(\\0)A F2A F1 7.5(;i)C F0(++)-7.5 E F1(\);)7.5 E F0 (This slightly more ornate e)72 590.4 Q (xample adds up the elements of an array:)-.15 E F1 1.5(sum = 0;)144 607.4 R 1.5(for\( i=0; i127 or <0\) into one pile.) -.25 F .379(Since this is pre-)5.379 F .515(sumably an isolated part of\ the program, good practice dictates making it a separate function.)72 267.2 R .514(Here is one)5.514 F -.1(wa)72 279.2 S(y:).1 E F1 1.5 (main\( \) {)108 296.2 R 1.5(int hist[129];)144 308.2 R(/)58.5 E/F4 14 /Courier@0 SF(*)4.9 I F1 1.5(128 legal chars + 1 illegal group)7.5 -4.9 N F4(*)7.5 4.9 M F1(/)-4.9 I F2(...)144 320.2 Q F1 1.5 (count\(hist, 128\);)144 332.2 R(/)4.5 E F4(*)4.9 I F1 1.5 (count the letters into hist)7.5 -4.9 N F4(*)7.5 4.9 M F1(/)-4.9 I (printf\()144 344.2 Q F2(...)7.5 E F1 40.2(\); /)7.5 F F4(*)4.9 I F1 1.5 (comments look like this; use them)7.5 -4.9 N F4(*)7.5 4.9 M F1(/)-4.9 I F2(...)144 356.2 Q F1(/)43.2 E F4(*)4.9 I F1 1.5 (anywhere blanks, tabs or newlines could appear)7.5 -4.9 N F4(*)7.5 4.9 M F1(/)-4.9 I(})108 368.2 Q 1.5(count\(buf, size\))108 385.2 R 1.5 (int size, buf[ ]; {)130.5 397.2 R 1.5(int i, c;)144 409.2 R 1.5 (for\( i=0; i<=size; i)144 421.2 R F0(++)A F1(\))7.5 E 1.5(buf[i] = 0;) 180 433.2 R(/)75 E F4(*)4.9 I F1 1.5(set buf to zero)7.5 -4.9 N F4(*)7.5 4.9 M F1(/)-4.9 I 1.5(while\( \(c=getchar\( \)\) !=)144 445.2 R/F5 10 /Symbol SF7.5 E F1(\\0)A F5A F1 -4.94 7.5(\){ /)7.5 H F4(*)-7.5 4.9 M F1 1.5(read til eof)7.5 -4.9 N F4(*)7.5 4.9 M F1(/)-4.9 I .4 LW 265 450.45 265 457.2 DL 268.666 450.45 268.666 457.2 DL 1.5 (if\( c > size)180 457.2 R 7.5(c<0\))20.666 G 7.5(c=s)216 469.2 S 45 (ize; /)-7.5 F F4(*)4.9 I F1 1.5(fix illegal input)7.5 -4.9 N F4(*)7.5 4.9 M F1(/)-4.9 I(buf[c])180 481.2 Q F0(++)A F1(;)A(})144 493.2 Q (return;)144 505.2 Q(})108 517.2 Q F0 2.891 -.8(We h)72 534.2 T -2.25 -.2(av e).8 H 1.291(already seen man)3.991 F 3.791(ye)-.15 G 1.291 (xamples of calling a function, so let us concentrate on ho)-3.941 F 3.791(wt)-.25 G(o)-3.791 E/F6 10/Times-Italic@0 SF(de\214ne)3.792 E F0 (one.)3.792 E(Since)72 546.2 Q F1(count)2.84 E F0 .34(has tw)2.84 F 2.84 (oa)-.1 G -.18(rg)-2.84 G .34(uments, we need to declare them, as sho) .18 F .34(wn, gi)-.25 F .34(ving their types, and in the case of)-.25 F F1(buf,)72 558.2 Q F0 .205(the f)2.705 F .206(act that it is an array) -.1 F 5.206(.T)-.65 G .206(he declarations of ar)-5.206 F .206 (guments go)-.18 F F6(between)2.706 E F0 .206(the ar)2.706 F .206 (gument list and the open-)-.18 F(ing `{'.)72 570.2 Q (There is no need to specify the size of the array)5 E F1(buf,)2.5 E F0 (for it is de\214ned outside of)2.5 E F1(count)2.5 E F2(.)A F0(The)97 585.8 Q F1(return)2.752 E F0 .251 (statement simply says to go back to the calling routine.)2.752 F .251 (In f)5.251 F .251(act, we could ha)-.1 F .551 -.15(ve o)-.2 H(mitted) .15 E(it, since a return is implied at the end of a function.)72 597.8 Q .233(What if we w)97 613.4 R(anted)-.1 E F1(count)2.733 E F0 .233 (to return a v)2.733 F .233(alue, say the number of characters read?) -.25 F(The)5.233 E F1(return)2.733 E F0(state-)2.733 E(ment allo)72 625.4 Q(ws for this too:)-.25 E F1 1.5(int i, c, nchar;)144 642.4 R 1.5 (nchar = 0;)144 654.4 R F2(...)144 666.4 Q F1 1.5 (while\( \(c=getchar\( \)\) !=)144 678.4 R F57.5 E F1(\\0)A F5A F1 7.5(\){)7.5 G 265 683.65 265 690.4 DL 268.666 683.65 268.666 690.4 DL 1.5(if\( c > size)180 690.4 R 7.5(c<0\))20.666 G 7.5(c=s)216 702.4 S (ize;)-7.5 E(buf[c])180 714.4 Q F0(++)A F1(;)A(nchar)180 726.4 Q F0(++)A F1(;)A EP %%Page: 11 11 %%BeginPageSetup BP %%EndPageSetup /F0 10/Courier@0 SF 7.5(CT)72 36 S(utorial)-7.5 E/F1 10/Symbol SF(-) 153.51 E F0(11)7.5 E F1(-)7.5 E F0(})144 84 Q(return\(nchar\);)144 96 Q /F2 10/Times-Roman@0 SF(An)72 113 Q 2.564(ye)-.15 G .064 (xpression can appear within the parentheses.)-2.714 F .063 (Here is a function to compute the minimum of tw)5.064 F 2.563(oi)-.1 G (nte-)-2.563 E(gers:)72 125 Q F0 1.5(min\(a, b\))108 142 R 1.5 (int a, b; {)130.5 154 R 1.5(return\( a < b ? a : b \);)144 166 R(})108 178 Q F2 1.6 -.8(To c)97 202.2 T(op).8 E 2.5(yac)-.1 G(haracter array) -2.5 E 2.5(,w)-.65 G 2.5(ec)-2.5 G(ould write the function)-2.5 E F0 1.5 (strcopy\(s1, s2\))108 219.2 R(/)52.5 E/F3 14/Courier@0 SF(*)4.9 I F0 1.5(copies s1 to s2)7.5 -4.9 N F3(*)7.5 4.9 M F0(/)-4.9 I 1.5 (char s1[ ], s2[ ]; {)130.5 231.2 R 1.5(int i;)144 243.2 R 1.5 (for\( i = 0; \(s2[i] = s1[i]\) !=)144 255.2 R F17.5 E F0(\\0)A F1 A F0 7.5(;i)C F2(++)-7.5 E F0(\);)7.5 E(})108 267.2 Q F2 1.316 (As is often the case, all the w)72 284.2 R 1.316(ork is done by the as\ signment statement embedded in the test part of the)-.1 F F0(for)72 296.2 Q/F4 16/Courier@0 SF(.)A F2(Ag)5.943 E .943 (ain, the declarations of the ar)-.05 F(guments)-.18 E F0(s1)3.442 E F2 (and)3.442 E F0(s2)3.442 E F2 .942(omit the sizes, because the)3.442 F 3.442(yd)-.15 G(on')-3.442 E 3.442(tm)-.18 G .942(atter to)-3.442 F F0 (strcopy)72 308.2 Q F4(.)A F2 (\(In the section on pointers, we will see a more ef)5 E(\214cient w) -.25 E(ay to do a string cop)-.1 E -.65(y.)-.1 G(\)).65 E .871(There is\ a subtlety in function usage which can trap the unsuspecting F)97 323.8 R .872(ortran programmer)-.15 F 5.872(.S)-.55 G(imple)-5.872 E -.25(va) 72 335.8 S .733(riables \(not arrays\) are passed in C by `).25 F .733 (`call by v)-.74 F(alue')-.25 E .732 (', which means that the called function is gi)-.74 F -.15(ve)-.25 G 3.232(na).15 G(cop)72 347.8 Q 3.14(yo)-.1 G 3.14(fi)-3.14 G .64(ts ar) -3.14 F .64(guments, and doesn')-.18 F 3.14(tk)-.18 G(no)-3.14 E 3.14 (wt)-.25 G .641(heir addresses.)-3.14 F .641(This mak)5.641 F .641 (es it impossible to change the v)-.1 F .641(alue of)-.25 F (one of the actual input ar)72 359.8 Q(guments.)-.18 E .163 (There are tw)97 375.4 R 2.663(ow)-.1 G .162(ays out of this dilemma.) -2.763 F .162(One is to mak)5.162 F 2.662(es)-.1 G .162 (pecial arrangements to pass to the function)-2.662 F .098 (the address of a v)72 387.4 R .098(ariable instead of its v)-.25 F 2.598(alue. The)-.25 F .098(other is to mak)2.598 F 2.598(et)-.1 G .098 (he v)-2.598 F .098(ariable a global or e)-.25 F .098(xternal v)-.15 F (ariable,)-.25 E(which is kno)72 399.4 Q (wn to each function by its name.)-.25 E 1.6 -.8(We w)5 H (ill discuss both possibilities in the ne).8 E(xt fe)-.15 E 2.5(ws)-.25 G(ections.)-2.5 E/F5 10/Times-Bold@0 SF 2.5(15. Local)72 423.4 R (and Exter)2.5 E(nal V)-.15 E(ariables)-.92 E F2(If we say)97 439 Q F0 1.5(f\( \) {)108 456 R 1.5(int x;)144 468 R F4(...)144 480 Q F0(})108 492 Q 1.5(g\( \) {)108 504 R 1.5(int x;)144 516 R F4(...)144 528 Q F0(}) 108 540 Q F2(each)72 557 Q F0(x)2.983 E F2(is)2.983 E/F6 10 /Times-Italic@0 SF(local)2.982 E F2 .482(to its o)2.982 F .482 (wn routine _ the)-.25 F F0(x)2.982 E F2(in)2.982 E F0(f)2.982 E F2 .482 (is unrelated to the)2.982 F F0(x)2.982 E F2(in)2.982 E F0(g)2.982 E F4 (.)A F2 .482(\(Local v)5.482 F .482(ariables are also called)-.25 F -.74 (``)72 569 S(automatic').74 E 3.274('.\) Furthermore)-.74 F .774 (each local v)3.274 F .774 (ariable in a routine appears only when the function is called, and)-.25 F F6(disappear)72 581 Q(s)-.1 E F2 .016(when the function is e)2.516 F 2.516(xited. Local)-.15 F -.25(va)2.516 G .016(riables ha).25 F .316 -.15(ve n)-.2 H 2.516(om).15 G .015(emory from one call to the ne)-2.516 F .015(xt and must)-.15 F .306(be e)72 593 R .306 (xplicitly initialized upon each entry)-.15 F 5.306(.\()-.65 G .306 (There is a)-5.306 F F0(static)2.806 E F2 .307 (storage class for making local v)2.806 F .307(ariables with)-.25 F (memory; we w)72 605 Q(on')-.1 E 2.5(td)-.18 G(iscuss it.\))-2.5 E .198 (As opposed to local v)97 620.6 R(ariables,)-.25 E F6 -.2(ex)2.698 G .197(ternal variables).2 F F2 .197(are de\214ned e)2.697 F .197 (xternal to all functions, and are \(poten-)-.15 F .417(tially\) a)72 632.6 R -.25(va)-.2 G .418(ilable to all functions.).25 F .418 (External storage al)5.418 F -.1(wa)-.1 G .418(ys remains in e).1 F 2.918(xistence. T)-.15 F 2.918(om)-.8 G(ak)-2.918 E 2.918(ev)-.1 G .418 (ariables e)-3.168 F(xternal)-.15 E(we ha)72 644.6 Q .3 -.15(ve t)-.2 H (o).15 E F6(de\214ne)2.5 E F2(them e)2.5 E (xternal to all functions, and, where)-.15 E -.15(ve)-.25 G 2.5(rw).15 G 2.5(ew)-2.5 G(ant to use them, mak)-2.6 E 2.5(ea)-.1 G F6(declar)A (ation.)-.15 E F0 1.5(main\( \) {)108 661.6 R 1.5 (extern int nchar, hist[ ];)144 673.6 R F4(...)144 685.6 Q F0 1.5 (count\( \);)144 697.6 R F4(...)144 709.6 Q F0(})108 721.6 Q EP %%Page: 12 12 %%BeginPageSetup BP %%EndPageSetup /F0 10/Courier@0 SF 7.5(CT)72 36 S(utorial)-7.5 E/F1 10/Symbol SF(-) 153.51 E F0(12)7.5 E F1(-)7.5 E F0 1.5(count\( \) {)108 89 R 1.5 (extern int nchar, hist[ ];)144 101 R 1.5(int i, c;)144 113 R/F2 16 /Courier@0 SF(...)144 125 Q F0(})108 137 Q 12(int hist[129];)108 154 R (/)12 E/F3 14/Courier@0 SF(*)4.9 I F0 1.5(space for histogram)7.5 -4.9 N F3(*)7.5 4.9 M F0(/)-4.9 I 12(int nchar;)108 166 R(/)72 E F3(*)4.9 I F0 1.5(character count)7.5 -4.9 N F3(*)7.5 4.9 M F0(/)-4.9 I/F4 10 /Times-Roman@0 SF .442(Roughly speaking, an)72 183 R 2.942(yf)-.15 G .441(unction that wishes to access an e)-2.942 F .441(xternal v)-.15 F .441(ariable must contain an)-.25 F F0(extern)2.941 E F4(decla-)2.941 E .176(ration for it.)72 195 R .176 (The declaration is the same as others, e)5.176 F .176 (xcept for the added k)-.15 F -.15(ey)-.1 G -.1(wo).15 G(rd).1 E F0 (extern)2.677 E F2(.)A F4(Furthermore,)5.177 E(there must some)72 207 Q (where be a)-.25 E/F5 10/Times-Italic@0 SF(de\214nition)2.5 E F4 (of the e)2.5 E(xternal v)-.15 E(ariables e)-.25 E (xternal to all functions.)-.15 E .822(External v)97 222.6 R .822 (ariables can be initialized; the)-.25 F 3.322(ya)-.15 G .822 (re set to zero if not e)-3.322 F .822(xplicitly initialized.)-.15 F .821(In its simplest)5.822 F (form, initialization is done by putting the v)72 234.6 Q (alue \(which must be a constant\) after the de\214nition:)-.25 E F0 12 (int nchar)108 251.6 R(0;)6 E 6(char flag)108 263.6 R F112 E F0(f)A F1A F0(;)A F4(etc)114.25 275.6 Q/F6 16/Times-Roman@0 SF(.)A F4 (This is discussed further in a later section.)72 292.6 Q 1.165(This en\ ds our discussion of what might be called the central core of C.)97 313.2 R -1.1(Yo)6.166 G 3.666(un)1.1 G 1.666 -.25(ow h)-3.666 H -2.25 -.2(av e).25 H 1.166(enough to)3.866 F .493 (write quite substantial C programs, and it w)72 325.2 R .492 (ould probably be a good idea if you paused long enough to do)-.1 F 2.5 (so. The)72 337.2 R(rest of this tutorial will describe some more ornat\ e constructions, useful b)2.5 E(ut not essential.)-.2 E/F7 10 /Times-Bold@0 SF 2.5(16. P)72 371.2 R(ointers)-.2 E F4(A)97 386.8 Q F5 (pointer)3.187 E F4 .687(in C is the address of something.)3.187 F .688 (It is a rare case indeed when we care what the speci\214c)5.687 F .037 (address itself is, b)72 398.8 R .037(ut pointers are a quite common w) -.2 F .037(ay to get at the contents of something.)-.1 F .036 (The unary opera-)5.036 F(tor `&' is used to produce the address of an \ object, if it has one. Thus)72 410.8 Q F0 1.5(int a, b;)144 427.8 R 7.5 (b=&)144 439.8 S(a;)-7.5 E F4 .201(puts the address of)72 456.8 R F0(a) 2.701 E F4(into)2.701 E F0(b)2.701 E F2(.)A F4 1.801 -.8(We c)5.201 H (an').8 E 2.701(td)-.18 G 2.701(om)-2.701 G .201(uch with it e)-2.701 F .202(xcept print it or pass it to some other routine, be-)-.15 F .097 (cause we ha)72 468.8 R -.15(ve)-.2 G(n').15 E 2.597(tg)-.18 G -2.15 -.25(iv e)-2.597 H(n).25 E F0(b)2.597 E F4 .097 (the right kind of declaration.)2.597 F .096(But if we declare that) 5.096 F F0(b)2.596 E F4 .096(is indeed a)2.596 F F5(pointer)2.596 E F4 .096(to an in-)2.596 F(te)72 480.8 Q(ger)-.15 E 2.5(,w)-.4 G(e')-2.5 E (re in good shape:)-.5 E F0 1.5(int a,)144 497.8 R F3(*)7.5 4.9 M F0 1.5 (b, c;)-4.9 J 7.5(b=&)144 509.8 S(a;)-7.5 E 7.5(c=)144 521.8 S F3(*)4.9 I F0(b;)-4.9 I(b)72 538.8 Q F4 .13(contains the address of)2.63 F F0(a) 2.631 E F4(and)2.631 E F0 -3.369(`c =)2.631 F F3(*)2.631 4.9 M F0(b') -4.9 I F4 .131(means to use the v)2.631 F .131(alue in)-.25 F F0(b)2.631 E F4 .131(as an address, i.e., as a pointer)2.631 F 5.131(.T)-.55 G(he) -5.131 E(ef)72 550.8 Q .078(fect is that we get back the contents of) -.25 F F0(a,)2.578 E F4 .078(albeit rather indirectly)2.578 F 5.078(.\() -.65 G(It')-5.078 E 2.578(sa)-.55 G -.1(lwa)-2.578 G .078 (ys the case that).1 F F0(`)2.578 E F3(*)4.9 I F0(&x')-4.9 I F4 .078 (is the)2.578 F(same as)72 562.8 Q F0(x)2.5 E F4(if)2.5 E F0(x)2.5 E F4 (has an address.\))2.5 E .618 (The most frequent use of pointers in C is for w)97 578.4 R .618 (alking ef)-.1 F .618(\214ciently along arrays.)-.25 F .618(In f)5.618 F .618(act, in the imple-)-.1 F .176(mentation of an array)72 590.4 R 2.676(,t)-.65 G .175(he array name represents the address of the zeroth\ element of the array)-2.676 F 2.675(,s)-.65 G 2.675(oy)-2.675 G .175 (ou can')-2.675 F(t)-.18 E .508(use it on the left side of an e)72 602.4 R 3.008(xpression. \(Y)-.15 F .508(ou can')-1.1 F 3.008(tc)-.18 G .508 (hange the address of something by assigning to it.\))-3.008 F(If)5.508 E(we say)72 614.4 Q F0(char)108 631.4 Q F3(*)7.5 4.9 M F0(y;)-4.9 I 1.5 (char x[100];)108 643.4 R(y)72 660.4 Q F4 .538 (is of type pointer to character \(although it doesn')3.038 F 3.038(ty) -.18 G .538(et point an)-3.038 F 3.038(ywhere\). W)-.15 F 3.038(ec)-.8 G .538(an mak)-3.038 F(e)-.1 E F0(y)3.038 E F4 .538(point to an ele-)3.038 F(ment of)72 672.4 Q F0(x)2.5 E F4(by either of)2.5 E F0 7.5(y=&)108 689.4 S(x[0];)-7.5 E 7.5(y=x)108 701.4 S(;)-7.5 E EP %%Page: 13 13 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF 2.5(CT)72 36 S 179.73(utorial -)-2.95 F(13 -)2.5 E(Since)72 84 Q/F1 10/Courier@0 SF(x)2.5 E F0(is the address of)2.5 E F1 (x[0])2.5 E F0(this is le)2.5 E -.05(ga)-.15 G 2.5(la).05 G (nd consistent.)-2.5 E(No)97 99.6 Q(w)-.25 E F1(`)2.5 E/F2 14/Courier@0 SF(*)4.9 I F1(y')-4.9 I F0(gi)2.5 E -.15(ve)-.25 G(s).15 E F1(x[0])2.5 E /F3 16/Courier@0 SF(.)A F0(More importantly)5 E(,)-.65 E F2(*)108 121.5 Q F1 27.6(\(y+1\) gives)-4.9 J(x[1])7.5 E F2(*)108 133.5 Q F1 27.6 (\(y+i\) gives)-4.9 J(x[i])7.5 E F0(and the sequence)72 145.6 Q F1 7.5 (y=&)144 162.6 S(x[0];)-7.5 E(y)144 174.6 Q F0(++)A F1(;)A F0(lea)72 191.6 Q -.15(ve)-.2 G(s).15 E F1(y)2.5 E F0(pointing at)2.5 E F1(x[1]) 2.5 E F3(.)A F0(Let')97 207.2 Q 3.643(su)-.55 G 1.143 (se pointers in a function)-3.643 F F1(length)3.643 E F0 1.143 (that computes ho)3.643 F 3.643(wl)-.25 G 1.144 (ong a character array is.)-3.643 F(Remember)6.144 E 1.156(that by con) 72 219.2 R -.15(ve)-.4 G 1.155 (ntion all character arrays are terminated with a `\\0'.).15 F 1.155 (\(And if the)6.155 F 3.655(ya)-.15 G(ren')-3.655 E 1.155 (t, this program will)-.18 F(blo)72 231.2 Q 2.5(wu)-.25 G 2.5(pi)-2.5 G (ne)-2.5 E(vitably)-.25 E 2.5(.\) The)-.65 F(old w)2.5 E(ay:)-.1 E F1 (length\(s\))108 248.2 Q 1.5(char s[ ]; {)130.5 260.2 R 1.5(int n;)144 272.2 R 1.5(for\( n=0; s[n] !=)144 284.2 R/F4 10/Symbol SF7.5 E F1 (\\0)A F4A F1 7.5(;\))C(n)180 296.2 Q F0(++)A F1(;)A(return\(n\);) 144 308.2 Q(})108 320.2 Q F0(Re)72 337.2 Q(writing with pointers gi)-.25 E -.15(ve)-.25 G(s).15 E F1(length\(s\))108 354.2 Q(char)130.5 366.2 Q F2(*)7.5 4.9 M F1 1.5(s; {)-4.9 J 1.5(int n;)144 378.2 R 1.5(for\( n=0;) 144 390.2 R F2(*)7.5 4.9 M F1 7.5(s!)-4.9 K(=)-7.5 E F47.5 E F1(\\0) A F4A F1 7.5(;s)C F0(++)-7.5 E F1(\))7.5 E(n)180 402.2 Q F0(++)A F1 (;)A(return\(n\);)144 414.2 Q(})108 426.2 Q F0 -1.1(Yo)72 443.2 S 2.751 (uc)1.1 G .251(an no)-2.751 F 2.751(ws)-.25 G .251(ee wh)-2.751 F 2.751 (yw)-.05 G 2.751(eh)-2.751 G -2.25 -.2(av e)-2.751 H .251 (to say what kind of thing)2.951 F F1(s)2.751 E F0 .251 (points to _ if we')2.751 F .252(re to increment it with)-.5 F F1(s) 2.752 E F0 .252(++ we)B(ha)72 455.2 Q .3 -.15(ve t)-.2 H 2.5(oi).15 G (ncrement it by the right amount.)-2.5 E(The pointer v)97 470.8 Q (ersion is more ef)-.15 E(\214cient \(this is almost al)-.25 E -.1(wa) -.1 G(ys true\) b).1 E(ut e)-.2 E -.15(ve)-.25 G 2.5(nm).15 G (ore compact is)-2.5 E F1 1.5(for\( n=0;)144 487.8 R F2(*)7.5 4.9 M F1 (s)-4.9 I F0(++)A F1(!=)7.5 E F47.5 E F1(\\0)A F4A F1 7.5(;n)C F0(++)-7.5 E F1(\);)7.5 E F0(The)72 504.8 Q F1(`)3.303 E F2(*)4.9 I F1 (s')-4.9 I F0 .803(returns a character; the)3.303 F F1(`)3.303 E F0(++)A F1(')A F0 .803(increments the pointer so we')3.303 F .803(ll get the ne) -.1 F .803(xt character ne)-.15 F .802(xt time)-.15 F 2.915(around. As) 72 516.8 R .415(you can see, as we mak)2.915 F 2.915(et)-.1 G .415 (hings more ef)-2.915 F .415(\214cient, we also mak)-.25 F 2.915(et)-.1 G .416(hem less clear)-2.915 F 5.416(.B)-.55 G(ut)-5.416 E F1(`)2.916 E F2(*)4.9 I F1(s)-4.9 I F0(++)A F1(')A F0(is)2.916 E (an idiom so common that you ha)72 528.8 Q .3 -.15(ve t)-.2 H 2.5(ok).15 G(no)-2.5 E 2.5(wi)-.25 G(t.)-2.5 E(Going a step further)97 544.4 Q 2.5 (,h)-.4 G(ere')-2.5 E 2.5(so)-.55 G(ur function)-2.5 E F1(strcopy)2.5 E F0(that copies a character array)2.5 E F1(s)2.5 E F0(to another)2.5 E F1 (t)2.5 E F3(.)A F1(strcopy\(s,t\))108 561.4 Q(char)130.5 573.4 Q F2(*) 7.5 4.9 M F1(s,)-4.9 I F2(*)7.5 4.9 M F1 1.5(t; {)-4.9 J(while\()144 585.4 Q F2(*)4.9 I F1(t)-4.9 I F0(++)A F1(=)7.5 E F2(*)7.5 4.9 M F1(s) -4.9 I F0(++)A F1(\);)A(})108 597.4 Q F0 2.032 -.8(We h)72 614.4 T -2.25 -.2(av e).8 H .431(omitted the test ag)3.131 F .431(ainst `\\0', becaus\ e `\\0' is identically zero; you will often see the code this w)-.05 F (ay)-.1 E(.)-.65 E(\(Y)72 626.4 Q(ou)-1.1 E/F5 10/Times-Italic@0 SF (must)2.5 E F0(ha)2.5 E .3 -.15(ve a s)-.2 H (pace after the `=': see section 25.\)).15 E -.15(Fo)97 642 S 2.5(ra).15 G -.18(rg)-2.5 G(uments to a function, and there only).18 E 2.5(,t)-.65 G(he declarations)-2.5 E F1 1.5(char s[ ];)108 659 R(char)108 671 Q F2 (*)7.5 4.9 M F1(s;)-4.9 I F0(are equi)72 688 Q -.25(va)-.25 G(lent _ a \ pointer to a type, or an array of unspeci\214ed size of that type, are \ the same thing.).25 E .068(If this all seems mysterious, cop)97 703.6 R 2.568(yt)-.1 G .068(hese forms until the)-2.568 F 2.568(yb)-.15 G .068 (ecome second nature.)-2.568 F -1.1(Yo)5.068 G 2.568(ud)1.1 G(on')-2.568 E 2.569(to)-.18 G .069(ften need)-2.569 F(an)72 715.6 Q (ything more complicated.)-.15 E EP %%Page: 14 14 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF 2.5(CT)72 36 S 179.73(utorial -)-2.95 F(14 -)2.5 E/F1 10/Times-Bold@0 SF 2.5(17. Function)72 84 R(Ar)2.5 E(guments)-.1 E F0 .262(Look back at the function)97 99.6 R/F2 10/Courier@0 SF(strcopy) 2.761 E F0 .261(in the pre)2.761 F .261(vious section.)-.25 F 1.861 -.8 (We p)5.261 H .261(assed it tw).8 F 2.761(os)-.1 G .261 (tring names as ar)-2.761 F(gu-)-.18 E .277 (ments, then proceeded to clobber both of them by incrementation.)72 111.6 R .277(So ho)5.277 F 2.777(wc)-.25 G .277(ome we don')-2.777 F 2.777(tl)-.18 G .277(ose the original)-2.777 F (strings in the function that called)72 123.6 Q F2(strcopy?)2.5 E F0 .562(As we said before, C is a `)97 139.2 R .562(`call by v)-.74 F (alue')-.25 E 3.062('l)-.74 G .562(anguage: when you mak)-3.062 F 3.062 (eaf)-.1 G .562(unction call lik)-3.062 F(e)-.1 E F2(f\(x\),)3.062 E F0 (the)3.062 E/F3 10/Times-Italic@0 SF(value)72 151.2 Q F0(of)2.859 E F2 (x)2.859 E F0 .359(is passed, not its address.)2.859 F .359(So there') 5.359 F 2.859(sn)-.55 G 2.86(ow)-2.859 G .36(ay to)-2.96 F F3(alter)2.86 E F2(x)2.86 E F0 .36(from inside)2.86 F F2(f)2.86 E/F4 16/Courier@0 SF (.)A F0(If)5.36 E F2(x)2.86 E F0 .36(is an array)2.86 F F2(\(char)2.86 E (x[10]\))72 163.2 Q F0 .262(this isn')2.762 F 2.762(tap)-.18 G .262 (roblem, because)-2.762 F F2(x)2.762 E F3(is)2.762 E F0 .262 (an address an)2.762 F(yw)-.15 E(ay)-.1 E 2.762(,a)-.65 G .261(nd you') -2.762 F .261(re not trying to change it, just what)-.5 F .223 (it addresses.)72 175.2 R .223(This is wh)5.223 F(y)-.05 E F2(strcopy) 2.723 E F0 -.1(wo)2.723 G .223(rks as it does.).1 F .223(And it')5.223 F 2.723(sc)-.55 G(on)-2.723 E -.15(ve)-.4 G .223(nient not to ha).15 F .523 -.15(ve t)-.2 H 2.724(ow).15 G .224(orry about mak-)-2.824 F (ing temporary copies of the input ar)72 187.2 Q(guments.)-.18 E .014 (But what if)97 202.8 R F2(x)2.514 E F0 .014(is a scalar and you do w) 2.514 F .014(ant to change it?)-.1 F .014(In that case, you ha)5.014 F .313 -.15(ve t)-.2 H 2.513(op).15 G .013(ass the)-2.513 F F3(addr)2.513 E(ess)-.37 E F0(of)2.513 E F2(x)2.513 E F0(to)72 214.8 Q F2(f,)2.5 E F0 (and then use it as a pointer)2.5 E 5(.T)-.55 G(hus for e)-5 E (xample, to interchange tw)-.15 E 2.5(oi)-.1 G(nte)-2.5 E (gers, we must write)-.15 E F2 1.5(flip\(x, y\))108 231.8 R(int)130.5 243.8 Q/F5 14/Courier@0 SF(*)7.5 4.9 M F2(x,)-4.9 I F5(*)7.5 4.9 M F2 1.5(y; {)-4.9 J 1.5(int temp;)144 255.8 R 1.5(temp =)144 267.8 R F5(*) 7.5 4.9 M F2(x;)-4.9 I F5(*)144 284.7 Q F2 7.5(x=)-4.9 K F5(*)4.9 I F2 (y;)-4.9 I F5(*)144 296.7 Q F2 7.5(y=t)-4.9 K(emp;)-7.5 E(})108 303.8 Q F0(and to call)72 320.8 Q F2(flip,)2.5 E F0(we ha)2.5 E .3 -.15(ve t)-.2 H 2.5(op).15 G(ass the addresses of the v)-2.5 E(ariables:)-.25 E F2 1.5 (flip \(&a, &b\);)108 337.8 R F1 2.5(18. Multiple)72 366.8 R(Le)2.5 E -.1(ve)-.15 G(ls of P).1 E(ointers; Pr)-.2 E(ogram Ar)-.18 E(guments)-.1 E F0 .196(When a C program is called, the ar)97 382.4 R .197 (guments on the command line are made a)-.18 F -.25(va)-.2 G .197 (ilable to the main pro-).25 F .765(gram as an ar)72 394.4 R .765 (gument count)-.18 F F2(argc)3.265 E F0 .764 (and an array of character strings)3.265 F F2(argv)3.264 E F0 .764 (containing the ar)3.264 F 3.264(guments. Ma-)-.18 F 1.598 (nipulating these ar)72 406.4 R 1.598 (guments is one of the most common uses of multiple le)-.18 F -.15(ve) -.25 G 1.598(ls of pointers \(`).15 F 1.599(`pointer to)-.74 F .866 (pointer to ...)72 418.4 R -.74('')-.7 G 3.366(\). By).74 F(con)3.366 E -.15(ve)-.4 G(ntion,).15 E F2(argc)3.366 E F0 .866 (is greater than zero; the \214rst ar)3.366 F .866(gument \(in)-.18 F F2 (argv[0]\))3.366 E F0 .865(is the com-)3.366 F(mand name itself.)72 430.4 Q(Here is a program that simply echoes its ar)97 446 Q(guments.) -.18 E F2 1.5(main\(argc, argv\))108 463 R 1.5(int argc;)130.5 475 R (char)130.5 487 Q F5(**)7.5 4.9 M F2 1.5(argv; {)-4.9 J 1.5(int i;)144 499 R 1.5(for\( i=1; i < argc; i)144 511 R F0(++)A F2(\))7.5 E 1.5 (printf\("%s ", argv[i]\);)180 523 R(putchar\()144 535 Q/F6 10/Symbol SF A F2(\\n)A F6A F2(\);)A(})108 547 Q F0 .245(Step by step:)72 564 R F2(main)2.745 E F0 .245(is called with tw)2.745 F 2.745(oa)-.1 G -.18 (rg)-2.745 G .245(uments, the ar).18 F .245 (gument count and the array of ar)-.18 F(guments.)-.18 E F2(argv)5.245 E F0(is)2.745 E 2.525(ap)72 576 S .024(ointer to an array)-2.525 F 2.524 (,w)-.65 G .024(hose indi)-2.524 F .024 (vidual elements are pointers to arrays of characters.)-.25 F .024 (The zeroth ar)5.024 F .024(gument is)-.18 F .794(the name of the comma\ nd itself, so we start to print with the \214rst ar)72 588 R .794 (gument, until we')-.18 F 1.095 -.15(ve p)-.5 H .795(rinted them all.) .15 F(Each)72 600 Q F2(argv[i])2.5 E F0(is a character array)2.5 E 2.5 (,s)-.65 G 2.5(ow)-2.5 G 2.5(eu)-2.5 G(se a)-2.5 E F2(`%s')2.5 E F0 (in the)2.5 E F2(printf)2.5 E F4(.)A F0 -1.1(Yo)97 615.6 S 2.5(uw)1.1 G (ill sometimes see the declaration of)-2.5 E F2(argv)2.5 E F0 (written as)2.5 E F2(char)108 632.6 Q F5(*)7.5 4.9 M F2 1.5(argv[ ];) -4.9 J F0 .082(which is equi)72 649.6 R -.25(va)-.25 G 2.582(lent. But) .25 F .082(we can')2.582 F 2.582(tu)-.18 G(se)-2.582 E F2 -3.418 (char argv[ ][ ],)2.582 F F0 .082(because both dimensions are v)2.582 F .081(ariable and there)-.25 F -.1(wo)72 661.6 S(uld be no w).1 E (ay to \214gure out ho)-.1 E 2.5(wb)-.25 G(ig the array is.)-2.5 E (Here')97 677.2 Q 2.508(sab)-.55 G .008(igger e)-2.508 F .008 (xample using)-.15 F F2(argc)2.508 E F0(and)2.508 E F2(argv)2.508 E F4 (.)A F0 2.508(Ac)5.008 G .008(ommon con)-2.508 F -.15(ve)-.4 G .009 (ntion in C programs is that if the).15 F 1.648(\214rst ar)72 689.2 R 1.648(gument is `)-.18 F F6(-)A F0 1.648 (', it indicates a \215ag of some sort.)B -.15(Fo)6.648 G 4.148(re).15 G 1.648(xample, suppose we w)-4.298 F 1.648(ant a program to be)-.1 F (callable as)72 701.2 Q F2(prog)108 718.2 Q F6(-)7.5 E F2 1.5 (abc arg1 arg2)B F4(...)7.5 E EP %%Page: 15 15 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF 3.125(CT)72 36 S 178.48(utorial -)-3.575 F .625 (15 -)3.125 F(where the `)72 84 Q/F1 10/Symbol SF(-)A F0 2.5('a)C -.18 (rg)-2.5 G(ument is optional; if it is present, it may be follo).18 E (wed by an)-.25 E 2.5(yc)-.15 G(ombination of a, b, and c.)-2.5 E/F2 10 /Courier@0 SF 1.5(main\(argc, argv\))108 101 R 1.5(int argc;)130.5 113 R (char)130.5 125 Q/F3 14/Courier@0 SF(**)7.5 4.9 M F2 1.5(argv; {)-4.9 J /F4 16/Courier@0 SF(...)144 137 Q F2 1.5(aflag = bflag = cflag)144 149 R 7.5(=0)15 G(;)-7.5 E 1.5(if\( argc > 1 && argv[1][0])144 161 R F0(==)7.5 E F17.5 E F2 7.5(\){)7.5 G 1.5(for\( i=1; \(c=argv[1][i]\) !=) 180 173 R F17.5 E F2(\\0)A F1A F2 7.5(;i)C F0(++)-7.5 E F2(\)) 7.5 E 1.5(if\( c)216 185 R F0(==)A F1A F2(a)A F1A F2(\))7.5 E (aflag)252 197 Q F0(++)A F2(;)A 1.5(else if\( c)216 209 R F0(==)A F1 A F2(b)A F1A F2(\))7.5 E(bflag)252 221 Q F0(++)A F2(;)A 1.5 (else if\( c)216 233 R F0(==)A F1A F2(c)A F1A F2(\))7.5 E(cflag) 252 245 Q F0(++)A F2(;)A(else)216 257 Q 1.5(printf\("%c?\\n", c\);)252 269 R F1(--)180 281 Q F2(argc;)A F0(++)180 293 Q F2(argv;)A(})144 305 Q F4(...)144 317 Q F0 .217(There are se)97 337.6 R -.15(ve)-.25 G .217 (ral things w).15 F .217(orth noticing about this code.)-.1 F .217 (First, there is a real need for the left-to-right)5.217 F -.25(eva)72 349.6 S .823(luation that && pro).25 F .823(vides; we don')-.15 F 3.323 (tw)-.18 G .823(ant to look at)-3.423 F F2(argv[1])3.323 E F0 .823 (unless we kno)3.323 F 3.323(wi)-.25 G(t')-3.323 E 3.323(st)-.55 G 3.323 (here. Second,)-3.323 F(the)3.322 E(statements)72 361.6 Q F1(--)144 378.6 Q F2(argc;)A F0(++)144 390.6 Q F2(argv;)A F0 .002 (let us march along the ar)72 407.6 R .003 (gument list by one position, so we can skip o)-.18 F -.15(ve)-.15 G 2.503(rt).15 G .003(he \215ag ar)-2.503 F .003(gument as if it had ne) -.18 F -.15(ve)-.25 G(r).15 E -.15(ex)72 419.6 S 1.053(isted _ the rest\ of the program is independent of whether or not there w).15 F 1.052 (as a \215ag ar)-.1 F 3.552(gument. This)-.18 F(only)3.552 E -.1(wo)72 431.6 S(rks because).1 E F2(argv)2.5 E F0 (is a pointer which can be incremented.)2.5 E/F5 10/Times-Bold@0 SF 2.5 (19. The)72 455.6 R(Switch Statement; Br)2.5 E(eak; Continue)-.18 E F0 (The)97 471.2 Q F2(switch)2.659 E F0 .159 (statement can be used to replace the multi-w)2.659 F .16 (ay test we used in the last e)-.1 F 2.66(xample. When)-.15 F (the tests are lik)72 483.2 Q 2.5(et)-.1 G(his:)-2.5 E F2 1.5(if\( c)108 500.2 R F0(==)7.5 E F17.5 E F2(a)A F1A F2(\))7.5 E F4(...)7.5 E F2 1.5(else if\( c)108 512.2 R F0(==)7.5 E F17.5 E F2(b)A F1A F2 (\))7.5 E F4(...)7.5 E F2 1.5(else if\( c)108 524.2 R F0(==)7.5 E F1 7.5 E F2(c)A F1A F2(\))7.5 E F4(...)7.5 E F2(else)108 536.2 Q F4 (...)7.5 E F0 .882(testing a v)72 553.2 R .882(alue ag)-.25 F .882 (ainst a series of)-.05 F/F6 10/Times-Italic@0 SF(constants,)3.382 E F0 .882(the switch statement is often clearer and usually gi)3.382 F -.15 (ve)-.25 G 3.381(sb).15 G(etter)-3.381 E 2.5(code. Use)72 565.2 R (it lik)2.5 E 2.5(et)-.1 G(his:)-2.5 E F2 1.5(switch\( c \) {)108 582.2 R(case)108 606.2 Q F17.5 E F2(a)A F1A F2(:)A(aflag)144 618.2 Q F0(++)A F2(;)A(break;)144 630.2 Q(case)108 642.2 Q F17.5 E F2(b)A F1 A F2(:)A(bflag)144 654.2 Q F0(++)A F2(;)A(break;)144 666.2 Q(case) 108 678.2 Q F17.5 E F2(c)A F1A F2(:)A(cflag)144 690.2 Q F0(++)A F2(;)A(break;)144 702.2 Q(default:)108 714.2 Q 1.5 (printf\("%c?\\n", c\);)144 726.2 R EP %%Page: 16 16 %%BeginPageSetup BP %%EndPageSetup /F0 10/Courier@0 SF 7.5(CT)72 36 S(utorial)-7.5 E/F1 10/Symbol SF(-) 153.51 E F0(16)7.5 E F1(-)7.5 E F0(break;)144 84 Q(})108 96 Q/F2 10 /Times-Roman@0 SF(The)72 113 Q F0(case)2.764 E F2 .264 (statements label the v)2.764 F .264(arious actions we w)-.25 F(ant;)-.1 E F0(default)2.764 E F2 .265(gets done if none of the other cases are) 2.764 F 3.071(satis\214ed. \(A)72 125 R F0(default)3.071 E F2 .571 (is optional; if it isn')3.071 F 3.071(tt)-.18 G .571 (here, and none of the cases match, you just f)-3.071 F .571 (all out the bot-)-.1 F(tom.\))72 137 Q(The)97 152.6 Q F0(break)3.152 E F2 .652(statement in this e)3.152 F .652(xample is ne)-.15 F 4.452 -.65 (w. I)-.25 H 3.152(ti).65 G 3.152(st)-3.152 G .652 (here because the cases are just labels, and after)-3.152 F .587 (you do one of them, you)72 164.6 R/F3 10/Times-Italic@0 SF .587 (fall thr)3.087 F(ough)-.45 E F2 .587(to the ne)3.087 F .587 (xt unless you tak)-.15 F 3.087(es)-.1 G .586(ome e)-3.087 F .586 (xplicit action to escape.)-.15 F .586(This is a)5.586 F(mix)72 176.6 Q .368(ed blessing.)-.15 F .369(On the positi)5.368 F .669 -.15(ve s)-.25 H .369(ide, you can ha).15 F .669 -.15(ve m)-.2 H .369 (ultiple cases on a single statement; we might w).15 F .369(ant to)-.1 F (allo)72 188.6 Q 2.5(wb)-.25 G(oth upper and lo)-2.5 E (wer case letters in our \215ag \214eld, so we could say)-.25 E F0(case) 108 205.6 Q F17.5 E F0(a)A F1A F0 15(:c)C(ase)-15 E F17.5 E F0(A)A F1A F0(:)A/F4 16/Courier@0 SF(...)32.12 E F0(case)108 222.6 Q F17.5 E F0(b)A F1A F0 15(:c)C(ase)-15 E F17.5 E F0(B)A F1 A F0(:)A F4(...)32.12 E F0(etc)115.5 234.6 Q F4(.)A F2 .549 (But what if we just w)72 251.6 R .548(ant to get out after doing)-.1 F F0 -2.952(case `a')3.048 F F2 5.548(?W)3.048 G 3.048(ec)-6.348 G .548 (ould get out of a)-3.048 F F0(case)3.048 E F2 .548(of the)3.048 F F0 (switch)3.048 E F2 .284(with a label and a)72 263.6 R F0(goto,)2.784 E F2 -.2(bu)2.784 G 2.784(tt).2 G .284(his is really ugly)-2.784 F 5.285 (.T)-.65 G(he)-5.285 E F0(break)2.785 E F2 .285(statement lets us e) 2.785 F .285(xit without either)-.15 F F0(goto)2.785 E F2(or)2.785 E (label.)72 275.6 Q F0 1.5(switch\( c \) {)108 292.6 R(case)108 316.6 Q F17.5 E F0(a)A F1A F0(:)A(aflag)144 328.6 Q F2(++)A F0(;)A (break;)144 340.6 Q(case)108 352.6 Q F17.5 E F0(b)A F1A F0(:)A (bflag)144 364.6 Q F2(++)A F0(;)A(break;)144 376.6 Q F4(...)120 388.6 Q F0(})108 400.6 Q(/)108 412.6 Q/F5 14/Courier@0 SF(*)4.9 I F0 1.5 (the break statements get us here directly)7.5 -4.9 N F5(*)7.5 4.9 M F0 (/)-4.9 I F2(The)72 429.6 Q F0(break)3.553 E F2 1.053(statement also w) 3.553 F 1.053(orks in)-.1 F F0(for)3.553 E F2(and)3.553 E F0(while)3.553 E F2 1.053(statements _ it causes an immediate e)3.553 F 1.053 (xit from the)-.15 F(loop.)72 441.6 Q(The)97 457.2 Q F0(continue)3.187 E F2 .687(statement w)3.187 F(orks)-.1 E F3(only)3.187 E F2(inside)3.187 E F0(for's)3.187 E F2(and)3.187 E F0(while's;)3.187 E F2 .687 (it causes the ne)3.187 F .688(xt iteration of)-.15 F .026 (the loop to be started.)72 469.2 R .026 (This means it goes to the increment part of the)5.026 F F0(for)2.525 E F2 .025(and the test part of the)2.525 F F0(while)2.525 E F4(.)A F2 1.72 -.8(We c)72 481.2 T .12(ould ha).8 F .42 -.15(ve u)-.2 H .12(sed a).15 F F0(continue)2.62 E F2 .12(in our e)2.62 F .12 (xample to get on with the ne)-.15 F .121(xt iteration of the)-.15 F F0 (for,)2.621 E F2 -.2(bu)2.621 G 2.621(ti).2 G 2.621(ts)-2.621 G(eems) -2.621 E(clearer to use)72 493.2 Q F0(break)2.5 E F2(instead.)2.5 E/F6 10/Times-Bold@0 SF 2.5(20. Structur)72 517.2 R(es)-.18 E F2 1.248(The m\ ain use of structures is to lump together collections of disparate v)97 532.8 R 1.247(ariable types, so the)-.25 F 3.747(yc)-.15 G(an)-3.747 E (con)72 544.8 Q -.15(ve)-.4 G .309(niently be treated as a unit.).15 F -.15(Fo)5.309 G 2.809(re).15 G .309 (xample, if we were writing a compiler or assembler)-2.959 F 2.809(,w) -.4 G 2.809(em)-2.809 G .309(ight need)-2.809 F .842 (for each identi\214er information lik)72 556.8 R 3.342(ei)-.1 G .842 (ts name \(a character array\), its source line number \(an inte)-3.342 F .841(ger\), some)-.15 F(type information \(a character)72 568.8 Q 2.5 (,p)-.4 G(erhaps\), and probably a usage count \(another inte)-2.5 E (ger\).)-.15 E F0 6(char id[10];)144 585.8 R 12(int line;)144 597.8 R 6 (char type;)144 609.8 R 12(int usage;)144 621.8 R F2 1.866 -.8(We c)97 642.4 T .266(an mak).8 F 2.766(eas)-.1 G .266 (tructure out of this quite easily)-2.766 F 5.266(.W)-.65 G 2.767<658c> -6.066 G .267(rst tell C what the structure will look lik)-2.767 F .267 (e, that)-.1 F .223(is, what kinds of things it contains; after that we\ can actually reserv)72 654.4 R 2.722(es)-.15 G .222 (torage for it, either in the same state-)-2.722 F(ment or separately)72 666.4 Q 5(.T)-.65 G (he simplest thing is to de\214ne it and allocate storage all at once:) -5 E F0 1.5(struct {)108 683.4 R 6(char id[10];)144 695.4 R 12 (int line;)144 707.4 R 6(char type;)144 719.4 R 12(int usage;)144 731.4 R EP %%Page: 17 17 %%BeginPageSetup BP %%EndPageSetup /F0 10/Courier@0 SF 7.5(CT)72 36 S(utorial)-7.5 E/F1 10/Symbol SF(-) 153.51 E F0(17)7.5 E F1(-)7.5 E F0 7.5(}s)108 84 S(ym;)-7.5 E/F2 10 /Times-Roman@0 SF .104(This de\214nes)97 104.6 R F0(sym)2.604 E F2 .105 (to be a structure with the speci\214ed shape;)2.604 F F0 -3.395 (id, line, type)2.605 F F2(and)2.605 E F0(usage)2.605 E F2(are)2.605 E /F3 10/Times-Italic@0 SF(mem-)2.605 E(ber)72 116.6 Q(s)-.1 E F2 (of the structure.)2.5 E(The w)5 E(ay we refer to an)-.1 E 2.5(yp)-.15 G (articular member of the structure is)-2.5 E F0(structure-name)108 133.6 Q/F4 16/Courier@0 SF(.)7.5 E F0(member)7.5 E F2(as in)72 150.6 Q F0(sym) 144 167.6 Q F4(.)A F0 1.5(type = 077;)B 1.5(if\( sym)144 179.6 R F4(.)A F0(usage)A F2(==)7.5 E F0 7.5(0\))7.5 G F4(...)A F0 1.5(while\( sym)144 191.6 R F4(.)A F0(id[j)A F2(++)A F0 7.5(]\))C F4(...)A F2(etc)153.375 203.6 Q/F5 16/Times-Roman@0 SF(.)A F2 .839 (Although the names of structure members ne)72 220.6 R -.15(ve)-.25 G 3.339(rs).15 G .839(tand alone, the)-3.339 F 3.339(ys)-.15 G .839 (till ha)-3.339 F 1.139 -.15(ve t)-.2 H 3.339(ob).15 G 3.339(eu)-3.339 G .839(nique _ there can')-3.339 F 3.338(tb)-.18 G(e)-3.338 E(another)72 232.6 Q F0(id)2.5 E F2(or)2.5 E F0(usage)2.5 E F2 (in some other structure.)2.5 E .575(So f)97 248.2 R .575(ar we ha)-.1 F -.15(ve)-.2 G(n').15 E 3.075(tg)-.18 G .575(ained much.)-3.125 F .575 (The adv)5.575 F .575(antages of structures start to come when we ha) -.25 F .876 -.15(ve a)-.2 H .576(rrays of).15 F 1.004 (structures, or when we w)72 260.2 R 1.003 (ant to pass complicated data layouts between functions.)-.1 F 1.003 (Suppose we w)6.003 F 1.003(anted to)-.1 F(mak)72 272.2 Q 2.5(eas)-.1 G (ymbol table for up to 100 identi\214ers.)-2.5 E 1.6 -.8(We c)5 H (ould e).8 E(xtend our de\214nitions lik)-.15 E(e)-.1 E F0 6 (char id[100][10];)144 289.2 R 12(int line[100];)144 301.2 R 6 (char type[100];)144 313.2 R 12(int usage[100];)144 325.2 R F2 -.2(bu)72 342.2 S 2.595(tas).2 G .096(tructure lets us rearrange this spread-out \ information so all the data about a single identifer is collect-)-2.595 F(ed into one lump:)72 354.2 Q F0 1.5(struct {)108 371.2 R 6 (char id[10];)144 383.2 R 12(int line;)144 395.2 R 6(char type;)144 407.2 R 12(int usage;)144 419.2 R 7.5(}s)108 431.2 S(ym[100];)-7.5 E F2 .835(This mak)72 448.2 R(es)-.1 E F0(sym)3.335 E F2 .835 (an array of structures; each array element has the speci\214ed shape.) 3.335 F(No)5.835 E 3.335(ww)-.25 G 3.335(ec)-3.335 G .835(an refer to) -3.335 F(members as)72 460.2 Q F0(sym[i])144 477.2 Q F4(.)A F0(usage)A F2(++)A F0 15.12(;/)C/F6 14/Courier@0 SF(*)-15.12 4.9 M F0 1.5 (increment usage of i-th identifier)7.5 -4.9 N F6(*)7.5 4.9 M F0(/)-4.9 I 1.5(for\( j=0; sym[i])144 489.2 R F4(.)A F0(id[j)A F2(++)A F0 7.5(]!)C (=)-7.5 E F17.5 E F0(\\0)A F1A F0 7.5(;\))C F4(...)A F2(etc) 153.375 501.2 Q F5(.)A F2 (Thus to print a list of all identi\214ers that ha)72 518.2 Q -.15(ve) -.2 G(n').15 E 2.5(tb)-.18 G(een used, together with their line number) -2.5 E(,)-.4 E F0 1.5(for\( i=0; i= 0 \))144 84 R(sym[index])180 96 Q /F2 16/Courier@0 SF(.)A F0(usage)A/F3 10/Times-Roman@0 SF(++)A F0 63.12 (;/)C/F4 14/Courier@0 SF(*)-63.12 4.9 M F0 1.5(already there)7.5 -4.9 N F2(...)7.5 E F4(*)7.5 4.9 M F0(/)-4.9 I(else)144 108 Q 1.5 (install\(newname, newline, newtype\);)180 120 R F2(...)144 132 Q F0(}) 108 144 Q(lookup\(s\))108 161 Q(char)130.5 173 Q F4(*)7.5 4.9 M F0 1.5 (s; {)-4.9 J 1.5(int i;)144 185 R 1.5(extern struct {)144 197 R 6 (char id[10];)180 209 R 12(int line;)180 221 R 6(char type;)180 233 R 12 (int usage;)180 245 R 7.5(}s)144 257 S 1.5(ym[ ];)-7.5 F 1.5 (for\( i=0; i 0 \))B (return\(i\);)216 298 Q(return\()144 310 Q F1(-)A F0(1\);)A(})108 322 Q 60(compar\(s1,s2\) /)108 346 R F4(*)4.9 I F0 1.5(return 1 if s1)15 -4.9 N F3(==)A F0 1.5(s2, 0 otherwise)B F4(*)7.5 4.9 M F0(/)-4.9 I(char)130.5 358 Q F4(*)7.5 4.9 M F0(s1,)-4.9 I F4(*)7.5 4.9 M F0 1.5(s2; {)-4.9 J (while\()144 370 Q F4(*)7.5 4.9 M F0(s1)-4.9 I F3 5(++ ==)B F4(*)7.5 4.9 M F0 1.5(s2 \))-4.9 J(if\()180 382 Q F4(*)7.5 4.9 M F0(s2)-4.9 I F3 5 (++ ==)B F17.5 E F0(\\0)A F1A F0(\))7.5 E(return\(1\);)216 394 Q (return\(0\);)144 406 Q(})108 418 Q F3 1.046 (The declaration of the structure in)72 435 R F0(lookup)3.546 E F3(isn') 3.546 E 3.546(tn)-.18 G 1.047(eeded if the e)-3.546 F 1.047 (xternal de\214nition precedes its use in the)-.15 F (same source \214le, as we shall see in a moment.)72 447 Q(No)97 462.6 Q 2.5(ww)-.25 G(hat if we w)-2.5 E(ant to use pointers?)-.1 E F0 9 (struct symtag)108 479.6 R({)7.5 E 6(char id[10];)144 491.6 R 12 (int line;)144 503.6 R 6(char type;)144 515.6 R 12(int usage;)144 527.6 R 7.5(}s)108 539.6 S(ym[100],)-7.5 E F4(*)7.5 4.9 M F0(psym;)-4.9 I 1.5 (psym = &sym[0];)144 563.6 R(/)15 E F4(*)4.9 I F0 1.5(or p = sym;)7.5 -4.9 N F4(*)7.5 4.9 M F0(/)-4.9 I F3 .198(This mak)72 580.6 R(es)-.1 E F0(psym)2.698 E F3 2.698(ap)2.698 G .197(ointer to our kind of structur\ e \(the symbol table\), then initializes it to point to the \214rst) -2.698 F(element of)72 592.6 Q F0(sym)2.5 E F2(.)A F3 1.218 (Notice that we added something after the w)97 608.2 R(ord)-.1 E F0 (struct:)3.718 E F3 3.718(a`)3.718 G(`tag')-4.458 E 3.718('c)-.74 G (alled)-3.718 E F0(symtag)3.718 E F2(.)A F3 1.218(This puts a)6.218 F .547(name on our structure de\214nition so we can refer to it later wit\ hout repeating the de\214nition.)72 620.2 R(It')5.546 E 3.046(sn)-.55 G .546(ot neces-)-3.046 F(sary b)72 632.2 Q(ut useful.)-.2 E(In f)5 E (act we could ha)-.1 E .3 -.15(ve s)-.2 H(aid).15 E F0 30(struct symtag) 108 649.2 R({)7.5 E F2(...)144 661.2 Q F0 1.5(structure definition)7.5 F (};)108 673.2 Q F3(which w)72 690.2 Q(ouldn')-.1 E 2.5(th)-.18 G -2.25 -.2(av e)-2.5 H(assigned an)2.7 E 2.5(ys)-.15 G (torage at all, and then said)-2.5 E F0 30(struct symtag sym[100];)108 707.2 R 30(struct symtag)108 719.2 R F4(*)36 4.9 M F0(psym;)-4.9 I EP %%Page: 19 19 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF 3.125(CT)72 36 S 178.48(utorial -)-3.575 F .625 (19 -)3.125 F(which w)72 84 Q(ould de\214ne the array and the pointer) -.1 E 5(.T)-.55 G(his could be condensed further)-5 E 2.5(,t)-.4 G(o) -2.5 E/F1 10/Courier@0 SF 30(struct symtag sym[100],)108 101 R/F2 14 /Courier@0 SF(*)7.5 4.9 M F1(psym;)-4.9 I F0(The w)97 121.6 Q (ay we actually refer to an member of a structure by a pointer is lik) -.1 E 2.5(et)-.1 G(his:)-2.5 E F1(ptr)144 138.6 Q/F3 10/Symbol SF(-)7.5 E F1 7.5(>s)C(tructure-member)-7.5 E F0 .039(The symbol `)72 155.6 R F3 (-)A F0 .039(>' means we')B .039 (re pointing at a member of a structure; `)-.5 F F3(-)A F0 .039 (>' is only used in that conte)B(xt.)-.15 E F1(ptr)5.039 E F0(is)2.539 E 3.375(ap)72 167.6 S .875(ointer to the \(base of\) a structure that con\ tains the structure member)-3.375 F 5.874(.T)-.55 G .874(he e)-5.874 F (xpression)-.15 E F1(ptr)3.374 E F3(-)A F1(>struc-)A(ture-member)72 179.6 Q F0 1.241 (refers to the indicated member of the pointed-to structure.)3.741 F 1.242(Thus we ha)6.242 F 1.542 -.15(ve c)-.2 H(onstructions).15 E(lik)72 191.6 Q(e:)-.1 E F1(psym)108 208.6 Q F3(-)A F1 1.5(>type = 1;)B(psym)108 220.6 Q F3(-)A F1 1.5(>id[0] =)B F37.5 E F1(a)A F3A F1(;)A F0 (and so on.)72 237.6 Q -.15(Fo)97 253.2 S 2.532(rm).15 G .032 (ore complicated pointer e)-2.532 F .032(xpressions, it')-.15 F 2.532 (sw)-.55 G .032(ise to use parentheses to mak)-2.532 F 2.532(ei)-.1 G 2.532(tc)-2.532 G .031(lear who goes with)-2.532 F 2.5(what. F)72 265.2 R(or e)-.15 E(xample,)-.15 E F1 1.5(struct { int x,)108 282.2 R F2(*)7.5 4.9 M F1 1.5(y; })-4.9 J F2(*)7.5 4.9 M F1(p;)-4.9 I(p)108 294.2 Q F3(-) A F1(>x)A F0(++)A F1 1.5(increments x)1.23 F F0(++)108 306.2 Q F1(p)A F3 (-)A F1 -4.77(>x so)B 1.5(does this!)7.5 F(\()108 318.2 Q F0(++)A F1 (p\))A F3(-)A F1 19.23(>x increments)B 7.5(pb)7.5 G 1.5(efore getting x) -7.5 F F2(*)108 335.1 Q F1(p)-4.9 I F3(-)A F1(>y)A F0(++)A F1 1.5 (uses y as a pointer, then increments it)28.83 F F2(*)108 347.1 Q F1 (\(p)-4.9 I F3(-)A F1(>y\))A F0(++)A F1 1.5(so does this)16.83 F F2(*) 108 359.1 Q F1(\(p)-4.9 I F0(++)A F1(\))A F3(-)A F1 10.83(>y uses)B 7.5 (ya)7.5 G 7.5(sap)-7.5 G 1.5(ointer, then increments p)-7.5 F F0 .376 (The w)72 371.2 R .376(ay to remember these is that)-.1 F F3(-)2.876 E F1 -3.124(>, .)B F0(\(dot\),)5.376 E F1 6.377(\(\))2.876 G F0(and)-3.5 E F1 6.377([])2.877 G F0 .377(bind v)-3.5 F .377(ery tightly)-.15 F 5.377 (.A)-.65 G 2.877(ne)-5.377 G .377(xpression in)-3.027 F -.2(vo)-.4 G (lving).2 E(one of these is treated as a unit.)72 383.2 Q F1(p)5 E F3(-) A F1 -3.5(>x, a[i], y)B/F4 16/Courier@0 SF(.)A F1(x)A F0(and)2.5 E F1 (f\(b\))2.5 E F0(are names e)2.5 E(xactly as)-.15 E F1(abc)2.5 E F0(is.) 2.5 E(If)97 398.8 Q F1(p)2.878 E F0 .378 (is a pointer to a structure, an)2.878 F 2.878(ya)-.15 G .378 (rithmetic on)-2.878 F F1(p)2.877 E F0(tak)2.877 E .377 (es into account the acutal size of the structure.)-.1 F -.15(Fo)72 410.8 S 3.561(ri).15 G(nstance,)-3.561 E F1(p)3.561 E F0 1.061 (++ increments)B F1(p)3.561 E F0 1.061 (by the correct amount to get the ne)3.561 F 1.061 (xt element of the array of structures.)-.15 F .252(But don')72 422.8 R 2.752(ta)-.18 G .252(ssume that the size of a structure is the sum of t\ he sizes of its members _ because of alignments)-2.752 F(of dif)72 434.8 Q(ferent sized objects, there may be `)-.25 E(`holes')-.74 E 2.5('i)-.74 G 2.5(nas)-2.5 G(tructure.)-2.5 E(Enough theory)97 450.4 Q 2.5(.H)-.65 G (ere is the lookup e)-2.5 E(xample, this time with pointers.)-.15 E F1 1.5(struct symtag {)108 467.4 R 6(char id[10];)144 479.4 R 12(int line;) 144 491.4 R 6(char type;)144 503.4 R 12(int usage;)144 515.4 R 7.5(}s) 108 527.4 S(ym[100];)-7.5 E 1.5(main\( \) {)108 544.4 R 1.5 (struct symtag)144 556.4 R F2(*)7.5 4.9 M F1 1.5(lookup\( \);)-4.9 J 1.5 (struct symtag)144 568.4 R F2(*)7.5 4.9 M F1(psym;)-4.9 I F4(...)144 580.4 Q F1 1.5(if\( \(psym = lookup\(newname\)\) \))144 592.4 R(/)30 E F2(*)4.9 I F1(non)7.5 -4.9 M F3(-)A F1 1.5(zero pointer)B F2(*)7.5 4.9 M F1(/)-4.9 I(psym)180 604.4 Q F3(-)7.5 E F1 7.5(>u)C(sage)-7.5 E F0(++)A F1 46.23(;/)C F2(*)-46.23 4.9 M F1 1.5(means already there)7.5 -4.9 N F2 (*)7.5 4.9 M F1(/)-4.9 I(else)144 616.4 Q 1.5 (install\(newname, newline, newtype\);)180 628.4 R F4(...)144 640.4 Q F1 (})108 652.4 Q 1.5(struct symtag)108 669.4 R F2(*)7.5 4.9 M F1 (lookup\(s\))-4.9 I(char)130.5 681.4 Q F2(*)7.5 4.9 M F1 1.5(s; {)-4.9 J 1.5(struct symtag)144 693.4 R F2(*)7.5 4.9 M F1(p;)-4.9 I 1.5 (for\( p=sym; p < &sym[nsym]; p)144 705.4 R F0(++)A F1(\))7.5 E 1.5 (if\( compar\(s, p)180 717.4 R F3(-)A F1 1.5(>id\) > 0\))B(return\(p\);) 216 729.4 Q EP %%Page: 20 20 %%BeginPageSetup BP %%EndPageSetup /F0 10/Courier@0 SF 7.5(CT)72 36 S(utorial)-7.5 E/F1 10/Symbol SF(-) 153.51 E F0(20)7.5 E F1(-)7.5 E F0(return\(0\);)144 84 Q(})108 96 Q/F2 10/Times-Roman@0 SF(The function)72 113 Q F0(compar)2.5 E F2(doesn')2.5 E 2.5(tc)-.18 G(hange:)-2.5 E F0(`p)2.5 E F1(-)A F0(>id')A F2 (refers to a string.)2.5 E(In)97 128.6 Q F0(main)2.704 E F2 .204 (we test the pointer returned by)2.704 F F0(lookup)2.704 E F2(ag)2.704 E .204(ainst zero, relying on the f)-.05 F .205(act that a pointer is by) -.1 F(de\214nition ne)72 140.6 Q -.15(ve)-.25 G 2.5(rz).15 G (ero when it really points at something.)-2.5 E (The other pointer manipulations are tri)5 E(vial.)-.25 E (The only comple)97 156.2 Q(xity is the set of lines lik)-.15 E(e)-.1 E F0 1.5(struct symtag)108 173.2 R/F3 14/Courier@0 SF(*)7.5 4.9 M F0 1.5 (lookup\( \);)-4.9 J F2 .129(This brings us to an area that we will tre\ at only hurriedly _ the question of function types.)72 190.2 R .128 (So f)5.128 F(ar)-.1 E 2.628(,a)-.4 G .128(ll of our)-2.628 F .35 (functions ha)72 202.2 R .65 -.15(ve r)-.2 H .35(eturned inte).15 F .35 (gers \(or characters, which are much the same\).)-.15 F .351 (What do we do when the func-)5.351 F .088 (tion returns something else, lik)72 214.2 R 2.588(eap)-.1 G .087 (ointer to a structure?)-2.588 F .087(The rule is that an)5.087 F 2.587 (yf)-.15 G .087(unction that doesn')-2.587 F 2.587(tr)-.18 G .087 (eturn an)-2.587 F F0(int)72 226.2 Q F2 .493(has to say e)2.992 F .493 (xplicitly what it does return.)-.15 F .493 (The type information goes before the function name \(which)5.493 F (can mak)72 238.2 Q 2.5(et)-.1 G(he name hard to see\).)-2.5 E (Examples:)5 E F0 1.5(char f\(a\))108 255.2 R 1.5(int a; {)130.5 267.2 R /F4 16/Courier@0 SF(...)144 279.2 Q F0(})108 291.2 Q(int)108 315.2 Q F3 (*)7.5 4.9 M F0 1.5(g\( \) {)-4.9 J F4(...)7.5 E F0(})7.5 E 1.5 (struct symtag)108 339.2 R F3(*)7.5 4.9 M F0 1.5(lookup\(s\) char)-4.9 J F3(*)7.5 4.9 M F0 1.5(s; {)-4.9 J F4(...)7.5 E F0(})7.5 E F2 1.158 (The function)72 356.2 R F0(f)3.658 E F2 1.158(returns a character)3.658 F(,)-.4 E F0(g)3.658 E F2 1.157(returns a pointer to an inte)3.658 F (ger)-.15 E 3.657(,a)-.4 G(nd)-3.657 E F0(lookup)3.657 E F2 1.157 (returns a pointer to a)3.657 F .702(structure that looks lik)72 368.2 R (e)-.1 E F0(symtag)3.202 E F4(.)A F2 .702(And if we')5.702 F .703 (re going to use one of these functions, we ha)-.5 F 1.003 -.15(ve t)-.2 H 3.203(om).15 G(ak)-3.203 E 3.203(ea)-.1 G (declaration where we use it, as we did in)72 380.2 Q F0(main)2.5 E F2 (abo)2.5 E -.15(ve)-.15 G(.).15 E (Notice th parallelism between the declarations)97 395.8 Q F0 1.5 (struct symtag)144 412.8 R F3(*)7.5 4.9 M F0 1.5(lookup\( \);)-4.9 J 1.5 (struct symtag)144 424.8 R F3(*)7.5 4.9 M F0(psym;)-4.9 I F2 .767(In ef) 72 441.8 R .767(fect, this says that)-.25 F F0(lookup\( \))3.266 E F2 (and)3.266 E F0(psym)3.266 E F2 .766(are both used the same w)3.266 F .766(ay _ as a pointer to a strcture _)-.1 F -2.15 -.25(ev e)72 453.8 T 2.5(nt).25 G(hough one is a v)-2.5 E (ariable and the other is a function.)-.25 E/F5 10/Times-Bold@0 SF 2.5 (21. Initialization)72 477.8 R(of V)2.5 E(ariables)-.92 E F2 .079(An e) 97 493.4 R .079(xternal v)-.15 F .079 (ariable may be initialized at compile time by follo)-.25 F .08 (wing its name with an initializing v)-.25 F(al-)-.25 E .911 (ue when it is de\214ned.)72 505.4 R .911(The initializing v)5.911 F .911(alue has to be something whose v)-.25 F .91(alue is kno)-.25 F .91 (wn at compile time,)-.25 F(lik)72 517.4 Q 2.5(eac)-.1 G(onstant.)-2.5 E F0 12(int x)108 534.4 R 18(0; /)30 F F3(*)4.9 I F0 1.5 ("0" could be any constant)7.5 -4.9 N F3(*)7.5 4.9 M F0(/)-4.9 I 12 (int a)108 546.4 R F130 E F0(a)A F1A F0(;)A 6(char flag 0177;) 108 558.4 R(int)108 570.4 Q F3(*)18 4.9 M F0 21.6(p&)-4.9 K 30(y[1]; /) -21.6 F F3(*)4.9 I F0 7.5(pn)7.5 -4.9 O 1.5(ow points to y[1])-7.5 F F3 (*)7.5 4.9 M F0(/)-4.9 I F2(An e)72 587.4 Q (xternal array can be initialized by follo)-.15 E (wing its name with a list of initializations enclosed in braces:)-.25 E F0 12(int x[4])108 604.4 R 42({0,1,2,3}; /)12 F F3(*)4.9 I F0 1.5 (makes x[i] = i)7.5 -4.9 N F3(*)7.5 4.9 M F0(/)-4.9 I 12(int y[)108 616.4 R 10.5(]{)7.5 G 42(0,1,2,3}; /)-10.5 F F3(*)4.9 I F0 1.5 (makes y big enough for 4 values)7.5 -4.9 N F3(*)7.5 4.9 M F0(/)-4.9 I (char)108 628.4 Q F3(*)12 4.9 M F0 3.6(msg "syntax)-4.9 J -1.5 (error\\n"; /)7.5 F F3(*)4.9 I F0 1.5(braces unnecessary here)7.5 -4.9 N F3(*)7.5 4.9 M F0(/)-4.9 I(char)108 640.4 Q F3(*)7.5 4.9 M F0 1.5 (keyword[ ]{)-4.9 J("if",)144 652.4 Q("else",)144 664.4 Q("for",)144 676.4 Q("while",)144 688.4 Q("break",)144 700.4 Q("continue",)144 712.4 Q(0)144 724.4 Q EP %%Page: 21 21 %%BeginPageSetup BP %%EndPageSetup /F0 10/Courier@0 SF 7.5(CT)72 36 S(utorial)-7.5 E/F1 10/Symbol SF(-) 153.51 E F0(21)7.5 E F1(-)7.5 E F0(};)108 84 Q/F2 10/Times-Roman@0 SF .174(This last one is v)72 101 R .174(ery useful _ it mak)-.15 F(es)-.1 E F0(keyword)2.674 E F2 .175 (an array of pointers to character strings, with a zero at the)2.674 F .079(end so we can identify the last element easily)72 113 R 5.079(.A) -.65 G .078 (simple lookup routine could scan this until it either \214nds a)-2.5 F (match or encounters a zero k)72 125 Q -.15(ey)-.1 G -.1(wo).15 G (rd pointer:).1 E F0 36(lookup\(str\) /)108 142 R/F3 14/Courier@0 SF(*) 4.9 I F0 1.5(search for str in keyword[ ])7.5 -4.9 N F3(*)7.5 4.9 M F0 (/)-4.9 I(char)130.5 154 Q F3(*)7.5 4.9 M F0 1.5(str; {)-4.9 J 1.5 (int i,j,r;)144 166 R 1.5(for\( i=0; keyword[i] != 0; i)144 178 R F2(++) A F0 7.5(\){)C 1.5(for\( j=0; \(r=keyword[i][j]\))180 190 R F2(==)7.5 E F0 1.5(str[j] && r !=)7.5 F F17.5 E F0(\\0)A F1A F0 7.5(;j)C F2 (++)-7.5 E F0(\);)7.5 E 1.5(if\( r)180 202 R F2(==)7.5 E F0 1.5 (str[j] \))7.5 F(return\(i\);)216 214 Q(})144 226 Q(return\()144 238 Q F1(-)A F0(1\);)A(})108 250 Q F2(Sorry _ neither local v)97 270.6 Q (ariables nor structures can be initialized.)-.25 E/F4 10/Times-Bold@0 SF 2.5(22. Scope)72 294.6 R(Rules: Who Kno)2.5 E(ws About What)-.1 E F2 2.841(Ac)97 310.2 S .341 (omplete C program need not be compiled all at once; the source te) -2.841 F .342(xt of the program may be k)-.15 F(ept)-.1 E .933(in se)72 322.2 R -.15(ve)-.25 G .933(ral \214les, and pre).15 F .933 (viously compiled routines may be loaded from libraries.)-.25 F(Ho)5.932 E 3.432(wd)-.25 G 3.432(ow)-3.432 G 3.432(ea)-3.432 G .932(rrange that) -3.432 F 1(data gets passed from one routine to another?)72 334.2 R 2.601 -.8(We h)6.001 H -2.25 -.2(av e).8 H 1.001(already seen ho)3.701 F 3.501(wt)-.25 G 3.501(ou)-3.501 G 1.001(se function ar)-3.501 F 1.001 (guments and)-.18 F -.25(va)72 346.2 S .446 (lues, so let us talk about e).25 F .446(xternal data.)-.15 F -.8(Wa) 5.446 G .446(rning: the w).8 F(ords)-.1 E/F5 10/Times-Italic@0 SF (declar)2.945 E(ation)-.15 E F2(and)2.945 E F5(de\214nition)2.945 E F2 .445(are used precisely)2.945 F(in this section; don')72 358.2 Q 2.5(tt) -.18 G(reat them as the same thing.)-2.5 E 3.024(Am)97 373.8 S .524 (ajor shortcut e)-3.024 F .524(xists for making)-.15 F F0(extern)3.024 E F2 3.024(declarations. If)3.024 F .524(the de\214nition of a v)3.024 F .524(ariable appears)-.25 F F5(be-)3.024 E(for)72 385.8 Q(e)-.37 E F2 (its use in some function, no)2.5 E F0(extern)2.5 E F2 (declaration is needed within the function.)2.5 E (Thus, if a \214le contains)5 E F0 1.5(f1\( \) {)108 402.8 R/F6 16 /Courier@0 SF(...)7.5 E F0(})7.5 E 1.5(int foo;)108 419.8 R 1.5 (f2\( \) {)108 436.8 R F6(...)7.5 E F0 1.5(foo = 1;)7.5 F F6(...)7.5 E F0(})7.5 E 1.5(f3\( \) {)108 453.8 R F6(...)7.5 E F0 1.5(if \( foo \)) 7.5 F F6(...)7.5 E F0(})7.5 E F2 .402(no declaration of)72 470.8 R F0 (foo)2.902 E F2 .402(is needed in either)2.902 F F0(f2)2.902 E F2 .402 (or or)2.902 F F0(f3,)2.902 E F2 .401(because the e)2.901 F .401 (xternal de\214nition of)-.15 F F0(foo)2.901 E F2 .401(appears be-)2.901 F(fore them.)72 482.8 Q(But if)5 E F0(f1)2.5 E F2 -.1(wa)2.5 G (nts to use).1 E F0(foo,)2.5 E F2(it has to contain the declaration)2.5 E F0 1.5(f1\( \) {)108 499.8 R 1.5(extern int foo;)144 511.8 R F6(...) 144 523.8 Q F0(})108 535.8 Q F2 .281(This is true also of an)97 556.4 R 2.782(yf)-.15 G .282(unction that e)-2.782 F .282 (xists on another \214le _ if it w)-.15 F(ants)-.1 E F0(foo)2.782 E F2 .282(it has to use an)2.782 F F0(extern)2.782 E F2 .227 (declaration for it.)72 568.4 R .227(\(If some)5.227 F .227 (where there is an)-.25 F F0(extern)2.727 E F2 .226 (declaration for something, there must also e)2.727 F -.15(ve)-.25 G (ntually).15 E(be an e)72 580.4 Q(xternal de\214nition of it, or you') -.15 E(ll get an `)-.1 E(`unde\214ned symbol')-.74 E 2.5('m)-.74 G (essage.\))-2.5 E .005(There are some hidden pitf)97 596 R .005 (alls in e)-.1 F .006(xternal declarations and de\214nitions if you use\ multiple source \214les.)-.15 F 1.6 -.8(To a)72 608 T -.2(vo).6 G (id them, \214rst, de\214ne and initialize each e).2 E(xternal v)-.15 E (ariable only once in the entire set of \214les:)-.25 E F0 12 (int foo 0;)108 625 R F2 -1.1(Yo)72 642 S 3.733(uc)1.1 G 1.233(an get a) -3.733 F -.1(wa)-.15 G 3.733(yw).1 G 1.233(ith multiple e)-3.733 F 1.233 (xternal de\214nitions on)-.15 F/F7 8/Times-Roman@0 SF(UNIX,)3.733 E F2 -.2(bu)3.733 G 3.733(tn).2 G 1.233(ot on)-3.733 F F7(GCOS,)3.733 E F2 1.233(so don')3.733 F 3.733(ta)-.18 G 1.232(sk for trouble.)-3.733 F .547(Multiple initializations are ille)72 654 R -.05(ga)-.15 G 3.048(le) .05 G -.15(ve)-3.298 G 3.048(rywhere. Second,).15 F .548(at the be)3.048 F .548(ginning of an)-.15 F 3.048<798c>-.15 G .548 (le that contains functions)-3.048 F 1.278(needing a v)72 666 R 1.278 (ariable whose de\214nition is in some other \214le, put in an)-.25 F F0 (extern)3.777 E F2 1.277(declaration, outside of an)3.777 F(y)-.15 E (function:)72 678 Q F0 30(extern int)108 695 R(foo;)18 E 1.5(f1\( \) {) 108 712 R F6(...)7.5 E F0(})7.5 E(etc)130.5 724 Q F6(.)A EP %%Page: 22 22 %%BeginPageSetup BP %%EndPageSetup /F0 10/Courier@0 SF 7.5(CT)72 36 S(utorial)-7.5 E/F1 10/Symbol SF(-) 153.51 E F0(22)7.5 E F1(-)7.5 E/F2 10/Times-Roman@0 SF(The)97 92.6 Q F0 (#include)2.682 E F2 .182 (compiler control line, to be discussed shortly)2.682 F 2.682(,l)-.65 G .182(ets you mak)-2.682 F 2.683(eas)-.1 G .183(ingle cop)-2.683 F 2.683 (yo)-.1 G 2.683(ft)-2.683 G .183(he e)-2.683 F(x-)-.15 E(ternal declara\ tions for a program and then stick them into each of the source \214les\ making up the program.)72 104.6 Q/F3 10/Times-Bold@0 SF 2.5 (23. #de\214ne,)72 128.6 R(#include)2.5 E F2 2.5(Cp)97 144.2 S(ro)-2.5 E (vides a v)-.15 E(ery limited macro f)-.15 E(acility)-.1 E 5(.Y)-.65 G (ou can say)-6.1 E F0 24(#define name)108 161.2 R(something)48 E F2 .479 (and thereafter an)72 178.2 R .479(ywhere `)-.15 F(`name')-.74 E 2.979 ('a)-.74 G .479(ppears as a tok)-2.979 F .479(en, `)-.1 F(`something') -.74 E 2.978('w)-.74 G .478(ill be substituted.)-2.978 F .478 (This is particularly)5.478 F (useful in parametering the sizes of arrays:)72 190.2 Q F0 24 (#define ARRAYSIZE)108 207.2 R(100)18 E 12(int arr[ARRAYSIZE];)144 219.2 R/F4 16/Courier@0 SF(...)151.5 231.2 Q F0 1.5(while\( i)144 243.2 R F2 (++)A F0 7.5(72 527.4 S .225(le to be compiled.)-2.725 F .225(As with)5.225 F F0(#define,) 2.725 E F2 .225(the \214rst line of a \214le containing a)2.725 F F0 (#include)2.725 E F2 .225(has to be)2.725 F .225(gin with)-.15 F 2.5(a`) 72 539.4 S 2.5(#'. And)-2.5 F F0(#include)2.5 E F2(can')2.5 E 2.5(tb) -.18 G 2.5(en)-2.5 G(ested _ an included \214le can')-2.5 E 2.5(tc)-.18 G(ontain another)-2.5 E F0(#include)2.5 E F4(.)A F3 2.5(24. Bit)72 563.4 R(Operators)2.5 E F2 2.5(Ch)97 579 S(as se)-2.5 E -.15(ve)-.25 G (ral operators for logical bit-operations.).15 E -.15(Fo)5 G 2.5(re).15 G(xample,)-2.65 E F0 7.5(x=x&0)108 596 S(177;)-7.5 E F2 .326 (forms the bit-wise)72 613 R/F7 8/Times-Roman@0 SF(AND)2.825 E F2(of) 2.825 E F0(x)2.825 E F2 .325(and 0177, ef)2.825 F(fecti)-.25 E -.15(ve) -.25 G .325(ly retaining only the last se).15 F -.15(ve)-.25 G 2.825(nb) .15 G .325(its of)-2.825 F F0(x)2.825 E F4(.)A F2 .325(Other operators) 5.325 F(are)72 625 Q 109 635.25 109 642 DL(inclusi)144 642 Q .925 -.15 (ve O)-.25 H(R).15 E 32.67(^\()108 654 S(circumfle)-32.67 E .625(x\) e) -.15 F(xclusi)-.15 E .925 -.15(ve O)-.25 H(R).15 E 32.67(~\()108 666 S .625(tilde\) 1)-32.67 F F1A F2 3.125(sc)C(omplement)-3.125 E 32.67 (!l)108 678 S .625(ogical NO)-32.67 F(T)-.4 E 22.22(<< left)108 690 R .625(shift \(as in x<<2\))3.125 F 22.22(>> right)108 702 R 29.705 (shift \(arithmetic)3.125 F .625(on PDP-11; logical on H6070, IBM360\)) 3.125 F EP %%Page: 23 23 %%BeginPageSetup BP %%EndPageSetup /F0 10/Times-Roman@0 SF 2.5(CT)72 36 S 179.73(utorial -)-2.95 F(23 -)2.5 E/F1 10/Times-Bold@0 SF 2.5(25. Assignment)72 84 R(Operators)2.5 E F0 .521(An unusual feature of C is that the normal binary operators lik)97 99.6 R 3.021(e`)-.1 G .521(+', `)-3.021 F/F2 10/Symbol SF(-)A F0 .521 (', etc.)B .522(can be combined with)5.521 F (the assignment operator `=' to form ne)72 111.6 Q 2.5(wa)-.25 G (ssignment operators.)-2.5 E -.15(Fo)5 G 2.5(re).15 G(xample,)-2.65 E/F3 10/Courier@0 SF 7.5(x=)108 128.6 S F2(-)-7.5 E F3(10;)7.5 E F0 (uses the assignment operator `=)72 145.6 Q F2(-)A F0 2.5('t)C 2.5(od) -2.5 G(ecrement)-2.5 E F3(x)2.5 E F0(by 10, and)2.5 E F3 7.5(x=)108 162.6 S 7.5(&0)-7.5 G(177)-7.5 E F0 .289(forms the)72 179.6 R/F4 8 /Times-Roman@0 SF(AND)2.789 E F0(of)2.789 E F3(x)2.789 E F0 .289 (and 0177.)2.789 F .289(This con)5.289 F -.15(ve)-.4 G .288 (ntion is a useful notational shortcut, particularly if).15 F F3(x)2.788 E F0 .288(is a compli-)2.788 F(cated e)72 191.6 Q 2.5(xpression. The) -.15 F(classic e)2.5 E(xample is summing an array:)-.15 E F3 1.5 (for\( sum=i=0; i.15 G .353 (oating point so f)-2.853 F(ar)-.1 E 2.853(,a)-.4 G .353 (nd the treatment here will be hasty)-2.853 F 5.353(.C)-.65 G .352 (has single and dou-)-2.501 F(ble precision numbers \(where the precisi\ on depends on the machine at hand\).)72 556.8 Q -.15(Fo)5 G 2.5(re).15 G (xample,)-2.65 E F3 1.5(double sum;)144 573.8 R 1.5(float avg, y[10];) 144 585.8 R 1.5(sum = 0)144 597.8 R F5(.)A F3(0;)A 1.5 (for\( i=0; i