You are on page 1of 350

S.

II 16945
db142

.
.

'-' tamina

. /; 1(>9'-tS-- 8d1v 42.

(. dp . G5,2.JS11

cwr

z.

.;l~.

..-

.,

500 , ,
,

".

.
.

Authorized translation from the English language edition, entitled PROGRAMMING LANGUAGE,
2nd Edition, 0131 103628 by KERNIGHAN, BRIAN W.; RICHIE, DENNIS, published by Pearson
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 , .; , ; Pearson Education,


lnc., Prentice Hall, 1988, 1978 Bell Telephone Laboratories,
lncorporated, Prentice Hall, lnc., Upper Saddle River, NJ 07458
.
, ,
,
.

2009,

:
-

CIP-

. . ~
004.432. 2
, .

. , . . -

: , 2009. - XIV, 350 . : . ; 2


. . .: The
Kerighan ,

programming language 1 Brian W.


Denis . Ritchie.-

ISBN 978-08-4535-48-5
1. . . . . . 2. , . [avtor]. 1. Kerighan, Brian W. , .
.,..,...- ~ - ( )
/
.. COB~SS.MK-ID 80939530

"''<' 0::1 Zl\ [ '. >~"Tf'Q T( X ,..o,p


-- t.o~ . ..' ."-.:""" - ::.>..l('l :or w-o
.... " ':" 1
L

._ ...

,ff;;~f:J.!:' ~r}.

. p{p_/:.l!J
5jr. rp

42.

,2 ~ 01

/2otto

xi
xii

r 3

5
5
8

1 - 3
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 - , 33
2.1
2.2
2.3
2.4
2.5
2.6
2.7
2.8
2.9
2.1
2.11
2.12

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- wh ile
3.7 break continue
3.8 goto

4- nporpaa
4.1
4.2
4.3
4.4 (. )
4.5 3
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
6.5 -
6.6
6.7 typedef
6.8
6.9

160
162
168
170
172
174

7 - n n
7.1
7.2 - printf
7.3
7.4 - scanf
7.5
7.6 - stderr exit
7.7
7.8
7.8.1
7.8.2
7.8.3 ungetc
7.8.4
7.8.5
7.8.6
7.8.7

177
177
179
182
183
187
190
192
194
194
194
195
195
195
196
197

8- UNIX
8.1
8.2 1/0- read write
8.3 open, creat, close, unlik
8.4 - elseek
8.5 - fopen getc
8.6 -
8.7 - (3)

199
199
200
202
205
206
21
216

1
2

2.1
2 . 2

2.3
2.4
2 .5
2.5 . 1
2.5 .2 3

2.5.3

223
223
223
223
224
224
224
225
225
226
227

Vlll


2.5.4
2.6

3
4
4.1
4.2
4.3

4.4
5

6. 1 ()
6.2
.
.4

6.5
.
6.7

void

6.8

void

7
7.1

7.2
7.3
7.3.1
7.3.2
7.3.3
7.3.4
7.4
7.4.1
7.4.2

7.4.3 ()
7.4.4
7.4.5

7.4.6
7.4.7
7.4.8
7.5

sizeof

(casts)

7.6
7.7

7.8
7.9
7.1
7.11
7.12
7.13

227
227
228
228
228
229
230
230
231
231
231
231
232
232
232
233
234
234
235
235
236
236
237
237
238
239
239
239
240
240
240
240
241
241
241
241
242
242
243
243
244
245
245
245

7.14
7 . 15
7 . 1

7.17
7.18
7.19

8
8 . 1

8.2
8.3
8.4
8.5

8 .6
8.6.1
8.6.2

8 .6.3
8.7
8 . 8

8 .9 typedef
8 . 1

9
9.1 (.

labeled)

9.2
9.3
9.4
9.5
9.6
1

10.1
10.2
11

11.1
11 .2
12

12.1
12.2

12.3
12.4
12.5
12.6

12.7
12.8

pragma

12.9

12.1

245
246
246
246
247
248
248
249
250
25 1
255
256
257
257
258
259
261
263
264
265
266
266
266
267
267
268
269
270
270
272
273
273
274
275
275
276
276
279
279
281
28 1
281
282
282


13

-
<stdio.h>
1.1
1.2
1.3
1.4

282

1 :

1.5

1 .6
1.7

2 :

<ctype.h>
<string.h>
4 : <math.h>
: <stdlib.h>
: <assert.h>
7 : <stdarg.h>
8 : <setjump.h >
9 : <signal.h >
1 : <time.h>
11 n: <limits.h> <f1oat.h>
3 :

291
29 1
292
294
297
299
300
300
30 1
302
302
304
305
308
309
309
31
311
313
317
321

"

1978 ,

. ,

. ,
,

UNIX.

, ,

,
,
.

1983 ,
National Standard lnstitute - ANSI) ,
" -
". ANSI .

(a.A merican

,
,

.
. , ,
, ,
, .

,
,
.

"

ANSI .

, .
;

.
.

.
, .
, -

Xl

xii

.
. ,
,

.
, .. .
, , ,
.
,

.
. , ,
, .
.

"

".
, .
.


. , ,
.

, , , .. , ,
, , , , ,
.
, , , ,
, , .

++

ANSI .

ANSI

.
.
.

.
.


, ,

. " " , , , " ",


. ,
,
, .
,

UNIX
DEC PDP - 11, . ,

UNIX-o (
) . ,

IBM System/370,
Honeywell 6000 lnterData 8/ 32. , ,
,
.

. ,
,
. ,

, .
, ,
. ,

.
, , ,
.
;

, . , , ,
n ,
.

,
. ,

X lll

xiv

.
, , , , , ,
. ,
, , , , , ,
, , , , , ,
, , , , ,

.
.
.

UNIX

UNIX

. , ,

();
,. "

.
BePL .

BePL

UNIX

1970
DEC PDP-7.

BePL ., ".

. ,

. ,

, , .

;
, (
). - .

: , (if-

else), (swi tch),


(whil) ( d), ,

(break).

, ,

, . .
., ",
. , ,
.
,
.
, (
) .

,

" ".

,
, , .

.


( ) , , .

,
.

(. stack), ;
(. heap) (. garbage collection). ,
/ ; READ WRITE ,

.

.

.
, , : , ,
, , ,

(",
?"),
. ,

. ,
.
,
.

(ANSI)

198

. AN$1 , ANSI
1988 .

.
.
;
, ,
, .
,

.
;
.

; ,

, , .
.
, .

.
.

. (

, ), ,

, .

,
.
, .

" /
" UNIX .
, , . ,
.

.
,
.
, ,
.

, ..

.
,

.
, ,

. ,
;

, .
, , .
,
. ,

;
.

; .

,
.

1,

. ,

,
. , ,

;
, ,

n=n+l. ,

, ,

;
, .

2
1,
, . 2
, . 3
: if-else , switch, while , for .
4 -
,

, , - , ,
.

. .

7 ,

ANSI ,
, /

.
8

UNIX, / ,

UNIX ,


.
.

ANSI .

. ,

, .

.
. , ,
.

1: 3

, ,

. , ,

( )

, ,
: , , , ,
.

.
, ,

,
.

.
,

, , .
,
.
.
.

,
, .

, .

1.1

2.


. , :

hello, world
: ,
, , ,

. ,
.

"Hello, world"

#include <stdio.h>
main()
{

printf("hello, world\n");

.
,

UNIX

". ", hello.

hello.c

,
. out. . out

a.out

hello, world
. ,
, . ()
,
, .

main. ,
, "main" -
main.
" main" .
main
; ,

101

#include<stdiooh>
,

1 ;

7

, ,

,
,

main
()

#include <stdiooh>

main ()

main

main i
u

printf( "hello , world\ n " ) ;

main u
pritf

\n

{ }

main
printf ( " hello, world \ n " ) ;
,
,

printf

" hello , world \n" printf


,
,

\n,

"hello ,
,

world

\ n ",

printf

\n

( )

. \n
,

printf ;

printf ( "hello , world


\\) ;
.

printf ,
,

#inelude <stdio.h>
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

-17
20 -6
40 4
60 15
80 26
100 37
120 48
140 60
160 71
180 82
200 93
220 104
240 115
260 126
280 137
300 148

main. " hello , world" ,


. , , ,
, , , .

#include <stdio.h>

/*

n -

fahr

= ,

20 ,

. .. , 300 */

main ()
{

int fahr , celsius ;


int lower, upper, step;
lower = ;
upper = 300 ;
step = 20;

/*
/*
/*

*/

*/

fahr = lower;
while (fahr <= upper) {
celsius = 5 * (fahr-32) 1 9 ;
printf("%d\t%d\n", fahr, celsius);
fahr = fahr + step ;

/*

n -

fahr

= ,

20 , ... , 300 */

*/

10

, .

/*

* 1

, , .

,
. ,

,
. ;
n,

int fahr , celsius;


int lower, upper, step;

int
float, , .
. int, floa t
; 16- int , -32678 +32767, ,
32- int. float 32-
, 6 10- 38
10+38
int float, :
;

char
short
long
double

""

""
n

, , . ,
,

. .

lower = ;
upper = 300;
step = 20;
.

("; " ) .

,
;

while

while (fahr <= upper) {

11

102

while : ,
(fahr upper) ,
( )

, , ,

(fahr

upper) ,

,
,

while

, , , ,

while (i < )
i = 2 * i ;
,

while,

( )


( )

celsius :

celsius

=5 *

(fahr-32) 1 9 ;

5/9 , ,
(

truncate)

5 9 , 5/ 9 ,

, , printf
printf ,
7
, %
(, , )
, %d ,

12

printf( "%d\ t %d\n", fahr , ce1sius) ;


ce1sius ,

( \ t)

fahr

% printf

, .

, .
,

printf ;
printf

, .

printf ANSI ,

, .

7 .
. ,

scanf 7 . 4 . scanf

printf ,

.
,
. ;

%d

printf

,
. ,
, ,

printf( "%3d %6d\ n", fahr , ce1sius) ;


:

20
40
60

-17
-6
4
15

26

100

37

:
, ;

, 0 F -17 . 8 , -17 . ,
, .
:

1.2


include

13

<stdio.h>

/* -

for fahr
main()

=,

20, ... , 300;

- */

fioat fahr, celsius ;


fioat lower, upper, step ;
lower = ;
upper = 300;
step
20 ;

/*

/*
/*

*/

*/

*/

fahr
lower;
while (fahr <= upper) {
celsius = (5.0/9.0) * (fahr-32.0);
printf( "%3.0f %6.lf\n", fahr , celsius) ;
fahr = fahr + step ;

fahr

celsius fioat,

5/ 9,

.
,

5 . /9.

,
.

,
.

32

(fahr-32) ,

. ,

,
, .

,

2. 3.

, :

fahr = lower ;

while (fahr <= upper)


, - int-o
.

%3. Of ,

fahr)

fioat
printf ,

14

,
. % . l f

(celsius)

- 17 . 8

20
40

- . 7

4.4

: % f

%. 2f
%f , ,

, ;

%d

%d

%f

%f

% 2f
% . 2f

1 2
1 1

printf, , n

: %

, % , % , % %% %.

1-3 .

1-4.

1.3 3 for

. .

#include <stdio.h>
/* print Fahrenheit-Celsius table */
main ()

1.3

for

15

int fahr ;
for (fahr = ; fahr <= 300 ; fahr = fahr + 20)
printf ("%3d %6 .lf\n" , fahr, (5 . 0/9. )* (fahr-32)) ;

, .

fahr, int.
for , ,

printf , .
-
,

printf
%6. lf,
.

for

while .

while, ,

for

, . ,
-. ,

fahr

, .
:

fahr <= 300


; , (

printf )

fahr

= fahr

+ 20

, , .

. while,
, , .
, , .
for while , .
, for
, while ,
.

16

1-5 .

1 . . 300 .

1.4

. "
"

300

20

. ,
.

tdefine

idefine
, ( )

, .
:

. ;
.

tinclude <stdio.h>
idefine LOWER
1*
idefine UPPER 300 /*
tdefine 20
/*
/* 'l'
ma.in ()

I 'l'aena'l'a
I

*/

*/

*/

'l'- 'l'

*/

int fahr ;
for (fahr = LOWER ; fahr <= UPPER; fahr = fahr + )
printf ("%3d %6 . lf\n" 1 fahr , (5 . 0/9.0)*(fahr-32)) ;

LOWER , UPPER

, ,

#define ,

-.

1.5

17

1.5 Bne3 3n3 3



.
.
, , .
,
, (.

streams)

;
.

;

.

. ,

getchar putchar
getchar

. . .,

= getchar () ;

.
; 7 .

putchar

putchar(c) ;
,
.

putchar printf ;

1.5.1

getchar putchar ,

.

, .

while ( )

18

#inelude <stdio.h>

/*

ain

1- */

()

int

getehar () ;
while ( != EOF)
putehar(e);
= getehar () ;

" ".

, ,
.

cha.r

. int , .

getehar

, v. t.
. EOF, "

(. end of file)".
getchar . t.
char npani BOF

ehar. int.

EOF <stdio. h>,


ehar .
,
.


. , ,

= getchar ()

.
.

while ,

1.5

19

#inelude <stdio.h>
n ,

/*

2-

*/

main()
{

int

while ((= qetehar()) != EOF)


putehar(e) ;

while

. ,

while
while .
, while , main .
, .

qetehar -

, ,

. .
(, ,
,
.)

, . ! =
=,

! =. ,

= qetehar()

!= EOF

= (qetehar()

!= EOF)

qetehar
( 2.)

1,

1- . qetehar ()

1-7.

! = EOF 1 .

EOF.

20

1.5.2

#include <stdio.h>
/*
main()

1-

*/

long nc ;
nc = ;
while (getchar() != EOF)
++nc ;
printf( "%ld\n", nc) ;

++nc ;
,

++ ,

). nc

= nc

+ 1, ++nc ,
--

{ ). ++ --

(++nc)

(nc++) ;

2,

++nc nc++

nc. .
,

int , long
long 32 .
, int long , int 16
, 327 67 ,
int.
%1d printf long.
double
(float ) . , while for
.

, , .

#include <stdio.h>
/*
main()

2-

*/

double nc ;
for (nc =

gechar() != EOF ; ++nc)

printf( "%. 0f\ n ", nc) ;

1.5

printf

%f

float

double; %.0f

21

, .

for ,
.

for . - ,
, .
.

,
,

while for

getchar , .
. while for
,

, ,
.
.

while for
.

1.5.3
.
,
,
. ,
.

jinelude <stdio.h>

/*

*/

main()
{

int

nl;

nl
;
while ((= getchar())
if ( =
'\n')
++nl;
printf( "%d\n", nl);

!= EOF)

while if ,
++nl. if ,
, ( )
.

22

,=, " " (

. EQ.

=
. :

=.

2,

, ,
.



. ,

. , , ' '

; ASCII , 65

. , ' '

6 5:

, ,
,

'\n'

10 ASCII. '\n '


; ,

"\n"

.
2 .

1-8 .

(- , ).

1-9.

, , -
-.

1-10.

, ,
(.

backspace)

\t ,

\b \\ .

,
.

1.5.4 3
, , , ,
, ,
, , .

UNIX wc.

1.5

23

#inelude <stdio.h>

/*
/*

#define IN 1
#define OUT

/*
main ()

*/
*/

*/

inte, nl, nw , ne, state ;


state=OUT;
nl = nw = ne = ;
while ( (= getehar () ) ! = EOF) {
++n.C;
if (= '\n')
++nl ;
if (=='' 11 = '\n' 11 e=::' \t' )
state =U;
else if (state=OU)
state = IN ;
++nw ;

printf ( "%d %d %d\n " , nl, nw , ne) ;

,
.

state

; " " ,
u .

IN u

1, ,
. , ,
,
. ,

nl = nw = ne =

.
,

24

nl

= (nw = (ne =0)) ;

1 1 ,

if (='' 11 = '\n' 11 = '\t')


" " .
(

\t

. )

&& ; ( )

11

&&

11


. - ,
,
. ,

, .
, ,

else,

if .
()

if

else

1
2

if-

else . , 1 ;
, 2

else , if

1-11 .

,
?

1-12 . ,

1.6
,

, , )

1.6

25

,
.
:

#inelude <stdio . h>

1*

. , : ,

*1

main()
{

int , i, nwhite, nother ;


int ndigit[10];
nwhite = nother = ;
for (i = ; i < 10 ; ++i)
ndigit[i] = ;
while ((= qetehar()) != EOF)
if (>= '0' && <= '9')
++ndiqit[e-'0'] ;
else if ( = ' ' 11 == '\n'
++nwhite;
else
++nother;
printf ("diqits =");
for (i = ; i < 10; ++i)
printf(" %d", ndigit[i]);
printf(" , white = %d, other
nwhite, nother);

11

'\t')

%d\n",

diqits = 9 3

1, white

= 123, other = 345

intndigit[10];

ndiqi t .
ndiqi t [] ,
ndigit[1], ... , ndiqit[9]. for
,
.


. ,

26

if

(>=

&&<=

'0'

'9')

. ,
-

'0'

'0', '1', ... ,

'9'
.

. ,

, ,
,

ehar

int

; - ''

9, ,
ndigit.

, ,

if

'0' && <= '9')


++ndigit[e-'0 ' ] ;
elseif (=' ' 11 == '\n' 11
++nwhite;
else
++nother;
(>=

==

'\t')

if

( )

else if

( )

else
.

. ,
~ ;
. (
)
, ( )

else. else ,
, .

1.7
else if

27

()

if

else.

; if
else ,
.

switch, 4 ,
,

. ,

switch 3 . 4 .

1-13.

.
;
.

1-14 .

1.7
,

, .
,

, .
,

; .
, ;
,
.

printf , getchar putchar


; .
** ,
power (m, n)

m, n .

, power (2 ,
5) 32. ,

,
( pow ( , ) )

power main ,

28

#include <stdio.h>
int power(int m, int n);
/*
main ()

power */

int i ;
for (i=O; i<lO; ++i)
printf ("%d %d %d\n", i, power (2, i) , power ( -3, i)) ;
return ;

/* power:
intpower(intbase, intn)

n ; n>=O*/

inti,p;
p=l;
for (i=l ; i<=n; ++i)
= *base;
returnp;

:
- - - ( , )

()
,

,
. ,
,

,
, . ,
,

na

n .

power main,

printf (" %d %d %d\n", i , power (2 , i) , power (-3, i)) ;

1.7

29

power

. ,

power (2 , i)

, 2

i. ( ;
4.)

power

int power (int base, int n)


,

power

power ,

:
. , ,

i
ma.in.

power

, l
1
.

power main
return. return :

return ;
;

return

, 1
. " " ,
.
.

return main .

main ,


. ,
;

return

main

, 1

intpower(intbase, intn);

30

main 1

int
npomomun
1 power.

int.

power

intpower (int 1 int) ;


,
.

ANSI

.
1

power

/* power : n
/* ( -) */
power(base n)
int base 1 n ;

n ; n>=O* /

int i ,

= 1;
for (i = 1 ; i <= n ; ++i)
= * base ;
return ;

1
;

int.

( )

power

int power () ;
,

power . ,
int ,

power
.

ANSI

1.8

31

, ,
, .

1-15 .

1. 2, .

1.8 -

,
, . ,

" " .
,
. ,

" ",

var ,

, .

;
.

, .
,

. , power
:

/* power: n
int power(int base, int n)
int
for

n; n>=O ;

2 */

;
(

= 1; n > ; --n)
base;

= *

return

n ,

(n for ) ;
i.

power , power
.

,
.
(, ) ,

32

.
.

.
, .

. .

1.9
.
,


. :

while ( )
if
( )


. , ,
.

,
. ,

qetline .
.
,

qetline ;


. ,
. ;
,

1.

. , ,
. ,

main

getline . :

1.9

#include <stdio.h>
#define AXLINE 1000 /*

33

*/

int getline(char line[] , int maxline) ;


void copy(char to[], char from[]) ;
/*
main()

*/

int len ;
/* */
int max ;
/* */
char line[AXLINE] ;
/ * */
char longest~] ;
/* .r .n 'r-* /
max = ;
while ((len = getline(line, AXLINE))
if (len > max) {
max = len ;
copy(longest , line);
if (max > ) /*
printf(" %s ", longest) ;
return ;

/ * getline:
int getline(char [] , int lim)

int

>)

*/

, * /

i;

for (i=O ; i < lim-1 && (c=getchar())!=EOF && c!=' \n'; ++i)
s[i] = ;
if (= '\n' )
s[i] = ;
++i ;
s[i] = ' \0 ';
return i ;

/ * : I! ufran" uto"; D!'1'110'


void copy(char to[], char from[])
{

int i ;
i

= ;

while ( (to [i]


++i ;

from[i]) != ' \ 0')

uto" 0Q110 0118:>*/

34

getline

main

getline

getline ,

int getline (char [] , int lim)


,

lim.

, .

getline , main. getline


return ,
power . , ,
getline int; int
,
.
; , ,

, .

void ,

getline '\ 0' (/l , )


, .

"hello\n"
,

, '\ ' .

printf

. ,
,

'\0 ',

. , main
?

getline

,
, .
,

main

.
.

1.1

35

,
,

(r.

overflow) .

getline ,
getline

( ) ,

1-16

,
, .

1-17

1-18

, , ,
.

1-19 reverse () .

1.1 w

main, line, longest , .,


main . main ,

; ,

i getline

i .

.
,
. ,
( 4 .

static ,

.)


, ,

. , .

.
,
(.

external) , . ,

. (

36

>N
)
,

, . ,
, . .
,
.
, ,
;

. , ,
;

. extern
, . ,
, ,

line , longest max


. , ,
.

#include <stdio.h>
#define

AXLINE

1000

int max ;
char line[AXLINE];
char longest[AXLINE];

/*

*/

/*
/*
/*

*/

*/

* /

int getline(void);
void copy(void);

/ * .
main()

; */

int len ;
extern int max ;
extern char longest[] ;
max = ;
while ((len = getline())
if (len > max) {
max = len ;
() ;

>)

if (max > ) /*
printf("%s ", longest) ;
return ;

*/

1.1
/* getline :
int getline(void)

37

*/

int , i ;
extern ehar line[] ;
for (i = ; i < AXLINE - 1
(e=getehar)) != EOF
line[i] = ;
if ( '\n' ) {
line[i] = ;
++i;

"

"

!= '\n'; ++i)

line[i] = '\0';
return i;

/* :
void eopy(void)

*/

int i ;
extern ehar line[], longest[] ;
i = ;
while ((longest[i]
++i ;

line [ i] ) ! =

'\ '

main , getline

n n , n

n. ,
n,
n , .

, n
;

extern.

extern n.

, n
n n ~ , n n extern
.

extern main,
getline . , n n n
n n

.
n n , n
"l ", "2", " ",

38

extern "2", "",


extern

header),

#include

h
, ,

<stdio h>
4, 7
getline ,

, getline () ()

,
;

void o
4

,
""
; ""
,

extern , -
,
,

,
-
, ,
,

,
,


,

,

1-20 detab
-
- -,

n n ?

101

39

1-210 entab

detab

- ,
?

1-22

" "

, n-

1-23



1-24
, (
, )

( )

( ,

2: ,

33

, .
.
.

.
.

ANSI

. (.

signed) ( .

unsiged )
t- .
n ; ,

long double

. .

const,

, ,

2.1

1 ,

. ; .

"_ " ;

,
.

, .
,
.

31

. -

41

42

31,
(.

assemblers)

(.

loaders)

. if , else, int, float, . , :


.
.


.
,

2.2 rn
:

char
int

,
,

float

double
, n
.

short long

short int sh;


long int counter ;

""'

int ,
.

short long

; int

short
16 , int 16 32 .

short

int

16 , long 32 , short int , ,

long.
signed unsigned char
. unsigned
,

2n , n
. , , char
8- , unsigned char

2.3

255,

43

signed char -128 127 (


char

,
.

1ong doub1e .
,
;

float, doub1e,

1ong doub1e

, .

<1imi ts. h> <float. h>


,

. .
2-1 . char, shor ,
int 1ong , , ,

. :

2.3

1 ()

L,

1234,

int . 1ong

123456789L;

int, , 1ong.
u

u,

( 1-2)

(123. 4)

; doub1e,

u1 UL

unsigned 1ong.

F float ; 1

1ong doub1e.
,

. ()

;
.

037,

31
Ox1f OxlF.
L, 1ong ,

unsigned: OXFUL unsigned Jong

15.

,
, ''

. ,
ASCII , ' '
48, .
'' 48, ,

44

n,


. n

n, n n
.

, n

\n

( )

, n . n,

n n
'\'
( 7)

'\xhh '
hh n ( .. 9 , .. f , . . F) .
n

"

#define V '\013' /* ASCII */


#define BELL '\007' 1* ASCII * /

#define V '\xb' 1* ASCII */


#define BELL ' \7' 1* ASCII */
n :
\

()

\b
\f
\n
\r

(.

carriage

\\
\?
\'
\"

\xhh

return)

\t
\v

'\0' n ,
'\0'
, .
.

(.

null)

. , n

n ,

2.3

45

, , ,

#define AXLINE 1000


charline[AXLINE+1];

#define LEAP 1 / * */
intdays[31+28+LEAP+31+30+31+30+31+31+30+31+30+31];
~ ,
:

"

sum string"

" " 1* n * 1
, ,
. ,
;

\"

(. tt) :

" hello , " " world"


" hello, world"



.
, .
'\ ' ,


.
,

strlen ()

, ,

,
'\ '

46

/* strlen:
int strlen(char [])
int i;
while ( [i]
++i;
return i;
strlen

*/

!= '\0')

<string. h> .


: '' " " .

,
. , , ( )

'\0'.

. .

, , ,

enumboolean {NO,

};

, 1, ,
.
,

,
:

enumescapes { BELL= '\', = '\b', = '\t',


NEWLINE= '\n', V= '\v', RETURN= '\r'} ;
enummonths { JAN= 1, FEB, R , APR, , JUN,
JUL, AUG, , , NOV, DEC } ;
/* FEB 2, R 3, */
.
.

#define,

enum ,

. ,

#define
. , (.

debbuger)

2.4

47

2.4
,
.

int lower , upper , step;


ehar , line [1000] ;

.

int
int
int
char
c har

lower ;
upper ;
step ;
;

line[1000] ;

,
.
.
,
:

ehar
int
int
float

= ' \\ ' ;

= ;

limi t

=AXLINE + 1 ;

= 1 . -5 ;

,
,

, .

.
.
(.. ) .

const

.

.

const

48

const double = 2 . 71828182845905 ;


const char msg [] = " warning";

const

int strlen (const char []) ;


const ,

2.5

+ ,

- , *, 1, %.

.
%

. , 400 ;

4,

100.

if

( (year %4 = && year %100 != 0) 11 year %400 ==)


printf ( "%d is leapyear\n " , year) ;
else
printf ( "%d is not leap year \ n ", year) ;
%

float

double .
%,

. (.

overflow

uderflow)
+ - ,
*, 1, %, , , + -.
.

2 . 1 ,

2.6

49

2.6 n nr

>

<

>=

<=

.
=-=-

! =

i < 1im-1

i < (1im-1) , .
1 1 &&

&&

11

. ,

get1ine
for

1:

(i=O ; i < 1im-1 && (e=getehar ()) ! = '\n'


s[i]

&&

! = EOF ; ++i)

= ;

,
, , ,

i < 1im - 1 .

, ,

, , EOF ,

getehar;

&&

1 1,

i < 1im-1

&&

(e=getehar()) != ' \n'

&&

!=EOF

. ,

! =

(e=getehar()) != '\n'
,

'\n'.

! ,
1.

50

if ( !valid)

i f (valid

== )

!valid

( " " ) ,

2-2.

for

, &&

2.7

1 1.

. ,
"

" " ",


, , ,

+ i. ,
float , .

,
,

int,

, , .

na

char

.
.

atoi,

/* atoi :
int atoi(char [])

*/

int i , n;
n

for (i = ; s[i] >= '0'


n
10 * n + ( [i] return n ;

s[i] <= '9 ' ; ++i)


'0') ;

&&

1,

2.7

51

s[i]- '0'
[i] , ,
''

, '1' ,

char int 1ower,

ASCII, .

1ower .

/ * lower: /
int lower(int }
if

(>=

' '

return

return

&& <=

''

ASCII */

'Z'}
'' ;

, , ASCII ,

,

. , EBCDIC,
.

<ctype. h >, ,

,
. ,

>=

tolower
lower . ,

'' & &

<= ' 9'

isdigit(c}
<ctype

. h>.
char int .

char , (.

siged) (. usigned) .

char

int,

, .

char 1
. , ,

(" "}

,
.

52

,
, .
, ,
,

. ()

signed

ehar ,

unsignedeapaja .

i > , && 11,


, 1, .

d = >=

'' & &

<= '9'

. ,

isdigit "" .

if, while, for ,

. " " , ,

,

. , (
),

+ -,

, " "

"" . " " .


.

unsigned ,

- long double , long


double.
- , double,
double.
- , fioat ,
BOfioat.
- , ehar short int, int.
-, long , long.
fioat
double ;

, <math . h>
.

fioat

, , ,

unsigned

.
,
. ,

int

2.7

53

16- , long 32. -lL < lU, lU, unsigned


int, signed long. -lL > lUL -lL
unsigned long, .
;
, . .
.

, ,
n.

.

inti;
ehar ;
i = ;
c=i ;
.
.

float i int, , = i i =
float int . n
double floa t, ,

n ,
n .

, ehar short n int , float


double.
int double ehar floa t.
, n n
, n n

cast) .

onepamop (.

( )

. n n
n ,

. n ,

sqrt

double, na

, n .

<math . h>.)

sqrt ( (double) n)

( sqrt

, n

54

doub1e

sqrt .

.
, .
, ,

. , sqrt

d oub1e sqrt (doub1e)

root2 = sqrt (2)


2 doub1e 2.
.

( .

seed); :

unsigned 1ong int next

/ * rand :
int rand(void)

= 1;

.. 32767 */

next = next * 1103515245 + 12345 ;


return (un signed int) (next/65536) % 32768 ;

/ * srand : n rand()* /
void srand(unsigned int seed)
{

n ext

= seed ;

2-3 . htoi () ,
( )
.

2.8 3

9, f,

F.

++

2.8

--

55

1 .

++ ,

if

(=

'\n')

++nl ;
n ++ --

n (n n , ,

++n) n
n++) . ,
n . ++n n
, n++ , n
n (n n:

.
,

++n n++

n 5,

x=n++ ;
n

x=++n;
n . ,

. n

(i+j) ++ .

,
,

if

(==

'\n')

nl++ ;
.
. n ,

squeeze ( , ) , n
:

1* squeeze :

void squeeze (char

[] ,

*1

int )

int i,

for (i=j=O ; s[i] != ' \0' ; i++)


i f (s[i] !=)
[ ++] = [ i] ;
[] = ' \0 ';

56

r ,

,
.

i f (s[i]

{
=s[i] ;

!=)

[]
++ ;

if

(=

qetline

1 .

'\n ' )

s[i]
++i ;

= ;

if

(=

'\n ' )
[i++] = ;

streat ( , t) ,
streat

strcat

/* strcat: ~ ro t JCP&jor
void streat(ehar [] , ehar t[])

; : 1010 01!

*/

int i ,

i = = ;
while (s[i] != '\0') /* */
i++ ;
while ((s[i++] = t[j++]) != '\0' ) /* n t */

, ++

, ,

2.9

2-4.

squeeze

57

(1 , 2)

1 2 .

2-5

. any ( 1, 2) ,
-1 1
2 . ( strpbrk ja
, . )
1, 2 ,

2.9

; , ,
. ,

char, short, int long ,

&

<<
>>

(.

shift)

()

&

, ,

n =n

&

0177

7 n, .
1 :

1 SET_ON ;

1 ,

1 _ ON .

"'
, .

&
&&

1 1,

--.

2,

& , && .

<<>>

, . << 2
, ;

4.

unsigned

58

,. " )

( ,.

") .

- ; . .
1- - .

= &

- 077

. &

-077

, , , &

0177700 ,

16- .

- 077

getbi ts

( , ,

n)

( ) n-

getbi ts

( , 4 ,

3)

. ,

4 ,

3,

2,

1* getbi ts :

n */
uns ignedgetbits (unsignedx , intp , intn)

return

>> (p+1- n))

>> (p+1- n)

& - ( - <<

n);

n -
<< n n ;
, - n .
. - ;

2- 6 .

setbits

( , ,

n,

) n

n- ,
.

2- 7 . invert ( , , n) n
, 1

, ( . .
)

2 - 7.

rightroot
.

(,

n)

2.10

59

2.1

i=i+2
n n ,
n nn

i +=2
n +=

onepamop .

n (n +
n) n

on=, on

+ - * 1 %<< >> & 1


,

1 n .
:

*=+

(+

1)

=*+1

n n

bitcount ,

, n .

60

1* bitcount:

1-

*/

int bi tcount (unsigned )


{

intb ;
for (b=O;
if

( &

!= ; >>=1)

01)

b++ ;
returnb ;

unsigned

, ,
, .
,
. ,
"

2 i" " i 2 ", " i , 2

, , i ". i += 2 i =

+ 2.

yyval [yypv [+4] + yypv [pl+p2] ] += 2


,
,

, .

.

;

while ( (= getchar () ) ! = EOF)

(+=, -=, . )
, .
,
, .

2-9. , &= (-1)

. .
bi tcount.

2011

2.11

61

n 33

if(a>b)
z=a;

else
z=b;

b ,

"?:"

2 : 3

()

, ,
3

2 3 ,

z b

z = ( >b) ? : b; /* z =max(a, b) */

2 3
, ,

float

n int,

(n>O) ?f: n

float,


?:

,
n- ,

10 ,

, ( )

for (i=O; i<n; i++)


printf("%6d%c", a[i], (i%10=9 11 i=n-1)? '\n'

' ');

62

, n- .
.
,

i f/ else.

printf( " Youhave %ditem%s . \n" , n , n==l ? "";

2-10.

"" ) ;

l ower,
if-else.

2.12
2 . 1 ()
, .
;

, ,

* , 1 % ,
+ -. "" ()
. -> .
; , sizeof (
) . * ( ) & (
) , ',' ( ) .

2.1-

onepamopume

() []-> .
! - ++ -- + -

(type) sizeof

* 1%
+-

<<>>
<<= >>=
== !=

&

&&

11

?:

= += - = *= / = %= &= = 1= <<= >>=

+ , -,

* .

2012

& ,

63

= ! =

i f ( ( & )

== 0)


, ,
( && ,

11 , ? :

' ,'

) ,

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++;


( )

,
printf, .)
, ,
. ,

,
,
.

3:

( ) ,
.
;

3.1

i++ printf {... )

()

- ,
= ;

i++ ;
printf{ ... ) ;
, - ,
.

, ,
.
;

if , else , while , for . {

; 4. )
-

3.2 if-else

if - else

65

66

if(upo)

else

else . , ;

( . . , ) , 1

( )

else , 2

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 ;

else- if

3.3

67

if (n>O)
for (i=O ; i<n; i++)
if ( [i] >) {
printf(" . .. " ) ;
return i;
/ **/

else

printf ( " error --n is negative\n") ;


,

else if.
;
, ,

. , -

if

if
z =

>b)

= ;

else
z=b;
, if ,
,.z = ;" - .

3.3 else- if

if

()

else if

()

else/ff ()

else if

()

else

if
. ; ,


. ,

68

else

1 , ,

;

else

, ,

""

1
v v

( n- 1 )
v

-1

, , ,

v ,
,
1
1

/* binsearch: n v[O] <= v[1] <=


int binsearch(int 1 int v[] 1 int n)

<= v[n-1] */

int low , high, mid;


low = ;
high = n - 1 ;
while (low <= high) {
mid = (low+high)/2 ;
if ( < v[mid])
high = mid - 1 ;
els~ if ( > v[mid])
low = mid + 1 ;
else /* n
return mid ;
return -1 ; /*

*/

*/

v [mid] ;

else- if

switch

3.4

3-1.

69

, (

) .
.

3.4switch
swi tch

.

switch

( )

- :
-:

defaul t :

(. )
.
, .

. defaul t
. defaul t
;

, .

defaul t .
1 ,
, , if . . . else
if . . . else . , swi tch :
break swi tch .
(a. label),
, npeoza ,

. break return
swi tch . break , ,
while , for do ,
.

.
,
. , ,

break ,
.
.
,
, .

70

#include <stdio. h>


main() / * ,

n5 ,

r* /

intc, i, nwhite , nother , ndigit[lO];


nwhi te = nother = ;
for (i=O ; i<lO ; i++)
ndigit[i] = ;
while ((= getchar ()) ! = EOF) {
switch () {
'0': '1' : ' 2':
'5': ' 6 ': ' 7 ':
ndigit[c-'0 ' ]++ ;
break;
' ':
' \n':
'\t':
nwhite++ ;
break;
default:
nother++ ;
break ;

' 3 ':
'8 ':

' 4':
' 9' :

printf( "digits=" ) ;
for (i=O ; i<lO; i++)
printf( "%d" , ndigit[i]);
printf ( " , white = %d , other = %d\n",
nwhite , nother) ;
return ;

, break
( defaul t)

, .
switch,

3-2.

( , t)

\n

\ t,

...___

swi tch.

, , .

- while

3.5

for

71

3.5 - while for


while for .

while

()

, . ,

, , .

.

for (l; 2; )

upl ;

while

(u2 )

continue, 3 . 7.
, for . up l
, 2 .
, -
. upl , ,
2, ,

for ( ;; )

" " ,
, , break

return.

for while .
,

/
while

((

; /*

= getchar())

'

'

11

==

'\n'

11

n n

' \t ' )

*/

, while .

for

72

,
.

for (i=O ; i<n; i++)

for .

, , ,

for

for

. ,

for ,

atoi

2;

-.

atof ,

.)

:
,
,

#include <ctype. h>


/* atoi : n
int atoi (char [])

2 */

inti , n, sign ;

for (i

= ; isp&ce(s[i]) ;

i++) /*

r n */

sign = (s[i] == '-' ) ? -1 : 1 ;


i f (s[i] == '+' 11 s[i] == '-') /*
i++ ;
for (n=O; isdigit(s[i]) ; i++)
n=10*n+ (s[i]- '0');
return sign *n ;

(+/-)*/

3.5

- while

for

73

strtol
5

- sort

. ,

1959

. .

n .

,
.
,
.

/* shellsort : v[O) . . . v[n-1)


void shellsort(int v[) , int n)

*/

int qap , i,

temp ;

for (qap = n/2; qap > ; qap /= 2)


for (i = qap; i < n ; i++)
for (j=i-qap ; >= && v[j)>v[j+qap) ; j-=qap) {
temp = v[j) ;
v[j) = v[j+qap) ;
v[j+qap] = temp ;

, ,

n/2

. , , n

nap nap
qap .

qap ,
. for
,

for

(',' ),

. , ,

. ,

for ,

, n, .

reverse () , .

74

#inelude <string.h>

/* reverse: */
void reverse(ehar [])
{

int

i,

for (i =

s[i] =
[]

= strlen(s)-1; i <

i++,

--)

[i] ;
[];

=;

,
, .

.
, for

reverse
.

reverse,

for (i=O, j=strlen(s)-1; i<j; i++ ,


e=s[i], s[i] =[] , [] =;

3-3.

--)

expand (1 , 2)

xyz 2.

a-z 1 abe ...

b- - zO - 9
z . - .

--

\
. n

do- while

1, while for

. ,
, do- while, ,
;

do

do

while

()

3.6

Do- while

75

, .

, , , , .

, . ,

do- while repeat- until .


do- while for
while. , - , ,
i toa ,
( atoi) .
, ,
.
, .

/* itoa: n
void itoa(int n, char [])

*/

int i, sign;
if ((sign =n) <) /* (+/-)
*/
n = -n;
/* n n */
i = ;
do {
/* */
s[i++] =n % 10 + '0';
/* */
} while ((n/= 10) >);
/* */
i f (sign < )
[i++] = '-, ;
s[i] = '\0';
reverse(s) ;

do - while , ,
, n
. ,

, ,
while while .

3-4 .

i toa , n
- (2n__- 1 ) .

, .

3-5. itob (n, , b)

, i

b, .

tob (n, , 16) n.

76

3-6 .

itoa , , n

. .

n
n n

. ( . n .

3.7 break continue


n n ,

n n .

break n
for , while do , swi tch. break n
swi tch .
, trim, ,
, break n
n

, ,
- " " .

/ * trim:

int trim(char

*/

[])

int n ;
for (n= strlen(s)-1 ; n>=
if (s[n] != ' ' && s[n]
break ;
[n+l] = '\0';
return n ;

n- -)
!= '\t '

&&

s[n]

!= '\n ')

strlen . for

. n n n

n . ( , )


n .

continue

break,

n ;

n n n

for , while do . while do


for , n
. c ontinue
, swi tch. n continue swi tch n
;

n .

goto

3.8

77

,
; .

for (i = ; i <n ; i++){


if (a[i] < ) /* r ~~
continue ;
... /* ~ ~~ */

continue

*/

n,

3.8 goto

goto
goto

goto.

goto .

.
break,

. :

for ( ... )
for ( ... )
if (disaster)
gotoerror ;
}

error :
/*

~ ~

*/

goto . ( . )

,
b .

78

for (i=O; i<n ; i++)


for (= ; j<m ; ++)
i f (a[i] =b[j])
goto found ;
/*
found :
/*

*/

a[i] -- b[j] * /

goto 1

. 1 1
:

found = ;
for (i = ; i < n && !found; i++)
for ( = 0 ; < m && !found; ++)
if (a[i] == b[j])
found = 1 ;
if (found)
/* : a[i-1]
b[j-1] * /
1

/*

*/

1 1

goto 1
goto .
1 , goto

, .

4:



, .

,
.


; ,
. .
,

. .
, .

ANSI
1,

. , ,
, .

. , ,
.

. :
.

t, , .

, .

4.1


, "" . (

79

80

grep UNIX) . ,
"ould"

Ah Love! could you and I wi th Fa te conspire


grasp this sorry Scheme of Things entire ,
Wouldnot we shatter it to bits -- and then
Re-mould i t nearer to the Heart' Desire!
:

Ah Love! could you and I wi th Fa te conspire


Wouldnotwe shatter it tobits - - and then
Re-~ould i t nearer to the Heart' Desire!

while (n )
if ( )

<.

main ,

n.
,
,

. .
" n ",

getline ,

1 , " " , printf


. n
t: .

strindex ( , t)
t , - 1

t.

, -1 ,

.
n ,

strindex ,
strstr

strindex ,

).

. ,

. , ,

n .
,
-

401

- ,

81

qetline ;

tinclude <stdio oh>


tdefine AXLINE 1000 /*

~ n

int getline(char line[], int max)


int strindex(char source[] , char searchfor[]) ;
char pattern[] = " ould"; /*
/* r
main ()

~ ~

*/

*/

char line[AXLINE]
int found = ;

while (qetline(line , AXLINE) > )


if (strindex(line, pattern) >=
printf( "%s ", line) ;
found++;

return found ;

/* qetline:
int getline(char
int

,
[],

*/

int lim)

i ;

while (--lim > && (c=qetchar())


s[i++] = ;
if ( == '\n ' )
s[i++] = ;
s[i] = '\0' ;
return i ;

'= EOF &&

! = '\n ' )

*/

82

/* strindex: t
int strindex(char [] , char t[])
int i ,

-1

*/

k;

for (i = ; s[i] != '\0 '; i++) {


for (j=i , k=O ; t[k] != ' \0' &&

[j]=t[k];

++ ,

k++)

if (k > && t[k] == '\ 0 ')


return i;
return -1;


- - - ( )

dummy() {}
.
, .
- ,

int .

.
,
, .
,

,
.

return
. return :

return ;
,

. ,
.


. , return , ;
, .

4.2

83

"

"
.
, , , ,

. , , "
" .

main ,

.
,
, .

UNIX,

, ,

. ,

main. , getline . trindex. .


main.

getline. strindex .


*. ,

. out. , ,

main. ,

main. getline . strindex.

". " . "


.
4-1. strindex (, t)
() t , -1 .

4.2

( void) int.
? , , sqrt, sin
double ; .
,

atof () ,
atof
atoi 2 .

,
.

84

,
.

atof
<ma th. h>.
, atof

, .

iinclude <ctype. h>


/* atof : ~ ~
doub1e atof(char [])

~n

double * /

doub1e val , power ;


int i , sign ;
for (i

= ;

isspace(s[i]) ; i++) / *

:r n * /

sign = (s[i] == ' - ' ) ? -1 : 1 ;


if (s[i] =
' + ' 11 s[i] == ' - ' )
i++ ;
for (va1 = 0 . 0; isdigit(s[i]) ; i++)
va1 = 10.0 * va1 + (s[i] - ' 0 ' ) ;
if ( [i] == ' .' )
i++ ;
for (power = 1 .0; isdigit(s[i]) ; i++)
va1 = 10.0 * va1 + (s[i] - ' 0 ');
power *= 10 ;
return sign * va1

power ;

iinc1ude <stdio.h>
idefine

AXLINE

/ * ~
main ()

100
~

*/

doub1e sum, atof(char []) ;


char 1ine[AXLINE] ;
int get1ine(char 1ine[] , int max) ;
sum = ;
while (getline(line, AXLINE) > )
printf( "\t %g\n", sum += atof(line)) ;
return ;

4.2

, , ,

85
atof

in t. ,

atof

( ).

, , ,

double sum, atof (char []) ;


double atof

char [] ,

double .

tof

. atof
main ,

. , ( )

, atof

, , tof double ,
main int ,
.

, .
,
,

swn += atof (line)


, ,
,

int,

. ,

double tof () ;

atof; n .
n

. n
. ,
, void .

, atof n ,
atoi ( int) atof :

86

1* atoi:

int atoi (char

atof */

[])

double atof (char

[]) ;

return (int) atof () ;

return.

return ;
, .
,

atof , double ,
return , int, atoi

int. ,
.

(. wa ring).

4- 2.

atof ,

123. 45-,
.

4.3

,
. ""
"",
.

, .
,
.


, ,
. ( .)
, N
.

,
, -

4.3

87


.
, .

, ,

. ,

1, ,
,
.

.
;
. , , ,

. ,
, ,

, .

.

+, -,

* 1.

()

,
.

,
;

(1-2) * (4+5)

12-45+*
;
.

. , ( .

stack);

, , . ,

1 2
-1 . ,

4 5, 9 .
,

-1 9

, -9.
. ,
11

, .

88

__)

while (

i f ( )

els e if

( )

else if ( )

else

n ,

,
,

na

, n ,

n n. , n
n n.
, n , n
nn .

main,
, n .

, 1114in n
; n

main

, n. ,
n n
push , main .
- .

n ,
:

#include -ou,
#define -o u,

main

1114in () { . . . )
push

voidpush ( double f) { . .. }
double (void) { . . . }
intgetop(chars[] ) { . . . }
getop

4.3

#include <stdio.h>
#include <stdlib.h>

89

/* for atof() */

#define 100 /* . r.n n n * 1


#define NUER '0' /* r n */
int getop(char []);
void push(double);
double pop(void);
/*
main ()

n */

int type;
double 2;
char [];
while ((type = getop(s))
switch (type) {

!= EOF)

NUER:

push(atof(s));
break;

' +' :

push(pop() + ());
break;
'*':
push(pop() * ());
break;
'-':
2 = () ;
push(pop() - 2) ;
break ;
'/':
2 = () ;
i f ( 2 !
. )
push(pop() 1 2);
else
printf("error: zero divisor\n" );
break;
'\n ':
printf("\t%. 8g\n", ()) ;
break;
default:
printf("error: unknown counand %s\n",
break;

return

) ;

90


. ID4in

swi tch

swi tch 1 . 4.

* ,

push(pop() -

());

/**/


. ,

#define

AXVAL

int
double

val[AXVAL] ;

= ;

main.

100 /*

/*

n n

/* push : f
void push(double f)

/*

val */
*/

*/

xyn */

if

<

AXVAL)

val[sp++]
f;
else
printf("error: stack full, can 1 t push %g\n" , f);

/* :
double pop(void)

*/

if

>

return val[--sp] ;
else {
printf( " error: stack empty\n" ) ;
return 0.0;

. , n ,
push , . ,
main -

.
getop, . .
. . --

4.3

91

. ,
. , {

, NUER, .

#inelude <etype . h>


int geteh{void) ;
void ungeteh{int);
/* getop:
int getop{ehar
int i,

while { { []

= geteh {) ) -

s[l] = '\0' ;
if {!isdigit{e) &&
return ;
i

*/

[])

'

'

11

'\t l )

!= '. ' )
/* */

= ;

if {isdigit(e))
/*
while {isdigit{s[++i] == geteh{)))
if

(==

' .1 )
/*
while {isdigit{s[++i] == geteh{)))

*/

* /

s[i] = '\0';
if { != EOF)
ungeteh(e);
return NUER;

geteh ungeteh?

,
.

: ,
. ,
,

"

" . , 1
1
. 1 ""
1 .
;

geteh
ungeteh ,

getch .
.

ungeteh
geteh

92

, getchar . ,
.
getch ungetch


, . ,

getch , ungeteh

#define BUFSIZE 100


char buf[BUFSIZE]; /* ungetch */
int bufp = ; /* n
int getch(void) /*
return (bufp >

void ungetch(int

/*

( )

? buf[--bufp]

ro

buf */

*/

: getehar() ;

*/

if (bufp >= BUFSIZE)


printf( " ungeteh : too many characters\n" );
else
buf[bufp++] = ;

ungetc
; 7. ,
, .

4-3.

. (%) .

4-4.

,
.

pow.

4-5 .

sin , ,

4-6.

<math . h>

4.

. (

26 .)

.
4-7 . ungets ()
. ungets buf bufp , , ,

ungetch?

(. )

4.4

4-8.

4-9.

getch

getch

ungetc h

ungetch

EOF .

93

4-10.

EOF

getline

; getch ungetch .
.

4.4 (. }
,
, ;

,
.
:

, ,

- ?

. ,
,
.

,
.
, .
.
,
.


, .

, main , , val , push ,


, . .

94

main () { ...
int = ;
double val [AXVAL] ;
void push (double f) {
double (void) { ... }

val

push

, ,

; .
main, push.

,
, , , ,

extern.

(, )

int ;
double val [AXVAL] ;
,
val, , . ,
. ,

extern int ;
extern double val [] ;
int , val

double

( )

, .

,
;

extern

. (

extern

extern

, push
, val

( . )

4.4

95

.
:
1:

extern int ;
extern double val [] ;
voidpush (double f) {
double (void) { ... }
2:

intsp= ;
double val

[AXVAL]

extern l

, ;
l .

val

4.5 3

,
.

main ,
main . ; push , ,
staek. ; gettop , gettop . . , geteh ungeteh
geteh . ;

96

calc.h:
#define NUER ' '
voidpush (double) ;
double (void) ;
intgetop(char []) ;
int getch (void) ;
void ungetch (int) ;
main.c:

getop . c:

#include <stdio. h>


#include <stddlib. h>
#include "calc.h"
#defineAXOP

100

#include <stdio . h>


#include<ctype . h>
#include " calc.h"
getop() {

main() {

stack . c:
#include <stdio. h>
#include " calc . h "
#define AXVAL 100
intsp=O;
double val [AXVAL] ;
voidpush(double) {

getch . c:
#include<stdio.h>
#define BUFSIZE 100
char buf [BUFSIZE] ;
intbufp=O ;
int getch (void) {

double (void) {

void ungetch (int) {

-
.
,
,

. ,

() calc. h ,

. ( #include
4 . 11) . :

,
.

, , ,

;
. ,
.

4.6

4.6

97

val

staek . ,

buf bufp geteh. ,

statie,

,
. ,

statie buf bufp


geteh-ungeteh,
, geteh ungeteh.

statie .
, :

statie ehar buf [BUFFSIZE] ;


statie int bufp = ;
int geteh (void) { ... }
void ungeteh (int

{... }

, buf
bufp
. ,

push ,

val .
statie , , ,

, , . ,
, .
,
.
.

,
,
.
,
.

4-11. getop,
ungeteh. : statie .

98

4.7

register

.
,
. , .
:

register int ;
register char ;

.
:

f (register unsignedm , register long n)


{

register int i;

.
,
. ,

register

. ,
( 5) , ,
, .
.

4.8 n


. ,

.
( )

,
.

.

4.8
if ( n

>

99

) {

int i;

/*

for (i

= ;

i* /

i < n; i++)

i if

; i i .
,

static

.
, ,

, .
,

intx;
inty;
f(doublex)
{

doubley;

f,
double, f,

int. .

4.9
,
. ,
.

(. . )
.

,
:

100

intx=1;
char squote = '\' ' ;
long day = lOOOL * L *

*24L ;

/* /*/

,
; ,
. ,

.
,
;
, . ,

3 . 3

intbinsearch(intx , intv[] , intn)


{

intlow=O;
int hight =n - 1 ;
intmid;

int low , high, mid;


low= ;
high= n - 1 ;
,
.

. , , ,

.

. ,

days

int d4ys [] = { 31, 28, 31, 30, 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31} ;


,

12 .

4.10

101

,
, .

.
, , ,

.
;
:

char pattern []

=" ould";

charpattern[]

= { ' ',

' u' , ' 1 ' , ' d' , '\0 ' };

, {

'\0').

4.1
; ,

.
. ,
:
,
.
.
,

, itoa .

6.

, printd
, .

, .

iinclude <stdio . h>

/* printd : n
void printd(int n)

i f (n

< 0) {

putchar ( ' -');


n = -n ;

1 10)
printd(n 1 10) ;
putchar(n % 10 + ' 0 ' ) ;
if (n

*/

102

,
,

, . printd (123)
printd n=123. 12 printd, , ,
1 . printd 1 ,
. printd 2,
. printd 3
.

quicksort ,
. . .

1962 . ,

, .

,
; .

quicksort , ,

.
.

/* qsort : v[left] . .. v[right]


void qsort(int v[] , int left , int right)

*/

int i, last ;
void swap(int v[] , int i , int
if (left >= right) /*

) ;

*/

return;
swap(v, left,

(left + right)/2) ; /* n
v[O] */
last = left ;
for (i = left + 1 ; i <= right ; i++) /* n */
if (v[i] < v[left])
swap(v , ++last, i);
swap(v , left, last) ; /*
n

*/

qsort(v , left , last-1) ;


qsort(v , last+1 , right) ;

swap ,

qsort .

4.11

103

/* swap: v[i] v[j] */


void swap(int v[] , int i, int )
{

int temp ;

temp
v[i]
v[j]

v [i] ;
v[j] ;
temp ;

qsort,
n.
,
. , n, . ,
n

.
, ,
. .

4-12.

printd ,

i toa ;
.

4-13 . reverse ()

4.11

#include ,

#define
.
.

4.11.1

(
)

#define

104

#include " - - "

#include <- - >


- - . - -
, n
n; n ,

< >,

n .

#include .

#include n
#define extern ,

nn nn

, , n,

<stdio.h>. ( , ;
nn n) .
#include
.
,
. ,
, n.

4.11.2

#define

n -
__. #define
.

n ,
, n

.
#define n
. n
.
, n,
n

printf ( " " )

N.

. n:

#define forever for (; ; ) / *

*/

4.11

, forever,

105

. ,
max:
#definemax(A , ) (()> ()? () : ())
, max in-

line ( ) .
( ) .

x=max(p+q, r+s) ;

= ( (p+q) > (r+s) ? (p+q) : (r+s)) ;

,
;
max ,
.

max , .
;
, . :

max(i++, ++)

/*

*/

,
;

#define square(x)

/*

*/

square ( z + 1) .

, .
<stdio. h>, getchar putchar


. <ctype. h>, ,
.

#undef
, , , , ,
, :

106

#undef qetchar
int qetchar (void) { ... }
(
). , #
,

.
, ,
.

#define dprint (expr) printf (#expr"

= %q\n", expr)

dprint (/)

printf ("/" "= &q\n",

/) ;

printf ( "/ = &q\n",

/) ;

, " \" \ \\ ,
.

##
.

,

##

##,

. , paste
:

#define paste (front, back) front ## back

paste (name, 1)

namel.

## ;
.

4-14. swap (t,x,y)

t. ( ) .

4 .11

107

4.11.3 n n

.
,

iif (
sizeof , enum ).
, #endif #elif #else
.

#elif else-if . defined


(name) #if 1 name , .
, hdr . h

#if !defined(DR)
#define DR

/*

hdr.h

*/

#endif
hdr. h DR ;
n

#endif .
n . ,

,
.

:

#if

==

SYSV
#define DR "sysv. h "
#elif = BSD
#define DR " bsd.h"
#elif = D
#define DR " msdos . h "
#else
#define DR " default . h "
#endif
#include HDR

\08

ftifdef

iifndef
ftif

ftifndefDR

ldefineDR

/*

iendif

hdr . h

*/

5:


; ,

goto

, ,

ANSI


, vid* (

void)

char*

5.1

char , short
long

( ) ,

char ,

109

110

& ,
= & ;

"
" . & :
. , ,

register

onepamop onepamop

; ,
.
, ip int.
&

int
1,
int *ip ;

=2,

= &;
= *ip;

*ip
ip

/* ip n
/ * 1 */
/* */
/* ip n

ip

z[10] ;
/* ip

= ;

= &z[O] ;

*:

int * /

*/

z[O] */

, , z .

ip ,

int* ;
;

*ip

int

,
.
. ,

doub1e*dp , atof(char*) ;

atof

*dp

atof ()
char.

double

,
:
. ( : "

void"

.
5.11)
ip , * ip
,

5.2

111

*ip=*ip+10 ;

*ip

10 .

& ,

y=*ip+1

ip,

1 ,

*ip += 1
ip ,

++*ip

(*ip)++
; ,

ip,

* ++

, ,

. , iqe int ,

iq= ip ;
ip iq, iq

ip.

5.2

, ,

. ,

swap . ,

swap(a, b) ;

swap

112

void swap(int

int

/*

*/

int temp ;

= ;

temp

= temp;

, swap b
.

b.

, -
:

swap (&, &b) ;


& , &
swap

voidswap(int*px , int*py) / *
{

int"temp;
temp=

* ;

*= *;
*=

temp;

:
:

b :

* *

*/

5.2

113

n
.

getint, ,
, ,

. getint
.
, ,

EOF,

getint EOF , return

,
.

scanf ; n 7 . 4 .

getint:
intn, array[SIZE], getint(int *) ;
for (n=

n< SIZE && getint (&array[n]) !=EOF; n++)

array [n]
n .
array[n] getint .

getint .
getint EOF ,

, , ,
.

linclude <ctype . h>


int getch (void) ;
void ungeteh (int);
/* getint: ro
int getint (int *pn)

*pn

*/

int , sign ;
while (isspace (= getch ())) / * n
if (!isdigit(e) && '=EOF&&c !='+ '
ungetch () ; 1* */
return ;

nu

&&!='-')

*/

114

sign= (= ' -')? -1 : 1 ;


i f (= ' +' 11 =='- ' )
e=geteh() ;
for (*pn =; isdigit () , = geteh ())
*pn = 1 * *pn + ( - ' ' ) ;
*pn *= sign ;
i f ( !=EOF)
ungeteh(e) ;
return ;

getint , *pn int . ,


geteh ungeteh ( 4. )

, ,
.

5-l .

getint

, .

5-2.

getfloat ,

getint ,
getfloat?

5.3

, ,
.
,
.

, .

inta[10] ;
10 , 10
[], [1] ,

... , [9] .
:

[][1]

[9]

[i] i- .

'\

.....

503

115

int *;

= &[];

; ,
[]
:

[]


= *;

[]
,

+1 ,

+i

i- ,

- i i- , [],

a[i]

[1],

+i

[i],

(pa+i)

(+1)

[]

,
"

"

, +1

, pa+i i-

,
,

116

=& [] ;

.
,

&

[i]

= ;

[i]

* (a+i) . [i] ,
* (a+i) ; .



&

[i]

& ,
a+i , , : a+i i -

. , ,
;

pa[i]

(pa+i) .

(. offset).

. , , = ++

. ; = ++ .
,
, .

, ,
, , .

strlen,

/* strlen :
int strlen(char *)

*/

int n ;
for (n = ;
n++ ;
return n ;

!= ' \0 ',

++)

, ;
++

strlen,
strlen .

strlen( " hello, world" ) ;


strlen(array) ;
strlen (ptr) ;

/* string */
/* char array[lOO] ; */
/* char *ptr ; */

5.3

11 7

char

char

*;

[] ;

;
.

1 .
1 .

. 1

f(&a[2])

f(a+2)
f
[ 2] . f

f(intarr[]) { ... }

f (int *arr) { ... }

l .
1
; [-1]

[ -2)

[]

. 1
.

5.4
, ++
1

p+=i

i -

. -

11 8

.
;
, .


. . ,

alloc (n) ,

alloc . , afree () ,
,

. " " afree


alloc. ,

alloc

( .

afree ,

stack)

8 . 7 .

alloc
allocbuf .
alloc afree . , ,

static

alloc afree ,

. ,
; malloc

.

allocbuf

alloc

allocp : "'-...

:L.. -1--L---LI---'-1----'-1--~--_____.
-+----+--

posle alloc

allocbuf

:1

------'~

-.

allocp :

11

"'-...

~~==~==~~~~~~~~~::::::~~;.:::~~~~~~-=~~

allocbuf .

allocp ,

. alloc n ,

allocp (. .

allocbuf.

alloc

, ) ,

, .

, alloc .

afree

()

allocp

allocbuf .

119

5.4
#define ALLOCSIZE 10000 / *

: n n

static char allocbuf[ALLOCSIZE ] ; / *


static char *allocp = allocbuf ; / *
char *alloc(int n) /*

*/

alloc */
*/

n~ ~

* /

if (allocbuf + ALID:SIZE - allocp >= n) { / *


allocp += n;
return allocp - n ; /* */
else /* n */
return ;

void afree (char

. n

*) /* n ~ n~

*/

*/

if

>= allocbuf
allocp=p;

&&

< allocbuf + ALLOCSIZE)

,
,

static char *allocp = allocbuf ;


allocp
allocbuf , . .
. , ,

static char *allocp = &allocbuf [ ]

if (allocbuf + ALLOCSIZE - allocp >= n) { /*

n*/

, allocp
allocbuf.
, alloc (
)

, alloc

120

,
.

.
:

, NULL,
. NULL
<stdio . h>. NULL .

if (allocbuf + ALLOCSIZE - allocp >= n) { /*

n*/

if

>= allocbuf

&&

< allocbuf + ALLOCSIZE)

. ,

. q
, =,

! =, <, >=,

. ,

p<q
, ,

q.

.

.

( :

,
.

p+n
n- .

,
.

int ,

int-o

, , :

< q,

q
q-p+l

q .

strlen :

5.4

/* strlen:
int strlen (char *)

121

*/

char *= ;
while (* != '\0')
++;

returnp-

, , . ,
. while ,

'\0'

. ,

++ ,

, . . , . (
int. <stdd.ef. h>
ptrdiff_ t
.

size_ t
strlen, .
size_ t sizeof . )

float,

float,

char

++

flo a t
alloc ,

, , ,
char float alloc

afree.

.
,

. .

, , ,
. floa t
double , , , ( void*)

5.5

,
"

sum string"

'\; .

122


.
,

printf (" hello , world\n");


,
;

printf

. ,

.
.

pmessaqe

char *pmessaqe ;

pmessaqe = " now is the time";


pmessaqe .

; .
.
:

char amessaqe[] = " now is the time " ; /* */


char *pmessaqe = " now is the time"; /* * /
amessaqe

'\ 0' .
amessaqe
, pmessaqe ,

,
.

;
,
.

pmessaqe:

~~--~-~lnow

amessage:

lnow is the time\0

is the t ime\0


,
.

strcpy ( , t) , t
. = t,

5.5

123

,
,

. ,

. :

/* strcpy: t ;
void strcpy(char * , char *t)

*/

int i ;
i

= ;

while ((s[i] = t[i]) != ' \0 ' )


i++;

strcpy

/* strcpy : n ro t ;
void strcpy(char *, char *t)

*/

while ( (*

*t) != '\0 ' )

++ ;

t++ ;

strcpy

t .

, , ,

'\ '

t, .

strcpy

/* strcpy : n ro t ,
void strcpy(char *, char *t)

2 */

while ( (*++ = *t++) != '\0')

t .

*t++

++

t,

t . ,

. ' \'
. t ,

' \0' .

124

, '\' ,
.
, , ,

/* strcpy: t ;
void strcpy(char * , char *t)

*/

while

(*++

= *t++)

,
,
.

(<string. h>)

strcpy

strcmp

(,

t) ,

t .

/* strcmp: < s<t,


int strcmp(char * , char *t)

s==t, >0

s>t */

s==t, >0

s>t */

int i ;
for (i = ; s[i] == t[i]; i++)
i f ( [i] = '\0')
return ;
return s[i] - t[i] ;

/* strcmp: < s<t,


int strcmp(char *, char *t)
for ( ;
if

== *t ; ++ , t++)
= '\0 ' )
return ;
return * - *t ;
*

(*

++ -- ,
*, , ++ --,
, . ,

5.6

125

*--

*++

val

= val ;
= *-- ;

/*
/*

val

*/
n

(.
;

val * /

push)

(. )

4 . 3.

<string . h>

5-3. strcat

2: strcat ( , t)

5-4. strend ( , t) , 1 t
, .

5-5 .
:

strncopy, strncat , strncmp , () n


strncpy ( , t, n)
n t .

. ,

5-6 .

.
(
(

4), atoi , itoa


3) strindex getop
1

5.6

(
(

2, 3

getline
4), reverse

4) .

sort UNIX.
Shell sort)

1 - (.

1 4

quicksort . ,

126

,
, ,

.

.

. ,

, ---
,
.

strcmp .

, ,
.



. :


,
, main
.
.


. ,
,
.
,
r .
,
.

5.6

127

#include <stdio.h>
#include <string.h>
#define
char

AXLINES

5000 /*

*1ineptr[AXLINES] ;

/*

.* 1

*/

int readlines(char *1ineptr[], int nlines) ;


void writelines(char *1ineptr[], int n1ines) ;
void qsort(char *1ineptr[] , int 1eft, int right) ;
/* .
main ()

*/

int nlines ; / *

n */

if ((nlines = readlines(lineptr , AXLINES)) >=)


qsort(lineptr, , nlines-1) ;
writelines(lineptr , nlines) ;
return ;
else {
printf( " error: input too big to sort\n");
return 1 ;

#define AXLEN 1000 / * * /


int getline(char * , int);
char *alloc(int);
/* readlines: n */
int readlines(char *lineptr[], int maxlines)
int len , nlines;
char * , line[AXLEN];
nlines = ;
while ((len = getline(line , AXLEN)) > 0)
if (nlines >= maxlines 1 1 = alloc(len)
return - 1 ;
else {
line[len-1] = ' \0 ' ; /*
strcpy(p , line) ;
lineptr[nlines++] = ;
return nlines ;

NULL)

*/

128

/* writelines: n */
void writelines(char *lineptr[], int nlines)
{

int i;
for (i = ; i < nlines ; i++)
printf( " %s\n" , lineptr[i]) ;
getline
lineptr :
char *lineptr [AXLINES]

1 . 9.

lineptr

AXLINES ,

*lineptr [i]

char .

lineptr [i]

, . ., i-

lineptr

, ,

wri telines

/* writelines : n */
void writelines(char *lineptr[] , int nlines)
{

while (nlines-- > )


printf(" %s\n", *lineptr++) ;

*lineptr ;
nlines .

Quicksort

strcmp .

, .

/* qsort : v[left] ... v[right]


void qsort(char *v[] , int left, int right)

*/

int i , last ;
void swap{char *v[], int i , int

if (left >= right) / *

) ;

5.7

129

*/

return;
swap(v 1 left 1 (left + right)/2);
last = left;
for (i = left+1; i <= right; i++)
if (strcmp(v[i] v[left]) <)
swap(v 1 ++last 1 i) ;
swap(v left last);
qsort(v 1 left 1 last-1);
qsort(v 1 last+1 1 right);
1

swap

1* swap: v [i] v [] */
void swap (char *v [] int i int )
1

char *temp;
temp=v[i];
v[i] =v[j];
v[j] =temp;
~ v (. .

temp

lineptr)

char

1 ~

5-7.

readlines
alloc

main
. ?

5.7 ~3

~ 1
.

, 1
. 1 1- 60-r
, 61- .

:
,

day_of_year
month_ day

~ ,
, ~

. ~

1 ~ :

month_day (1988 60,


1

&m 1

&d)

130

m 2 d 29

(29- )

( "

" )

,

,

:

staticchardaytab[2] [13] = {
{0, 31, 28, 31, 30, 31, 30 , 31 , 31, 30 , 31 , 30, 31} '
{0 , 31, 29, 31 , 30 , 31 , 30, 31 , 31, 30, 31 , 30, 31}
};

/* day_of_year: :
int day_of_year(int year, int month , int day)

*/

int i , leap ;
leap = year%4 == && year%100 !=
for (i = 1 ; i < month; i++)
day += daytab[leap] [i];
return day;

1 1 year%400

/* month_day: , */
voidmonth_day(intyear , intyearday , int *pmonth , int *pday)
{

int i, leap;
leap = year%4 == && year%100 ! = 11 year%400 = ;
for (i = 1; yearday > daytab[leap] [i] ; i++)
yearday -= daytab [ leap] [ i] ;
*pmonth=i ;
*pday = yearday;
,

,
daytab daytab
day_of_year month_ day ,
char
char
leap ,

() ()

5.7

131

daytab .
1

, , .

daytab[i] [] /* [] [] */

daytab[i,j] /* */
,
.

, , ,
.

. daytab

,
1 12,

11.

, ,
.

, ;
, , ,
, 13 int .

, 13

int . , daytab
f, f :
f(intdaytab[2] [13]) { ... }
,

f(intdaytab[] [13]) { ... }


, , ,

f (int (*daytab) [13]) { ... }


13 .

*.

int *daytab[13]

132

13 . ,

() ;
.

.12

5-8.

(day_of_year

month_day)

. .

5.8 n3

month_name (n),

n-
.

name

static . month_

.
.

/* month_name:
char *month_name(int n)

n-

*/

static char *name[] = {


"Illegal month",
"January" , "February",
"April ",

" " ,

"&rch" ,

"June " ,

" July", " August" , " September",


" October", " November" , " December"
};

return (n< 1 11 n > 12) ? name[O]

name [n] ;

name, ,
lineptr .

; .
i- ,

name [i] .

name

5.9

5.9

3 3

133

name

int [10] [20] ;


int *b [10] ;
[]

[4]

b[]

[4]

. :

200

int

int-

20*row+col [row] [!] .


b , 10
; , ,
.

b 20
int , 10

. ,

. ,

2 ,

20 ;

name:
illegal month\0

Jan\0

Feb\0

r\0

,
, t

month_ name.

char *name[]

= { "Illegalmonth",

"Jan" "Feb" "Mar"};


1

charname[] [15]

={

"Illegalmonth" "Jan" "Feb"


1

"r "

};

134

aname :
1~llegil mont\O

Jan\0

r\0

Feb\0

15

45

30

5-9. day_of_year month_day

5.1 r
,
, .

main ,

arqc ,

. (

arqument count) ,
(argv , arqument vector) ,

, ,

.
.

echo ,

. ,

echo hello, world

hello , world
,

arqv[O]

1,

argc 1 .

argc

argc , arqv [ ] ,
ce" echo", " hello", "wrld" .
argv[l] , argv[argc-1] ;
, argv[argc] NULL .

. ,

argv[l],

argv[2]

5.10

echo argv

#include <stdio . h>


/* echo r
main(int argc , char *argv[])

135

1 -

*/

int i ;
for (i = 1 ; i < argc ; i++)
printf ( "% % ", argv[i ],
printf( " \n " ) ;
return ;

(i

< argc-1 )

"") ;

argv ,
, .
argv, char ,

argc :
#include <stdio . h>
/* echo r
main(int argc , char *argv[])

2-

*/

while (--argc > 0)


printf( "%s %s ", *++argv,
printf( " \n") ;
return ;

(argc > 1) ?

"" ) ;

argv ,

argv[1]

argv[O].

(++argv)

*argv

, argc ; ,

. , printf

printf ( (argc > 1) ?

"% "

"% ",

*++argv) ;

printf , ,
. ,

, 4 . 1 . ,
,
. UNIX -o a a

grep, ,
, .

136

linclude <stdio.h>
linclude <string . h>
ldefine AXLINE 1000
int getline(char *line , int max);

/* find :

n 1'1'8 : or'CIIaPU'1' o6nJcoor 1-'l' rt

*1

main(int argc , char *argv[))


{

char line[AXLINE) ;
int found = ;
if (argc != 2)
printf( " Usage : find pattern\n" );
else
while (getline(line, AXLINE) >)
if (strstr(line , argv[l)) != NULL)
printf( "% s " , line);
found++ ;
return found;

strstr ( , t)

t , NULL
.

<string. h> .

. .
"
"; "

".

UNIX ,
flag)

, (.

. - ( " ")

- n ("" ) ,

find

-n pattern

.
,
. ,
,

find -nx pattern


:

5.10

137

#inelude <stdio . h>


#inelude <string.h>
#define AXLINE 1000
int getline(ehar *line 1 int max) ;

/* find: t 151'8 xat n:818


main(int arge 1 ehar *argv[])

~ 1-

*/

{
eharline[AXLINE];

long lineno = ;
int 1 exeept = 1 number = 1 found = ;
while (--arge > && (*++argv)
while (= *++argv [])
switeh () {

[]=

'-

' ':

exeept= 1 ;
break ;
' n ' :
number= 1 ;
break ;
default :
printf ("find: illega1 option %e\n" ,
arge = ;
found= -1 ;
break;

) ;

if (argc != 1)
printf (" Usage : find - - n pattern\ n " ) ;
else
while (getline(line , AXLINE) >0) {
lineno++ ;
if ( (strstr (line 1 *argv) ! =NULL) != except) {
if (number)
printf( " %ld: " 1 lineno) ;
printf( " %s ", line) ;
found++;

return found ;

argc

argv

. , ,

argc

, argv -

138

argc 1 , *argv
*++argv ,
na ( *++argv) [] n . (
* *++argv.) [] * ++ ,
; n *++ (argv [] ) . ,
.

n ,
" " . ,

*++argv[O]

argv[O]!

n ; ,

n .

5-10. expr,

, n n
. n,

expr234+ *

(3+4 ) .

5-11. n entab detab (


1) tab .
tab .

5-12 . entab detab

entab - m +n
- ,

m.

( ) .

5-13 . tail , n
n , , 10,

. n ,

tail -n
n .

n.

- 5 . 6, /

5.11

5.11

139

, ,
, ,
, ,
, .
,
,

-n,

, .

,
,
.
.

, ,

strcmp; , nncmp ,
,

strcmp. main ,
qsort.
, .
#include <stdio. h>
#include <string. h>
#define

AXLINES

5000 /*

char*lineptr[AXLINES] ;/*n * /

int readlines (char *lineptr [] , int nlines) ;


voidwritelines(char*lineptr[] , intnlines) ;
void qsort (void *lineptr [] , int left , int right ,
int (*comp) (void * , void *)) ;
in t numcmp (char * , char *) ;
*/
main ( int argc, char * argv [] )

1*
{

intnlines ;/ *poj n

int numeric =; 1* 1 */
if (argc>l&&strcmp(argv[l] , " -n" )
numeric= 1 ;

==)

*1

*/

140

if ( (nlines = readlines (lineptr, AXLINES)) >=)


qsort ( (void**) lineptr, , nlines-1 ,
(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 *

void*

qsort

.
,

/* qsort: v[left] . . . v[right]


void qsort(void *v[] , int left, int right,
int (*comp) (void * , void *))

*/

int i, 1ast;
void swap(void *v[], int , int) ;
if (left >= right) /*~ t11ro iDD _ . , _ 88 n::I81IICY Q N 81111 */
return;
swap(v , left, (left + right)/2);
last = left;
for (i = left+1 ; i <= right ; i++)
i f ( (*comp) (v[i], v[left]) < )
swap(v, ++last, i);
swap(v, left, last);
qsort(v , left, last-1, comp) ;
qsort(v , last+1 , right , comp) ;

qsort

5.11

141

int (*comp) (void *, void *)



comp void*
int. comp

i f ( (*comp) (v[i], v[left])


:

<)

comp , *comp

(*comp) (v[i] , v[left])


.
;

int *comp(void *

void *) /*

*/

comp int ,
strcmp,
. nwncp ,
, atof :
.

ftinclude <stdlib. h>


/* nwncp:
int nwncp (char *sl, char *2)

sl

*/

double vl , v2 ;
vl = atof (sl) ;
v2 = atof (2) ;
if (vl < v2)
return -1;
else if (vl > v2)
return 1;
else
return ;

swap , ,

void * .

142

void swap (void *v [] , int i , int

;)

void *temp;
temp=v[i] ;
v[i] =v[j];
v[j] = temp ;


; ,

5-14.
-r ,


()

-r - n.

5-15 . - f

; , .

5-16.

-d

( " " ),

, .
-f.

5-17 . ,

. ( -df

5.12

- n )

n n

,
.
;
, ,
,

int *f () ; 1* f :

: .

int */

int (*pf) () ;/*pf:

int */

5.12

() ,

143

, ,

, ,
, .

typedef ,

6. 7 .

. .
,

dcl,

char **arqv
arqv : char
int (*daytab) [13]
daytab: [ 13] int
int *daytab[13]
daytab: [13] int
void *comp ()
comp : void
void (*comp) ()
comp: void
char (* (*()) [ )) ()
: []
char
char (* (*[3]) ()) [5]
: [3)
[5] char
dcl

dcl:
direct-dcl:

8 .5;

* direct-dcl

name
(dc/)
direct-dcl()
direct-dcl{onuca ]

,
* .

dcl direct- dcl

dcl ,
direct-dcl , direct-dcl
Direct- dcl ,

, ,

.
,

144

(*pfa[]) ()

pta

()

name

dir-dcl
1

dir-dcl
1

dcl
1

dir-dcl
1

dir-dcl
1

dcl
pfa direct-dcl . pfa [ ,
, direct-dcl. ooa, *pfa [] dcl, (*pfa [])
direct-dcl. , (*pfa []) () direct-dcl, dcl.
( direct-dcl
dir-dc[) :
dcl , dcl dir- dcl ,
.
, ,
, ;
.

1* dcl:

n */

void dcl (void)


{

int ns ;
for (ns=O ; qettoken() = ' * '; ) / *count*'s*/
ns++ ;
dirdcl() ;
while (ns-- >)
strcat(out , " pointer to " ) ;

/ * dirdcl :

*/

5012

145

void dirdcl (void)


{

int type;
if (tokentype=' ( ') { / * ( dcl ) */
dcl ();
if(tokentype!= ' ) ' )
printf ("error : missing) \n");
else if(tokentype==NAE)
1* nI */
strcpy (name, token) ;
else
printf( " error : expectednameor (dcl)\n") ;
while((type=gettoken())==PARENSI ltype==BRACETS)
if (type==PARENS}
strcat(out, "functionreturning" ) ;
else{
strcat(out ,"array") ;
strcat(out, token} ;
strcat(out , "of" );

,
, dcl o

char

int ,

, const
,

main

#include <stdiooh>
#include <string oh>
#include <ctype oh>
#define

enum

N,

100

PARENS ,

void dcl (void} ;


void dirdcl(void) ;

int gettoken(void) ;

};

146

int tokentype ;
/* */
char token[AXTOEN];
/* */
char name[AXTOEN] ;
/ * */
char datatype[AXOEN] ; / * data type = char , int , . */
char out[lOOO];
/* */
main()
/* * /
{

while (gettoken()

!= EOF) { /*

*/

strcpy(datatype , token) ;
out[O] = '\0';
dcl() ; /* * /
if (tokentype != '\n')
printf( " syntax error\n" ) ;
printf( "%s : % %s\n", name , out, datatype) ;
return

gettoken ,
nap

; " " ,
,

nap

int gettoken(void) /*

int , getch (void) ;


void ungetch (int) ;
char * = token ;
while ( (c=getch() == ' ' 11 = '\t ')
if(c== '('){
if ( (c=getch()) = ' ) ' )
strcpy (token , " () " ) ;
return tokentype = PARENS ;
} else {
ungetch(c) ;
return tokentype

= ' (' ;

} elseif (= ' [ ' ) {


for (*++= ; (*p++=getch()) != ' ] '; )
* = '\0' ;
return tokentype = R ;
} else if (isalpha ()) {

*/

5.12

for

(*++ = ;

147

isalnum (= geteh ()) ; )

*++ =;

*= '\0';
ungeteh(e) ;
return tokentype = N ;
} else
return tokentype =;

geteh ungeteh

4 .

,
.

undel

"

ehar" ,

x()*[]*()ehar

ehar ( * ( * () ) []) ()
gettoken . undel
/* undel : n */

main()
{

int type ;
ehar temp [N] ;
while (gettoken () ! = EOF)
strcpy(out, toke n) ;
while (( type = gettoken () ) ! = '\n ' )
if (type == PARENS 11 type = R)
streat(out , token) ;
elseif (type= '*') {
sprintf (temp, " ( * % ) ", out) ;
strepy(out , temp) ;
} elseif (type=NAE) {
sprintf(temp , " % % " , token, out) ;
strepy(out , temp) ;
} else
printf ( " invalid input at %s\n", token);
return

del .

148

5-18 .

5-19.

dcl

undcl

5-20.

dcl
const .

6:


,
. ( , ,

""

(. record) . )

, ,

, .
:
, ,

, .
: , ,
. , , :
, , ,
.

ANSI

,
.

, . ,
.

6.1

.
,
.

(4,3)
(0,0)

149

\50

structpoint {
intx ;
inty ;
};

struct ,

, . ,
( , ,

structure tag)
struct ( point ) .

,
.

.

( ) ,

, . ,
,
.

struct

, ,
. ,

struct { ... }

, ,

z;

intx,

z;

;
. ,
,
. ,

point,

structpointpt;

pt

struct point.


,
, :

struct pointmaxpt = { 320 , 200 } ;

6.1

151

, ,
n n.
n n n

__ . __

n "

" n

. , n

pt,

n :

printf( " %d,%d", pt . x, pt.y) ;


n n
(,

0):

double dist, sqrt (double) ;


dist =sqrt ( (double)pt. * pt . x + (double)pt .

* pt. );

. n
,

nap

pt2

ptl
--~~----------------~

struct rect {
struct point ptl;
struct point pt2 ;
};

rect point .

struct rect screen ;

screen

152

ptl 1

screen. ptl .
screen.

1 n

6.2
n n , n

n n

& nn . n

n
. n .

; .

n n
n. nn : n
n n

n n . n .

makepoint 1 n l

point :

/* makepoint:
struct point makepoint(int

int

*/

struct point temp ;


temp . x = ;
temp.y = ;
return temp;

n
; 1 n
n .

makepoint

, n, n
:

struct rect screen ;


struct point mi.ddle ;
structpointmakepoint(int 1 int) ;
screen. ptl = makepoint ( ) ;
screen .pt2 =makepoint(XAX 1 );
middle = makepoint ( (screen. ptl. + screen . pt2. ) / 2 1
( screen. ptl . + screen. pt2 . ) / 2) ;
1

6.2

153

.
,

/* addpoints: */
struct addpoint (struct point pl, struct point 2)
{

pl.x +=2 . ;
pl . += 2 . ;
returnpl ;

, ,
.

pl

ptinrect,

,
.

1* ptinrect:

&Jto
int ptinrect(struct point

return
&&

.
.

r ,
struct rect r)

nn
,

>= r.ptl.x &&


>= r.ptl.y &&

.
.

CIIpO'l'JIHO

*1

< r.pt2.x
< r.pt2 . y ;

ptl

pt2 .


() :

ldefine min(a, b)
ldefine max(a, b)
/* canonrect:
ur

(()

< (b) ?

()

(()

> (b) ?

()

(b))
(b))

*1

struct rect canonrect(struct rect r)


{

struct rect temp ;


temp.ptl.x = min(r.ptl . x ,
tep.ptl.y = min(r.ptl . y,
tep.pt2.x = max(r.ptl.x,
tep.pt2.y = max(r.ptl.y ,
return temp ;

r . pt2.x) ;
r.pt2.y) ;
r.pt2 . x);
r.pt2.y);

154

,
,
.

structpoint * ;

point ,

struct point.
* , (*) . (*) .

. , ,
,

structpoint origin ,

* ;

= &origin ;
printf( "originis (%d ,%d) \n",

(*) ., (*) . ) ;

(*).

* .

. *.
* (. ) ,


. ,
- > __

. ( -+:

>). ,

printf ("origin is (%d ,%d) \n",

->

-> , ->) ;

struct rect r , *rp = &r ;


r.ptl.x
rp->ptl.x
(r .ptl) .
(rp->ptl) .

()

[] ,

-> ,

. ,

6.3

155

struct {
int len ;
char *str ;
} * ;

++p->len

len,

++(-> l).
(++) ->len , len,
->len len. (
. )
, *p->str str ;
*p->str++ str
str ( *++) ; (*p->str) ++
str ; *p++->str ,
str .
:

(++)

6.3

.
.
,

keyword keycount ,

char *keyword[NEYS];
int keycount[NEYS];
, ,
. :

char *word;
intcount ;
.

structkey {
char *word;
int count;
} keytab [N] ;

156

key, keytab

.
. , ,

structkey {
char *word;
int count ;
};
truct

key keytab [N] ;

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 <stdio . h>


#inc1ude <ctype . h>
#inc1ude <string.h>
Jdefine

AXWORD

100

int getword(char * , int) ;


int binsearch(char * , struct key * , int) ;
/*
main ()

*/

int n ;
char word[AXWORD]

whi1e (getword(word, AXWORD) != EOF)


if (isa1pha(word[O]))
if ((n= binsearch(word, keytab , N))
keytab[n] .count++ ;
for (n = ; n < N ; n++)
if (keytab [n] . count > 0)
printf (" %4d %s\n",
keytab[n] .count , keytab[n] .word);
return ;

>=)

/* binsearch: r n. word tab[O] ... tab[n-1] */


int binsearch(char *word, struct key tab[] , int n)
int cond ;
int 1ow, high , mid;
1ow = ;
high = n - 1 ;
whi1e (1ow <= high) {
mid = (1ow+high) 1 2 ;
if ((cond = strcmp(word, tab[mid] .word))
high = mid - 1 ;
1 if (cond > )
1ow = mid + 1 ;
1

return mid ;
return -1 ;

<)

158

keytab

main,
qetword,

keytab

, qetword ;

qetword ,

N keytab
, , r
,
(,
) ,

keytab

, ,

( size of)

keytab 1 strct key

sizeof

sizeof

sizeof

(mn )


, ( ,
,

sizeof
size_ t <stddef h> )

int

double ,

, ,

#define

N :

tdefine N (sizeof keytab 1 sizeof (struct key))

6.3

159

#define N ( 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

isalnum

6-1 .

unqetch
. Getword, , isspace
, isalpha

<ctype. h>

qetword

, ,

. .

6.4

,
,
.

binsearch

keytab

main

Jinclude <stdio.h>
linclude <ctype.h>
linclude <strinq.h>
Jdefine AXWORD 100
int qetword(char *, int);
struct key *binsearch(char *, struct key *, int) ;

/* ~.
main()

u ; ~

char word[AXWORD];
struct key * ;
while (qetword(word, AXWORD) != EOF)
if (isalpha(word[O]))
if ((p=binsearch(word, keytab, N)) != NULL)
p->count++;
for ( = keytab ; < keytab + N ; ++)
if (p->count > 0)
printf( " %4d %s\n" , p - >count, p->word);
return 0;

*/

6.4

16 1

/* binsearch: n 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 < high)
mi.d = 1ow + (high-low) 1 2 ;
if ((cond=strcmp(word , mi.d->word))
high =mi.d ;
1 if (cond >)
low=mi.d+ 1 ;
else
return mi.d ;

<)

return NULL ;

binsearch ,
struct key ; binsearch .
binsearch , ;
, NULL.
, keytab .
binsearch .
1ow high
.

mid = (low+high) 1 2

/*

*/

. ,

high- low ,

mid=low+ (high- low) / 2


mi.d low high .

,

. &tab [-1] &tab [n)
tab . ,

. ,
,

162

(. .

&tab [n])

main :

for

= keytab; < keytab + N; ++)

, ,
, ++

, ""
. , ,

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
18

/"-the

/"men of/

for

all/

/""-

good
"""

"

pa.rty

""" time
their
/ """

to

a.id come
,
n .
, n . ,
,
. ,
, , ,

. ,

164

.
,
.

,
:

struct tnode {
char *word;
int count ;
struct tnode *left;
struct tnode *right ;

/*
/*
/*
/*
/*

*/

*/
*/

*/
*/

};
,
. ,

struct node *left;


left

tnode,

tnode .

(
) : .
:

struct t {
struct

* ;

/*

*/

};

struct

struct t *q; /* q

t */

};
,

getword,
getword

main

addtree .
linclude <stdio. h>
iinclude <ctype. h>
iinclude <string. h>
idefine AXWORD 100
struct tnode *addtree (struct tnode * , char *) ;
void treeprint (struct tnode *);
intgetword(char *, int);

6.5
/*
main ()

165

*/

struct tnode *root ;


char word[AXWORD] ;
root=NULL;
while (getword(word, AXWORD) != EOF)
i f (isalpha(word[O]))
root = addtree (root , word);
treeprint(root) ;
return ;

addtree . main
( ) . ,


addtree. ,
(n
)

.
,

addtree

struct tnode *talloc (void) ;


char *strdup (char *) ;

1* add tree :

., w, . * 1
struct treenode *addtree (struct tnode *, char *w)

int cond;
NULL) { / * 'l'r /
= 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< ) / * otuUI , . */
p->left addtree (p->left , w) ;
else / *axo , , ..*/
p->right =addtree (p->right , w) ;
returnp;

if

( =

166

talloc,


, strdup . (
. ) ,

NULL .
, . ( )

strdup

talloc .

treeprint ;
, (
)

1 (

).
, treeprint
.

/* treeprint: in-order
void treeprint(struct tnode *)

*/

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 ,


. D~Alloc

<stdlib. h>. , talloc :

#include <stdlib. h>


/ * talloc : (tnode) */
struct tnode *talloc (void)
{

return (struct tnode *) malloc (sizeof (struct tnode)) ;

strdup

, malloc

char *strdup(char

*)

/*

*/

char

* ;

(char *) malloc(strlen(s)+l) ; /* +1 f o r
!= NULL)
strcpy(p , ) ;
return ;
if

'\0 ' */

malloc NULL ; strdup


, .

6-2 .

malloc
free ; 7 8 .

, .
.

6-3.

" "


. ,
.

168

6-4 .

.
.

6.6 n

(.

table-lookup) .

. 1
idefine.

idefine IN 1
IN 1 . 1
IN

state =IN;

1.

install ( 1 t) ; t
.

lookup ()

. NULL .
-

1
.

. NULL
.

defn

6.6

169

n n
, .
n .

struct nlist { /* : */
struct nlist *next ; /*
char *name ; /* */
char *defn; /* ~ */

*/

};

idefine

ASHSIZE

101

static struct nlist


n

*hashtab[ASHSIZE] ;

/*

*/

lookup

install ,

, .

/* hash :
unsigned hash(char *)

*/

unsigned hashval ;
for (hashval = ; * != '\0 '; ++)
hashval = * + 31 * hashval ;
return hashval % ASHSIZE ;
(. unsiged)

hashtab ;

, n
.

lookup. lookup

n , , NULL .

/* lookup : ro hashtab */
struct nlist *lookup(char *)
{

struct nlist *np ;


for (np = hashtab[hash(s)] ; np != NULL ; np = np->next)
if (strcmp(s, np->name)
)
return np ; /* */
return NULL ; /* */

==

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 */
struct nlist *install(char *name, char *defn)
{

struct nlist *np;


unsigned hashval;

==

if ((np = lookup(name))
NULL) { /* */
np = (struct nlist *) malloc(sizeof(*np)) ;
if (np
NULL 11 (np->name = strdup (name) )
NULL)
return NULL;
hashval = hash(name);
np->next = hashtab[hashval] ;
hashtab[hashval] = np ;
else /* */
free ( (void *) np->defn) ; /* ro dfn * 1
if ((np- >defn = strdup(defn)) == NULL)
return NULL ;
return np ;

6-5 .

undef

lookup install.

6-6.

#define

( , ) ,
. getch

ungetch.

6.7typedef
typedef
. ,

6.7

typedef

171

typedef int Lenqth ;


Lenqth

int.

Lenqth

, .,

int:

Lenqth len, maxlen ;


Lenqth * lenqths [] ;

typedef char *Strinq ;


Strinq

char*,

. .

Strinqp, lineptr[AXLINES]
intstrcmp(Strinq , Strinq) ;

(Strinq) malloc (100) ;

alloc (int) ;

typedef
, typedef . , typedef
extern , sta tic . typedef

.
, typedef
:

typedef struct tnode *Treeptr ;


typedef struct tnode
char *word;
int count ;
struct tnode *left ;
struct tnode *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 ,

,
n.

typedef
na n
short, int long n. size_t
ptrdiff_t n.
n typedef
- n reeptr n
,

n .

6.8
( )

,
n . n
,
n
. variant n .
n
n, m

int, float

n .
n , , n,

. -
n . :

6.8

173

union u _ tag {
intival ;
fioat fval;
char *sval;
} u ;

; .

u
:

.
;
.


__.

__- >

utype

u ,

==

if (utype
IN)
printf( "%d\n " 1 u.ival) ;
if (utype-= FLOAT)
printf( " %f\n" 1 u.fval) ;
if (utype = SRING)
printf ("% s\n", u . sval) ;
else
printf ( " bad type %d in utype\n", utype) ;
, .
(
) .
,

174

struct {
char *name ;
intflags;
intutype ;
union {
intival;
float fval;
char *sval;
} u;

} symtab [N] ;

ival

symtab[i] .u.ival

sval

*symtab[i] . u.sval

symtab[i] .u.sval[O]
,
, "

" , .
:
, .

.
() 8
,
.

6.9

,
;

, ,
.

6.9

175


.
, , ,
.

char int.

, "
" ,

#define EYWORD 01
#define EXTRENAL 02
#define STATIC 04

enwn {

EYWORD

= 01 ,

EXERNAL

= 02, STATIC = 04 } ;

2.
,
2.

flags 1= EXTERNAL 1 STATIC ;


( 1) EXERNAL STATIC flags ,

flags &=- (EXTERNAL 1 STATIC) ;


( ),

i f ( (flags &

(EXERNAL

1 STATIC) )

=)

.
,
,
.
,

,
"" .

. n ,
#define n ,
:

176

struct {
unsigned int is_ keyword : 1;
unsigned int is_extern : 1;
unsigned int is_static : 1;
} flags;
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

ANSI 1




; 1

<stdio h> 1
<string h> 1 <ctype h>

7.1

3 33


;
1 ,
1
( carriage return)

linefeed

int getchar (void)

177

getchar :

178

getehar

, EOF

. EOF

<stdio.h>.

-1 ,

EOF, .
,
<;

getehar ,

prog

prog <infile
prog infile .
prog ; ,

argv .

" <infile"

, ,
; ,

otherprog 1 prog

otherprog

otherprog

prog n
prog .

( .

pipes)

intputehar (int)
:

putehar ()

putehar

n ,

EOF . ,

>fi lename: prog putehar ,

prog >outfile
n

outfile.

n n,

prog 1 anotherprog
prog , anotherprog.
printf, , n
putehar printf n -

n .

/
,

linelude <stdio. h>

7.2

n n .

- printf

179

< >
UNIX

( ,

, /usr/include)


; , getchar , putchar,
printf .

,
:

#include <stdio. h>


#include <ctype. h>
main() /* lower:

*/

intc
while ((= getchar ()) ! = EOF)
putchar(tolower(c)) ;
return ;

tolower

<ctype.h>;

, "" getchar putchar <stdio. h>


,

. . .

<ctype . h>

7- 1 .

, ,

argv [] .

7.2

- printf

printf .

printf

, ; , .

intprintf (char *format, argl, arg2 , ... ) ;

180

printf

fortnAt.

: 1
1

printf .

. %

, .
.

.
( ,
) .

- , .

, ,

,
, .

- h

%,

short,

( )

long .
.1 .

, .

. 1 printf

d, i

int;
int ; ( )
int; ( ),
abcdef ACDEF 10 1 , 15
int ;
int;
char* ; '\ '

double ;

[-] m. dddddd,

d-

6) .
double ; [-]m . dddddde+- xxili [-]m .ddddddE+- xx ,

d- (n

g, G

double ;

% %

6) .

-4

%f .

void* ;

; %

( )

7.2

printf

*,

181

. , max ,

printf (" %. * " ,

1114 , ) ;

,
.
.

" hello , world" (12

:%:
:%10:

:%.10:

:% -lOs :
:%.15:
: %- 15:

: %15.10:
:%-15.10:

: hello, world :
:hello, world:
: hello, wor:
: hello, world :
:hello , world:
:hello, world
hello, wor:
: hello , wor
printf

, printf
. , ,
:

printf(s);
printf( " %s " ,s) ;

sprintf

1*

/*

% *1

*/

printf ,

int sprintf (char *strinq, char *forlll4t, arql , arq2, ... ) ;


sprintf arql, arq2 , . ,
forlll4t , ,
;
.

7-2 .

,
.

182

7.3

r n n

printf,

()

. , ,
minprintf
printf

printfe
intprintf(char*fmt , ... )

. . .

...

. Haominprintf

voidminprintf (char *fmt, ... )


printf.
mi.nprintf ,
.

<stdarg . h>


.
, .
va_list
;

minprintf , , "
va_surt

" .

.
. ;

va_arg

va_start .
va_arg ;

. , va_end .
.

printf :

7.4

iinclude <stdarg.h>
/* minprintf : printf
*/
void minprintf(char *fmt , . .. )

scanf

183

va_list ; / * n
char *, *sval ;
int ival ;
double dval ;
va_start(ap, fmt) ; /*

*/

for(p=fmt ; *p ; p++){
if(*p!='% ' ){
putchar (*)
continue ;

arg

*/

switch(*++p){
' d ':
ival=va_arg(ap , int);
printf ("%d " 1 ival) ;
break ;
' f ':
dval=va_arg(ap , double) ;
printf ("%d ", dval) ;
break ;
' ' :
for(sval=va_arg(ap , char *) ; *sval ; sval++ )
putchar(*sval) ;
break ;
default:
putchar(*p) ;
break ;

va_end(ap) ; /*

*/

7 - . minprintf

printf .

7.4 n- scanf
scanf printf 1
, n .

184

int scanf (char *format 1

scanf 1
fori~~At .
; 1

1
.

printf 1
scanf 1 .
scanf 1

.
.
. EOF ;
1 1

. scanf

,

.
sscanf 1

int sscanf (char *string 1 char *format 1 argl, arg2 1


string 1 format
argl 1 arg2 1 . format 1

.
:

- (%)

%1

h 1 1 L


.
.

. ;
,
.

scanf

1 1

7.4

return,

. ( , ,

scanf

185

carriage

formfeed .)

,
.

7.2.

7.2 : scanf

3;

d
i

;
;

int*

int

( )

( )

char

unsiqned int*

*. (

1)

.
;

% 1

( );

char *,

'\0'
.

e , f,g

float*

% ; .

d, i,

u,

int,

h,
short

( )

1ong

, 4 ,

scanf ,

#include <stdio.h>
main(){

/*

*/

double sum , v;
sum=O ;
while(scanf( "% l f" , &v)==1)
printf("\t%.2f\n", sum+=v);
return ;

186

25 Dec 1988
scanf

int day, year ;


char monthname [20] ;
scanf ( "%d % %d ", &day , monthname , &year) ;
& monthname,
.

scanf ;
. ,

mm/dd/yy scanf :

intday, month , year ;


scanf("%d/%d/%d", &month , &day , &year) ;
scanf .
,
( , , , .)

, ,

sscanf .

while (getline (line , sizeof (line)) >)


if (sscanf (line , "%d % %d " , &day, monthname , &year) == 3)
printf ( " valid : %s\n", line); 1* 25 Dec 1988 form */
elseif (sscanf(line , "%d/%d/%d ", &month , &day , &year) =3)
printf ( " valid : %s\n ", line) ; 1* mm/dd/yy form *1
else
printf ( " invalid: %s\n" , 1ine) ; 1* invalid form * /

scanf
.

scanf.

: scanf sscanf
. :

7.5

187

seanf ( "%d" , n) ;

seanf ( "%d " , &n);


.

7-4.

seanf minprintf

7-5 . 4.
seanf /
sseanf .

7.5
,

,
.

eat,
eat


. ,

eatx . cy . e
. . ( )
.

, .
.
,

fopen. fopen

. . ,

( )

, ,

, ,
n ,

188

.
, <stdio . h>

FILE.

FILE *fp ;
FILE * fopen ( char *n&llle, char *mode) ;
fp FILE fopen FILE.

FILE , int , ()
; typedef. ( fopen

UNIX

fopen

8. )

fp = fopen (name, mode) ;


fopen

. mode, , ,
.
("r" ), ( " w" ) ( " " ) .

; ,

" b " mode.


,
, ( ) .
,
, .

, , ,
.

, fopen

NULL. ( ;

. )


, .

getc

; ,
.

int getc (FILE *fp)


getc fp ; EOF
.

putc :
intputc(intc , FILE.*fp)

7.5

189

putc fp ,
EOF . getehar putehar, gete pute
, .
,

stderr,

stdout stderr

<stdio . h> .

stdin, stdout

stdin
stdin stdout

,
7 . 1 .

getehar putehar gete , pute , stdin ,


stdout :
idefine getehar () gete (stdin)
idefine putehar () pute (() , stdout)
,

fseanf

fprintf.

seanf

printf ,


;
.

int fseanf (FILE *fp, ehar *format, ... )


int fprintf (FILE *fp, ehar *format, ... )
,

eat .

. ,


. , .

190

#include <stdio.h>
/* cat: ,
main(int argc, char *argv[])

1 */

FILE *fp;
void filecopy(FILE * , FILE *)
if (argc

==

1) /*

r ;

*/

filecopy(stdin, stdout) ;

else
while(--argc >)
if ( (fp = fopen(*++argv, " r " ))
NULL) {
printf( " cat : can' t open %s\n, *argv) ;
return 1 ;
else {
filecopy(fp, stdout) ;
fclose(fp) ;

==

return

/* filecopy: ifp
void filecopy(FILE *ifp, FILE *ofp)

ofp */

int

while ((= getc(ifp))


putc(c, ofp);

!= EOF)

stdin stdout FILE *.


, , .

int fclose (FILE *fp)


fopen,
fopen
.

,
, ,
cat . fclose
putc .

fclose

7.6

stderr

exit

191

. (

stdin stdout.
freopen. )

7 .

- stderr exit
cat

. ,

,
.

,
.

,
,

stderr

stderr

stdin stdout .

cat

#include <stdio.h>
/* cat: ,
main(int argc, char *argv[])

2 */

FILE *fp;
void filecopy(FILE *, FILE *);
char *prog = argv [] ; 1 * n

*1

if (argc == 1 ) /* r; */
filecopy(stdin, stdout);
else
while (--argc > 0)
if ( (fp = fopen(*++argv, "r")) == NULL) {
fprintf(stderr, "% : can ' t open %s\n" ,
prog, *argv) ;
exit(l);
} else {
filecopy(fp, stdout) ;
fclose(fp) ;
if (ferror (stdout) ) {
fprintf (stderr, "%: error wri ting stdout\n", prog) ;
exit(2);
exit(O) ;

192

. ,

fprintf stderr ,

, arqv []

, ,
.
, exi t,

exi t ,

. ,

;
.

exi t

fclose

.
(main) , return expr
exi t (expr) . exi t


, .

ferror fp.

int ferror (FILE *fp)


, , ( ,
)

feof (FILE * ) ferror ;

int feof (FILE *fp)


,
,
, .

7.7

n3 33

fgets
getline :

char *fgets (char *line, intmaxline, FILE *fp)


fgets

fp line ; maxline- 1

. '\0'. , fgets
line ; NULL . (

getline

, ;

.)

7.7
,

fputs

193

) :

int fputs (ehar *line, FILE *fp)

EOF , .

gets puts fqets fputs ,

stdinstdout. , gt

'\n',

ut .

fqets

fputs ,

, :

/* fqets : n
ehar *fqets(ehar *, int n , FILE *iop)

iop */

reqister int ;
reqister ehar *;

while (--n> && (= qete(iop)) != EOF)


if ((*++ =) == '\n ' )
break ;
* = '\0 ';
return ( == EOF &&
) ? NULL
;
/* fputs :
int fputs (ehar * , FILE *iop)

iop */

int ;
while (= *++)
pute ( , iop) ;
return ferror ( iop) ? EOF :

EOF

ferror ; fputs

qetline fqets:

/* qetline: ,
int qetline (ehar *line, intmax)

if (fqets (line, max, stdin)


return ;
else
return strlen (line) ;

=NULL)

*/

194

7-6 .

7-7.

1 5 1

.
?

7-8.

1
.

7.8

.
.
.

7.8.1

n r

strlen 1 strcpy 1 streat 1


strcmp 1 <string. h> . 1 t ehar * 1 n
int.

strcat ( t)
strneat(s 1 t 1 n)
strcmp ( 1 t)
1

strncmp(s 1 t 1 n)
strcpy ( 1 t)
strncpy(s 1 t 1 n)
strlen(s)
strchr(s 1 c)
strrehr(s 1 e)

s<t 1 s==t 1 s > t


strcmp 1

n t

NULL

NULL

7 .8.2

<c type . h >

. 1 int unsigned char


EOF.

int .

7.8
isalpha ()
isupper ()
islower {)
isdigit ()
isalnum(e)
isspaee ()
toupper ()
tolower ()

195

,
,
,
,

isalpha ()

isdigit(e) ,
return , fonnfeed,

, , ,

7.8.3 ungetc

ungeteh

4 ;

ungete .

int ungete (int , FILE *fp)


EOF .
ungete
seanf , gete , g etehar .
fp

7 .8.4 3wtb
system ( ehar * )
, .
. ,

UNIX ,

system( " date" ) ;


date ;
system
UNIX ,

.
.

exit.

7 .8.5 tb

ma.lloe ealloe

void *malloe (size_t n)

196

. , NULL .
.

malloc calloc
,
,

int *ip;
ip = (int *) calloc (n, sizeof (int)) ;
free () ,
malloc calloc .
,
malloc calloc .

, .
, ,
:

for (p=head;
free(p) ;

!=NULL ; p=p->next)

/*

*/

for (p=head; !=NULL ; p=q)


q=p->next;
free(p) ;

8 . 7
malloc ,
.

7 .8 .6

20 <math . h> ;
.

double double.

7.8
sin(x)

()

atan2(y , x)

()

log(x)

loglO(x)

pow(x,y)

sqrt ()

()

fabs

197

/ ,

( )
(

10)

(>)
(> )

(>)

7.8.7
rand ()
RAND_ <stdlib . h>.
,

idefine frand() ( (double) rand()

1 (RAND_AX+1 . 0))

(
, .

srand (unsigned)

rand .

rand srand

2.1 .

7-9. isupper
. .

8: UNIX

UNIX

,
, .

UNIX,

, ,
.
,
; ,

ANSI

UNIX,

, , .
; /,
() .

UNIX .

7 /

.

. UNI-

8 . 1

UNIX,

, ,
,

.
, ,

. ,

199

200

UNIX

? ?)

.
, ,
. (
, (.

handle)
MS-DOS . )

;
.
/

,
. ( ,

r.

she11)
1, 2,

, , ,

, .

1 2, /

1/0 (Input/Output-

) < >:

prog <infile >outfile


,

1 .

( .

pipe) .

8.2 1/0- read write


read write,
read

wri te.

, .


. .

int n_ read

= read(int

int n written

fd , char *buf, int n) ;

= write(int

fd, char *buf, int n) ;

8.2

1/0 - read

write

20 1

. ,

.
, -1 . ,

.
1, ( "
" ), 1024 4096,

. ,
.

1 .
, ,
.

ftinclude " syscalls.h"


main() /*

*/

char buf[BUFSIZ];
int n;
while ((n= read(O, buf , BUFSIZ)) > 0)
write(1 , buf , n) ;
return 0 ;

syscalls. h

, . BUFSIZ, ,

syscalls. h;

BUFSIZ

read

wri te;

read

read write
getchar, putchar, .

getchar ,

202

UNIX

#include " syscallsoh"


/ * g etchar :
int getchar(void)
char

*/

return (read(O ,

1) -- 1) ? (unsigned char)

& ,

EOF ;

char, read
unsigned char ,

()

getchar,

# include " syscalls oh "


/* getchar :
int getchar(void)

*/

static char buf [BUFSIZ] ;


static char *bufp = buf ;
stati c int n = ;
if (n == 0) { / * * /
n = read (O , buf , sizeof buf) ;
bufp = buf ;
re t urn ( --n >=

? (u n sign ed char) *bufp++

EOF;

getchar <stdio h>,


#undef getchar,

8.3 Open, creat, close, unlink


, ,

open creat [ sic] open fopen 7,


FILE,
, int , open - 1 0

8.3

Open, creat, close, unlink

203

#include <fcntl.h>
int fd ;
int open(char *name , int fiags , int perms) ;
fd = open(name, fiags , perms) ;

fopen, name
int

. fiags

; :

RDONLY
WRONLY
RDWR

system v UNIX, <fcntl. h> ,


(BSD) <sys/file. h> .

Berkley

fd

= open(name ,

O_RDONLY,O);

open

perms

.
,

crea t.

int creat(char *name, int perms) ;


fd

= creat(name ,

perms) ;

-1 .
,

creat

creat

, .
,

perms.

creat

UNIX,

, ,
, , , ,

.
. , 0755
, ,
.

, UNIX-oaa
, .

204

UNIX

#include <stdio . h>


#include <fcntl . h>
#include "syscalls.h"
#define R 0666 /* 'l>m!l&e

cacliiE5aot, rpyna,

: OC'1'llllir1

*/

void error(char *, ... ) ;

/* : fl f2 */
main(int argc , char *argv[])
{

int f1 , f2 , n ;
char buf[BUFSIZ] ;
if (argc != 3)
error("Usage : from to " );
if ((f1 = open(argv[1] , O_RDONLY , ))
-1)
error( " cp : can' t open % " , argv[1]) ;
if ((f2 = creat(argv[2] , R))
-1)
error( " cp : can ' t create % , mode % 0 " ,
argv[2], R) ;
while ((n= read(f1 , buf , BUFSIZ)) > 0)
if (write(f2, buf, n) !=n)
error( " cp : write error on fi.le % ", argv[2]) ;
return ;

==

==

0666.

stat,

8.

error ,
printf. error,
printf .

printf,

vprintf

, va_
start . , vprintf vsprintf fprintf sprint .
( 20)

. ,


. close (int fd)

; fclose

,
.

exit

return .

8.4

- lseek

205

#include <stdio . h>


#include <stdarq.h>
/* error: n nopaxa
void error(char *ft, ... )

*/

va_list arqs;
va_start(arqs, fmt) ;
fprintf(stderr , "error: " ) ;
vprintf(stderr, fmt , arqs);
fprintf(stderr , " \n" ) ;
va_end(arqs);
exit(l) ;

unlink (char* name)

remove

name

8-1. t 7 , read,
wri te, open close
.
.

8.4 n - lseek
:

wri te

read

. , ,
.

lseek,

,
.

lonq lseek(int fd , lonq offset , int oriqin) ;



fd,

offset,

, 1,

origin .
origin

2 offset

, ,
, . , (

>>

UNIX,
:

, .

" "

fopen),

206

UNIX

lseek(fd, OL, 2);


, (" "),

lseek(fd , OL ,

) ;

OL;

( long) ,

, lseek ,.

lseek,

. ,

, .
,

-1 .

#include "syscalls . h "


/*get: n n */
int get(int fd , long , char *buf , int n)
if (lseek(fd , , 0) >= 0) /* get to
return read(fd , buf , n) ;
else
return -1 ;

-1

lseek

long

*/

fseek

lseek, FILE*
, .

8.5

fopen

getc

,
, fopen getc .

, .

: ,
; ;
; ;
/ , , .

<s tdio . h>, ( #incl ude)


/ . ,
, .

8.5

<stdio . h>,

- n fopen getc

207

,
, ( )
.

#define
#define
#define
#define

NULL
EOF {-1)
BUFSIZ 1024
OPEN_ 2 /*

u:. ,:!1> C1I!q)E!I wn<et* /

typedef struct iobuf


int cnt ;
/ * */
char *ptr ;
/* n * /
char *base ;
/* */
int flag ;
/* nn */
int fd;
/* n */
FILE ;
extern FILE _iob[OPEN_AX] ;
#define stdin (&_iob[O])
#define stdout {&_iob[1])
#define stderr {&_iob[2])
enum _ flags {
_READ = 01 ,
WRITE = 02,
UNUF = 04,
EOF = 010 ,
ERR = 020

/ * * /
/* n */
/* */
/* EOF n */
/* */

};

int _fillbuf{FILE *);


int _fiushbuf{int , FILE *);
#define feof{p) {(p)->fiag & _EOF) !=)
#define ferror{p) {{p)->flag & _ERR) != 0)
#define fileno{p) ({p)->fd)
#define getc{p) (--{p)->cnt >= \
? {unsigned char) *(p)->ptr++ : _fillbuf{p))
#define putc(x , p) {--{p)->cnt >= \
? *{p)->ptr++ = {) : _fiushbuf{{x) , ))
#define getchar{) getc{stdin)
#defme putcher{x) putc{{x), stdout)

208

UNIX

getc ,

. ( tdefine
) , , getc _fillbuf
,
. unsigned, .
,

_flushbuf

getc,

putc

. ,

,
.

fopen .

- .

fopen

; _fillbuf .
include

<fcntl . h>
" syscalls . h "
fdefine R 0666 / * RW
include

n,

rpyna ,

*/

FILE *fopen(char *name, char *mode)


{

int fd ;
FILE *fp ;
if (*mode != ' r ' && *mode != ' w' && *mode != '')
return NULL ;
for (fp = _iob ; fp < _iob + N_; fp++)
if {(fp->flag & (_READ 1 _WRITE)) ==)
break ; /* found free slot */
if (fp >= iob + N_) /* n o free slots */
return NULL ;
if ( *mode = 'w' )
fd = creat(name , R) ;
else if (*mode == ' ') {
if ((fd = open(name, O_WRONLY , 0)) -- - 1)
fd = creat(name , R) ;
lseek(fd , OL , 2);
else
fd = open(name, O_RDONLY, 0) ;
if (fd == -1) /* couldn't name */
return NULL ;
fp->fd = fd ;
fp->cnt = ;
fp->base = NULL ;
fp- >flag = (*mode -- ' r ' ) ? READ
_WRITE ;
return fp ;

8.5

fopen

fopen getc

209

,
. ,

Fopen " b"


UNIX , " + "

,
.

getc

_fillbuff . _fillbuff

EOF.

( )

, _ fillbuff

read

.
,


. _fillbuf .

iinclude " syscalls . h "


/* _fillbuf :
int _fillbuf(FILE *fp)

*/

int bufsize ;
i f ( {fp->flag& (_READ I_EOF_ERR))

!= _READ)
return EOF ;
bufsize = (fp- >fiag & _UNUF) ? 1 : BUFSIZ ;
if (fp->base == NULL) /* */
if ((fp->base = (char *) malloc(bufsize)) == NULL)
return EOF ; /* */
fp->ptr = fp->base;
fp->cnt = read(fp->fd , fp->ptr, bufsize) ;
if (--fp->cnt < ) {
if (fp->cnt == -1)
fp- >fiag 1= _EOF ;
else
fp->fiag 1= _ERR ;
fp->cnt = ;
return EOF ;

return (unsigned char) *fp->ptr++;

. _ iob

FILE

{
{
{
} ;

iob[OPEN_AX]
,
,
,

(char * )
(char *)
(char *)

,
,
,

{ /*
(char
(char
(char

stdin, stdout stderr:

stdin , stdout, stderr */


*) , _READ , } ,
*) , _WRITE , 1 } ,
*) , _WRITE , 1 _UNUF , 2

210

UNIX

flag stdin
, stdout stderr .

8-2 .

fopen

_fillbuf

.
.

8-3. _ flushbuf, fflush fclose.

8-4 .

int fseek(FILE *fp , long offset , int origin)


fp ,
int , .
fseek. fseek

fseek

8.6

, .

ls UNIX-

, ,
, , .

dir - .
UNIX ,

ls ,
. , ,
, , .

, ,
; , - .
,
,
.

fsize.
fsize ls

. , fsize
. ,
.

UNI-
.

. " "

8.6

" inode list". " inode"

211

,
. ,
inode-o .
,
. ,
.

Dirent

opendir, readdir

closedir

inode-o

fsize .

version 7 v UNIX;

.
Dirent

inode .

opendir
DIR, FILE
readdir closedir.
dirent. h.
N_, .

#define

14 /*

*/

/*

typedef struct { /* n
long ino ;
/*
char name[NAE_AX+1] ; /*+
Dirent;
typedef struct
int fd ;
Dirent d ;
DIR ;

/*
/*
/*

DIR :

*/

*/

inode*/
'\0' */

.*/

*/
*/

DIR *opendir(char *dirname) ;


Dirent *readdir(DIR *dfd);
void closedir(DIR *dfd);
stat
inode - o ,

char *name;
struct stat stbuf;
int stat(char *, struct stat *) ;
stat(name, &stbuf) ;

-1 .

212

stbuf

UNIX

inode

. stat </

stat. h>,

struct stat

/* inode

dev t
ino t
short
short
short
short
dev t
off t

st_dev ;
st_ino ;
st_mode ;
st_nlink ;
st_uid; /*
st_ gid ; / *
st_rdev ;
st_size ;
t~e_t st_atime ;
time_t st_mtime;
time t st_ctime;

/* inode * /
/* inode */
/* */
/* ~

stat*/

~* /

~ ~ n~* /
~ n* /

/*
/*
/*
/*

/*

n. ~*/
~ */
n nn

*/

n ~

*/

n inode-o

*/

};
.

dev_t ino_t <sys/ types .h>, , ,


.
ts_ mode .
, , <sys/ types. h>;

#define
#define
#define
#define
#define
/*

.. .

IF 0160000
IFDIR 0040000
IFCHR 0020000
IFLK 0060000
IFREG 0010000

/*
/*
/*
/*
/*

n */

*1
*/
n ~ */
r */
~
n

*/

fstat

fsize .

. ,

;
- , .

(main) ;
fsize .

8.6
#include
#include
#include
#include
#include
#include
#include

<stdio . h>
<string . h >
" syscalls . h "
<fcntl.h>
<sys/types.h>
<sys/stat.h>
" dirent.h"

/*
/*
/*

213

nw
n n

*/

*/

stat */

void fsize(char *)
/* n * /
main(int argc , char **argv)
{

/* n :
if (argc == 1)
fsize (". " ) ;
else
while (--argc > )
fsize(*++argv) ;
return ;

*/

fsize .
fsize dirwalk
. _IF _ IFDR
<sys/stat.h> .
, ,

, & = .

int stat(char *, struct stat *) ;


void dirwalk(char * , void (*fcn) (char *) );

/* fsize: n
void fsize(char *name)

" name " */

struct stat stbuf ;


if (stat(name, &stbuf) == -1) {
fprintf(stderr , " fsize : can ' t
return ;

%s \ n ", name) ;

if ((stbuf. st_mode & IF) = S_IFDIR)


dirwalk(name , fsize) ;
printf( "%81d %s \ n " , stbuf.st_size , name);

dirwalk

. ,

, ,
.

fsize dirwalk

214

#define

UNIX

1024

/* dirwalk: n fcn
void dirwalk(char *dir 1 void (*fcn) (char *))

dir */

char name[AX_PATH]
Dirent *dp ;
DIR *dfd ;

==

if ((dfd = opendir(dir))
NULL) {
fprintf(stderr 1 " dirwalk: can 1 t open %s\n"
return ;

dir) ;

whi1e ((dp = readdir(dfd)) != NULL) {


if (strcmp(dp->name 1 " . " ) == 11 strcmp
(dp->name
continue ; /* ski p self and parent */
if (strlen(dir)+strlen(dp->name)+2 > sizeof(name))
fprintf(stderr 1 " dirwalk : name % % too long\n"
dir 1 dp->name) ;
else {
sprintf(name 1 "% / % " 1 dir 1 dp->name);
( *fcn) (name) ;
1

"

"

))

closedir(dfd);

readdir
,

NULL .

, ".",

" . . ";

, .
,
.

opendir,
readdi r closedir .
v ersion 7 System v UNIX;
<sys/ dir . h>, :
#ifndef DIRSIZ
#define DIRSIZ 14
#endif
str uct direct { /* n */
ino_t d_ino ; /* inode */
char d_name[DIRSIZ] ; / * '\0 1 */
};

8.6

215

ino_t

typedef inode -o .
unsigned short ,

;
,

typedef .
<sys /types . h>

" "

opendir ,
fstat, stat
) ,

(
:

int fstat(int fd , struct stat *);


/* opendir:
DIR *opendir(char *dirname)

readdir

n* /

int fd;
struct stat stbuf ;
DIR *dp;
if ((fd

= open(dirname ,

O_RDONLY , 0))

==

-1

11 fstat(fd , &stbuf) == -1
1 1 (stbuf.st_mode & _IF) != S_IFDIR
11 (dp = (DIR *) malloc(sizeof(DIR))) -- NULL)

dp->fd = fd ;
return dp ;
closedir

return NULL ;

/* closedir:
void closedir(DIR *dp )

opendir */

if (dp) {
close{dp->fd) ;
free(dp);

readdir read

. (

) ,
,

inode , .
inode static ,

.
.

216

UNIX

#include <sys / dir .h> /* * /


/* readdir: */
Dirent * readdir (DIR *dp)
{

struct dir ect di rbuf; /*


r

*1

static Dirent d ; / *

*/

while (read(dp->fd , (char *) &dirbuf , sizeof(dirbuf))


== sizeof(dirbuf)) {
if (dirbuf . d_ino == 0) /* slot not in use */
continue ;
d . ino = dirbuf. d_ino ;
s t r ncpy( d . narne, dirbuf.d_name, DIRSIZ) ;
d . name (DIRSIZ] = '\ 0' ; /* */
return &d ;
return NULL ;

fsize

, ,

. , " ";

. ,
,
.
,
.
.

8-5.

fsize

inode

8.7 - ()

, - .
.

malloc free ; malloc



.
- -
- - , , ,

typedef.

, malloc
.

807

- ( )

2 17

, ,
,
,

malloc
malloc

(., ") ,

, ( )

~i~:n4=91
...... .

..

00

..

.. ... .. ..

00

..

....

. ...... . .

use

..

00

00

00

:= , maiJoc
[}l'] , malloc
1 : ::::::: 1

malloc

., " (

" first fit" ), .,"( "best fit " )


,
,
,

, ,
,

, ,
,


, 5,

malloc,

: ,
,

int ,

double;

long

,
, ;

""
,

218

UNIX

.

,

typedef long Al.ign ; / *


long* /

long .

n. r.

union header
/* r
struct {
union header *ptr ;

*/

/*

( n)

*/

/* */

unsigned size ;
;

Align

;/ * n

*/

};

typedef union header Header ;

Align

malloc,
; ,
,

size .
malloc ,

. ,
,

.,.

SIZe

-.....__

malloc

malloc

.

- ( )

8.7

base

m.alloc,

freep

NULL,

219

; , .
, .

(freep)

; .
, ;

.
,
, .

static Header base ;


/* n n */
static Header *freep = NULL ; /* n */
/* malloc: n
void *malloc(unsigned nbytes)

*/

Header * , *prevp ;
Header *moreroce(unsigned) ;
unsigned nunits ;
nunits = (nbytes+sizeof(Header)-1)/sizeof(header) + 1;
if ((prevp = freep)
NULL) {/*
* 1
base . s . ptr = freeptr = prevptr = &base;
base . s.size = ;

==

for

( = prevp->s.ptr ; ; prevp = , = p->s . ptr) {


if (p->s.size >= nunits) {
/* */
if (p->s . size == nunits)
/* */
prevp->s . ptr = p- >s . ptr ;
else { /* */
p->s.size -= nunits ;
+= p->s.size ;
p->s.size = nunits ;

freep = prevp;
return (void *)
if

if

(+1)

freep) /*
= morecore(nunits)) == NULL)
return NULL ; /*

*/

((

*/

morecore .

. -

220

UNIX

malloc, morecore NALLOC ;


.
,

morecore
free.
UNIX- sbrk (n) n
. sbrk - 1 , NULL
. -1 char*

. ,

. , ,

sbrk .
,
. ,

malloc

#define NALLOC 1024 /*


*/

/* morecore:
static Header *morecore(usigned nu)
char * , *sbrk(int) ;
Header *up ;
if (nu < NALLOC)
nu
NALLOC;
= sbrk(nu * sizeof(Header)) ;
if (
(char * ) - 1) / *
return NULL ;
up = (Header *) ;
up- >s.size
nu ;
free ( (void *) (up+1)) ;
return freep ;

*/

==

*/

free . ,
freep, .

. ,
, .

.

- ( )

8.7

/* free: ~
void free(void *)

22 1

*/

Header *bp,

*;

bp = (Header *) - 1; /* n
*/
for ( = freep ; ! (bp > && bp < p->s.ptr) ; = p->s.ptr )
if ( >= p->s.ptr && (bp > 11 bp < p->s . ptr))
break; /* n= : CUIfATa*/
if (bp + bp->size == p->s.ptr) { /* n nbr * /
bp->s.size += p->s.ptr- >s.size;
bp->s.ptr = p->s.ptr->s.ptr;
else
bp->s.ptr = p->s.ptr;
if ( + p->size
bp) { /* n nbr */
p->s . size += bp->s.size;
p->s . ptr = bp->s.ptr;
else
p->s . ptr = bp;
freep = ;

==

. typedef

union

( sbrk
)


. ,
.

8-6. calloc (n, size)


n

size, .
calloc, malloc .

8-7 . malloc

; free
.
.

8-8. bfree (, n)
n malloc free .

bfree,
.

1.
n n n n-

ANSI 31 1988 , "


- n , . 1591989. " n n n ,
, n .

, ,
, n, n ,
. n

, n
n, n
.
, ,
. ,

ANSI
,

2.
n ()
. n
12

1,

. nn
12, n .

2.1
: , ,
, , n n. , -

223

224

, ,
( " ")

.
, .

,
.

2.2

/*

* 1.

2.3

.
;

. .

31 ; .


(All. 2) .
:

2.4 n 3
, :

auto
break

char
const
continue
default
do

double
else
enum
extern
float
for
goto
if

int
long
register
return u
short
signed
sizeof
static

struct
switch
typedef
nion
unsigned
void
volatile
while

225

, ,

fortran asm o
const, signed vo1ati1e AN $1
; enwn void ,
; entry, ,

2.5

2 :

-
-

2.5. 1

, ,
, n

8 9

10 15

(r

u,
unsigned) ,
1ong

( 4 )
, 11
:

int, 1ong int 1 unsigned long int o

int , unsigned int 1 1ong


int , unsigned 1ong int o u u unsignedint ,
unsigned 1ong int 1 L 1ong int , unsigned 1ong
int UL , unsigned
1ongo
1

1ong

226

2.5 .2

''

,
.

' ;

, ,

N L (LF)

\n

\t

VT

\v
\b

\\
\?
\'

\"

CR

\r

FF
BEL

\f

1, 2

000

hh

\ xhh

, .
\ ( )
NUL .

\xhh

, .
,

, . ,

char

(.

slgned) ,
char.

\ ,

.
,

char .

L, , L ' ',
wide) . wchar_ t ,
<stddef. h> .
(.

wchar_ t.

, ,

wchar_ t

char ;

.2

227

2..
,
, , , (+

-)

f ,

F,

1 L .

.
( ) ;
, (

float , L

. ; F
1ong doub1e , doub1e .

2.5.4
( . 4)

int.

2 .6


" .. . ". " " static
( 4 )
.

,
, .
.
,

\ 0,

,
.

,
L , L "x".
"

wchar_ t ".

.
,
,

ANS I ,

.
, ,

228

3.

,
.
; ,

, " ".

" opt",

, ,

t }

l.
n ,
n

4.
, , :
;

; ; ; .
, ,
, :

mun.

;
.

, , , . .
,
.

Al l .

4. 1

( ):

. ,
, .
.
,

auto ,

register

( )

.4

229


,
.

, ,

static.

, ,
.
,

sta t i c;

.
, , ,

extern ; .

4.2
.

<l imi ts.

h>

(char)
. 1<0

char ,

char , ,

,
.

uns igned char

,
; ,

signed c har,

, .

unsigned char

signed char .
char , ,
short int, int long int . int

. .
,_,
,
.

int ,

,
2"
,

na

u nsigned ,

230

(.

overflow) .


,
.

(float) ,

(double) (long double)

,
.

long double

double;

long float

( . 4) . ,


.

,
.

char int

, ,
.

double .
void

float, double

long

4.3 n

, :
;
;
;
;

.

4.4
n .

const

; volatile -

23 1

. . 2.


;
(.

lvalue)

. ;

, *
. " " El

2 El .

3
, ,

.
. 5 ;
.

.1 ()
, ,
,
.

int

int;

unsigned int.
( ).

.2 3


, ( )

232


. ,

, .

. n
,
;

. ,

, ,

.

6.4 n

.

.

.

.
, , .

.
-,

long d o uble ,

long double .

- , double,

double .
-,

float ,

float .

- , ; -

unsigned long int ,

233

unsigned long int.


-,

int,

long int ,

unsigned

unsigned int
long int .
-, long int ,
long int .
- , unsigned int ,
unsigned int.
-, int.
2 . , floa t n nn

long int ;

long int ;

unsigned int

, ; n n

n . ,
n ,

unsigned
.

.
n n

, , n
.

.
,

;
( 7 . 7) .
;

( 7 . 7) .
,

void* ,

, ,

.
,
.
,
.
, . .

cast

( ) ( 7.

8 .8 ).


; .
, .

.
-

234

,
.

;
" " ,

char .

. , void*

.
,

.
; ,
,
.

A6.7void
( ) void
, ,
e-void .

void

, , ( 9. 2) ,
" "

(,) ( 7 .18) .

void .
,

void

void

6.8

void

void*
.
, .

, . ,

void*

void*

char* .

void*

ANSI

,
.

.7

235

7.33

, ,
, n + ( 7

7)

7 ol 7

n

l n

n ,
,
, ()
, n

, n
n , n

n n
n n

,

, n

n n
n n
n

,
n
;

7.1
n " ",

n
" n "
n & ++ ,

-- , sizeof,
"

n n

"

" ",

n & , " n "

236

7.2

, ,

-:

( )

- ,
.
.

( ) ,
, .
- .

, 2. .
-. "

char"

( , "

wchar_ t " ) ,

7 .1, " char"

(wchar_ t) .
7.

, .

-,
.
.

7 .3
.
-:
-

- {]
- (-- )
-.

-- >
-++
-----:

- -

---, --

.7

237

7.3.1 3

.
"

1"',

; .

El [2] ( )

* ( (El) + (2))

8 . . 2 .

7.3.2
,

( 7 .17),

.
,

extern int (};


.
(

7 . 1) "

" .
" "

. ANSI


. .


; (
) ,
. " ()" "
() "
.

,
; .


,
. ,


.
.

238

; , , .
, .

8. . 10. 1 .

,
: (
,

float

. 1)

doub1e.

,
.


. ,
,

, .
,
,

.
,
,. "

( , ... ) .

,
;


, ,
.

.
.

.
, ,
. .

7 .3.3

.
,
. ,
.
.

.7

239

n n ( - >) n
n -. n

n ,
.

n n ,
n n ; .

1-> (*1) . . . .


; ,

. ANSI .

7.3.4
n

++

--n n .

n.

, n ++

--.

; (
7. 7) n ( 7 . 17) n
n n. .

7.4

n rpynpa .
- :

-
++-

---
- - ->

sizeof -
sizeof (-- )
-:

& "_ + --!

7.4.1
n ++ --n .
n

++

--

1 . -

240

()

;
( 7. 7) ( 7 .17)
.
.

7.4.2
& .

,
register , .
.
, " ''

7.4.3 ()
*
. ,
, ,
. " ", .

7.4.4
+
.

. .

+ ANSI .
-

7.4 .5

.
.

1; .
.

.7

241

7.4.6 3

- ,
.
. ,
.

, -
. .

7 .4.7 r r

!
,

7.4.8

int .

sizeof

sizeof

.
, .

sizeof

char

1;

,
: n n
.

.
;

<stddef . h> ( )

size_ t .

7 .5

.
-- :

(--) --

242

.
.

8.

7.6 n n

* , 1,

- :

* --

- 1 - -
-

% - -

.
.

; ,

. ,
)

(a/b*b+a%b

7.7

- .

, .
,

-:
-

+ -

- - -
+ .
.

.
,
. ,
,

p+l

.7

243

, .

.
.

- .
,

.
,

1. ptrdiff_ t
<stddef. h>.
;
, (+ 1) -

1.

7.8 3 .

<< >> .

.

. ,
.
u--:

u-
--
--

<< -
>> -u

1<<2 1 ( )

2 ; 2 2
1>>2 1 2 .
22 , 1
; .

7 .9

,
;

a<b<c

(a<b)

<,

(a<b)

1.

244

-:
--

< - -

> --

<= - -
- >= - -

< ( )

, > () , <= ( )

>= (

1 int

( ) ;


; ,
; ,

p+l
p+l

7.10 3
-- :
-

- -

== -

--!= -

()

!=

( )

( a<b=c<d

e=l

a<b c<d )

, :

, voido

.7

245

7.11 n
-:
- -

&

--

;
.
.

7.12 n n
- - :

-
--

& -

;
.
.

7.13 n

-:
--

- 1--
;
.
.

7.14r
--:

-
--

&& -

&& . 1
, . & , &&

:
; , .
, -

246

1 . ,

int .

7.1 5 r
- - :
- -

--

11

1 1 --
. 1

, .

1 , 1 1

:
; ,

1.

1 , . ,
.

int.

7 .1
-:

--
--

? : -

;
, , ,

. .

,
,

. void , ,
, .
, ,

, . void ,
,

v oid , . ,

,

.

7.17 33 3

;
.

.7

247

-- :
-

- -- - -

--:

*=

1= % =

+ = -= <<=

>>=

&=

1=

,
: , ,
. ,

const;

const .

,
.

=,

.
: ,
;
;

void,

const

volatile . El = 2
El = El 2 El .

7.18
:

--

--

.
, , ( 7.3 .2 )

( 8.7),
,
, , ,

f(a,

(t= ,

t+2) ,

, .

248

7.19 33

- :
-

, ,
, ,
.

,
, ,
;

sizeof.

, , ,

;
, .

, , ,
. (,

sizeof.)


;
& ,

.
&

.
/
.

#if; sizeof

15.5.

8

;

. .

.8

249

- -- .,;
--
; ()

.
- :
u- -- - .,
- - - opt

-- - <
--:
-
--, -
-:

( 8.5);
.

,
, ;
.

8.1
:
--u-:

auto
register
static
extern
typedef
4. 4.

auto

register


. , ,
.

register

auto

250

; . ,

register,

&

, , .

register,

auto.

static

, .
,
; All. 2.

extern,

All. 2.

typedef
; . 9.

.
:

auto ;

extern;

static

; AlO - All.

8.2

:
--u:

void
char
short
int
long
float
double
signed
unsigned
- --
-u
typedef-ue

long short

int; int . long


double .

signed unsigned

.8

251

int short long ,

char. int.

signed char ;

.
,

int.

, ,
.
--:

const
volatile

.

const ,

volatile .
const volatile ANSI .
const
, , n . volatile
n n n

. n , -n
/ , n n

volatile

n n

n n.
n n

const n

.

.
, .

-- -:
-- opt { --
--
-- :

struct
union

252

--
:

- -:

-
-- -

-:
- - --;

--:
- -- opt

- -- opr
--:
-

--, -

,
. , ,
. , , ;
.

-:

' : -

--

{ - -

:
--
,

mun.

, , ( )

typedef 1 .
1 -

.8

253

. ,

} .


. , ,
;
,
.

-- ;
,

.
( All . l) ,
.

ANSI.

, ,
;
.


.
, .
,
. ,

ANSI .

. (
)

int, unsigned int

signed int

int

n n.

, , .
n

, , ,

. ,
n,
.

ANSI

"
", .

254

. ,


.
;

.
.

.
.
, .

struct tnode {
char tword[20] ;
intcount;
struct tnode *left;
struct tnode *right ;

20 ,

. ,

struct tnode

, *;

, .

sp->count

count

s . left
,

s.right->tword[O]

tword

r,
. , -

255

:
,

, :

union {
struct {
int type ;
} n;

struct {
int type ;
int intnode ;
} ni ;
struct {
int type ;
floa t fl.oa tnode ;
} nf ;
} u;

uonf type =FLOAT ;


u nf floa tnode =3 14 ;
if (uono type == FLOAT)
sin(u onf ofl.oatnode)

8.4

.-:

enum .opt { -.}


enum .
-. :

.
- , .

.:

.
. = ..-

256


it .

=,

1,

. =
;
.

,
.
uu u u
;

. uu- uu
,
;

uu - uu
.
n , ,

.
:

:
opr u-
-:

()

- {- opr ]
u - ( - - -)
u - ( -uu 0' )
:

* - - opt
* --r
-u u -u:

- -u
u-uu-u ll-- u

.8

257

,
; .

.

.

,
-.

,
.

,
.

( 8 . 2) , "
D", D .

.
D

.
D

( Dl )

Dl D.

, .

8.6.1

D D

* --opt Dl
Dl "--
" ,

"- - -

- ':

, .
,

int*ap[) ;

258

, [] Dl ; " int * []

" (n)

" int",
, " " ,
, "

int"

int i , *pi , *const cpi &i;


const int ci = 3, *pci ;
i pi
cpi ;
, ci
, ( ,

) pci " const int" pci


,

pci

8.6.2

Dl {-.,
TDl " -- :
D " -- "
-, ,
,

, ,

, (
)
,

, ( AlO 2) ,
( 8 . 7) . ,

floatfa[17] , *afp[17] ;
.
,

static int d[] [5] [7] ;

.8

259

1 d :

; .

x3d 1 xd[i]

xd[i] [], xd[i] []

[k]

. "",
d [ i

[ ]

int.

d [ i]

* (El+E2) .

El [2]

, 1

+ ( . , 7 . 7 , 7. 7) ,
Ele 2 1 El [2] 2- El
, xd[i] []

[k]

* (d[~] []

+ k).

d[i [] 7 . 1 "
"

7 . 7,

.
(n )

8 .6.3

D,

Dl

(- - - )


"

TDl

"--

"--

- - - "

-- - :

-
r-

-
-

-:
-
- -r

260

,
. ,


void . ",

... ",


, 7 . . 2

, ;

AlO. 1. ,
register,
.
,

,
.
.

D, D

D 1(--)

" ,

D1

"

--

"--

". ( )

--u:

- -,
,

( 10 . 1) .
.

int f () , *fpi () , (*pfi) () ;


f , fpi
pfi .
; .

intstrcpy(char*dest , constchar*source) , rand(void) ;

.8

261

strcpy int, ,
, .

. rand int.

ANSI .

" "
,

:
.
,

void

.
..

... "

, ,

<stdarg. h>,

, .

++

8.7 n3

, -
.
=, ,

. ,
.
:

- -

{ -}
{ -,}
- :

-,

7 .19.

auto register , ,

. , ,
,

.
,

. ANSI ,

.

262

,
( ) .

, . .
,

.
, .
.

.
, ,
.
;
.

;

. ,

2 . )

wchar_ t.

, , ,

; ,
, ,
.

.
. "

" , .

ANSI

, .
:

, ,
, ,

; n
. ,
,
;

.8

263

int [] = { 1 , 3, 5 } ;
,

floa t

[ 4] [3]

={

{1,3 , 5} ,
{2,4,6},
{3,5 , 7},
};
:

1 , 3, 5
[1] , [] [2] .
[1] [2] .
( 3] .

(], .. [] [] , []

floa t

[ 4 ] [ 3] = {
1 , 3, 5 , 2 , 4 , 6 , 3, 5 , 7

};
, [

.
, [1] [2]. ,

floa t

[ 4]

( 3]

={

{1}, {2} , {3} , {4}


};
, .
,

charmsg[]

="Syntaxerror on line %s\n";

8.8 n

n,

sizeof)

264

. --,
.
-- :

- - -opt
-:

t --
--:
( -)
-- opt [- ,

-- opt (-- - opt>



,
.

. ,

int
int *
int*[]

int (*) []
int * ()
int (*[]) (void)

" ", "

", " " , "

", " ,
", "
".

8.9 Typedef
,

typedef

;
. typedef .
typedef-ue:

typedef -

.8

265

( . ) . ,
typedef
.
,

typedef long Blockno, *Blockptr ;


typedef struct { double r , theta ; } Complex ;

Blocknob ;
extern Blockptr bp ;
Complex z , * zp ;
.
;

typedef

zp

long,

bp

long,

. , b lon g
.
typedef
,
.

extern Blockno ;
Blocko,

extern int Blockno ;


.

8 . 1 n

,

( , long

long int) .

, ,

. 8) , typedef
,
.
.

266

9
, .
.
.
:

-
-

-
--
-

9. 1 03 (.

labeled)

.
- :

default:
( .

label)
goto .

. All.l.

defaul t swi tch (

9. 4) . .
(
) .

9.2 33
,
-:
r ;

267

,
;

9.3

, ( , "" )

-:

{ -

,- opt }

-:

-:

-
f
, (

All ol) ,

All) ;

static ,

9.4 3 3

--:

if ()
if () else

swi tch

()

268

if ,
,
, . ,

. else

else if else .

swi tch
,
. switch .


( 9. 1) .
( .1) ,
. swi tch ,
. defaul t

swi tch. swi tch ; defaul t


swi tch .
swi tch
.
,

defaul t

, .

defaul t ,

swi tch .

in t

swi tch

9.5 3
.
-- :

while ()
do apea while ();
for (opt ; opt ; upt )
while do ,
;

. while , (),
, ; do

for ,
. .

. 9

for

269

1
. .

. continue 1

for

(l ; 2;3)

l;

while (2) {

}
.
l , , .

9.6
.
-:

goto ;
continue;
break;
return opf

goto , (

9 .1) .

.
continue

.
cotinue . ,

while ( ... )

do {

for ( ... )

contin :

contin:

contin :

} while ( . .. } ;

continue
goto contin .
break

270

switch ,
;
.

return.

return

, .

return . .
1. n
( )
; ,
.

- :

-
-- -

-:
-

.
,

10.1

-:
- opr - opr
-
,
, exter

static;

All. 2.

, , ,
void, . -

.10

27 1

; . .

, (

8.6 . ) .

- (---)

- (- opr)
-
. ,

typedef .

, ,

;
.

void , ,

", .. . "

va_arg <stdarg. h>


,

. .
,

, .

int .


, ,

register.

,
(,

" mun",
"

mun",

" mun"
" mun".
, ;
7 . .

2.

ANSI .
, ;

double.

float

272

intb, inte)

intIAx(inta ,

intm;
?:

m= (a>b)

b;

return (m>e) ?m:

int ; max (int , int b , int


{ .. . }

) ,

intmax(a, b,
inta, b, ;

/* . . . */

int max (, b,
.

) ,

int , b,

10.2 w

,
. " "
,

extern; ,
, extern statie.

,
, .

. 10. ,
,
( 8 . 3)

. ,
( 8. . 2) , ,
, ,

. , ,
,
.

statie , ;

.
11 . 2.

A.ll

273

extern

. 1
.

.
1 1
.

1
.

. 1
.

UNIX
1

1 .
1

1 .

11. nop3yBatbe

, : 1

;
1 1

.

11 .1 r

274

, ,

. : , ,

typedef enum ;

; , ;
.

.

;
, , , ;
r

.
,

.
.


;

.

,
.
. , ,

) ( )


, ,

11.2 3

, .

,
.

AlO. 2,

static,

extern ,

.12

275

extern

,
,
, .

12.
,

. #,
, .
;

( ) .

; (n
12

.2

#include

( 12 .

4) ;

. ,
,
.

, , .

1.

, ,

12 .

1,

.
,
.

2.

, ( 12. 2)

. ;
.

, (
12.3-

4.

Al2.10).


; .

. ,

,

.

276

12.1

ASCII, ISO 646-1983


Invariant Code Set.
,
.

??=
??/
?? '

?? (
??)
??!

#
\

[
]
1

??<
??>
??-

ANSI .

12.2

\
.

12.3

dejine --


;
. #define


,
.

# define u (u-) --u


(,

,
,

.12

277

ft undef

. tundef
.

(,

),

.
;
, .

, .
.
,

.

.

##,


, .
. ,
,

i,

.
\, "

ti ,

ii,

.

11.

ti

1
. 1

1
;
.

#,
.

n n nn n

ANSI

278

# ##,

, ,
( )

, "
" ,

#define TASIZE 100


int table [TASIZE] ;

#define ASDIFF ( , b)

(()>

(b) ?

()-

(b) : (b)-

())


,
,
, ,
,

#define tempfile(dir) #dir


"/ % "

tempfile (/usr/tmp)

" /usr/tm.p" "/ % "


#define t

( , )

##

cat (var, 123) var123o , cat(cat(1,2) , 3)

: ##
,

cat ( 1 , 2) 3

) 3 (

)
,

#definexcat(x ,

cat(x , y)

; xcat (xcat (1, 2) , 3) 1 , 2 , 3,


xcat ##

, ASDIFF(ASDIFF(a , b) ,)

. 12

279

12.4

1t include <-- >


. --

>

", ', \, /* .
.

lt include "

--

"

ycnee,
. , \, /*
,

, > .
,

f include --

;

< .. .> " . .. ", .

linclude

12.5 n
,

- :

if-uuja elif-eou l-r fendif


if-:

ft if -
ft ifdef
ft ifndef

280

elif-eou:

elif-uuja
elrif-eou opt
elif-uuja:

lf elif -
l-:

/-
l-:

lfelse

(if-line 1 elif-line 1 else-line 1 and lfendif)


lfif

lfelif
; .
. " "

1
; .

lfif

lelif

lelif lelse 1 1
.

lelse

lfelse .

- .

#if lfelif .
1

defined

defined

( )

l lL
OL .

OL.
1

long unsigned long .


.19)

' 1 sizeof 1

(cast)

.12

28 1

#ifdef
#ifndef

# if defined
# if ! defined
#elif

defined ,

, .

12.6
,

# line
# line

" --

"

, ,



. , .
.

12.7 rpewa

# error ,-- opt


12.8 Pragma

# pragma --

o pt

282

12.9

#
.

12.1

. ,

LINE

defined, .

FILE

DATE

"mm dd "
" hh :mm: "
D

1 .
1,
.

#error ipragma ANSI ;

,
.

13.

. , .


, -, - , ,

, - ; n
.

.
,

.13

283

" " ( -)

opt,

. , tdf

- typedef-ue ,
-.

if'-else.

--:

-
-- -
-:
-

-:
- opt -t
-
:

- -- .;
-:

-
- :

--- - opt
- - op t

- - opt
---:

auto register static extern typedef'

mun :
void char short int long float double siqned

unsigned ---

typedef-ue
-:

const volatile
---:

-- opt { -
}
--

'

284

--:

struct union
--:

-
- - -
--:
-
--, -
-:

-:

-- --;
--:

- -- opt
- -- '
--:
-
--, -
-:

opt: -
-:

enum opt { -}
enum
-:

-,

= -

:
opt -

.13

-:

()

- [- opr]
- (---)
- (-opt )
:

" -- opt

"--.,
--:
-
-- -
---:
-

- ,

...

-:

-
- , -
-:

-
- - opt
-:

-,
:

--

{ -}
{ -, }
-:

-,

--:

- - -opt
-:

opt --

285

286

-_m-:
(-)

-- opt { -
-- opt (--- )
typedef-ue:

:
-

-
-

--
--
-
-:

:
- :
defalt :

-:
.,;

-:

{ -opt - ,}

-:

-
--:

if()
if() else
switch ()
--:

while ()
do while ();
for (,; ,; )

.13

-:

goto ;
contine;

break;
retrn opf
:

--
, --
--:
-

- -- --

assignment-operator:
= *=!= %= += -= <<= >>= &= = 1=
-:

--
--? : -

-:

--:

--

--ii --
--:
-
-- && -

-:
--

-i --
--:

-
-- -
-:

--

- & --

287

288

--:
-

-- == -
-- != -
- :
--
-

< --

> - -

<=

>= --

--

--:

--
-- << --
-- >> --
--:
-

-- + -
-- --
-:

* - -

- 1 --
-

% --

--:

( --) --
-:


++
--
- --

sizeof-
sizeof ( -- )
:

& *+ --!

13

289

-:
-

- []

- (--)
-
-

->+
++
- --

-:

()
--:

--
---, --
:

-
-

-
-

, ,
,

-:

# define --
# define (, .. , )
--

# ndef
# inclde < -- >
# inclde "-- "
# line " -- "
# line
# error -- opt

# praga -- opt
#
-

290

-:

if- elif-eou l-opr #endif

if-uuja:

# if -
# ifdef
# ifndef
elif-eou:

elif-uuja
elif-eouopr
elif-uuja:

elif-

l-:

l-
l-:

#else

ANSI
. ,

,
.
;

; "
"; . . , ,
.

,
:

<assert.h> <fioat.h>
<ctype . h > < limi ts . h>
<errno. h> <locale. h>

<math.h>
<setjmp. h >
<signal . h >

<s tdarg . h>


<stddef . h >
< stdio .h>

<stdlib.h>
<string.h>
<time.h>

ltinclude <>

.

. .

,
,
.

1. :

<stdio.h>

, <stdio . h >
.

29 1

292

UNIX, .

;
'

\n' .

' \n'

) .

,
, .

;
.

FILE ,
. " "
" ", .

,
tderr

stdin, stdout

1.1
. size t
sizeof .

FILE *fopen (const char *filename, const char *mode)


fopen , , , , NULL
mode :

"r "
" w"

;
,

" "

;
,

" r+ "

( .

" w+ "

" + "

),

, , ,
,
.

fflush
. b ,

.1

<stdio.h>

293

"rb" "w+b", .
FILENAE_AX .

FOPEN_

FILE *freopen (const char *filename, const char *mode , FILE *stream)
freopenja
. , NULL . freopen
stdin , stdout stderr .
int fflush (FILE *stream)
, fflush

, ; ,
. EOF ,
. fflush (NULL) .

int fclose (FILE *stream)


fclose ,
,
, . EOF
.

int remove (const char *filename)


remove

.
.

int rename (const char *oldname , const char *newname)


rename ;
.

FILE *tmpfile (void)


tmpfile

"wb+"

tmpfile

NULL

char *tmpnam(char [L_tmpnam])


tmpnam (NULL)
, . tmpnam ()
;

L_ tmpnam . tmpnam

;
_

. tmpnam , .

294

int setvbuf (FILE *stream , char *buf , int mode, size_ t size)
setvbuf ;
.

_ IOFF

_ IOLF

, _IONF .

buf NULL, ,

. size
. setvnuf .

void setbuf (FILE *stream , char *buf)


buf NULL , .
, setbuf (void) setvbuf (stream , buf ,
_IOFF , BUFSIZ) .

1.2 33

printf .
i nt fprintf (FILE *stream , const char *format , ... )
fprintf

stream

format. ,
. :

, , ,

fprintf. %
. % , , :

( )

-,

+,

: ,
.

: , n
( )

#,

n . ,

. , n
. , ,

G,

f , g,

G , ; g

.1

<stdio.h>

295

,
.
, nn (
, ) .

n , ,
,

, , n.

, n

n ,
, ,

q G , n

( n
)

- h, 1 ( ), L . " h "
short unsiqned short ;
" 1" lonq unsiqned lonq . " L "
1onq doub1e .

, n

*.

n ( )

(), () ()

int .

n .l .
n % ,
.

intprintf (const char *format , ... )


prin tf ( ... ) fprin tf ( tdou t , ... ) .
int sprintf (char * , const char *format, ... )
sprint printf n
.

, '\'

. ,

'\0 ' .

296

n . 1

printf

d, i

int ;

int ; ( ) .
unsigned int ; (
) , abcdef ACDEF
.

int;

int ; , unsigned char

char* ;
'\0'

double ; [- ]mmm.ddd,
d .
,

double ; [-]m.dddddd

[-]m.dddddd

, d

. ,

g, G

double ; % %
-4 ;
%

void *;

f . .

. )

int * ;
printf

: %

int vprintf (const char *format , va_list arg)


int vfprintf (FILE *stream , c onst char *format, va_list arg)
int vsprintf (char *, const char *format , va_list arg)

vprintf , vfprintf vsprintf ,

printf ,

.l

arg 1

va_ arg.

<stdio.h >

va_ start
<stdarg. h> 57.

297

1.3

scanf .

int fscanf (FILE *stream 1 const char *format 1 )


fscanf stream format 1

1
. retur

format . fscanf EOF

. :

- 1 .
- ( %) 1
.

- % 1 *
1

h1 11


. 1
. * 1 %* 1
l , ; .

;
.
scanf 1
. ( ,
, , , m . )

.
.

2.
d 1 i 1 n 1 1 u h
short int 1 1 ( )
1ong . 1 f 1 g
1 doub1e
floa t 1 L 1ong doub1e.

298

scanf

.2

n ;

int*.

int*. (

) ( )

( ) ;

( )

unsiqned int* .

char* .

int* .

; int* .

,
; 1 . '\ '

n
;
,

%ls .

char*,

) ;

' \'

e,f , g

n ;

+-,

float* . float

printf

( " % " ) ;

void*.

n ;

int* .

[ ... ]

[] ... ]

char* . ' \ ' .

].

npunaza ;
' \ '

[ ... ] ... ]

char* .

%. n .

int scanf (const char *format, ... )


scanf ( .. . ) fscanf (stdin, . . . ) .

].

.1

int sseanf (eonst ehar * , eonst ehar *format , ... )


sseanf (, . . . ) seanf ( .. . )

<stdio.h>

299

1.4

int fgete (FILE *stream)


fgete stream unsigned
ehar ( int) , EOF
.

ehar *fgets {ehar *, int n , FILE *stream)


fgets n-1

;
'\ '

. fgets

NULL

EOF

int fpute (int , FILE *stream)


fpute ( unsigend ehar)
stream. , EOF .

int fputs {eonst ehar * , FILE *stream)


fputs ( \n)
stream; EOF .
int gete (FILE *stream)
gete fgete ,
, stream .
int getehar {void)
getehar

ehar *gets (ehar *)


gets

gete (stdin) ,

'\0' .

NULL

int pute (int , FILE *stream)


pute fpute,
, stream .
int putehar (int )
putehar ()

pute ( , stdout) .

300

int puts (eonst ehar *)


puts stdout.
EOF , .

int ungete (int 1 FILE *stream)


ungetc ( unsigned ehar)
stream, .
.

EOF

. ungetc EOF
.

1.5 3 3 33

size_t fread(void *ptr , size_t size , size_t nobj 1 FILE *stream)


fread nobj size
stream ptr. fread ;
.

feof

ferror .

size_tfwrite(constvoid*ptr , size_tsize ,size_tnobj , FILE*stream)


fwri te nobj size ptr
stream.
nobj .

1.6 3 n3

int fseek (FILE *stream 1 long offset 1 int origin)


fseek

stream ;


. ,
offset origin , _ (n
)

, SEEK_CUR

( ) SEEK_END ( )

, offset ,
ftell ( origin _) . fseek
.

long ftell (FILE *stream)


ftell stream -1
.

void rewind {FILE *stream)


rewind(fp)

fseek

(fp,Ol,

_ );

. l

<stdio.h>

301

clearer(fp) .
int fgetpos (FILE *stream, fpos_ t *ptr)
fgetposs stream *ptr ,
fsetpos. fpos _ t
. fgetpos
.

int fsetpos (FILE *stream, const fpos_t *ptr)


fsetposs stream
fgetpos *ptr. fsetpos
.

61.7 3 rpewa

.
. ,

errno ( <errno . h>)


.

void clearerr (FILE *stream)


clearerr
stream.
int feof (FILE *stream)
feof

stream.

int ferror (FILE *stream)


ferror
stream.
voidperror (const char *)
perror ()
errno ,
fprintf (stderr, " %: %s\n" , , " " );

strerror .

302

2. :

<ctype.h>

<etype. h> .
int
EOF unsigned ehar ,
int. ()

, .

isalnum(e)
isalpha(e)

isalpha(e)
isupper ()

isentrl(e)

isdigit(e)
islower ()

isdigit(e)
isgraph(e)
islower(e)

isprint(e)

ispunet(e)

isspaee ()

, , ,


, ,

isupper ()
isxdigit ()

ASCII ,
020

( ' ')

( '-') ;

NUL OxlF (US) ,

Ox7F

(DEL).

, :

int tolower ()
int toupper ()
, tolower () ,
toupper () ; .

3. :

<string.h>

<string. h>.

mem .

str;

mermnove ,

, .
unsigned ehar.

eonst ehar* ; n

size_ t;

t ehar* ; et
int ehar.

: < stri g . h >

ehar*strcpy (, et)

ehar*strncpy ( . et, n)

' \0';

ehar*streat ( , et)

et

303

et ;
et

'\0 '

et

ehar*strneat ( , et, n)

et
'\0 ';

intstrcmp(es, et)

es<et,
intstrncmp(es, et , n)

es=et

et ,
es>et

<

>,

et ; < es<et , es==et


es>et

>,

ehar*strehr(es ,

,
NULL

ehar*strrehr(es ,

size_t strspn ( , et)

NULL

size t
et)

strespn(es ,

et

ehar*strpbrk(es, et)

et

et

NULL

ehar*strstr(es,et)

et
size_t strlen ()
ehar*strerror(n)

NULL

ehar*strtok(s , et)

et ;

strtok ( , et) ,

et . e-NULL ,
et;

'\ '
. ,

NULL

,
. strtok

NULL .
et .

mem ... -

304

; .

t void* ; etce eonstvoid*; ne size_t;


int

unsigned ehar.

void* memcpy ( , et, n)


void*mennove(s, et, n)

n et
memepy ,

intmemcmp(es, et, n)

n
strcmp

void* memehr (,

et ;

n)

NULL

void* memset (,

n)

n .

64. :<mth.h>
<math. h> .

EDOM ERANGE

<errno .h>)

HUGE_ VAL

double


. ,

errno

D ;

double .

n , UGE_VAL
,

errno

ERANGE .

, ;

ERANGE

double .

errno

double, n int ,

sin(x)
()

t&n(x)
asin(x)

sin-L()
[-1,1].

[ -pi/2 , pi/2 ,

()

- 1 () [ ,pi ]

atan ()
atan2(y,x)
sinh(x)

,
tan-L() [ -pi/2 , pi/2] .
t&n-1 (y/x) [-pi ,pi].

[-1, 1] .

cosh ()

tanh(x)

()

-'

log(x)

loglO(x)

pow(x 1 y)

xr.

ln ()

305

>.

log10 ()

<stdlib.h>

>.

= <= 1

< .

sqrt ()

1 >=.

ceil ()

1 double.

floor ()

1 double .

fabs(x)

ldexp(x 1 n)

*2 "

f r
int*exp)

1 1

[ 1/2 1 1) 1 2
*. 1 .

m d f
double*ip)

1
. *ip 1
.

fmod(x~y)

! 1 . 1
.

65. : <stdlib.h>
<stdlib. h>

double atof (const char *)


atof double;
char**)NULL) .
int atoi (const char

strtod

*)

int ;
(char**)NULL 1 10).

long atol (const char *)


long;
NULL 1 10).

(int) strtol ( 1

strtol ( 1 (char**)

double strtod (const char * 1 char **endp)


strtod double 1 l
;
*endp endp NULL.

306

, UGE_VAL ;
.

errno

ERANGE.

long strtol (const char *, char **endp, int base)


strtol long,
;

*np, np . base 2 36 1

base

8 1
1

16;

1 .

16.

10 base-1;

1 LONG_ LONG_ MIN,

errno

ERANGE.

unsigned long strtoul (const char * 1 char **endp 1 int base)


strtoul strtol
unsigned long ULONG_ .

int rand (void)


rand RAND_ 1
32767.
void srand (unsigned int seed)
srand seed

. 1 .

void *calloc (size_t nobj 1 size_t size)


calloc nobj ,
size NULL
. .

void*malloc(size_tsize)
malloc
size 1 NULL .
.

void *realloc (void * 1 size_ t size)


realloc size .

.

realloc NULL

1 * .

: <stdlib.h>

307

void free (void *)


free ;
NULL.
calloc, malloc, realloc.
void abort (void)
abort ,

raise(SIGART).

void exi t ( int sta tus)


exit . atexit
,
,

. status
, .

EXIT_SUCCESS

EXIT_FAILURE,

, .

int atexit(void (*fcn) (void))


atexit

fcn

;
.

int system (const char *)


system .
NULL system
. NULL,
.

char *getenv (const char *name)


getenv name NULL
. .

void*bsearch(constvoid*key,constvoid*base,size_tn , size_tsize,
int (*cmp) (constvoid *keyval, constvoid *datum))
bsearch base [] ... base [n-1]
*key. cmp
( )

( )

base

. bsearch

NULL

308

voidqsort(void*base, size_tn, size_tsize, int (*cmp)


( const void *, const void *) )
qsort
size: base [] ... base [n-1] . cmp
bsearch .
int abs (int n)
abs

long labs (long n)


labs

int .

long .

div_tdiv(intnum, intdenom)
div num/denom.
int quot rem div_t.

ldiv _ t ldi v ( long num, long denom)


ldiv num/denom.
l?ng quot rem ldiv_ t.

. r: <assert.h>

assert

void assert (int )


assert () ,

assert

stderr

, ,

Assertion failed: , file filename, line nnn


abort .

FILE

LINE

NDEBUG <assert. h> ,

assert

.8

7. r:

<setjump.h>

309

<stdarg.h>

<stdarg. h>

.
lastarg f

. f

va_list

va_listap;
va_start
:

va_start (va_list , lastarg);


,

va_arg

va_ arg :

typeva_arq (va_list , type);

void va_end (va_list ) ;


,

f.

8. :

<setjump.h>

<serjump. h>

,
n.

int setjmp (jmp_buf env)


set_jump env
longmp.
setjmp , n
longjmp. setjmp
, if, swi tch ,
, .

310

i f (setjmp(env) == )
/* n n*/
else
/* n longjmp */
void longjmp ( jmp_ buf env , in t val
longjmp
setjmp , env,
setjmp
val.
setjmp .

longjmp ,

e-volatile

setjmp
setjmp .

9. rn:

<signal.h>

<signal. h>
,

void (*signal (int sig, void (*handler) (int))) (int)


signal .
handler SIG_DFL,
, SIG_ IGN,
; , handler,
. :

SIGART , ,

SIGFPE

abort

SIGILL , ,
SIGINT , , interrupt
SIGSEGV , ,

SIGTER ,

signal handler ,
SIG_ERR n .

sig ,

; -

.10

(*handler) (sig) .

handler

<time.h>

311

int raise (int sig)


raise sig ;

1. :

<time . h>

<time.h>

. 1
1 , .

time_ t 1
tm :
int tm_sec;
int tm_min ;
int tm_hour ;
int tm_mday ;
inttm_mon ;
int tm_year;
int tm_wday ;
int tm_yday ;
int tm_isdst ;

clock_ t
struct

(0 1 61)

59)

(0 1 23)

(1 , 31)

(0 1 11)

1900

(, )

1 ( 1 365)

tm_isdst

1 .

clock_tclock(void)
clock
1 -1
.

clock ()

/CL_PER_SEK .

time_t time (time_t *tp)


time -1
. tp NULL 1
*tp .
double difftime ( time_ t time2 1 time_ t time1)
difftime time2-time1 .

312

time_tmktime (struct tm *tp)


mktime

*tp

time.
mktime

-1 .

char *asctime (const struct tm *tp)


asctime</tt< *tp
Sun Jan 3 15 : 14:13 1988\n\0
char *ctime (const time_t *tp)
ctime

*tp

asctime(localtime(tp))
struct tm *gmtime (const time_t *tp)
gmtime *tp .,
" (. Coordinated Universal ime - U). NULL
u . gmtime .
struct tm *localtime (const time_t *tp)
local time *tp

size_t strftime (char *, size_t Slll4x , const char *fmt, const struct
tm *tp)
strftime
*tp, fmt,
printf. (
' \') . %

smax . strftime
,

'\'
1114 .

%b

.ll

: <limits.h> <floath>

%d

(24- )

%!

(12- )

%m

(01-31)
(00-23)
(00-11)
(001-366) .

(01-12) .
(00-59) .

%U

31 3

(00-61).

( 1- )

(00-

53) .
%w

(-

%W

( 1- )

%Z

%%

%.

(00-53).

(00-99) .

11. n: <limits.h> <float.h>

<limi ts. h>

. ;

BIT

UCAR R

char

MIN

char

MIN

char
IN

32767

int
IN

MIN

-32767

int
LONG

2147483647

-2147483647

long
LONG MIN

long

314

+127

signedchar
R

-127

MIN

signedchar

+32767

short

SHRT MIN

-32767

short

255

UCAR

unsigned char

UINT

65535

unsigned int

ULONG

4294967295

unsigned 1ong

USHRT

65535

unsigned short
, <float. h>,
. ,

FLT RADIX

FLT ROUNDS

2 , 16

FLT DIG

FLT EPSILON

1-5

FLT

FLT
FLT

DIG

, 1.

+ ~ 1 .
FLT_ RADIX

1+37

FLT MIN
FLT MIN

1-37

n ,
FLT_RADIX"-1

10n

DBL DIG

10

DBL EPSILON

1-9

DBL

DBL

DIG


, 1.

=1.

FLT_RADIX

1+37

(double)

.11

DBL

: <limits.h> <floath>

DBL MIN

lE-37

n,

315

FLT_ RADrxn- 1


(double)

DBL MIN

FLT RADIX

8: rn

,
.

;

,
.
& ,
. ,

ANSI

, ,

, .
,
.


. , ;
,

,
.

, ; ;

(ft) ;

(#ft),
#pragma ;

, ftelif


; .

" \ " ,
. . . 12 .

-
l;
, . (
.

3 17

318

- ??
.

# \ [] { } 1 -,

12 . 1 .


? ? .

- (void, const, volatile, signed ,


enum) . entry .
- ,
. \

. 2. .

- :

8 9 .

L , F L

. ,
.

- ;
2.6.

- , ,

signed

unsigned.

long float double


long double

- unsigned char.
signed char

void

char

void*

; ,

,
, .

Rt!

<limi ts. h> <float. h>

319

++ ,

const

( 8.

2) .

" " , ,

" , unsigned ; ,

double" "
". . .

=+

, ;
, , .

+ -.

*.

sizeof, int ; ,

unsigned .

, size_ t ,
<stddef. h>.
(ptrdiff_t) .
7. 4. 8 7 . 7 .

&

register,
.

. 7 . 8 .

320
-

, ;

..7.7.

- ( (++)
,

. 7.3.2 ,

86.3 , 57 .

l .

- ,
, , .
,

, .

( ) .

extern

l .

All. 1. ,


. ( . )

1 ,

(\0- )

swi tch 1


' 21.43-45,226
-- (
1) 20, 55, 124, 238
- 49, 241
- 238-239
! 50, 238-239
!= . 18, 49, 243
9, 22, 45, 227
# 105.278
## 105,278
#def i 15,104.276
#def ine enum 46,175
#def ine 104
#def ine, 104
#else, #elif 107,281
#endif 107
#error 282
#if 107, 158, 279
#ifdef 107,281
#ifndef 107,281
#include 38, 103, 178, 279
#line 282
#pragrma 282
#undef105,202, 278
% n
() 49, 241
%1d 20
& 109, 238
&& 24, 49, 58, 243
&. : 57, 243
* 49, 241
* 110,238
, 73, 247
. 150, 235
.. . 182, 237
.h 38
1 11,49,241
?: 61, 246
\\ (. backslash) 9, 45
\ null ( , , ) 35, 45, 226
\ 45,226
\b (. backspace) 9,

45,226
\f _ (. formfeed), 45,226
\ _, 7, 17, 22. 43-45, 226, 291
\r (. carriage retur) 45,226
\t , 9, 12, 45,226
\v , 45,226
\ 43, 226
: 57, 243
_ 4 1 , 226,291
_FILE_ m 309
_ LINE_ 309
_fillbuf 209
_IOFBF, _IOLBF, _IONBF 294
1 : 57, 243
11 24, 49, 58, 246
- 58, 238-239
+ 49, 241
+ n n 238-239
++ (
1) 20, 55,1 24, 238
+= 59
< n 49, 242
n 58, 242
<= n n 49, 242
<assert.h> (. header) 308
<ctype.h> 51,301
<errno.h> 301
<float.h> 42,314
<liraits.h> 42,314
<locale.h> 291
<math. h> 52, 304
<setjmp.h> 309
<signal .h> 311
<stdarg.h> 182, 205, 309
<stddef .h> 121 , 158, 291
<stdio.h> , 18, 104-105, 119, 177-178,291
<stdio.h>, 207
<stdlib.h> 83, 166, 305
<string.h> 46, 124, 302
<time.h> 311
= n 19, 50, 246
== n 21, 49, 243

32 1

322

> n 49, 242


-> n

154,235

>= 49, 242


>> n 58, 242
0... 43, 22

n 3,
n 3, 87,
, 93,

228

228

275

asin 305
asm 224
atan, atan2 305
atexit 308
atof 305
atof 83
atoi 305
atoi 51, 72, 8
atol 305
auto n n
249

, 3, 47, 99, 22
n

, n

241
32, 110,238
249
n, & 109, 238

294

(. null)

, \

200

, n setbuf, setvbuf BUFSIZ

getchar 202

20, 2
119, 232
(. null) 45
(. null), , \ 35, 45, 22
223, 27
(. null) n

45, 22

(. American National Standards lnstitute) (ANSI)


ix, 2, 223
, ,' 21,43-45, 22

, 27
,

, n

105, 278
71, 104
7, 79, 93
n 187,291-292
13

, n

23
n n

117

28, 235

, n

54, 237

117
28, 237

n ,

182,205, 237,20,270, 309

, for ~;)

58, 175

n , &

57, 243
n , 1 57, 243

, void(npaa) 38,8,20,270

n ,

, n

n, 17,25 1

133-1 38
110,115,117-121, 137,

n, n

12,241

- 5, 98, 28

50, 232

98, 28

_n

229

n 2,

150, 253

49

n (),
n

57, 243

57, 243

234

a.out , 82
abort 306
abs 308
305
addpoint 152
addtree 15
afree 119
alloc 118
argc 133
argv 133, 190
ASCII 21, 43, 51, 27, 302
asctime 312

102,1 29
10, 20, 42,314
binsearch 9, 157, 1 1
bitcount 59
break 70, 7, 29
bsearch 308

19, 24, 1
151
, , \v 45, 22
164, 253

17, 177, 200

200
200
, 91

gmtime 312
goto 77, 29

, f

17, 177
/, 192, 301
178, 188, 200
,

41, 224
97 _
228, 275

11, 21 , 2,
15

()

103, 279
200
, 187, 206, 292
, 188, 199
, 187, 209, 292
, 187, 199, 202
, 187, 20, 292
, 204
, n 187, 199, 209, 292
,

/, ()

35, 235

, ()

()

18-19, 201,204

101,132, 156

( )

101,262

(),

9,

323

266

187

130

131, 23

50, 232
va_list, va_start, va_arg, va_end 182,205,297,309
void 109, 121, 140, 233
void 38, 8, 260, 270
void 35, 229, 233, 250
volatile, 229, 250
vprintf, vfprintf, vspr i tf
205, 297

()

void
, 93, 275
, 47, 95, 99, 22
7, 11, , 98
, 11
1, 20, 42, 314
162,239
121, 232
21 , 77
getbits 58
getc 188, 299
getc 207
getch 92
getchar, ubuffered 201
getchar, 202
getchar, 17, 177, 188, 299
getenv 308
getint 114
getline 33, 37, 81, 193
getop 91
gets 192, 299
gettoken 146
getword 10

if-else

, 28,

129,131,263

283

25-20

257

, 23

259
10, 47, 249-254
typedef 171 , 249, 265
union 172, 251

249
36, 270

25, 130,257
25 7
n 11 , 117, 257
150, 251
259-260
, 31 , 85, 235
38, 93, 249
, 176, 251
, 270-272
, , -- 20, 55, 124, 238
, , 1 11, 49, 241
, 11, 49
228, 273-275
93, 275
93, 275
78, 2, 275
, 273
, n 93, 273

1, 11 , 229

, ,>>

7,

70

58, 242

324

28,235

38, 273

27

249

28, 235
28, 81 , 270

, #udef
,

273
na 54, 230

(),

210

scanf, 184,297
19, 24, 1
, 19, 24, 1, 24
, 52, 24
, n , += 59
, n , = 19, 50, 246
, , 50, 59, 246
, 24
41 ,224
224
35, 45, 122
63, 105, 234, 237
, 163
, 144
day_of_year 130
defined 107, 281
del n 146
del 144
difftime 312
DIR, 212
dir.h 215
dirdcl 145
Dirent, 212
dirwalk 214
div 308
div_t, ldiv_t 308
,

d, 75 , 29

do-nothing 82
double 43,227
double, 11, 20, 42, 229, 250
double-float 54, 232

, n

24, 58, 63, 75, 90, 105,

111,234
,

46, 107,226-227, 25

25

227, 256

49,243
21, 49,243

229
61 , 97, 103, 166,219
43, 227
EBCDIC 51
echo, 134-135
EDOM304
else if-else
else-if 26, 67
end of file EOF
enum #define 46, 175
enum n 4,25
EOF 18, 177, 292
ERANGE 304
erro 301, 304
error 205
errors, / 192, 301
exit 190, 30
EXIT_FAILURE, EXIT_SUCCESS 306
305
expansion, 278
extern
3, 38, 93, 249

189,192
291

,
n,
,

38, 96
235

()

166,217-221
, n,,

73, 247

22, 163
17, 177
302
, 52,228
, 52, 228
52-54, 208, 226
21,43, 22
, 43
, 226
-, 26,50, 230
( ), 184, 194,
297,302
/

, 27

, ,==

, ,

25

, , EBCDIC

ASCII 21, 43, 51,276, 302


51
, , 150 276
, 22,32,122

, ,

17, 178, 190, 200

194, 301

238

, , ,

n n 52, 230

224
goto 78
union () 218

178

234-247

235

19, 24, 1, 246


65, 67, 2
, 45, 69,107, 247
, 234
, 62, 234
224
, .h 38
, 98
, of 41, 224
, 273
31 , 85, 235
, 117
114,11 6,259
26,114,235,259
(), , *
110,238
273
, 99, 247
47, 99, 260
98, 268
101 ,262
35, 47, 99, 262
47, 95, 99, 262

,


( )

131,263

101,132,262
119,162
47, 99, 262
150,262
262
156

234, 311

278
if-else , 268, 283
if-else 21 ,24,65,268
inode 210
instali 170
int type 1, 42, 250
isalnum 160,302
isalpha 160, 194, 302
iscntrl 302
isdigit 194,302
lseek 205
isgraph 302
islower 194,302
ISO 276
isprint 302
ispunct 302
isspace 160, 194, 302
isupper 194,302
isxdigit 302
itoa 76

jump, 269

85, 87, 89, 185


246, 291
228
, auto
,

249
, automatic

36,228
extern
n 36, 38, 93, 249
n, register
97, 249
n, static 36, 97, 228
n, static n
97, 249
, 249
,

n,

101,

251
, n , ++

79-80, 88, 90
11 , 65

, 2

176, 253
162, 1,

, printf,

229

2-270

174,195,217, 233
17, 178, 190, 200
, 9, 21, 43-45,226,276
, , \ 43, 226
, 45, 226
(n)

325

250

, n

250
20, 55, 124,

224

326

, 15

133-1 38

28

217-221

179

249

, () 1,

10,223-224, 27

82
79, 93, 273
, , - 58, 238-239
( ) 105, 278

( )

( ), , %

45,105,227

241

,
,

43, 22

43, 224

, ( . backslash), \\

9, 45

302
103, 27-278

(a.lvalue)

230

, , <<

58, 242

138

273
223

21

, 27

, n,

main
main, return 29, 192
makepoint 152
malloc 18, 195, 30
malloc 219
memchr 304
memcmp 304
memcpy 304
memmove 304
memset 304
mktime 312
modf 305
month_day 130
month_name 132
morecore 220

224

, ,!

, ,

21
50, 238-239
OR, , ! ! 24, 49, 58, 24
, 52
, , 5.& 24, 49, 58, 243
, 291
labs 308
ldexp 305
ldiv 308
localtime 312
log,log 10 305
long double, 43, 227
long double, 42,227
long, 43,22
long, 11, 20, 42, 227, 250
LONG_MAX, LONG_MIN
longjmp 309
lookup 170
lower 51
ls 210

49,

27, 32, 39, 79,87-88, 126


241

49, 241

45,9, 107,247

131,

259

278

43, 22

249

103,275-282
104

9, 22, 45, 227

54, 237

, 52, 230
() 3, 86, 228
() ,

f3,270
() ,

of38,273
()

270-271
41,

( ) ,

224
()

() 8,

97
224, 228,

250, 275
,

33, 37
scientific) 43, 8
getchar 201
200
117
, , != 18,49,243
251
85
119-1 21, 12,
(.

241

327

n 49, 243

n n 57, 242

52,228

n n

114, 116-117, 122, 132

22, 32, 122


n 125
155
, 32, 117, 131

n,

241

41
57, 243

n,

n,

n,n

, n (n

154-155, 234

n,

101, 132, 156

241

n, n (n )

19, 62, 111 ,

18, 49, 242

n, 63

129, 131, 263

257
25, 130, 257
, 25, 114, 235, 259
, 101 , 132, 262

n n,

131,263

121

174
n 200
n 202

, n

54,230
11,49,241
#undef

, n

129, 259
116,234

, n

131,259

n n

235
, ( . carriage return), \r 45, 226
_ (. formfeed), , \f 45, 226
_ 223, 276
_, , \n 7, 17, 22,43-45, 226, 291
, n n , 123
n

52
50, 52

138NULL, 119

numcmp 141

228, 230

() ( . pattern), n f

79,81 , 135-137

87

n 79, 93, 273


, n ,

- 49, 241
121, 162, 232

77,266
69,266
, default 69, 266
, 78, 266, 275
52, 228
77, 266
n 42,250
, 235
43
, ... 43, 226
n ,+ 49,241
n of 62, 234
n 50, 59, 246
,

250

250
O_RDONLY. O_RDWR, O_WRONLY 202
opendir 215
... 43, 226

98, 116,236

n,

28, 235
napcep, -n 144
n 144
, 82
n 129,259
24
n 26, 67
n () 31
31 , 111 ,236
91
228, 273-275
, 228, 275
, 86, 224, 228, 250, 275
( ), 269
n , , >= 49, 242
, n, > 49, 242
, , _ 41 , 224, 291
n, 117
125, 139
, 138
n, 138
, n 126, 139
11
n 187, 206,292
n 160
138, 172, 235

328

li

, void

114, 116-117, 122, 132

* 109, 120, 140,233

, - 2,50, 230

null) 119, 232

, ( )

234

110, 117,257
, 119, 12
, 1, 232, 241
114,1 1,259
, 120
, 125
, 120, 12, 232
, 119, 12, 219, 225
110, 115, 117-120, 137,
1 2, 241
,
,

12,

121, 232
119-121,

241

-,

231-233, 241

87

, ,

, ,

<= 49, 242

< 49, 242

, -

232-234, 241
54, 230
, - 54
, - 233, 241
, - 13, 230
, 54, 232-234, 241
, 54, 16, 195, 232, 241,
263
, 103, 275-282
, defied 107,281
, # 105, 278
n , ## 105, 278
, _FILE_
309
, _LINE_ 309
, 282
++-- 55, 124
(. overflow) 49,234,304, 311
, -

n (. uderflow)

49, 304, 311

++--

57, 242
55, 123
, 187,291-292
, 17, 177, 291

187, 209, 292


273
calculator 85, 87, 89, 185
cat 187, 189-192
del 14
echo 134-1 35
fsize 213
udcl 147
22, 163
19
19

45

204

234

ull

52, 232

117

, (.

, float-double

82

25, 70
n , 223
, 184, 194, 297, 302
, 275
, 223, 275
, 223, 270, 273
() 19, 2, 111 ,
154-155, 234
3, 43, 51 , 58, 172, 177, 179, 217
, 49, 130
, 50, 232
() 231-233
( ) 11, 234
166,232,241
retur 8, 270

15

18-19, 201,204

33, 37

( , .

pattern)79, 81, 135-137


126,139

234

(. retur) 8,

21
187

25, 70

()

50, 52, 232

21

270

13-15,17

52, 246
54, 232-234, 241
, double-float 54, 232

179

9-1 ,

18
11, 21 , 25, 47, 12, 223


n,

11, 61, 76, 101,172

228

182,205,236,260,270,309
36,87,228
n, 32, 11 , 238
,

, () 36, 86, 228


,

224
41 , 224
nn 29, 35, 54, 85, 140, 236
perror 301
90
pow 27, 305
power 28, 31
printd 102
printf 7, 12, 20, 179, 296
printf , 180, 2%
pintf , 14,180
ptlnreet 152
ptrdiff_t 121, 172, 242
push 90
putc 188,299
putc 207
putchar 17, 178, 188, 299
puts 192, 299
,

329

register
n 97, 249
remove 292
rename 292
return (n) main 29, 192
return, cao 28,35, 82, 86,270
return, n 86, 270
reverse 73
rewind 301
Richards, . 1
Ritchie, D. . xi

164,253

266
268

41

41 , 224

227
n 199
65, 98, 266, 270-272
, 236
n

249
n ,

auto 249
n,

extern 36, 38, 93, 249


n ,

-,

54, 230

register 97, 249


n n ,

13, 43, 227


, 229
r, 249

static 97, 249


n ,

/,

(n) n

24, 58, 63, 75, 90, 105,

250

enum 46, 256

struct

111 , 234

275
42,224

napcep 144
101,163,165,214, 237,269

276

249

17, 491 , 242


RAHD_MAX 306
raise 311
rand 306
rand 55
read, n 200
readdir 21
readlines 127
realloc 306

256

union 256

250

119, 162, 219, 243

50, 52

177, 188, 200


178,188,200
188,200

291
28, 38, 85, 237
, 47, 99, 262
( ) 27, 305
7, 22, 35, 45, 116, 122, 227
-,

101 , 262
,

227

330

, 234
,

35, 45, 122


, 45,105,227
, 151
, 162, 239
, 150,251
, 150, 253
, 150, 262
, , . 150, 235
, ,-> 154, 235
, 160
, 164, 253
, ( , ) 150, 251
, 164, 253
, 156
, 155
, 237
, 237
, 226
sbrk 219
scanf 112, 184, 298
scanf 184, 297
scanf , 185, 298
SEEK_CUR, SEEK_END, _ 301
setjmp 309
setbuf 294
setvbuf 294
Shell, D. L. 72
shellsort 73
short, 11, 42, 239, 250
SIG_DFL, SIG_ERR, SIG_IGN 311
signal 31 1
sin 305
sinh 305
iz_t 121 ,1 58, 172,239,292
sizeof, 107, 121 , 158,238-239, 299
sprintf 182, 297
sqrt 305
squeeze 56
srand 306
srand 55
sscanf 298
stat 212
stat 21 2
stat.h include file 212-213
static 36, 97, 228
static , 97
static , ( ) 97
static 97
static,
97, 249
stderr 188, 190, 292

stdin 188, 292


stdout 188, 292
str index 81
strcat 302
strcat 57
strchr 302
strcmp 302
strcmp 124
strcpy 302
strcpy 123-1 24
strcspn 304
strerror 304
strf time 312
strle 304
strlen 46, 11, 121
strncat 302
strncmp 302
strcpy 302
strpbrk 304
strrchr 302
strspn 304
strstr 304
strtod 305
strtok 304
strtol, strtoul 306
struct 251
swap 103, 112, 129, 141
switch, 69, 88, 268
syscalls.h 201
system 195, 308

, n

168
printf 180,296
printfpep 14,180
scanf 185, 298
45, 226
63
291
(, ) enumeration 256
(, ) union 256
r (, ) 150, 251
, 17,177,200
17, 177, 291
, 125, 139
, 9-1 ,
13-15, 17
, 17
43, 226
234
, 257
, 246, 250


n, n

251

85
n, n 250
, 250
n, 229
n, 265
n, 1, 11, 229
n, 263
n, 229
n 11, 17, 20, 65, 67
276
tll 166
tan 305
tanh 305
Thompson, . L. 1
time 312
time_t n 311

_294

, n

n,

scanf

printf
1, 42, 228
194, 301
, 259-260
, n 31, 85,235
, 235
, 138, 172, 235
, 234
, - 236
, - 29, 38, 85, 236
, static 97
28, 81 , 270
28, 233

, n, n, n,

238-239

+ 238-239

174

,
, ( , )

262
251

, n
n

223, 275

tmpfile 294
tmpnam 294
tolower 179, 194, 302
toupper 194, 302
treeprint 166
trim 77
typedef 171, 249, 265
types, 229
types, 1, 42, 228
types.h 21 3, 215

, n

331

?: 61, 246

107, 279

ULONG_MAX 306
undcl 147
ungetc 194, 299
ungetch 92
union, 172, 251
union, n 218
uion, 251
UNIX 199,210
unlink 205
unsigned char, n 42, 201
unsigned long 43,226
unsigned 43, 226
unsigned, 42, 59, 229, 250

259

41 , 224
235
, 235
n 29, 35, 54, 85, 140, 235
n 35, 235
fpos_t 301
fprintf 188, 294
fputc 299
fread 299
fabs 305
fclose 189, 292
fcntl.h 202
feof 192, 301
feof 207
ferror 192, 301
feror 207
ff\ush 292
fgetc 298
fgetpos 301
fgets 192, 299
fgets 193
FILE 187
filecopy 189
FILENAME 292
float, 43, 227
float, 1, 42, 229, 250
float-double, n 52, 232
floor 305
fmod 305
fopen 187,292
fopen 208
FOPEN_MAX 292
,

332

for n while 15, 71


for( ;;) 71 , 104
for, 14, 20, 71 , 269
fortran 224
fputs 192, 299
fputs 193
free 195, 306
free 220
freopen 189, 292
frexp 305
fscanf 188, 297
fseek 301
fsetpos 301
fsize 214
fsize, n 213
fstat n 215
ftll 301
fwrite 299

, \ 43,
, ...

226

43, 226

hash 169
hash 169
Hoare, . . R. 102
HUGE_VAL 304

178, 200

-, n 54
-n, n

-, n
t

233,241
13,230

13,43,226

while, for, do

calloc 195, 306


canonrect 154
(a. label ) 69, 266
cat, n 187,189-190
6, 82
ceil 305
char, n 11 , 42, 227, 250
clearerr 301
clock 311
clock_t n 31 1
CLOCKS_PER_SEC 311
close n 205
closedir 216
const 47, 229, 250
continue, 77, 269
33, 38
305
cosh 305

creat n 202
CRLF 177, 291
ctime 312

150, 253

226
227

qsort 308
qrt 102, 129, 140

w
wchar_t n 226
while n for 15, 71
while, 11, 71 , 269
write n 200
writelines 127

1 .

.
,

. , ,

.
, ..
.
,

"

".
, .
.

ISBN 978-608-4535-48-5

9 786084 535485

You might also like