Вы находитесь на странице: 1из 86

-

.. , ..

-
2001

519. 682 (075)


32. 973 18.1
95
. ., . .
95 : . / .
., 2001. 86 .
. ,
, , . .
, , ,
,
,

:
()
( . . );

. . . -
. .

, 2001

.., .., 2001


C 70-
.
,
. C
, , ,
, , , , . ,
, , , , . , , ,
.
.
,

. , ,
, . ANSI
, Borland International Inc.,
. ,
, , - ++.

1.
1.1. , , ,
,
. C , , . C, ,
. COLOR, color,
Color .
. ( ),
( ).
. .
, . , .
, , .
( ), , .
C ,
, 32 .
C , .
,
. ANSI C
4

ayto
break
case
char
const
continue
default
do

double
else
enum
extern
float
for
goto
if

int
long
registr
return
short
signed
sizeof
static

struct
switch
typedef
union
unsigned
void
volatile
while

C .
, ,
, , .
, .
, , ,
. C , /* */.
1.2.
:
# include < stdio.h >
/* 1 */
main()
{
int year;
year = 2000;
printf( %d \n,year);
}
# include < stdio.h > stdio.h. , /
. C , .
stdio.h , printf () , .
/* 1 */ .
5

main() .
C . main(). C
.
, main(). C
( ). .
int year; , year,
, . C , . .
year = 2000; . year 2000. ,
C . C .
printf( %d \n,year); printf(), . : printf()
%d \n year, .
printf() . .
printf() %d \n, . , %. , . ,
%d , year, printf(). %d , . \n printf()
.
.
. main().
6

,
.
scanf(),

# include < stdio.h >
/* 2: */
main()
{
int radius;
float length;
printf( : \n);
scanf(%d,&radius);
length = 3.1415*2*radius;
printf(-%d\n,- %f\n,radius,length);
}

.
-, : radius
(int); length (float), .
-, scanf()
. scanf() %d
, . , .
& () radius
scanf().
.
2 radius
3.1415 float. C .
printf(), 4 . %f length float.

. , radius
float, scanf() %f.
7

1.3.
(source cod) .
(object cod) ,
. .
(linker) ,
.
C
.
(library) , , , .
(compiler time) , . , .
(run time) , .

2. , ,
2.1.
C . 5 , :
char ;
int ;
float ;
double ;
void , .
char int . char ,
ASCII. ,
, , .
. char , ,
char. int ,
float double.
double float .
void ANSI C++.
void.
, .
. , .
ANSI C :
9

signed ;
unsigned ;
long ;
short .
signed unsigned char
int. short long int. long double. signed
unsigned short long int.
. 2.1
.
2.1

char
unsigned char
signed char
int
unsigned int
signed int
short int
unsigned short int
signed short int
long int
signed long int
unsigned long int
float
double
long double

1
1
1
2
2
2
2
2
2
4
4
4
4
8
10

128 127
0 255
128 127
32768 32767
0 65535
32768 32767
32768 32767
0 65535
32768 32768
2147483648 2147483647
2147483648 2147483647
0 4294967295
3.4E38 3.4E+38
1.7E308 1.7E+308
3.4E4932 3.4E+4932

3.4E-38 3.4*1038,
.

, .
, 10

, , .
, +5 int
0000000000000101
, . 5
, .. 0 1, 1 0 1. 5

1111111111111011
2.2.
C , , . ,
.

int a,b,c;
float radius,length;
unsigned char ch1,ch2;
long double integral;
. , , .
C , . -, , main().

(
, ). -,
, .

.
. , . ,
11

, , .
#include <conio.h>
/* 3 */
#include <stdio.h>
/* */
char ch; /* */
main()
{
int n; /* main() */
printf( );
ch=getche(); /* */
printf( );
scanf(%d,&n) /* */
print_str(n); /* */
}
print_str(int m) /* print_str() m*/
{
int j; /* print_str*/
for (j=0; j<m; j++) /* j*/
printf(%c\n,ch); /* ch*/
}
:
;
,
(
);
, , .

12

2.3.
C , . .
char
int
unsigned int
long int
short int
float
double

a, \n, 8
1, 134, -580
52500
87000, -37, 7L
11, 13, -128
133.34, 3.36E-6, 5E+5
133.34, 133340, -2.789

.
int,
int.
int,
37750, unsigned.
unsigned,
long.
double,
.
. u,l,h,U,L,H. l,L,f,F.
13h, 35H
25L, -223l
87lu 88Lu 89ul
55uh unsigned
27.43f 7.7E-6F
1.41l 3.2E+12L

short int
long int
unsigned long
short
float
double

. 0x. .
.
0,1,2,3,4,5,6,7. 13

0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F. .
2.2 , .
2.2

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F

0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111

00
01
02
03
04
05
06
07
08
09
0A
0B
0C
0D
0E
0F

00
01
02
03
04
05
06
07
10
11
12
13
14
15
16
17


. , 0xAC1F
A
1010

C
1100

1
0001

F
1111

(strings) C
. () , . , 14

,
.
, \0.
.
b , , b , . b b ,
b \0 , b
2 , b .
C ,
. , ASCII 31 ,
.
. . 2.3:
2.3

