-
" "
/P 2.1
.
1988
. -
, -
,
.
- ,
,
.
" ", "" ,
-
,
, ,
. , -
-
"" . -
,
,
, -.
.
UNIX,
, UNIX.
, , -
;
, -
,
,
.
- " ".
, ,
, , , .
, -
.
, -
, -
, , ,
. , , PL/1,
.
, -
,
. -
-. -
.
, , -
: , , -
, , -
, .
-
. ,
.
-1-
. -
.
, , -
, -
, ,
. -4, , -
32-
-
. , ,
-, ,
, ,
, ; -
.
- ,
, -
,
. -
, -
"" , .. -
,
.
-68.
, -
PL/1. -
, -
.. .
,
, lint.
lint ,
, -
.
, , -
-
, ..
,
/ .., -
, -
. , -
UNIX, , -
/,
, ,
.
, ,
(
-2-
" "),
/,
.
(
3 4), man(2) man(3). -
[1], -
[2].
, -
. -
( ).
-
"_",
( ).
. -
, ,
"",
{ }
, -
. 1.
, -
, :
_:
_
__
:
_:
_ __
,
,
IBM/370 (OS-360) 7 , 1
VAX 11 (UNIX) 7 , 2
-
:
-3-
int extern else
char register for
float typedef do
double static while
struct goto switch
union return case
long sizeof default
short break entry
unsigned continue
auto if
entry
- ; -
.
fortran asm.
,
.
, ,
, 0 (
), . 8 9
10 11 . -
, 0 (, -
) 0 ( X-),
.
a () A () f ()
F () 10 15. ,
-
, ;
,
, .
, -
, l (-)
L (-), . -
.
- , -
, , , ''.
-
.
-4-
, '
\ -
-
:
012 \n
011 \
010 \
015 \r
014 \f
0133 \\
047 \'
0ddd \ddd
\ddd
, 1, 2 3
, -
.
\0 ( ),
.
,
.
, -
, , e () E (-
) . ,
.
, ( ) ;
, e ( -
) . -
.
- ,
, , , "...". -
static (. ). -
. ,
, .
\0,
.
" -
\;
, -
, .
\, ,
-5-
.
, -
(. xstr).
,
. ,
,
.
1.
-----------------------------------------
| CM- IBM 370 (OS) VAX-11 |
| -8 ebcdic ASCII |
| char 8 8 8 |
| int 16 32 32 |
| short 16 16 16 |
| long 32 32 32 |
| float 32 32 32 |
| double 64 64 64 |
| range -38/+38 -76/+76 -76/+76 |
|_______________________________________|
:
.
, ;
, ,
.
: , -
, . -
.
, ,
.
-
,
. ( -
) ;
-
.
:
-6-
.
- , (char),
,
,
-
, -
. -
,
-. ( -
-0177 0177.)
.
- , -
short int, int long int.
, , -
,
, ,
. ""
,
;
.
.
- , unsigned, -
2**n, n -
. ( CM- -
).
.
- (float) -
(double)
. ( float 32
, double - 64).
,
"0" - "" "1" - ""
( , 0,
"").
, -
. char int -
. float double
.
-
, -
:
- ;
- , ;
-7-
- ;
- ,
;
- ,
.
,
.
;
l_ ( ) - ,
. l_
. ,
l_; , , e - -
, *e l_,
, . "l_"
e1=e2,
l_.
,
l_ l_.
. , -
.
, -
; -
.
,
. -
.
;
.
,
, - ,
-
.
CM- -128 127; ASCII
. , -
, -
-
; , '\377' -1.
-8-
-
char, ;
.
. , float
, double
. double
float, , ,
double float.
-
,
double (, -4). -
( cc).
-; , -
. ,
.
.
, .
-
; -
, -
.
; ,
.
, -
,
.
,
( 2** ).
-
.
long, -
.
-9-
,
.
-
. -
"
".
char short int,
float double. , -
double,
double, . ,
- long,
long, .
, -
unsigned, unsigned,
.
int, .
,
. , , -
, + (.0.4),
, ..0.1-0.3. -
.
. -
-
.
. , -
, -
, -
. , , -
. , -
(*,+,&,|,^), -
;
-
.
-
-.
( )
; 0
.
-10-
, ., ->,
, .
:
()
_ []
_ (_)
_l_ .
_ ->
_:
_,
,
, .
. , ,
..., -
, , -
,
.... ,
l_. -
, , -
.... ,
, -
, ....
.
int, long double.
.
; ,
, -
, -
. ( -
; . .)
-
,
. -
, l_ .
,
, . -
. -
..., int,
"...". e1[e2]
* ((e1) + (e2)). ,
-11-
, ; -
, * +
.. 0.1, 0.2 0.4 ;
.
,
, , -
. -
, ...,
"...". ,
, -
,
, , ;
, -
, .
float
double, char
short int, , , -
.
; ,
.
, -
.
; , -
.
,
.
, ,
,
.
; -
.
.
, -
, .
l_, , -
.
l_, -
.
, (-
- >) , -
.
,
. l_,
,
.
-12-
, e1->mos ,
(*e1).mos. -
.
,
(. "-
").
.
_:
*
& l_
-
!
~
++ l_
-- l_
l_ ++
l_ --
(-)
sizeof
sizeof (_)
* :
, l_,
, .
...,
"...".
&
, l_. l_
"...", ....
- () -
, .
unsigned 2**n
( n), n- int.
+ () .
!
1, 0, 0,
. int. -
-
.
~ ( "", ^
) ( -
) .
. .
-13-
, l_ -
++, .
, l_. ++ -
+= 1 . -
(. 0.4) (.
0.14).
--
++, l_.
++ l_
,
l_. , ,
,
++. ,
l_.
-- l_
,
l_. , ,
,
--. ,
l_.
,
,
.
(cast). .
sizeof .
( , , -
sizeof. -
,
char).
.
.
, . -
,
, - .
sizeof -
.
.
sizeof () ,
sizeof () - 2
(sizeof ()) - 2.
*, /, %
. .
-14-
_:
*
/
%
* . *
,
.
/ . -
, , -
.
, . , (a/b)*b+a%b
a ( b 0).
%
. -
. .
+ - .
. -
,
.
_:
+
-
+ .
.
,
. -
, , -
,
. , p
, p+1
.
-
.
+ , -
.
-15-
- .
.
, -
, , -
, .
, ( )
int, ,
.
, ,
, , -
, .
<< >> .
-
,
. int; -
. ,
,
.
_:
<<
>>
e1<<e2 e1 (
), e2 ; -
. e1>>e2
e1, e2 . e1
unsigned,
( );
( CM-) (
).
,
; a<b<c ,
, , , ((a<b)<c).
_:
<
>
<=
>=
< (), > (), <= ( ) >=
( ) 0, ,
1, . int.
-16-
.
; -
. -
, -
.
_:
==
!=
== () != ( )
, ,
. ( -
a<b==c<d 1 , a<b c<d
).
,
- ,
0. , , -
0,
0; -
.
_:
&
& , &
. -
;
'' .
.
__:
^
^ ( , -8 0136)
, ^ -
.
; -
'' . -
.
__:
|
-17-
| , |
. -
;
'' .
.
__:
&&
&& . 1,
, 0 -
. & &&
; , 0, -
.
,
, -
. int.
__:
||
|| . 1,
, 0 -
. | || -
; ,
, .
,
, -
. int.
_:
? :
.
, ,
;
.
, -
, ,
; ,
, ;
,
- 0, -
. -
.
-18-
,
.
l_, -
.
,
, .
.
_:
l_ =
l_ +=
l_ -=
l_ *=
l_ /=
l_ %=
l_ >>=
l_ <<=
l_ &=
l_ ^=
l_ |=
'=',
,
l_. ,
.
e1 = e2, - -
,
e1 = e1 (e2), , e1 -
. += -= -
, ()
,
. 0.4;
.
-
, -
. -
- .
-
,
. , -
0 ,
.
-19-
, -
.
.
__:
,
, , -
. -
.
. ,
, , ,
-
, , ,
; ,
f(a,(t=3,t+2),c)
, 5.
. ,
, ;
. , -
, *, "/" "%" -
, , "+" "-".
-20-
2
------------------------------------------
| | |
|____________________|___________________|
| () [] -> . | |
|____________________|___________________|
| ~ ++ -- - f | |
|(type) * & sizeof| |
|____________________|___________________|
| * / % | |
|____________________|___________________|
| + - | |
|____________________|___________________|
| << >> | |
|____________________|___________________|
| < <= > >= | |
|____________________|___________________|
| == != | |
|____________________|___________________|
| & | |
|____________________|___________________|
| ^ | |
|____________________|___________________|
| | | |
|____________________|___________________|
| && | |
|____________________|___________________|
| || | |
|____________________|___________________|
| ?: | |
|____________________|___________________|
| = += -= .. | |
|____________________|___________________|
| , | |
|____________________|___________________|
,
&, ^ | == !=.
, ,
if (( & mask) == 0) ...
, :
: x & mask == 0
: x & ( mask == 0 )
-21-
, -
; -
, .
:
_ _;
-
. -
.
_:
_ _
__ _
c_... - _...
, -
.
:
__:
auto
static
extern
register
typedef
typedef
" "
; .
(. " ").
auto, static register
, -
. extern
-
, .
register
auto ,
. -
. ,
-22-
; CM- int, char .
:
&. -
-
, -
-
.
3 -
register .
.
, , auto,
, extern
. :
.
.
_:
char
short
int
long
unsigned
float
double
___
_
__
long, short unsigned -
; :
short int
long int
unsigned int
long float
, double. -
. , -
, int.
.
0.5, - .0.6;
typedef . 0.9.
-23-
,
.
_:
_
_,-
_
_:
.0.6.
,
. :
:
( )
*
()
[-]
, .
,
, ,
,
.
; ,
.
-
, , -
.
,
. .
t di
t - ( int ..), di - -
. , ,
...t, "..."
, di (
-24-
int int). , di
*d
...
t.
di
d()
... , -
t.
di
d[_]
d[ ]
... t. -
, -
int. (
). " "
, ;
, ,
-
. , -
,
, . -
, -
. -
.
, ,
( -
).
,
, .
:
,
; , -
. , -
,
.
-25-
int i, *ip, f(), *fip(), (*pfi)();
i, ip , -
f, , fip, -
, pfi , -
. -
. *fip() *(fip()),
,
fip -
() . (*pfi)()
, , -
, ,
, ;
.
float fa[17], *afp[17];
float -
float. ,
static int 3d[3][5][7];
3*5*7. : 3d -
; ;
.
3d, 3d[i], 3d[i][j] 3d[i][j][k] -
.
"", int.
- ,
.
. - ,
.
.
___:
__ { -
__ }
___-
{ -- }
___
__:
struct
union
-26-
__ -
:
__:
_
_ -
__
_:
_ -
__
__:
_
_,_-
_
-
.
.
;
.
_:
: _
: _
, -
. ,
, ,
; -
. , ,
; . ,
, -
.
CM-, .
, ,
, ,
.
0
.
, " "
, ,
.
,
, -
- . ,
-27-
int . CM-
-
.
&,
.
,
0 -
, .
.
,
.. :
struct {__}
union {--}
(
) , .
,
struct
union
, -
; -
. ,
, -
-
, .
-
.
.
-
; ,
, -
. -
,
,
, .
:
-28-
struct tnode {
char tword[20];
int count;
struct tnode *left;
struct tnode *right;
};
20 ,
.
,
struct tnode s, *sp;
, s ,
sp .
sp->count
count , sp;
s.left
s,
s.right->tword[0]
tword
s.
. -
:
_:
enum _
enum _
enum
_:
_,
:
=
-29-
_ -
_; -
. ,
enum color {red, white, black, blue };
. . .
enum color *cp, col;
color ,
c
, col - .
_ -
, ( )
.
( =), 0 -
1
. = -
; -
.
-
.
, , -
lint .
CM_
, int.
-
.
, -
=.
:
=
= {_}
= {_,}
_:
_,_
{_}
_ - _
-30-
,
, -
, , -
, (,
) .
, -
.
,
0;
.
(
),
, . -
;
, .
(-
), -
-
.
. ,
.
, .
.
.
, -
; ,
, .
, -
; -
,
. ,
.
-
char .
.
,
int [] = {1,3,5};
; -
,
-31-
, ,
.
-
:
float *y[4][3] = {
( 1, 3, 5 ),
( 2, 4, 6 ),
( 3, 5, 7 ),
};
1, 3 5 y[0],
y[0][0], y[0][1] y[0][2]. -
y[1] y[2].
, , , y[3]
.
,
float y[4][3] = {
1, 3, 5, 2, 4, 6, 3, 5, 7
};
y ,
y[0] . 3
.
y[1] y[2].
float y[4][3] = {
{1}, {2}, {3}, {4}
};
y (
), .
,
char msg[] = "syntax error on line %s\n";
.
(
sizeof)
. -
" ",
,
.
-32-
:
_ _
_:
(_)
*
_ ()
_ [-
]
(_)
, _ .
_, ,
. -
-
. ,
int
int *
int *[3]
int (*)[3]
int *()
int (*)()
"", " ",
" ", "
", " , "
" , ".
, " "
typedef, .
, -
, ,
.
__:
typedef , ,
,
, .
0.4 . ,
typedef int miles, *klicksp;
typedef struct { double re, im;} complex;
-33-
miles distance;
extern klicksp metricp;
complex z, *zp;
; distance
int, metricp - " int", z -
zp -
.
typedef -
, ,
. -
distance
, ,
int.
,
.
-
,
;
.
, -
,
, (
""):
:
{_ _}
_:
_
_:
_
- _
-34-
, -
.
-
.
( )
; -
. -
, -
.
-
, .
:
if ()
if () else
,
, . ,
, .
, else
else if, else.
while
while ()
,
.
.
do
do while ()
,
.
.
for
(1;2;3)
-35-
for :
1;
while (2) {
3;
}
,
; , -
, ,
;
,
.
.
, while -
while(1);
.
switch (),
,
.
switch ()
-
, int. -
.
case, -
:
case :
int.
.
.
, -
default:
switch
-
.
, -
, .
-36-
default, ,
. -
default , -
.
case default -
, ,
.
break (.0.8).
, , -
.
,
.
:
switch (regim) {
case 'x': regx++;
case 'X': case 'Y': regY++; break;
case '-': regx = 0; break;
default: err(""); goto next;
}
break;
while, do, for switch; -
, -
.
continue;
while, do
for; . ,
while(...) { | do { | for(...) {
... | ... | ...
contin: ; | contin: ; | contin: ;
} | } while(...); | }
-37-
continue goto contin. (
contin: ; . . 0.13.).
-
return,
return;
return ;
. -
-
. , -
, , .
-
.
,
( ).
-
goto 1
(. 0.12),
.
, :
:
, -
goto.
, ,
.
:
;
,
} -
,
while.
-38-
-
. -
extern ( ), ,
, static, .
; ,
int.
, ,
,
. -
, ,
.
_:
_ -
_ _
,
-, extern
static; .
, -
..., ,
.
_:
(_)
:
, _
_
_:
_ _
. ,
, int.
register; ,
-
, , .
:
-39-
int max(a, b, c)
int a, b, c;
{
int m;
m = (a>b) ? a:b;
return((m>c) ? m:c);
}
int - -, ma(a,b,c) -
_, int a,b,c; - -
, { ... } - , .
float -
double, -
, float,
double. ,
( ) -
, -
... -
... , -
,
(
, , ).
, ,
,
.
:
_:
extern ( ,
) static, auto register.
- -
;
.
,
.
:
, ,
,
-40-
,
" "; -, -
,
, -
.
,
,
, .
,
, ,
. ,
, .
,
.
-
, -
;
, .
, ,
, ,
, -
.
, ,
, , , -
, -
, ,
. -
,
. ,
typedef, , -
.
,
:
typedef float distance;
...
{
auto int distance;
...
int -
,
distance.
-41-
,
extern, - ,
,
. ,
,
, ,
-
, ,
.
extern
, -
. , -
-
, extern, -
. ,
-
, extern. -
,
.
.
extern.
, , , -
.
-
.
,
static, .
static.
.
.
, ,
, int;
, , -
auto. -
, auto
( -
);
, ..., -
extern.
,
(,
-42-
, int.
/* extern */ int tab[100];
static /* int */ t1;
/* int */ func(i) /* int i; */
{ register /* int */ k;
/* auto */ char buf[512];
/* extern int f1(); */
... f1(a,b) ...
, -
,
. , #,
a.
;
(
) .
,
,
(, -77).
#define _
( )
, -
.
#define (-
,...,) _
"("
, -
. ,
"(", -
")",
. , -
, -
. -
, -
; ,
, -
.
. -
.
-43-
, -
. -
,
"\".
-
" ", , ,
#define TABSIZE 100
int table[TABSIZE];
:
#define max(a,b) ((a)>(b)?(a):(b))
x = max(y,20)
( a b , ,
-
.
#undef
-
.
#define, ,
cc.
#include "filename"
-
filename.
, "" , -
.
#include <filename>
.
/usr/include.
#include .
-44-
#if
, -
. :
#ifdef
,
, ..
#define. :
#ifndef
,
.
-
,
#else
:
#endif
,
#else #endif . ,
#else ,
#else, #endif .
.
:
#ifdef DEBUG
fprintf(stderr,"i=%o j=%d\n",i,j);
#endif
, .
, -,
:
#line "_"
-45-
( -
), ,
,
_. _ ,
. :
#line 250 "gram.y"
, -
.
-
:
( .), (
&), -
, ,
. -
.
CM-
:
, , -
, . -
, -
, ; -
, ;
.
"" ,
( . ->)
, -
.
, -
. .
l_ , l_
, -
. , ,
->, .
, ,
.
, .
.
-46-
:
.
, ,
. ,
,
int f();
...
g(f);
g :
g(funcp)
int (*funcp)();
{
...
(*funcp)();
...
}
, f
, g(f)
"(".
, , ,
, -
. -
l_.
"[]" , e1[e2]
*((e1)+(e2)). -
, +, e1 - ,
e2 - , e1[e2] e2- e1.
, ,
.
. e n-
i*j*...*k, e
(n-1)- j*...*k. -
* , , ,
,
(n-1)- , -
.
, , :
int u[3][5];
u - 3*5.
-47-
u -
5 . u[i],
*(u+i), u ,
; i u,
i , ,
5 . ,
( 5 ),
.
,
;
.
,
( )
-
, ,
, .
-
. -
.
.
-
, .
int long, (
int).
-,
,
.
.
. -
-
, -.
-
. -
,
,
. ,
,
.
, alloc
,
; -
.
-48-
extern char *alloc();
double *dp;
dp=(double*) alloc(sizeof(double));
*dp=22.0/7.0;
alloc (- -
), -
double; -
.
CM- 16-
. char
;
.
,
: -
case, -
.
,
sizeof,
+ - * / . % & | ^ << >> == 1= <> <= >=
- ~
?:
,
.
(
) ;
&
-
, .
& ,
.
,
, -
.
,
-49-
, ,
UNIX -
. , -
, , -
,
-. -
,
.
, ,
,
, . -
-
. , ,
( -
) , ,
,
.
_ .
register,
, ,
.
;
.
, -
. ,
, -
.
; CM- PDP-
11 VAXR-11 DEC
. ,
, .
-
int, -
, . , -
, ,
, -
_.
. -
, (-
, , int char
),
.
-50-
, ,
. -
,
CM- ,
, "unsigned char" -
,
.
-
. -
, , -
.
-
=, =, ,
=-1
, = - -
,
-1 .
:
, , -
,
int = 1;
int 1;
_
int f (1+2)
,
.
/
. " -
/", ,
/ - -
. ,
-
. , -
"
-51-
", .
, "-
" , -
, ,
, -
, -
,
.
/ , -
/.
( 4) -
("man(3)"). , ,
/
, ,
.
,
,
#include <stdio.h>
stdio.h ,
/.
" " ( -
) getchar.
getchar()
. ,
,
"<".
prog getchar,
prog <infile
, prog infile,
. ,
prog ;
"<infile"
(. ). -
, -
. ,
otherprog | prog
, otherprog prog, ,
-52-
prog other-
prog.
getchar EOF, -
, .
EOF
-1 ( #define stdio.h),
EOF, -1, -
.
putchar(c),
'c' " ",
. -
">".
prog putchar,
prog > outfile
outfile,
. -
.
/ "" getchar
putchar .
-
.
: printf scanf (-
)
.
.
printf(control, arg1, arg2, ...)
, control,
.
: , -
, ,
-
printf.
"%" (, -
). "%" -
:
- , -
.
-53-
- , . -
, ,
.
, ,
( , -
)
.
,
, (
).
- ,
.
- ();
, ,
float double.
- l, , -
long, int.
:
d - ;
o -
( );
x - -
( 0);
u -
;
c - ;
s - :
,
, -
;
e - , float
double,
[-]m.nnnnnne[+-], n -
.
6;
f - , float
double,
[-]mmm.nnnnn, n -
. 6.
-54-
,
f ;
g - %e %f, ; -
.
"ld" "D", "lo" - "O",
"lx" - "X".
% -
, ; , %
, %%.
.
, -
. -
"hello, world" (12 -
). ,
.
:%10s: :hello, world:
:%10-s: :hello, world:
:%20s: : hello, world:
:%-20s: :hello, world :
:%20.10s: : hello, wor:
:%-20.10s: :hello, wor :
:%.10s: :hello, wor:
: printf
.
,
.
scanf
printf
.
scanf(control, arg1, arg2, ...)
,
, control,
.
; ,
, ,
.
-
, -
.
-55-
:
- , ("
"), ;
- ( %), -
"
" ;
- , %,
*,
,
.
. -
, .
, , * -
,
.
, " -
";
, ,
. ,
, scanf , -
.
, -
("scanf(3)").
; ,
scanf . -
:
d - ;
.
o - (
);
.
x - ( -
0 ); -
.
h - short;
short.
c - ;
;
-56-
.
-
; , -
, -
%1s.
s - ;
,
,
\0.
f - ; -
float.
e - e f.
float -
, , -
, -
e, ,
.
d, o x
l, ,
long, int.
, l -
e f, ,
double,
float.
,
int 1;
float ;
char name[50];
scanf("%d %f %s", &i, &, name);
25 54.32e-1 thompson
i 25, - 5.432
name - "thompson",
\0. -
, ,
.
int i;
float ;
char name[50];
scanf("%2d %f %*d %2s", &i, &, name);
-57-
56789 0123 4572
i 56, - 789.0, 0123
name "45". -
a. -
name , ,
&.
-
, :
#include <stdio.h>
main() /* */
{
double sum, v;
sum =0;
while (scanf("%lf", &v) !=EOF)
printf("\t%.2fFI\n", sum += v);
}
scanf ,
,
-
.
.
. EOF; -
, 0, , -
. scanf
-
.
: scanf
. ,
scanf("%d", n);
scanf("%d", &n);
scanf printf sscanf
sprintf, ,
, . -
:
-58-
sprintf(string, control, arg1, arg2, ...)
sscanf(string, control, arg1, arg2, ...)
, sprintf
arg1, arg2 .. ,
control, string,
. , string ,
. , name - -
, n - ,
sprintf(name, "temp%d", n);
name "tempnnn", nnn - n.
sscanf -
string
control -
arg1, arg2 .. -
.
sscanf(name, "temp%d", &n);
n ,
temp name.
, -
.
,
, /
.
-
,
open . fopen
( .c "temp002")
,
.
, " ", -
,
, , -
,
.
, ,
stdio.h, .
: FILE *fp;
-59-
, fp FILE.
, file ,
int, ;
"#define".
fopen :
fp=fopen(name,mode);
fopen ,
"name".
mode () , -
, .
: (r), (w) (a),
+ (, r+), -
, , .
, ,
, (
).
. -
. -
(,
, ). -
NULL
( stdio.h).
, . -
, getc putc .
getc ; -
, , .
: c=getc(fp)
c , -
fp, EOF, .
putc: putc(c,fp)
c fp c.
getchar putchar, getc putc ,
.
, .
,
;
stdin, stdout stderr.
, stdin stdout -
.
getchar putchar -
getc, putc, stdin stdout :
#define getchar() getc(stdin)
#define putchar(c) putc(c, stdout)
-60-
fscanf fprintf. -
scanf printf, , -
, ,
; -
.
stdin stdout
-
; ,
FILE *. , , -
, , .
fclose fopen;
,
fopen,
. -
,
. fclose ,
, putc
(
fclose
).
,
, -
.
,
, stdin stdout,
, stderr.
, , stderr, -
,
(
,
).
exit
, -
. exit
, -
,
. , 0
,
, -
.
exit fclose -
,
, _exit.
_exit
-61-
- ; ,
.
fgets.
fgets(line, maxline, fp)
( ) -
fp line;
maxline-1 .
\0. fgets
line; NULL.
fputs
( )
:
fputs(line, fp)
gets puts
fgets fputs,
; gets -
, puts
:
gets(line)
puts(line)
,
.
ungetc(c,fp)
c fp.
.
-
,
.
calloc . -
-62-
calloc(n, sizeof(objct))
,
n , NULL,
. -
. malloc
, :
malloc(size)
,
,
char *calloc();
int *ip;
ip=(int*) calloc(n,sizeof(int));
free(p) ,
p, p
calloc.
, -
, calloc mal-
loc, .
,
, .
, 4 ( ), -
(man(3)). -
, -
:
- (string);
- sscanf sprintf" (atoi,
itoa, atof, ftoa);
- (sin, exp, ...);
- (ctype);
- .
-63-
, ,
-77, .
cc.
:
cc 1.c 2.c ...
1.c, 2.c, ... - , -
(
.c).
. -
, a.out,
, :
a.out
( ).
: -
, ,
(
cc). :
cc 1.c 2.c ...
cc N.c N1.c ...
cc 1.o 2.o ... N.o ...
,
, a.out,
cdeb, ..
(cc(1), ld(1)).
shell cshell :
_ __
, , .
main.
, -
.
-
. -
main , -
. ( int, argc)
,
-64-
; (argv)
,
, .
-
echo,
,
. ,
echo hello, world
:
hello, world
argv[0] ,
, argc 1.
argc 3, argv[0], argv[1]
argv[2] echo, hello, world.
argv[1], -
argv[argc-1]. argc 1,
. -
echo:
main(argc, argv)
int argc;
char *argv[];
{
int i;
for (i = 1; i < argc; i++)
printf("%s%c", argv[i],
(i<argc-1) ? ' ' : '\n');
}
argv ,
,
, -
. :
main(argc, argv)
int argc;
char **argv;
{
while (--argc > 0)
printf("%s%c",*++argv,
(argc > 1) ? ' ' : '\n');
}
,
, , -
.
-65-
( ). , TERM
, . -
getenv:
char *getenv();
par = getenv("_")
- -
, NULL, .
.
-
. :
/, , , . -
,
"", "", "".
-
-
( 2 3). , -
popen(3),
, 4,
, 3;
, man 3 popen.
/ -
,
. -
-, . -
/
, .
-
,
, -
, .
, -
.
-
; "" .
,
( ? -
?), ,
,
-66-
. ,
,
, (
read (5,...) write (6,...) ).
;
.
-
. (shell) -
, , -
, , -
0, 1 2 -
.
,
0 1 2,
,
.
, <
>:
prog < infile > outfile
0 1 .
2 ,
.
,
. ,
shell
( cshell), . ,
0 1 2 ,
,
.
/
- , -
;
. -
: read write. -
. -
,
. -
. :
n_read=read(fd,buf,n);
n_written=write(fd,buf,n);
-67-
, -
. -
,
.
, "-1" - .
-
; , -
,
.
, ,
.
"1", -
(.. ), "512",
-
.
,
.
. .
,
.
#define BUFSIZE 512
main() /*copy input to output*/
{
char buf[BUFSIZE];
int n;
while((n=read(0,buf,BUFSIZE))>0)
write(1,buf,n);
}
BUFSIZE,
read ,
write;
read .
, -
, ,
,
. : open
creat.
open fopen, -
, ,
,
int.
int fd;
fd=open(name,rwmode);
-68-
fopen, name
, . -
, , : rwmode : 0 -
, 1 - , 2 - .
- , open "-1";
.
, ,
. creat
. :
fd=creat(name,pmode);
, -
name, "-1" . -
, , :
creat .
, creat
,
pmode.
,
, ,
. -
,
. , 0755
,
-
.
( 15 - 25)
, -
. ,
, -
. close
-
. exit
.
unlink(filename)
filename (, filename,
, -
).
: read
write , -
.
-69-
.
lseek , -
.
lseek(fd,offset,origin);
fd
offset (), ,
origin ( ).
.
offset long; fd origin int.
origin 0, 1 2, -
, offset -
,
. , ,
:
lseek(fd,0l,2);
, :
lseek(fd,0l,0);
0l;
(long) 0.
lseek
, , -
.
. , , -
.
/* n pos buf */
get(fd,pos,buf,n)
int fd, n;
long pos;
char *buf;
{
lseek(fd,pos,0); /*get to pos */
return(read(fd,buf,n));
}
.
,
, .
-70-
- -
system:
system(" ")
- , -
, , -
shell .
-
(0, ).
/ >, <, ..
, , -
, system
, fflush.
, -
execl - -
, .
:
execl(,0,1,...,N,NULL);
"" - , -
. , pr
/bin/pr.
, 0
, -
.
execl -
,
execl (, -
).
, -
(execl(2), execv(2),
execvp(2), ...) ,
.
, ,
-
. "fork" (-
):
-71-
proc_id = fork()
, -
.
- "" - fork
0, - "" - ,
"".
fork execl -
:
if( fork() == 0)
{ /* - */
... ...
execl(... );
/* execl */
perror(" ");
exit(1);
}
... ...
fork , -
("" "") , -
, . -
"",
wait:
int status;
...
if( fork() == 0)
{ ... execl(...); exit(1);
}
wait(&status));
wait - "",
status .
- 8 -
;
exit"
0. 8 ,
exit,
exit; 0 ,
, .
fork, execl ,
fork
, -
/. -
,
/,
fork - execl.
, / -
fork(),
-
-72-
"", .
- ,
,
, ,
. -
shell
cshell, :
ls | pr
popen pclose,
, -
, , ,
,
(. popen(3)).
, -
.
pipe:
int fd[2];
...
stat = pipe(fd);
if(stat == -1) /* */...
fd - , -
- fd[1] , fd[0] .
.
, ,
,
; ,
. , ,
"0" - .
, pipe,
fork.
, -
. execl -
-
.
, -
, dup
. ,
pr ,
-73-
pr
:
int fd[2];
#define R 0
#define W 1
pipe(fd);
if(fork() == 0)
{ close(fd[W]); close(0); dup(fd[R]);
close(fd[R]);
execl("/bin/pr","pr",NULL);
exit(1); /* execl */
}
close(fd[R]); close(1); dup(fd[W]);
close(fd[W]);
.... , ,
.... .
close(1);
.
dup(fd)", fd,
. -
, 0
dup 0
dup . dup
fd[0] fd[1] .
"".
(,
,
"" - interupt), -
.
,
, signal .
,
, ,
"" "
".
signal.h:
#include <signal.h>
signal (,)
- ,
SIGINT, SIGKILL, ... ( . signal(2)). -
, -
, :
-74-
SIG_IGN - ,
SIG_DFL - .
signal
. ,
:
#include <signal.h>
main()
{
int onintr(); /* */
if(signal(SIGINT,SIG_IGN) != SIG_IGN)
{ signal(SIGINT, onintr); }
...
exit(0);
}
onintr()
{ unlink(tempfile);
exit(1);
}
(if(signal...) , SIGINT
, .
,
shell -
SIGINT, ,
. -
SIGINT onintr
shell .
. -
,
. ,
, -
, ,
read . -
,
, ,
read,
, -
.
-
, , ,
:
-75-
signal(SIGINT, onintr);
...
if(fork() == 0)
{ signal(SIGINT, SIG_DFL);
execl(...)
...
}
signal(SIGINT, SIG_IGN);
wait(&status);
signal(SIGINT, onintr);
,
,
.
.
:
:
_
*
&
-
!
~
++ l_
-- l_
l_ ++
l_ --
sizeof
( )
_
? :
l_ _
,
-76-
_:
^ ()
_ ( )
_ []
l_ .
->
l_:
_ []
l_ .
_ ->
*
(l_)
() [] . ->
.
* & - ! ~ ++ -- sizeof( )
, -
, , -
. . -
,
-
:
:
* / %
+ -
>> <<
< > <= >=
== !=
&
~
|
&&
||
?:
-
:
= += -= *= ?= %= >>= <<= &= ~= |=
-77-
.
:
_ _-
_;
_:
_ _
__ -
_
__:
auto
static
extern
register
typedef
_:
char
short
int
long
unsigned
float
double
___
__
_
__:
_
_,-
__
_
_
:
()
*
()
-78-
[ ]
___:
struct __
struct {_-
_}
struct
union {__}
union {_-
_}
union
__:
_
_ _-
_
:
_ _-
_
__
_
_,_-
_
_:
:
:_
:
=
= {_}
= {_}
:
_,_-
{_}
_:
_ -
_
-79-
_:
{_}
* _
_ ()
_ [-
_]
__:
_:
enum _
enum _
enum
_:
_,
:
=
_:
{_ _}
_:
_
_:
_
-80-
:
;
if ()
if () else
while ()
do while ();
for(1;2;3)
switch ()
case _ :
default:
break;
continue;
return;
return ;
goto ;
:
;
:
_
_
_:
_
_
_:
_ _-
_
_:
(_)
_:
, _
_:
__ _
_:
{ _}
-81-
:
extern _ -
;
static
;
#define _
#define (-
,...,) _
#undef
#include "_"
#include <_>
#if _
#ifdef
#ifndef
#else
#endif
#line "_"
1: fgets fputs (. "-
/. / ").
-82-
#include <stdio.h>
char *fgets(s,n,iop) /*<=n */
char *s; /* iop */
int n;
register FILE *iop;
{
register int c;
register char *cs;
cs = s;
while(--n>0&&(c=getc(iop)) !=EOF)
if ((*cs++ = c)=='\n')
break;
*cs = '\0';
return((c==EOF && cs==s) ? NULL : s);
}
fputs(s,iop) /* s */
register char *s; /* iop */
register FILE *iop;
{
register int c;
while (c = *s++)
putc(c,iop);
}
2.
,
.sh 1 ...
-83-
#include <stdio.h>
#define NEWH ".sh 1" /* */
/* :
cc -o ds ds.c
:
ds
:
ds a pref suff
a
pref00.suff, pref01.suff, ...
*/
main (ac,av)
char **av;
{
int nfile=0; /* */
char str[512]; /* */
if(ac != 4)
{
fprintf(stderr,
" 0);
exit(1);
}
/* freopen fopen,
,
.
stdin */
if(!freopen(av[1],"r",stdin))
{
fprintf(stderr,
" :%s0,av[1]);
exit(2);
}
/* . */
of(av[2],nfile,av[3]);
while( gets(str))
{
/* strncmp(s1,s2,l)
0, l
*/
if(strncmp(str,NEWH,strlen(NEWH))== 0)
{
fclose(fp);
nfile++;
/* */
fprintf(stderr,
" %d0,nfile);
fp = of(av[2],nfile,av[3]);
}
puts(str);
if(ferror(stdout)) {
-84-
fprintf(stderr,
" %.2d0,nfile);
exit(4);
}
}
exit (0);
}
/*
*/
of(s1,n,s2)
char *s1,*s2;
{
register FILE *f;
char buf[100];
/* sprintf */
if(( f = freopen(
sprintf(buf,"%s%02d.%s",s1,n,s2)
,"w",stdout))== NULL)
{
fprintf(stderr,
" :%s0,buf);
exit(4);
}
return;
}
-85-
'''' ................... 2
1. .......................................... 1
2. ............................ 3
2.1. .................................. 3
2.2. ....................................... 4
2.2.1. ............................... 4
2.2.2. (long) ...................... 4
2.2.3. .......................... 4
2.2.4. ........................ 5
2.3. .......................................... 5
2.4. ............... 6
3. ' .................................. 6
3.1. ................... 6
3.2. l_ ............................ 8
3.3. .................................. 8
3.3.1. ............................... 8
3.3.2. float double ........................... 9
3.3.3. ......... 9
3.3.4. ............................. 9
3.3.5. ............................... 9
3.3.6. ................. 10
4. ......................................... 10
4.1. ............................. 11
4.2. ................................ 13
4.3. ...................... 14
4.4. ............................. 15
4.5. ................................. 16
4.6. .............................. 16
4.7. .............................. 17
4.8. '' .......................... 17
4.9. '' ........... 17
4.10. '' ............ 17
4.11. '' ......................... 18
4.12. '' ...................... 18
4.13. ............................... 18
4.14. ........................... 19
4.15. .......................... 20
4.16. '' .............................. 20
4.17. . ............... 20
5. .......................................... 22
5.1. ..................... 22
5.2. .............................. 23
5.3. ....................................... 24
5.4. ................................ 24
-86-
5.5. ................. 26
5.6. ................................ 29
5.7. ................................... 30
5.8. ..................................... 32
5.9. typedef ............................... 33
6. ......................................... 34
6.1. ........................... 34
6.2. ( ) ................... 34
6.3. .............................. 35
6.4. while .................................. 35
6.5. do ..................................... 35
6.6. for .................................... 35
6.7. switch ................................. 36
6.8. break .................................. 37
6.9. continue ............................... 37
6.10. ............................... 38
6.11. goto ................................... 38
6.12. ............................. 38
6.13. ................................. 38
7. ............................... 39
7.1. ..................... 39
7.2. ...................... 40
8. .................. 40
8.1. .................... 41
8.2. ........ 42
8.3. ................................ 42
9. '' ........................... 43
9.1. ................................... 43
9.2. ................................ 44
9.3. ............................. 45
9.4. #line ................................... 45
10. ................. 46
10.1. ......................... 46
10.2. ......................................... 47
10.3. , ................. 47
10.4. ................. 48
11. ............................. 49
12. ....................... 49
12.1. ..................................... 51
13. ............. 51
13.1. .............. 52
13.2. ........................ 52
13.3. - printf ................ 53
13.4. - scanf .................. 55
13.5. ............... 58
-87-
13.6. ................................. 59
13.7. - stderr exit .............. 61
13.8. .............................. 62
13.9. ungetc .................................. 62
13.10. ...................... 62
13.10.1. ............................ 62
13.10.2. .................. 63
14. ............ 63
14.1. ............ 64
14.2. ..................... 64
15. ........................... 66
15.1. / ...................................... 66
15.1.1. ............................ 66
15.1.2. /. .................... 67
15.1.3. , , ....... 68
15.1.4. - lseek ................... 69
15.2. ........................... 70
15.2.1. system ................................ 71
15.2.2. - execl ...... 71
15.2.3. - fork ............. 71
15.2.4. ..................... 73
15.3. ............................ 74
16. ...................... 76
16.1. ....................................... 76
16.2. ........................................ 78
16.3. ....................................... 80
16.4. ............................. 81
16.5. .................................... 82
17. ............................ 82
-88-
Last-modified: Mon, 29 Jun 1998 14:37:17 GMT