350
S.II 16945 db142 '-' tamina

Program Ski Jazik C (II Izdanie) (Brajan v. Kernigan & Denis M. Richi)

  • Upload
    -

  • View
    146

  • Download
    8

Embed Size (px)

Citation preview

S.II 16945 db142 . . '-' tamina dp .G5,2.JS cwr z. /;1(>9'-tS-- 8d1v42. ..- 500 . . Authorized translation from the English language edition, entitled PROGRAMMING LANGUAGE, 2ndEdition,0131 103628byKERNIGHAN,BRIANW.; DENNIS,publishedbyPearson Education, lnc, publishing Prentice Hall, Copyright 1988, 1978 by Bell Telephone Laboratories, lncorporated, by Prentice Hall, lnc., Upper Saddle River, NJ 07458 All the rights reserved. No part of this book may be reproduced or transmitted in any form or by any means, electronic and mechanical,including photocopying, recording or by any information storage retrieval system, without permission from Pearson Education, lnc. MACEDONIAN language edition published by ARS LAMINA DOO, Copyright 2009 0131103628 PearsonEducation, lnc., PrenticeHall, 1988,1978 Bell TelephoneLaboratories, lncorporated, Prentice Hall, lnc., Upper Saddle River, NJ 07458 2009, CIP- 1 : 2009. - XIV, 350 : The programming language 1 Brian W. Denis Ritchie.- ISBN 1. 2. [avtor]. -1.Brian W. . .,..,...- - /..80939530 Zl\ [ '. v ..'."-.:""" ...." L 42. rp,2 01 /2otto 1 - 1.1 1.2 1.3 for 1.4 1.5 1.5.1 1.5.2 1.5.3 1.5.4 1.6 1.7 1.8 - 1.9 1.1 2 - 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 2.1 2.11 2.12 v xi xii 1 5 5 8 14 16 17 17 19 21 22 24 27 31 32 35 41 41 42 43 47 48 49 50 54 57 59 61 62 Vl - 3.1 3.2 if- else 3.3 else- if 3.4 switch 3.5 while for 3.6 do- while 3.7 break continue 3.8 goto 4- 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 4.1 4.11 4.11.1 4.1 1.2 4.11.3 - 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 5.1 5.11 5.12 6- 6.1 6.2 6.3 65 65 65 67 69 71 74 76 77 79 79 83 86 93 95 97 98 98 99 101 103 106 104 107 109 109 111 114 117 121 125 129 132 133 134 139 142 149 149 152 155 6.4 160 6.5 162 6.6 168 6.7 typedef170 6.8 172 6.9 174 7 - 177 7.1 177 7.2 printf179 7.3 182 7.4 scanf183 7.5 187 7.6 stderr exit190 7.7 192 7.8 194 7.8.1 194 7.8.2 194 7.8.3ungetc195 7.8.4 195 7.8.5 195 7.8.6 196 7.8.7 197 8- UNIX199 8.1 199 8.2 1/0- read write200 8.3 open, creat, close, 202 8.4 elseek205 8.5 fopen getc206 8.6 21 8.7 216 - 223 223 223 223 224 224 224 225 225 226 227 Vlll 227 227 228 228 228 229 230 230 231 231 231 231 232 232 232 233 void234 void234 235 235 236 236 237 237 238 239 239 239 240 240 240 240 241 241 sizeof241 (casts)241 242 242 243 243 244 245 245 245 245 246 246 246 247 248 248 249 250 251 255 256 257 257 258 259 261 263 typedef264 265 266 labeled) 266 266 267 267 268 269 1 270 10.1 270 10.2 272 11 273 11.1 273 11.2 274 12 275 12.1 275 12.2 276 12.3 276 12.4 279 12.5 279 12.6 281 12.7 281 12.8 pragma281 12.9 282 12.1 282 13 282 - 291 291 1.1 292 1.2 294 1.3 297 1.4 299 300 300 1.7 301 302 302 304 305 308 309 309 31 1 311 11 313 - 317 321 1978 UNIX. 1983 National Standard lnstitute - ANSI) - ANSI ANSI Xl xii . . , , . , . . . , , , . , - . . , , , . . ," " . , . . . , , . , , , . . , , , , , , , . , , , , , , . ANSI . ++ , ANSI . . . . . UNIX DECPDP- 11, IBM System/370, Honeywell 6000 lnterData 8/ 32. Xlll xi v . . , , , , , , . , , , , , , , , , , , , , , , , , , . . . UNIX UNIX . BePL BePL 1970 UNIX DEC PDP-7. BePL (if-else), (swi tch), (break). 2 stack), heap) garbage collection). READ WRITE (ANSI) . AN$1 ANSI 1988 3 . UNIX 4 : 1, . , , . , , ; , , , , n=n+l. , , , ; , . 2 , 1, , . 2 , . 3 :if-else ,switch,while ,for . 4 - , , - , , . 5 . . 7 , . ANSI , , / , . 8 UNIX, / , . UNIX , , . . ANSI . . , , . . . , , . 1: 3 . , , . , , ( ). , , : , , , , . . , , , . . , , , . , . . . . , , . , . , 2. 1.1 . , : 5 6 1 hello,world "Hello,world" #include main() { printf("hello,world\n"); UNIX hello. hello.c out. out a.out hello,world main. "main" - main. "main" main 101 7 #include 1 7 main () #include main () printf( "hello, world\ n " ) ; main main main \n { } main printf(" hello,world\n " ) ; printf " hello ,world\n"printf "hello ,world\ n ", printf \ n , 8 1 \n , \n printf ; printf("hello ,world \\); printf . #inelude main() { printf("Hello,") ; printf("world" ) ; printf(" \n" ); \n \n \ t \b \ " \\ backslash) . 2 . 3 1-1 . "Hel1o,world" 1-2 . printf 1.2 (5/ 9)(F-32) 1.2 9 -17 20-6 404 6015 8026 10037 12048 14060 16071 18082 20093 220104 240115 260126 280137 300148 main. " hello ,world" , #include /* fahr 20 ,. .. ,300*/ main () { intfahr ,celsius ; intlower,upper,step; lower= /* */ upper=300 ;/* */ step=20;/* */ fahr=lower; while(fahrmax){ max= len; () ; if(max> /* */ printf("%s",longest) ; return1.1 37 /*getline : */ int getline(void) int i ; externeharline[] ; for(i= i>= shift) & n=n &0177 7 n, 1 1 SET_ON; 1 1 _ ON. &1 &&1 1 , 12, & && >(p+1- n))&n); >>(p+1- n) n &1

1 * 1) bitcount , 60 1* bitcount: */ int bi tcount (unsigned { intb; for (b=O; if&01) b++; returnb; 2 unsigned 2 i" i 2 ", i , 2 i ". i+= 2 i= i+ 2. yyval [yypv + yypv [pl+p2] ]+= 2 while( getchar () )! =EOF) (+=,-=, 2-9. &= bitcount. 2011 61 2.11 if(a>b) z=a; else z=b; z b "?:" ? : , 2 3, z b z>b)? b;/*z =max(a, b)*/ ffloat n int, (n>O)?f: n float, n , 10 for (i=O; i . sizeof . * & , ',' . 2 . 1 - onepamopume ()[]-> . !- ++ -- + - *(type)sizeof * 1% +- = == != & && 11 ?: = += - = *= / = %=&= 1= = + ,-, * 2012 63 &,1 =! = if (( & )== 0) , , ( && , 1 1 ,?:' , ' ) , x=f() +g(); f g, ; , f g , , , printf ("%d %d\n",++n, power (2 ,n));1* * 1 , n power ++n ; printf("%d%d\n", n, power(2,n)); , , " " - , a[i] = i++; i ( ) , .( , printf, . ) , , . , , , . 3: 3.1 i++ printf {... ) i++ ; printf{ ... ); {} if ,else,while , for . 4. ) 3.2 if-else if - else 65 66 else 3 else , ,

else

if, if (izraz) if (izraz else else if else if else if (n>O) if(a>b) z else z=b; else if , if (n>O){ if(a>b) z=a; else z=b; 3.3 if (n>O) for(i=O;ib) z else z=b; if, = 3.3else- if if else if else/ff else if else if 68 3 1 else 1 1 else 1 1 1 1 v v n- 1 ) v 1 -1 v 1 1 /*binsearch: v[O]= n- -) if(s[n]!=''&&s[n]!='\t '&&s[n]!='\n ') break; [n+l]='\0'; returnn ; strlen for n . continue break, for ,while do while do for , c ontinue swi tch. continue swi tch 3.8goto 77 for(i i= allocbuf && < allocbuf + ALLOCSIZE) allocp=p; static char *allocp = allocbuf ; allocp allocbuf , static char *allocp =&allocbuf if(allocbuf+ALLOCSIZE- allocp>=n){/* n allocp allocbuf. alloc . alloc 120 5 NULL, NULL . NULL . if(allocbuf+ ALLOCSIZE- allocp>=n){/* if>= allocbuf && < allocbuf + ALLOCSIZE) q ! =, =, , p < q q. p+n n int q < q, q-p+l q . strlen: 1 5.4 /*strlen: */ int strlen (char{ char while!= '\0') returnp- 121 while '\0' - , int. ptrdiff_ t size_ t strlen, size_ t sizeof .) float, char float, floa t alloc, char float alloc afree. floa t double void*) 5.5 sumstring" 122 5 printf (" hello,world\n"); printf pmessaqe char *pmessaqe ; pmessaqe = " now is the time"; pmessaqe charamessaqe[]= " nowisthetime" ;/* */ char*pmessaqe= "nowisthetime";/* */ amessaqe '\0' . amessaqe pmessaqe pmessaqe: isthet ime\0 amessage:lnowisthetime\0 strcpy t) , t = t, 5.5 123 . /*strcpy: t */ voidstrcpy(char char*t) { inti ; i= while((s[i]=t[i])!=' \0' ) i++; strcpy /*strcpy: rot voidstrcpy(char char*t) { while( t++; *t)!='\0' ) strcpy t , t, strcpy /*strcpy: rot 2*/ voidstrcpy(char char*t) { while( =*t++)!='\0') t *t++ t, t ++ t t . t ' \0' . 124 5 /*strcpy: t */ voidstrcpy(char char*t) { while=*t++) strcpy () strcmp t) , t t . /*strcmp: s0 s>t*/ int strcmp(char char*t) int i ; for(i = s[i]==t[i];i++) if[i]='\0') returnreturns[i]- t[i] ; /*strcmp: s0 s>t*/ int strcmp(char char*t) for(; ==*t ; t++) if ='\0' ) returnreturn - *t; 5.6 125 , = val ;/* val */ val= /* val*/ push) 4 . 3. 5-3. strcat 2:strcat t) t 5-4. strend ( t) , 1 t 5-5 . strncopy,strncat ,strncmp , n strncpy t, n) n t 5-6 . getline 14),atoi ,itoa 2,34),reverse 3)strindex getop 4). 5.6 . 1 sort UNI X. 1 Shellsort) 1 4 quicksort . 126 5 , , , . . . , , - - - , . strcmp . , , . . : , , main . . . , , . , r . , . 5.6 #include #include 127 #define 5000/* 1 char /* * / intreadlines(char*1ineptr[],intnlines) ; voidwritelines(char*1ineptr[],intn1ines) ; voidqsort(char*1ineptr[] ,int1eft,intright) ; /* */ main () { intnlines ;/ * if((nlines=readlines(lineptr , { qsort(lineptr, nlines-1) ; writelines(lineptr ,nlines) ; returnelse{ printf(" error:inputtoobigtosort\n"); return1 ; #define 1000/ * intgetline(char* ,int); char*alloc(int); /*readlines: intreadlines(char*lineptr[],int maxlines) intlen,nlines; char nlines=while((len=getline(line , >0) if(nlines>=maxlines1 1= alloc(len) return- 1 ; else{ NULL) line[len-1]=' \0' ;/* */ strcpy(p,line) ; lineptr[nlines++]=returnnlines ; 128 /*writelines: */ voidwritelines(char*lineptr[],intnlines) { inti; for(i= iprintf("%s\n",*lineptr++) ; *lineptr nlines Quicksort 4 strcmp . /*qsort : v[left] ... v[right] voidqsort(char*v[] ,intleft,intright) { inti ,last ; voidswap{char*v[],inti ,int if(left>=right)/ * 5.7 129 */ return; swap(v1 left1 (left+right)/2); last=left; for(i=left+1;i0){ lineno++; if ( (strstr (line1 *argv)! =NULL)!= except){ if (number) printf(" %ld: "1 lineno) ; printf(" %s ", line) ; found++; return found ; argc argv argc argv 138 5 argc 1 ,*argv *++argv na ( *++argv) ( **++argv.) [] *++ , *++ (argv ). *++argv[O] argv[O]! 5-10. expr, expr234+ * 2*(3+4). 5-11. entab detab 1) tab tab 5-12 . entab detab entab - m+n m. 5-13 . tail , n n , 10, tail -n n n . 5 . 6, 5.11 139 5.11 - n , - strcmp; strcmp. main, qsort. . #include #include #define 5000/* * / int readlines (char *lineptr [] ,int nlines) ; voidwritelines(char*lineptr[] ,intnlines) ; void qsort (void *lineptr [] ,int left ,int right , int (*comp)(void * ,void *)) ; intnumcmp ( char * ,char *) ; 1* */ main ( intargc, char * argv [] ) { * 1 int numeric 1* 1 */ if (argc>l&&strcmp(argv[l] ," -n" )numeric= 1 ; 140 if ( (nlines = readlines (lineptr, qsort ( (void**)lineptr, nlines-1 , 5 (int (*)(void* ,void*)) (numeric? numcmp:strcmp)); writelines(lineptr, nlines) ; return } else { printf ("input toobig to sort\n" ) ; return 1 ; qsort, strcmp numcmp & qsort qsort void* . void * qsort void* /*qsort: v[left] . . . v[right] */ voidqsort(void*v[] ,intleft,intright, int(*comp)(void* ,void*)) inti,1ast; voidswap(void*v[],int ,int) ; if(left>=right) iDD _.,_ n::I81IICY */ return; swap(v,left,(left+right)/2); last = left; for(i=left+1 ;i=r.ptl.x&&=r.ptl.y&& struct rect r ,*rp = &r ; r.ptl.x rp->ptl.x (r .ptl) (rp->ptl) . () [] , 6.3 struct { int len; char *str ; } ++p->len 155 len, ->len len, ->len len. ) *p->str str ; *p->str++ str str ;(*p->str) str;*p++->str str . 6.3 keyword keycount , char int char *word; intcount; structkey { char *word; int count; } keytab ; 156 key, keytab structkey { char *word; int count ; } ; truct key keytab ; keytab structkey { char *word; intcount; } keytab [ ]={ } ; "auto","break", "char", "const", " continue", " defaul t", 1*... *1 "unsiqned", "void"," volatile", "while", {" auto", } , {"break", } , { } , 6.3 157 keytab [] . #inc1ude #inc1ude #inc1ude Jdefine100 intgetword(char* ,int) ; intbinsearch(char* ,structkey* ,int) ; /* main () { intn ; char whi1e(getword(word, !=EOF) if(isa1pha(word[O])) if((n=binsearch(word,keytab, keytab[n] .count++; for(n= n< n++) if (keytab [n] . count > 0) printf (" %4d %s\n", keytab[n] .count ,keytab[n] .word); return /*binsearch: word tab[O] ... tab[n-1]*/ int binsearch(char*word,structkeytab[] ,intn) int cond; int 1ow,high ,mid; 1ow=high=n- 1 ; whi1e(1ow1ow=mid+1 ; returnmid; return-1 ; 158 keytab main, qetword, keytab qetword; qetword keytab keytab size of) keytab 1 key sizeof sizeof sizeof sizeof size_ t int double , #define tdefine (sizeof keytab 1 sizeof (struct key)) 6.3 159 #define ( sizeof keytab 1 sizeof (keytab ) ) sizeof /fif /fdefine, getword. getword, getword EOF /*getword: */ int getword (ehar *word, int lim) { int geteh (void) ; void ungeteh (int) ; ehar *w = word; while(isspaee geteh ())) if!=EOF) *w++= if (! isalpha *w='\0' ; return for( ;--lim>O;w++) if (! isalnum(*w = geteh ())) ungeteh(*w); break; *w ='\0' ; return word ; getword geteh ungeteh 4 . getword 160 unqetch Getword, isspace isalpha isalnum 6-1 . qetword 6.4 keytab main binsearch Jinclude linclude linclude Jdefine100 int qetword(char*,int); structkey*binsearch(char*,structkey*,int) ; /* */ main() { char structkey while(qetword(word, !=EOF) if(isalpha(word[O])) if((p=binsearch(word,keytab, !=NULL) p->count++; for =keytab;count>0) printf( " %4d%s\n" ,p - >count,p->word); return0; 6.4 161 /*binsearch: nojaa}'8a&e tab[O] . ..tab[n-1]*/ struct key *binsearch (char *word,struck key *tab,int n) { int cond; struct key *1ow =&tab ; struct key *high =&tab [n]; struct key *mi.d; while(low word)) high =mi.d; if (condlow=mi.d+ 1 ; else return mi.d; return NULL; binsearch struct key; binsearch . binsearch NULL. keytab binsearch . 1ow high mid=(low+high)12/* */ high- low mid=low+(high- low)/ 2 mi.d low high. &tab [-1]&tab [n) tab . 162 &tab [n]) main for = keytab; < keytab + . char int , struct { char inti ; } ; sizeof . struct key *binsearch (char *word,struct key *tab, int n} structkey * binsearch (char *word,struct key *tab,int n} . 6.5 6.5 163 ) - - - - , - now is the t ime for all good men to come to the aid of their party". now /"-18the /"- /""" formenoftime /""""/""" allgoodpa.rtytheirto /""-a.idcome 164 structtnode{ char*word; int count ; } ; structtnode*left; structtnode*right; /* */ /* */ /* */ /* */ /* */ struct node *left; left tnode, tnode . structt{ struct /* */ } ; struct{ structt*q;/*q t*/ } ; getword, main getword addtree . linclude iinclude iinclude idefine 100 struct tnode *addtree (struct tnode * ,char *) ; void treeprint (struct tnode *); intgetword(char *, int); 6.5 /* */ main () { struct tnode *root ; char ; root=NULL; while(getword(word, != EOF) if (isalpha(word[O])) root = addtree (root ,word); treeprint(root) ; return 165 addtree main ( addtree. , addtree . struct tnode *talloc (void) ; char *strdup (char *) ; 1*addtree : w, * 1 struct treenode *addtree (struct tnode char *w) { int cond; if=NULL) / =talloc ();1* */ p->word = strdup (w); p->count =1 ; p->left .. p->right = NULL; } else if ( (cond= strcmp(w, p->word))=0) p->count++; 1 / else if (cond< */ p->left =addtree (p->left ,w); else / *axo p->right = addtree (p->right ,w); returnp; 166 talloc, strdup NULL . strdup talloc . treeprint , 1 treeprint /*treeprint:in-order */ voidtreeprint(structtnode { if!=NULL){ treeprint(p->left) ; printf("%4d%s\n",p->count ,p->word) ; treeprint(p->right) ; 1 1 1 char struct tnode , 1 alloc 6.5 167 mallo c , 8, malloc. malloc malloc void, . talloc #include / *talloc: (tnode)*/ struct tnode *talloc (void) { return(struct tnode *) malloc (sizeof (struct tnode)) ; strdup malloc char*strdup(char / * */ { char =(char*)malloc(strlen(s)+l) ;/*+1f o r'\0 '*/ if !=NULL) strcpy(p, returnmalloc NULL strdup malloc free ; 7 8 . 6-2 . 6 6-3. 168 6-4 . . . 6.6 n ( .table-lookup) . . 1 idefine. idefine IN 1 IN 1 . 1 IN state = IN; 1 . . install ( 1t) ;t .lookup ( ) . NULL . - - 1 . . NULL . defn 6.6 169 struct nlist{/* */ } ; structnlist*next ;/* */ char*name ;/* */ char*defn;/* */ idefine 101 staticstructnlist /* */ lookup install , /*hash: */ unsignedhash(char { unsignedhashval ; for(hashval=!='\0';hashval= +31*hashval ; returnhashval% . hashtab; lookup. lookup NULL. /*lookup: ro hashtab*/ structnlist*lookup(char { structnlist*np; for(np=hashtab[hash(s)] ;np!=NULL ;np=np->next) if(strcmp(s,np->name)==returnnp;/* */ returnNULL;/* */ 170 for lookup for(ptr=head; ptr !=NULL; ptr=ptr->next) install lookup install NULL struct nlist*lookup(char*) ; char*strdup(char*) ; /*install: (name,defn) hashtab*/ structnlist*install(char*name,char*defn) { structnlist*np; unsignedhashval; if((np=lookup(name))==NULL){ */ np=(structnlist*)malloc(sizeof(*np)) ; if(np=NULL11(np->name=strdup (name) )=NULL) returnNULL; hashval=hash(name); np->next=hashtab[hashval] ; hashtab[hashval]=np ; else/* */ free ( (void*)np->defn) ;/* ro dfn* 1 if((np- >defn=strdup(defn))==NULL) returnNULL ; returnnp ; 6-5 . undef lookup install. 6-6. #define getch ungetch. 6.7typedef typedef 6.7typedef171 typedef int Lenqth; Lenqth int. Lenqth int: Lenqth len, maxlen ; Lenqth * lenqths [] ; typedef char *Strinq; Strinq char*, Strinqp, alloc (int) ; intstrcmp(Strinq,Strinq) ; = (Strinq) malloc (100); typedef typedef . typedef extern,static typedef typedef typedefstructtnode*Treeptr; typedefstructtnode char*word; intcount ; structtnode*left; structtnode*riqht ; Treenode ; /* */ /* */ / * */ /* */ /* */ Treennode Treeptr . talloc Treeptr talloc (void) { return (Treeptr)malloc(sizeof(Treenode)); 172 typedef typedef ltdefine typedef int (*PFI)(char *, char *) ; PFI, int", PFistrcmp,numcmp; typedef. typedef typedef na short,int long size_t ptrdiff_t typedef - 6.8 variant int,float . . 6.8 union u _ tag { intival ; fioat fval; char *sval; }u ; 173 u u : utype u , if (utype == printf("%d\n" 1 u.ival) ; if (utype-= FLOAT) printf(" %f\n" 1 u.fval) ; if (utype = printf ("%s\n", u . sval) ; else printf ( "bad type %d in utype\n", utype) ; 174 struct { char *name ; intflags; intutype ; union { }u; intival; float fval; char *sval; } symtab ; ival symtab[i] .u.ival sval *symtab[i] . u.sval symtab[i] .u.sval[O] u 8 6.9 . 6.9 175 char int. #define 01 #define EXTRENAL 02 #define STATIC 04 enwn {= 01 , = 02, STATIC = 04}; 2. 2. flags1= EXTERNAL1 STATIC; 1) STATIC flags , flags &=- (EXTERNAL1 STATIC); if ( (flags & 1 STATIC) ) #define 176 struct { unsigned int is_ keyword :1; unsigned int is_extern :1; unsigned int is_static :1; } flags; 6 flags unsigned int flags. is_ keyword,flags. is_ extern, . flags. is_extern =flags. is_static = 1; 1; flags. is_ extern = flags. is_ static if (flags. is_extern && flags. is_static int; signed unsigned. & 7: 1 1 1 1 1 1 ANSI 1 ; 1 1 1 7.1 3 3 3 11 ; 1 , 1 ( carriage return) linefeed 1 1 getchar : int getchar (void) 177 178 7 getehar EOF EOF . -1 , EOF, prog getehar , prog fi lename: prog putehar , prog >outfile outfile. prog 1 anotherprog prog, anotherprog. printf, putehar printf - linelude 7.2 - printf179 < UNIX / usr/include) . getchar ,putchar, printf . #include #include main()/*lower: */ { intc while getchar ())! = EOF) putchar(tolower(c)) ; return tolower ; getchar putchar . 7- 1 . argv . 7.2 - printf printf printf intprintf (char *format, argl, arg2 ,... ); 180 7 printf 1 fortnAt. 1 1 1 printf . 1 - - - - h short, 1 long. 7. 1. %, 7 . 1 printf d, i u f g , G 1 int; int ; int; abcdef 101 ,15 int ; int; char* ; double ;[-] m. dddddd, 6). double ;[-]m. dddddde+- xxili [-]m.ddddddE+- xx, 6). double ; -4 %f . void* ; % % 7.2 printf181 *, . max printf (" %. " hello,world"(12 . :%-lOs : : hello, world: :hello, world: : hello, wor: : hello, world: :hello,world: :hello, world hello, wor: : hello,wor printf printf printf(s); printf( " %s " ,s) ; 1* %* 1 / * */ sprintf printf , int sprintf (char *strinq, char *forlll4t, arql ,arq2,... ); sprintf arql,arq2 , , forlll4t 7-2 . . 182 7 7.3 printf, . minprintf printf printfe intprintf(char*fmt ,... ) ... ... voidminprintf (char *fmt,... ) printf. mi.nprintf va_list minprintf , va_surt va_start va_arg va_arg va_end printf : 7.4 scanf iinclude /*minprintf: printf */ voidminprintf(char*fmt ,. .. ) { va_list / * arg*/ char *sval ; intival ; doubledval ; va_start(ap,fmt) ;/* */ for(p=fmt ; *p; p++){ if(*p!='%' ){ putchar continue; switch(*++p){ ' d ': ival=va_arg(ap,int); printf ("%d " 1 ival) ; break; ' f ': dval=va_arg(ap,double) ; printf ("%d ", dval) ; break; : 183 for(sval=va_arg(ap ,char *) ; *sval ;sval++) putchar(*sval) ; break; default: putchar(*p) ; break; va_end(ap) ;/* */ 7 - minprintf printf . 7.4 scanf scanf printf 1 184 7 int scanf (char *format1 ) scanf 1 1 1 printf 1 scanf 1 scanf 1 EOF; 1 1 scanf . sscanf 1 int sscanf (char *string 1 char *format1 argl, arg2 1 ) string 1 format argl 1 arg2 1 format1 1 - 1 . - % 1 * 1 1 h 1 1 L 1 * 1 scanf 1 1 7.4 scanf185 n carriage return, formfeed .) 7.2. 7.2 : scanf d i u int* int unsiqned int* char 1) char*, '\0' e , f,g , float* % d,i, u, h, short int, 1 1ong 4 , scanf , #include main(){ * / { doublesum,v; sum=O; while(scanf( "%lf" ,&v)==1) printf("\t%.2f\n",sum+=v); return186 7 25Dec1988 scanf int day, year ; char monthname [20] ; scanf ( "%d%d ", &day,monthname ,&year); & monthname,