\b
\f
\n
\r
\t
\v
\"
\'
\\
\0
\a
\N
\xN
\?

BS,
,
,



, .
15

2.4.
1 ,
0
255 128 127, ,
. . : d, +, 8.

# include < stdio.h >
/* 4 */
main()
{
char ch;
ch=c;
printf(%c,ch);
ch=+;
printf(%c%c,ch,ch);
}
prinf() %c. . scanf() .
/ getche(). , - ,
. ,
# include < conio.h >
# include < stdio.h >
/* 5 */
main()
{
char ch;
printf( );
ch=getche();
if ( ch == a) printf( a\n);
printf( %c,ch);
}
16

C ,
. C (
Pascal) ,
. .

char str[80];
char , str ,
80.
, str[12]. , . . str[0]
, str[2] , str[79] 80- .
, , , ,
,
. , english ,
:
, .

gets().
gets() , . gets() ,
Enter.
.
,
# include < stdio.h >
/* 6 */
main()
{
char str[80];
printf( );
gets(str);
printf( - %s,str);
}
17

%s.
/ .
2.5.
,
.
.
= ;
:
int a=230;
char c=c, ch=0;
,
. , . .
.
. ANSI .
Borland C , ,
( ).
2.6.
.
, , .
, ,
: , , . .
. :
18

- ;
+ ;
* ;
/ ;
% ;
++ ( );
- - ( ).
, ,
, .
. , . .
, , .
, . , /
, . % . .
++ . ++ , .
( ).
,

a=a+1; ++a; a++.


,
# include < stdio.h >
/* 7 */
main()
{
int a=10;
int b=70;
a++;
++b;
printf(a=%d b=%d\n,a,b);
printf(a=%d b=%d\n,a++,++b);
}
19

:
a=11, b=71;
a=11, b=72.
a printf(), b . a ,
printf(). :
a++ a ,
;
++a a ,
.
:
++,
- ( )
*, /, %
+, , , . ,
.
.
.
:
<
,
<=
,
>
,
>=
,
==
,
!=
.
:
&& (AND)
|| (OR)
! (NOT).
.
x<1, 200>=199, c==C
20

: ,
. . , ,
(true) (false). . .
, . .
#include < stdio.h >
/* 8 */
main()
{
int tr, fal;
tr=(99<100); /* */
fal=(100>101); /* */
printf(true - %d false - %d\n,tr,fal);
}
AND(&&), OR(||) NOT(!), . 2.4 .
2.4

X

X AND Y

X OR Y

NOT X

X XOR Y

0
1
X
0
1

0
0
Y
1
1

0
0
X AND Y
0
1

0
1
X OR Y
1
1

1
0
NOT X
1
0

0
1
X XOR Y
1
0

XOR .
XOR, AND, OR, NOT. ,
.

, .
:
21

!
>
==
&&
||

<
!=

>=

<=

, . ,
.
, , , .
. = , . , .
If ((c=a-b)<0) printf ( b)
a-b, c,
. , , : a=b=c=x+y.
x+y, c, b, a.
, .
, , lvalue. 3 = 3 , : lvalue.
+=, -=,
/=, *=, %=.
a=a+7 a+=7.
+= , , , ,
.
.
,
=, . . , .
, a+=7 ,
=+7.
22

( ). . float, double,
long double. .
()
&
AND,
|
OR,
^
XOR,
~
NOT,
<< ,
>> .
AND, OR, NOT XOR , . , .

, .
,
char, (). ,
0 1. . :
1) A
127
A&127

11000001
01111111
01000001

2) A
11000001
128
10000000
A| 128 11000001


, .
( 0 1) /
/. 8 .
ch ,
:
If ( ch & 8 ) printf(4 1);
23


8=00001000.
<< >> .
,
. :
value <<
value >> .
: x=9: 00001001,
x=9<<3 01001000
x=9>>3 00000001
x=9>>5 00000000

. << >>
-
.
: |=, &=, ^=, <<=, >>=.
( ) [ ].
.
.
? , .
( 1 )?(2):(3)
(1). , (2), . (1)
, (3)
. : (2) (3). , a b
max=(a>b)?a:b;
a
abs=(a>0)?a:-a;
lvalue, .
. ,
lvalue.
24

:
:
(a>b)?a:b=1;
. . ,
. (1),
(2) (1), (2).
.
sizeof.. sizeof : sizeof()
sizeof().
.
, . : sizeof( double ) sizeof( a ).
sizeof void.
,
, &. . -> .
2.7.
, . - .
. , , :
1. char short int int, float double.
2. , long double,
long double; double,
double; long,
long; unsigned, unsigned.
3.
, .
,
, :
( )
25

. ,
a int 2
float, ( float ) a/2. (float) (a/2)
, . ,
,
/2.
. .

26

3.
3.1.
:
if ( ) ;
else ;
, (
), . ,
else. if (. . else) . , ,
. . if , () ().
if
# include < stdio.h >
/* 9 */
main()
{
int sgn;
float x;
printf ( );
scanf (%f,&x);
if ( x>0 )
{ sgn=1; printf( %f sgn = %d\n,x,sgn); }
if ( x==0 )
{ sgn=0; printf( %f sgn = %d\n,x,sgn);}
if ( x<0 ) { sgn=-1; printf( %f sgn =%d\n,x,sgn);
}
}
27

if-else-if:
if ( 1 ) 1;
else if ( 2) 2;
else if( 3) 3;

else ;
if .
, , , . ,
:
# include < stdio.h >
/* 10 */
main()
{
int sgn;
float x;
printf( );
scanf(%f,&x);
if ( x>0 ) { sgn=1; printf( %f sgn =
%d\n,x,sgn); }
else if (x<0) {sgn=-1; printf( %f sgn =
%d\n,x,sgn); }
else { sgn=0; printf( %f sgn = %d\n,x,sgn);
}
}
, ,

if ( x==0 ) printf( );
else printf( );
,
if ( !x ) printf( );
else printf( );
if
if( x )
if( y ) 1;
else 2;
28

, if else.
else if . else if( y ).
else if( x), :
if( x ){
if( y ) 1;
}
else 2;
if( y ) .
3.2.
, switch.
/* 11 */
switch ( ) {
case constant1:

break;
switch ( ) {
case constant2:

break;

switch ( ) {
case constantN:

break;
default

}
switch.
( case constant1 . . ) ,
, . , . 29

switch, , default.
switch, default .
break.
break,
break switch
. break
switch . break , . break , switch
break switch. break :
# include < stdio.h >
/* 12 */
main()
{
char ch;
printf( );
ch=getchar();
if ( ch >= && ch <=)
switch (ch)
{
case :
printf(\n);
break;
case :
printf(\n);
break;
case :
printf(\n);
break;
case :
printf(\n);
break;
default:
printf(, \n);
break;
30

}
else printf( \n);
}
# include < stdio.h >
/* 13 */
main()
{
char ch;
printf( );
ch=getchar();
if ( ch >= && ch <=)
switch (ch)
{
case :
printf(\n);
case :
printf(\n);
case :
printf(\n);
case :
printf(\n);
default:
printf(, \n);
}
else printf( \n);
}
, .
:

, .
:

,
, , default.
31

3.3.
,
, , .
: for, while do-while.
for :
for ( ; ; ) ;
:
for ( 1; 1; 1 ) ;

. , , . . , , . ( ) . , .
, for .
for
for ( i=0; i<10; i++) printf(%d\n,i);
0 9.
:
for ( i=9; i>=0; i) printf(%d\n,i);
for
,
. char, :
unsigned char ch;
for ( ch=A; ch<=; ch++) printf(%c,ch);

for ( ch=0; ch!=N; ) scanf(%c,&ch);
, N. , , , . ,
32

, .
:
for ( ; ; ) printf( );
for ( i=1; 1; i++ ) printf( );
for ( j=12; j>8; j++ ) printf( );
, , . , , . 1 9:
# include < stdio.h >
/* 14 */
main()
{
int i,j;
for ( i=1; i<10; i++);
{
for ( j=1; j<10; j++) printf(%d * %d = %d,i,j,i*j);
printf(\n);
}
}

1 1 9,
2 1 9 .
, for . .
while.
:
While ( ) ;
, (
while). . , .
,
. for,
33

while , .
.
do-while . do-while :
do {

} while ( );
,
. . do-while .
,
( do-while ) ()
. for while
. do-while.
# include < stdio.h >
# include < stdlib.h >
# include < time.h >
/* 15 */
/* , 1 100 */
main()
{
int s,x;
int n=0;
randomize();
s=random(100)+1; /* */
do {
printf( 1 100:);
scanf(%d, &x);
n++;
if ( s<x ) printf( \n);
if ( s>x ) printf( \n);
} while ( s-x );
printf( \n);
printf( %d \n,n);
}
for, while do-while
( ) ,
34

. , while
do-while .
, x.
3.4. break continue
break . case
switch. ,
. break
( ),
, :
# include < stdio.h >
/* 16 */
main()
{
int i;
for ( i=0; i<1000; i++)
{
printf(%d - %d\n,i,i*i*i);
if ( i*i*i > 10000 ) break
}
}
,
i 10000.
continue. continue ,
. while do-while , for . ,

,
# include < stdio.h >
/* 17 */
main()
{
int i;
for ( i=7; i<1000; i++)
{
35

if ( i%7 != 0 ) continue;
printf(%d,i);
}
}
, 7 1000. i
7,
i.
3.5.
:
goto ;
, . , . , . ,
goto. goto ,
.

36

4.
4.1. ,
()
.
. ,
.
, . .
N :
< >[1][2][N]
:
< >[];
, . :
< >[1][2];

, . . [2], < >[1].
. . ( ).
.
.
, 0. int a[100]; , , 100
a[0] a[99]. ,
:
37

=< >*< > .


. . ,
, .
4.2. , .
char .

(string). , char.
char, . \0.
. ,
N , N+1 .
, int str[12], ,
11 , .
, . , . ,
Borland C++, .
\0. , Borland C++
:
B

\0

. scanf() %s.
, scanf() . gets(),
stdio.h. gets() ,
. Enter.
. .
38

printf() puts().
. puts() .
printf()
.
:
# include < stdio.h >
/* 18 */
/* */
main()
{
char str[80];
printf( 80 );
gets(str);
printf( %s\n,str);
printf( );
scanf(%s,str);
printf( );
puts(str);
}
,
string.h. .
strcpy() strcpy(s1,s2). s2 s1. s1 , s2.
, , , ,
.
strcat() strcat(s1,s2). s2 s1 ,
s1, s2 . ,
s1, s2.
strcpy(), strcat() .
:
39

# include < stdio.h >


# include < string.h >
/* 19 */
main()
{
char s1[20], s2[20];
strcpy(s1, Hello, );
strcpy(s2, World !);
puts(s1); puts(s2);
strcat(s1,s2);
puts(s1); puts(s2);
}
strcmp() strcmp(s1,s2). s1 s2 , ,
. . . . s1
s2, strcmp() , .
strlen() s, . strlen(hello) 5. ,
, :
# include < stdio.h >
# include < string.h >
/* 20 */
main()
{
char s[80];
printf( : );
gets(s);
printf( \n%s\n %d \n,s,strlen(s));
}

40

4.3.
,
. ,
.
int a[3][4] . 4.1.
4.1
int a[3][4]

a[0][0]
a[1][0]
a[2][0]

a[0][1]
a[1][1]
a[2][1]

a[0][2]
a[1][2]
a[2][2]

a[0][3]
a[1][3]
a[2][3]

, . ,

< >*< >*< >.
.
, ,
.

( ). :
# include < stdio.h >
# include < string.h >
/* 21 */
main()
{
char text[5][20];
strcpy(text[0],Turbo Basic);
strcpy(text[1],Turbo Pascal);
strcpy(text[2],Borland C++);
strcpy(text[3],Turbo Prolog);
strcpy(text[4],Turbo Fortran);
}
text[][], ,
strcpy , .
text[][] . 4.2.
41

4.2
text[][]

T
T
B
T
T

u
u
o
u
u

r
r
r
r
r

b
b
l
b
b

o
o
a
o
o

B a s i c \0
P a s c a l \0
n d
C + + \0
P r o l o g \0
F o r t r a n \0

4.4.

, . .
. , :
float ff[5]={1.4, 2.5, 3.6, 12.8, 0.9};
int z[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};
,
:
char str z[3][4]={ {1,2,3,4},{5,6,7,8},{9,10,11,12}};
,
. .
:
char str[12]={a,b,c,d};
:
char str[12]=abcd;
, .
:
char str[]=abcd;

, . :
int a[]={10, 20, 30, 40, 50};
42

, :
int a[][3]={1, 2, 3
5, 6, 7
8, 9, 10};
:
# include < stdio.h >
/* 22 */
main()
{
int arr[10]={9, 12, 43, 2, 4, 78, 15, 34, 11, 27};
int i,j,tmp;
printf( : );
for ( i=0; i<10; i++) printf(%d , arr[i]);
printf(\n);
for ( i=0; i<8; i++)
for ( j=i+1; j<9; j++)
if( arr[i] < arr[j] )
{
tmp=arr[i];
arr[i]=arr[j];
arr[j]=tmp;
}
printf( : );
for ( i=0; i<10; i++) printf(%d , arr[i]);
printf(\n);
}
4.5. , ,

.
,
.
43

.
,
. ,
.
:
*< >;

, . * , . :
char *ch;
int *temp, i, *j;
float *pf, f;
ch,temp,j,pf, i int f float.
& *.
, . . , . & () . *
() .
AND ,
, .
, , .
:
# include < stdio.h >
/* 23 */
main
{
float x=12.3, y;
float *p;
p=&x;
y=*p;
printf( x = %f y = %f,x,y);
44

*p++; /* 1 , p */
printf( x = %f y = %f,x,y);
y=1+*p*y; /* 1
p y */
printf( x = %f y = %f,x,y);
}
,
. :
# include < stdio.h >
/* 24 */
main
{
int x=12;
int p, g;
p=&x;
g=p;
printf(%p,p);
printf(%p,g);
printf(%d %d,x,*g);
}
%p
printf(), .
void,
. void
.
void. , :
void *pv;
float f, *pf;
pf=&f;
pv=pf;
pf=(float*)pv;
. (int p), ,
, float (float x),
45


p=&x . ,
int float : p=(int*)&x;
, , .
: .
. :
# include < stdio.h >
/* 25 */
main
{
int *p;
int x=12;
p=&x;
printf(%p %p,p,++p);
}
, ++p,
p 2, 1. ,
, ( 2 ). , ++p
, .
.
p 4000 . p=p+5;
4010. p=p+m; :
<p>=<p>+m*< >
. , . ,
p1 p2 ,
p1-p2 , .
, . .
, . .
46

. 6 . p<g , , p, , g. p g
, , p,
, g.
4.6.
,
,
, . ,
int plus[20]; , plus , pl=plus; pl=&plus[0];
. 8-
plus plus[7] *(pl+7), pl=plus;. .
,
, .
,
. . , :
# include < stdio.h >
# include < ctype.h >
/* 26 */
main()
{
char str[]=String From Letters in Different Registers;
int i;
printf( );
while ( str[i] )
printf(%c, toupper(str[i++]));
}
# include < stdio.h >
# include < ctype.h >
main()
{
char str[]=String From Letters in Different Registers;
47

char *p;
p=str;
printf( );
while ( str[i] )
printf(%c, tolower(*p++));
}
, ,
, , . ,
toupper() tolower() , .
,
toupper() (,
). tolower() . ctype.h.
4.7.
, , . 12
int *x[11]; . ;
, y: x[4]=&y;.
y, , *x[4].
:
# include < stdio.h >
# include < string.h >
# include < stlib.h >
# include < conio.h >
/* 27 */
main()
{
char *ext[]={exe,com,dat,c,pas,cpp};
char ch, sl[80];
for( ; ; )
{ do
48

{ printf( :\n);
printf(1. exe\n);
printf(2. com\n);
printf(3. dat\n);
printf(4. c\n);
printf(5. pas\n);
printf(6. cpp\n);
printf(7. Exit\n);
printf( : \n);
ch=getche();
printf(\n);
}
while ((ch<1)||(ch>7));
if ( ch == 7 ) break;
strcpy(sl,dir *.);
strcat(sl,ext[ch-49];
system(sl);
}
}
, system(). (dir ).
. 49 48 1. 48- . ,
pas , , 53.
53-49 ext ( ).
,
. , :
char *errors[]={Cannot open file,
Cannot close file,
Allocation error,
System error };
49


, , , , .
, char*( char). :
char *p;
p=Hello, World !;
, . :
int **point;
point int. ,
, point, **point. :
# include < stdio.h >
/* 28 */
main()
{
int i;
int *pi;
int **ppi;
i=12;
pi=&i;
ppi=&pi;
printf(i = %d pi = %p ppi = %p **ppi = %d\n,i,pi,ppi,**ppi);
}
, , ,
, . , , .
,
null, .
,
.
50

5.
5.1. . return
, ,

. :
< > ( )
{

}
, return. , , ( int ).
,
. ,
. . return . -,
. , . return, .
.
, a b :
/* 29 */
float step(float a, int b)
{
float i;
51

if ( a < 0 ) return (-1);


for ( i=1; i<=b; i++) a*=a;
return a;
}
1, , a b , .
:
/* 30 */
int max( int a, int b)
{
int m;
if ( a > b ) m=a;
else m=b;
return m;
}
, :
/* 31 */
int max( int a, int b)
{
if ( a > b ) return a;
else return b;
}
:
int max( int a, int b)
{
if ( a > b ) return a;
return b;
}
, return ,
, . ,
, . ,
, ,
52

, , .
, , , . ,
char.
:
/* 32 */
char* find( char* string )
{
int i=0;
while (( string[i] != ) && (string[i] != \0)) i++;
if ( string[i] == ) return &string[i];
else return NULL
}
,
void. , , , ,
:
/* 33 */
void gorisont_line( char ch )
{
int i;
for ( i=0; i<80; i++ ) printf(%c,ch);
}
, ,
void, int, . , , .
5.2.
ANSI ,

,
53

.
. :
< > ( );
.
, . ,
.
.
:
/* 34 */
int func( int a, float b, char* c);

int func( int , float , char* );
:
# include < stdio.h >
float sqr( float a );
main()
{
float b=7.8;
printf( %f %f,b,sqr(b));
}
float sqr(float a)
{
return a*a
}
,
void.

main(). void main(void)
main(void).
5.3.
, , .
. , .
54

, goto
.

: , . , .
, , . : . , . , . .
, . (
)
# include < stdio.h >
void ff( void );
/* 35 */
main ( void )
{
int i=1;
ff();
printf( main i %c\n,i);
}
void ff(void)
{
int i=12;
printf( ff i %c\n,i);
}
,
i, main(), .
, . , . , ,
. , .
55

, . ,
. .
:
;
;
- . , ,
.
5.4.
, , , . .
,
:
auto ;
extern ;
static ;
register .
, . ,
static int sum; register int plus.
,
auto. .
(auto) . , . , , .
( ) .
, 56

, ,
.
, , .
(registr) , , . . , , . : registr int q;
(extern) .
, :
void ff(void)
{

extern int x; /* */

}
extern ,
. . ,
.
, float global; ,

.
, ,
. extern float global; , , . , ,

. , . extern
,
. auto, ,
, .
,
57

. .
:
/* 36 */
int var; /* var */
main(void)
{
extern int var; /* */

}
func1()
{
extern int var1; /* var1 */
/* var */
}
func2() /* var */
{
/* var1 */

}
int var1; /* */
func3() /* var1 - */
{
int var; /* var */
/* */
}
func4() /* var */
{
auto int var1; /* var1- */

}
static. . . ,
58

.
. , . static int count=12; count . ,
, . .
# include < stdio.h >
void trystat(void);
/* 37 */
main(void)
{
int i;
for ( i=1; i<=3; i++)
{
printf( - %d\n,i);
trystat();
printf( - %d\n,i);
trystat();
}
}
void trystat(void)
{
int aut_l=1;
static int stat_l=1;
printf(aut_l = %d stat_l = %d\n,aut_l++,stat_l++);
}
, . .
static .
,
,
, , . , ,
,
59

. . 5.1 :
5.1

auto
registr
static
static

extern


, .
var:
# include < stdio.h >
void ff( void );
void fl( void );
/* 38 */
int var=5;
main( void )
{
int var=10;
printf( var = %d\n,var); /* var = 10 */
{
int var=100;
printf( var = %d\n,var); /* var = 100 */
}
printf( var = %d\n,++var); /* var = 11 */
ff();
printf( var = %d\n,++var); /* var = 12*/
fl();
printf( var = %d\n,++var); /* var = 13 */
fl();
60

printf( var = %d\n,++var); /* var = 14 */


}
void ff( void )
{
int var=55;
printf( var = %d\n,var); /* var = 55 */
}
void fl( void )
{
printf( var = %d\n,var); /* var = 5 */
}
5.5.
.

, , .
. .
, .
.
,
, . , swap(), , :
/* 39 */
void swap(int a, int b)
{
int tmp=a;
a=b;
b=tmp;
}
.
, , . . .
.
, .
61

,
.
swap() :
/* 40 */
void swap(int *a, int *b)
{
int tmp=*a;
*a = *b;
*b=tmp;
}
:
# include < stdio.h >
/* 41 */
void swap( int a, int b);
void swap1( int *a, int *b);
void main( void )
{
int x=10, y=20;
printf( x = %d y = %d\n,x,y);
swap(x,y);
printf( x = %d y = %d\n,x,y);
printf( \n);
swap1(&x,&y);
printf( x = %d y = %d\n,x,y);
printf( \n);
}
void swap(int a, int b)
{
int tmp=a;
a=b;
b=tmp;
}
void swap1(int *a, int *b)
{
int tmp=*a;
62

*a = *b;
*b=tmp;
}
: x=10
y=20, x=20 y=10. x y ,
. ,
swap() , swap(10,20). swap1() swap1(&10,&20)
.
,
.
:
func(int ar[10]);
func(int ar[]);
func(int *ar);
.
, :
# include < stdio.h >
/* 42 */
void sort(int arr[], int n);
void main( void )
{
int mass[10]={1,-6,21,3,-7,4,-12,9,5,17};
int i, size=10;
printf( : \n);
for ( i=0; i<10; i++)
printf(%d,mass[i]);
printf(\n);
sort(mass,size);
printf( : \n);
for ( i=0; i<10; i++)
printf(%d,mass[i]);
}
void sort(int arr[], int n);
{
63

int i,j,tmp;
for ( i=0; i<n-2; i++)
for ( j=i+1; j<n-1; j++)
if ( arr[j] < arr[i] )
{
tmp=arr[i];
arr[i]=arr[j];
arr[j]=tmp;
}
}
, :
void mult( int a[10][10], int b[10][10], int c[10][10]);
5.6.
,
.
, N
/* 43 */
Int factorial(int n)
{
int a;
if ( n==1) return 1;
a = factorial(n-1)*n;
return a
}

, .
,
. ,
, .
. ,
, .
64

5.7.
, . pfunc , type type1, type2,
:
type (*pfunc)(type1 t1,type2 t2);
. .
, , .
:
# include < stdio.h >
# include < math.h >
/* 44 */
double f(double x);
double f1(double x);
double f2(double x);
double ff(double (*pf)(double x), double x); /* */
void main(void)
{
double z=2.3, y;
double (*ptrf)(double x); /* */
ptrf=f; /* */
y=(*ptrf)(z); /* 1- */
printf(z=%f y=%f\n,z,y);
y=ptrf(z); /* 2- */
printf(z=%f y=%f\n,z,y);
ptrf=sin; /* */
y=ptrf(z);
printf(z=%f sin(z)=%f\n,z,y);
y=(*ptrf)(4.6) /* */
printf(z=%f y=%f\n,z,y);
y=ff(f1,z); /* ff f1*/
printf(z=%f y=%f\n,z,y);
65

y=ff(cos,z); /*
*/
printf(z=%f y=%f\n,z,y);
y=ff(ptrf,z); /* */
printf(z=%f y=%f\n,z,y);
}
double f(double x)
{
puts( In f());
return x;
}
double f1(double x)
{
puts( In f1());
return x*x;
}
double f2(double x)
{
puts( In f2());
return x*x*x;
}
double ff(double (*pf)(double x), double x)
/* */
{
puts( In ff() %f\n,pf(x));
return pf(x);
}
. jump table.

int f0(void);
int f1(void);
int f2(void);
int f3(void);
int(*jtable[](void)=(f0,f1,f2,f4);

val = (*jtable[i])(); val = jtable[i]();
66

6. ,
6.1.

:
(structure),
(union),
(enumeration),
(bit fields),
typedef ()
.
, .
, , ,
. :
/* 45 */
struct student{
char name[30];
int kurs;
char group[3];
int stip;
};
, . ,
, . student ,
, , struct student.
struct student,
struct student stud1, stud2;
stud1 stud2. .
67

.
/* 46 */
struct student{
char name[30];
int kurs;
char group[3];
int stip;
} stud1, stud2;
student stud1 stud2.
()
(dot). :
strcpy(stud1.name, .);
stud2 student , :
printf(%s,stud2.group);
, ,
. , ( student), :
Struct student stud1kurs[200];
200
student stud1kurs[0], stud1kurs[1] ..
kurs 35-
stud1kurs[34].kurs
,

stud1 = stud2;

. , ,
.
, .
68

, . , ,
.
:
func1(right.a); func2(&left.b);
, & , .
#include <stdio.h>
/* 47 */
struct stru{
int x;
char y;
};
void ff( struct stru param);
main(void)
{
struct stru arg;
arg.x=3;
arg.y=a;
ff(arg);
}
void ff(struct stru param)
{
printf(%d %c\n,param.x,param.y);
}

. :
struct stru *adr_pointer;
adr_pointer struct stru.
, . ,
. ,
69

. . ,

/* 48 */
struct complex{
float x;
float y;
} c1,c2;
struct complex *a;
a = &c1;
1.
1 :
(*a).x;
, ,
( , , ).
, ,
(*a).x . -> ( , arrow).
,
. (*a).x a-> x.
.
,
,
.

Struct addr{
Char city[34];
Char street[76];
Int house;
};
struct fulladdr{
struct addr addres;
int room;
char name[48];
} a,b;
70

addr ,
fulladdr fulladdr.
house addres

a.adress.house = 234;
6.2.
.
, . . , , .
,
, (bit-fields).
, , . :
struct _ {
1: ;
2: ;
3: ;
...
N: ;
};
:
int, unsigned, signed.
1 ,
( ). . ,
Struct onebit{
Unsigned one_bit: 1;
} obj;
obj 8 ,
.
.
71

6.3.
( union ).
,
/* 49 */
union u{
int i;
char ch;
long int l
};
.
union u alfa, beta;
. union
, , .
alfa 4 . , i
2 , ch 1 , l 4 . . , :
u.ch = N;
->, .
, , , :
#include < stdio.h >
#include < conio >
/* 50 */
struct byte{
int b1: 1;
int b2: 1;
int b3: 1;
int b4: 1;
int b5: 1;
72

int b6: 1;
int b7: 1;
int b8: 1;
};
union bits{
char ch;
struct byte b;
} u;
void decode ( union bits b );
main ( void )
{
do {
b.ch:=getche();
printf(;);
decode(u);
} while ( u.ch != q);
}
void decode ( union bits b )
{
if ( b.byte.b8 ) printf(1);
else printf(0);
if ( b.byte.b7 ) printf(1);
else printf(0);
if ( b.byte.b6 ) printf(1);
else printf(0);
if ( b.byte.b5 ) printf(1);
else printf(0);
if ( b.byte.b4 ) printf(1);
else printf(0);
if ( b.byte.b3 ) printf(1);
else printf(0);
if ( b.byte.b2 ) printf(1);
else printf(0);
if ( b.byte.b1 ) printf(1);
else printf(0);
printf(\n);
}
73

6.4.
( enumeration )
. . :
enum _ {_} ;
.
enum season { win, spr, sum, aut };
enum season s;
,
win, spr, sum, aut . ,
, , .
printf(%d %d,win,aut); 0 3.
,
enum value { one=1,two,three,ten=10,thousand=1000,next};

printf(%d %d %d %d %d\n,one,two,ten,thousand,next);
1 2 10 1000 1001, .. ,
.

:
enum ;
;
enum (i=win+aut).
++
enum.

.
74

6.5.

. typedef. . :
typedef char SYMBOL;
typedef unsigned UNSIGN;
typedef float real;
typedef :
/* 51 */
typedef struct st_tag{
char name[40];
int kurs;
char group[5];
int stip;
} STUDENT;

struct st_tag avar;

STUDENT avar;

75

7. /
7.1. -
-. ,
-,
. -
. -
, , (, , . .). -
, .
- #include
stdio.h, -, , , .
fopen.
FILE, , . NULL, stdio.h.
, . .
fopen ,
, , .
: r , w , a -
, r+ , w+
, a+ . fclose.
. ftell fgetpos, fseek fsetpos. stdio.h
: stdin (), stdout (), stdprn , stderr
76

. , ,
fopen. , - (getc, putc ..) , stdin stdout. , - ( getch, getche,
putch, cprintf ..). #include
conio.h.
7.2.
. 7.1.
7.1

fread
fgetchar
getchar

int


stdin

fgetc getc
getw

gets

fgets

scanf

sscanf

fputc putc
putw

puts

fputs

printf

fprintf

0
EOF
EOF
NULL
EOF


stdin

fputchar

putchar

sprintf

EOF
EOF
EOF
EOF

77

,
autoexec.bat ,
15 .
#include <stdio.h>
#include <conio.h>
/* 52 */
main(void)
{
FILE *f; int a; long int n;
if((f =fopen(c:\\autoexec.bat,r))==NULL) /* f */
{ prinf( \n); exit(1); }
while((a=fgetc(f)) != EOF /* */
fputc(a,stdout); /* */
n=-15;
fseek(f,n,SEEK_END); /* 15 */
while((a=fgetc(f)) != EOF) /* */
putchar(a); /* */
fclose(f); /* */
getch();
}
7.3. -
-,
, ,
.
7.4.
fopen , filename type.
FILE *fopen(char *filename, char *type);
fclose stream.
int fclose(FILE *stream);
fcloseall .
int fcloseall( void );
remove filename.
int remove( char *filename );
rename oldname newname.
78

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


ftell , stream.
. ,
ftell, fseek. ftell
. 1L.
long int ftell( FILE *stream);
fseek ,
stream, ,
offset ,
fromtwhere. fromtwhere 0,
1 2, ,
stdio.h, :
SEEK_SET(0) , SEEK_CUR(1)
, SEEK_END(2) (EOF); fseek
0, , .
int fseek(FILE *stream, long int offset, int fromwhere);
fgetpos ,
stream, , pos. fgetpos 0.
int fgetpos(FILE *stream, fpos_t *pos);
fpos_t - typedef long
fpos_t.
fsetpos - , stream , , fgetpos. fsetpos 0.
Int fsetpos( FILE *stream, const fpos_t *pos);
7.5. -
fgetc stream.
int fgetc(FILE *stream);
fgetchar stdin.
int fgetchar( void );
79

fgets s n
stream.
char *fgets(char *s, int n, FILE *stream);
fputc stream.
int fput(int c, FILE *stream);
fputchar- c stdout.
int fputchar(int c);
fputs string stream.
int fputs(char *string, FILE *stream);
gets s stdin.
char *gets(char *s);
getc stream .
int getc(FILE *stream);
getchar stdin.
int getchar( void );
putc c stream.
int putc(int c, FILE *stream);
putchar stdout.
int putchar(int c);
puts s stdout.
int puts(const char *s);
putw stream w.
int putw(int w, FILE *stream);
getw stream .
int getw(FILE *stream);
7.6. -
fread n size stream
ptr.
size_t fread(void *ptr, size_t size, size_t n, FILE *stream );
size_t
typedef usigned size_t.
fwrite n size ptr
stream.
Size_t fwrite( void *ptr, size_t size, size_t n, FILE *stream);
80

7.7. -
printf stdout.
int printf(const char *format [,argument,]);
scanf stdin.
int scanf(const char *format [,adress,]);
fprintf stream.
int fprintf(FILE *stream, const char *format [,argument,]);
fscanf stream.
int fscanf(FILE *stream, const char *format [,adress,]);
sprintf buffer.
int sprintf(char *buffer, const char *format [,argument,]);
sscanf buffer.
Int sscanf(const char *buffer, const char *format [,adress,]);
7.8. - (conio.h)
cprintf .
int cprintf(const char *format [,argument,]);
7.2

assert.h
ctype.h
errno.h
float.h
limits.h
locale.h
match.h
setjmp.h
signal.h
stdarg.h
stddef.h
stdio.h
stdlib.h
string.h
time.h

81

getch .
int getch(void);
getche c .
int getche( void );
putch .
int putch(int c);
, ,
. ANSI 15 (. 7.2).
, ,
graphics.h
conio.h.

82


1. . . IBM PC/ . . . . ; . . . .
.: , 1991. 429 .
2. . / . . . . ; .
. . . .: 1991. 384 .
3. . . : / .,
. , . ; . . . . .: , 1988.
512 .
4. . ., . . ++. .: , 1999. 288 .

83


.............................................................................................
1. ................................................................
1.1. , , , ..
1.2. ......................................................
1.3. ..............................................
2. , , ...........................
2.1. ...................
2.2. ................................
2.3. ....................................................................................
2.4. ..........................................
2.5. .....................................................
2.6. .....................................................................................
2.7. ..................................................................................
3. .............................................................................................
3.1. .....................................................................
3.2. ............................................
3.3. .......................................................................
3.4. break continue ........................................................
3.5. ...............................................
4. ...........................................................................
4.1. , .....................................
4.2. , . ..
4.3. ....................................................................
4.4. ...........................................................
4.5. , ,
...........................................................................
4.6. .....................................................
4.7. ....................................................................
5. ................................................................................................
5.1. . return ....................................
5.2. ...................................................................
5.3. ...................................
5.4. .............................................................................
5.5. .............................................
5.6. ................................................................
5.7. ................................................................
84

3
4
4
5
8
9
9
11
13
16
18
18
25
27
27
29
32
35
36
37
37
38
41
42
43
47
48
51
51
53
54
56
61
64
65

6. , ...................................
6.1. ....................................................................................
6.2. .......................................................
6.3. ...............................................................................
6.4. .....................................................................
6.5. ..............................................................
7. / .........................................................
7.1. - .........................................................
7.2. .............................
7.3. - ..........................................
7.4. ................................................
7.5. - .........................
7.6. - ..............................................
7.7. - .............................
7.8. - (conio.h) ................................
....................................................................

67
67
71
72
74
75
76
76
77
78
78
79
80
81
81
83

85

. .
. .
020341 07.05.97. 11.09.01. 04.10.01.
6084 1/16. . 3. . . . . 4,65. . .-. 4,77.
. -. . 5,0. 100 .
-
-

190000, -, . . , 67