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

 004.43 .

,
, , , - ,
 32.973-018.2 . ,
15
, .

Mike McGrath
C PROGRAMMING IN EASY STEPS, 4th edition
By Mike McGrath. Copyright 2015 by In Easy Steps Limited. Translated and reprinted under a licence agreement from the Publisher:
In Easy Steps, 16 Hamolton Terrace, Holly Walk, Leamington Spa, Warwickshire, U.K. CV32 4LY.

, .
15 
      C       /      ;
[.  
. .    ].   :  , 2016. 192 .
(
          ).
! "  
   #    
  $ ,   %&   -
%'# &  * &   ,  %*    & & %& C. ! "  
  -
   +  ,   '   # * &      %& C  *  #   ,
'      *' <   < * #!

 #    
  ,      * '  '

%& , ' , %'  %& C,  +  ,   #        >-
  #' # #    ' #    $'

    .


004.43
32.973-018.2


C
( )
. . .
.
: antishock, VikaSuh / Shutterstock.com
Shutterstock.com

: http://eksmo.ru/certication/
9 : . ;<
-

02.10.2015. 84x1081/16.
. . . . 20,16. -

 ..,    , 2016


ISBN 978-5-699-79117-0 .  ! ", 2016

1 9

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

2 21
21

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

3 41
41

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52

4 53
53

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72

5 7
733

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86

6 87
87

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98

7 9
999

. . . . . . . . . . . . . . . . . . . . . . . . 100
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110

8 111
111

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
9 12
1255

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
. . . . . . . . . . . . . . . . . . . . . . . . . . 128
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140

10 141
141

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162

163
163

ASCII- . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187

18
1888

, (Mike McGrath),
C . , , -
C,
Windows Linux, Microsoft Visual Studio. -
, .

,
.

, , ,
, .

main.c header.h


, , ZIP- -
, Windows Linux, IDE Microsoft
Visual Studio. , www.eksmo.ru. -
.

, C
, .

C
C.
C
,
C
C
C
,

.

1.

-
, (Dennis
MacAlistair Ritchie) Unix
Digital Eauipment Corporartion PDP-11 1972- .

C ()
, 1970- .

Unix
C. , -
-
, , Windows.

C ,

(American National Standards Institute, ANSI).
ANSI , -
. ,
.
C.
ANSI C C. -
.
10


C?
C ,
Java, C++ C#.
C, , , . C,
, , -
, .

-

C, -
, . , C++ C.
C , , -
C.
, -
. , -
, C , . -
, -
.
C, -
-
, .

ANSI C , -
,
,
C. ,

, C.
.h.
. C
.


stdio.h , -
.
, -
C,
C
ctype.h
string.h
math.h

11
stdlib.h -
, . .
assert.h , -

stdarg.h , -


setjmp.h , -
, -

signal.h , -
,

time.h -
,
limits.h
C
float.h ,


1.


C
, .. -
, (Notepad) -
Windows
.

, C, -
-, -
. C
GNU - ,
, .
Gnu's Not Unix (Gnu
,
Unix)
, .
--.
C GNU C
, Compiler (GCC) General Public
www.gnu.org. License (GPL). -
Linux. GNU C Compiler -
12

,
GNU C Compiler, gcc -v.
- , .

-
(
).

Linux
GNU C Compiler ,
-,
.

Windows, -
Minimalist GNU for Windows
(MinGW), GNU C Compiler.
1. - MinGW:
sourceforge.net/projects/mingw.
2. MinGW Download,
MinGW (Automated Min-
GW Installer) , mingw-get-inst-setup.exe. -

3. . , -
4. C:\MinGW, . -
Continue (), . www.mingw.org,
-
, .
GNU C Compiler C:\MinGW\bin.
, -
. Support ()
Continue (), MinGW
. sourceforge.net.
All Packages ( ),
mingw32-gcc ( The GNU C Compiler). -
Installation Apply Changes ( -
) Apply (-
). .

13
5. Environment Variables (
):
Control panel ( ) -
Windows;
System ();
Advanced system settings (-
);
Environment Variables ( ).
6. Path,
;C:\MinGW\bin.
7. GNU C Compiler,
gcc -v, Enter,
C++ -
.
C, -

C++

,
C.

1.


C , -
, , -
:

- - () {---}

, -

, , -
- . ,
- .

, ,
, main. -
- main() , -
, C, ,
. main().

,
, , ,
14

.
C, -
.

(), ,
, .
- .

#, - {} , -
. -
<>. .

-
, Hello World.
1. , (Note-
pad),
, :
#include <stdio.h>
hello.c
C,

stdio.h.
, .
-
, -
, , .
2. -
main:
int main()
{

}
,
int.
C -
3. , ,
, -,
: -
printf ( "Hello World!\n" ) ; .

printf() -
. C
. Hello
World \n, -
.

15
4. ,
0,
return 0;
0 -
,
.
5. , ,
, , -
( Enter
) hello.c.
.
#include <stdio.h>

int main()
{
printf("Hello World!\n");
return 0;
}

.

1.


, C,
, . -

MyPrograms,
gcc -help, -
Windows C:\MyPrograms, Linux /home/
Enter,
MyPrograms. hello.c, -

, -
.
, ,
-.
1. cd
MyPrograms, .
2. , MyPrograms, -
gcc hello.c, Enter, -
.
, , -
.
a.out Linux a.exe
Windows. -
16

, C, MyPrograms -
.
, -
hello.c . -
, .
3. , MyPrograms, gcc
hello.c o hello.exe, Enter, -
.
( Windows, Linux)
hello.exe -
:
4. Windows

, Enter, -
,
. ,


,

, -

.

Linux -
,

17
,
./.
5. Linux ./hello.

exe, Enter,
, - -
. Windows
-

.

hello.

, Hello
World, . -
,
.

1.


(.c)
C -
, .
-
C -
, . ,
#include. ,
(.i)
, , , -
.i.
() -
.i -
. , -
, ,
(.s)
, .s.
-
.s . -
, , -
18

.o.

(.o)
.o .
, ,
.exe.

, -
, -
.
, , -
,
(.exe) .

, ,
-
. -
, -
. ,
,
.
, -
, ,
-save-temps, -
.
1. MyPrograms gcc hello.c -save-temps
-o hello.exe, Enter, -
.

2. hello.i ,
(Notepad).
, stdio.h.

19
3. hello.s ,

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

. -

C .

1.

-
C.
, C++ C#, -
C.
C -
, ,
.
C ,
.h.
C ,
..
GNU C Complier (GCC) -
Minimalist GNU for Windows (MinGW).
, -
, .
,
,
20

.
, C, -
main().
,
.
, ,
{},
.
-
, , .
GNU C Compiler gcc,
-o,
.
, ,

-save-temps.
2

,
,






C.






2.


, C, -
.
C
-

. ,
VAR, Var var
, .

.

Volatile
C
a+b*c

- %$#@!

no spaces
2bad
good1
22


UPdown

is_ok

, -

. -
C , . -
. :

- -;

, -
. ,
. ,
, -
, . C,
. -
, , -
:

- -1, -2, -3;


C . -
C -
.


char , 'A'


int 100
float 0.123456


double 0.123456789


. char,
. char
double , . -
. -

23
, float, double
, -
. .

,
, .
, .
.

, , -
, ,
,
/* */, :
= -
int num1, num2; /* */ L-, -
char letter; /* */ R-.

float decimal = 7.5; /* */


num1 = 100; /* */
num2 = 200;
letter = 'A'; /* */

2.



printf(), 1 -
Hello World. -
printf(), -
,
.


%d -32768 100
+32767
%ld 123456789
231 +231
%f 0.123456
%c 'A'
%s "Hello World"
%p - 0x0022FF34
24

,
,
- %. , ,
- ,
, %7d. ,
. 0. ,
, ,
,
%07d.

, -
, %f -
. , -
, %.2f.
-
,
,
. , ,
, , -
%07.2f.
, -
, .
. -
.
1. ,
/.
#include <stdio.h>
2. main, -
vars.c
:
int main()
{
int num = 100;
double pi = 3.1415926536;
}
3. main ,
.
%
printf("Integer is %d \n", num);
printf(),
printf("Values are %d and %f \n", num, pi);
printf("%%7d displays %7d \n", num); %, -

25
printf("%%07d displays %07d \n", num); .
printf("Pi is approximately %1.10f \n", pi);
printf("Right-aligned %20.3f rounded pi \n", pi);
printf("Left-aligned %-20.3f rounded pi \n", pi);
4. main ,
0, .
return 0;
5.
, ,
. ,
, -
,
, -

, -
.

2.


- stdio.h
scanf(),
. scanf() ,
, ,
.

scanf() -
, 22, -
. , "%d",
. - . scanf() -
- , &, .
36 , - & -
. C, ,
, ,
.

-
, .
. -
26

, .

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

scanf() -
. -
, , ,
scanf() - .
, -
, &.
. .

& ,
, -
.

printf() scanf()
.
1. , -
/.
#include <stdio.h>
2. main, .
setvars.c
int main()
{
char letter;
int num1, num2;
}
3. main
, .
printf("Enter any one keyboard character");
scanf("%c", letter);
printf("Enter two integers separated by a space:");
scanf("%d, %d", &num1, &num2);
4. ,
.

27
printf("Numbers input: %d and %d \n", num1, num2);
printf("Letter input: %c", letter);
printf("Stored at: %p \n", &letter);
5. main 0, -
.
return 0;
6. ,
, , -

.
-
: %p.

2.


int
. . -
-
.

int
( ), , ,

2.147.483.648 +2.147.483.647.
,
, int
. ( ), , ,
32.768 +32.767.

-
short long , , :

short int num1; /* */

long int num2; /* */


28

limits.h
. -
. int, , INT_MAX
INT_MIN. short int
SHRT_MAX SHRT_MIN, long int LONG_MAX LONG_MIN.

int -
- unsigned ,
,
.
int
unsigned short int, , 0 65.535,
,
,
-
short int. unsigned long int, ,
,
0 4.294.967.295, -

, long int.
, -
long int. sizeof C ,
, -
.
. , -
65.535,
unsigned short int, 2 ,
long int, 4 .
1. , -
/ -
.
#include <stdio.h>
sizeof.c
#include <limits.h>
2. main, , -
short int.
int main()
{
printf("short int \tsize: %d bytes %d \t", sizeof(short int));
printf(" %d %d", SHRT_MAX, SHRT_MIN);
}
3. , -
long int.
printf("long int \tsize: %d bytes %d \t", sizeof(long int));
printf(" %d %d", LONG_MAX, LONG_MIN);
4. ,

29
.
printf("char \tsize: %d bytes %d \n", sizeof(char));
printf("float \tsize: %d bytes %d \n", sizeof(float));
printf("double \tsize: %d bytes %d \n", sizeof(double));
5. , 0,
.
return 0;
6. ,

, .
,
-
-

\t.

2.


, ,
. , , -
, ,
.


, .

,

auto .

, -
.
, , .
, , -
. -
.

.
, .
extern, , -
30

, .
.
C

,
- .
. , ,
. . ,
- ,
, , -
static.
, -

, ,
static -
. . -
, , ,
-
. static
.

static. -
, ,
.
,
.
1. , -
/.
#include <stdio.h>
2. -
, global_1.c
.
static int sum = 100;
3. main, -
, -
. static, -

int main()
num,
{ -
extern int sum; ,
printf("Sum is %d \n", sum); -
.
}
4. -
.

31
extern int num;
printf("Num is %d \n", num);
5. , 0,
, .
return 0;
6. , -
.
int num = 200;
7.
, , - global_2.c
, -
.

2.


,
register, , -
. ,
, , -
, .
, -
.

register
. , -
,
.
.

,
register , -
, .
,
register .
32

register -
volatile. ,
, -
, .
, -
.

register
,
register .
volatile , .
- .

, ,
,
- , volatile.

5, , ,
.
, -
,
register.
1. , -
/.
#include <stdio.h>
2. main,
register.c
register. -
, 0.
int main()
{
register int num = 0;
}
3. , ,
5, .
while (num < 5)
{ }
4. , -

.
++num;

33
printf("Pass %d \n", num);
5. main 0, -
, .
return 0;
6. , ,
, -
, . ,

register,
&,
-
-
, .

2.


,
. , float
,
float.

, ,
, , , -
ASCII ( .
) ,
American Standard Code for . -
Information Interexchange , unsigned, . -
(American Standard Code for :
Information Interexchange),
-2 = ( -) -1;
.


, ,
ASCII-
.
.
34

float int, -
-
. , float num
= 5.75, 5, 6.

char int, -
ASCII-, . -
65-90,
97-122. ,
char letter = 'A', 65. -
char
.


, /, - float, -
-
C. . , int x=7, y=5, -
float z = x / y, 1.000000.
-
(float) x / (float) y, 1.400000.

double
float -
, . ,
double decimal = 0.1234569 0.123457.
1. , -
/.
cast.c
#include <stdio.h>
2. main, -
, .
int main()
{
float num = 5.75;
char letter = 'A';
int zee = 90;
int x = 7, y = 5;
double decimal = 0.1234569;
}
3. , -
.

35
printf( "Float cast to int: %d \n" , (int)num ) ;
printf( "Char cast to int: %d \n" , (int)letter ) ;
printf( "Int cast to char: %c \n" , (char)zee ) ;
printf( "Float arithmetic: %f \n" , (float)x / (float)y ) ;
printf( "Double cast to float: %f \n" , (float)decimal ) ;
4. main 0, -
, .
return 0;
5. , float, -
, - int, -
. , double,
float
.

2.


-
,
.

, -
.
- 0, 1, . .
, .
C -
, (-
).
, :

- - [-]

,

, . ,

36

: int arr[3] = {1, 2, 3};

,
, -
. , arr[0] .

,

,
.

-
C , -
. char
. - \0 -
. , -
: char str[4] = {'C', 'a',
't', '\0'} ;.

, , -
-

. printf()

%s. , str
\0.
: printf("%s", str);.
1. , -
/.
#include <stdio.h>
2. main, -
array.c
.
int main()
{
int arr[3];
}
3. , -
,
.
arr[0] = 100;
arr[1] = 200;
arr[2] = 300;
4. ,
, .
char str[10] = {'C', ' ', 'P', 'r', 'o', 'g', 'r', 'a', 'm', '\0'};

37
5. ,
.
printf( "1st element value: %d \n" , arr[0] ) ;
printf( "2nd element value: %d \n" , arr[1] ) ;
printf( "3rd element value: %d \n" , arr[2] ) ;
printf( "String: %s \n" , str ) ;
6. main 0, -
, .

return 0;
7. ,
, , -
. char.

.

2.


,
, , -
.
, .

A B C D E
[0] [1] [2] [3] [4]

, -
. , , -
, :

[0] A B C D E
[1] F G H I J
[0] [1] [2] [3] [4]
38

, ,
, . ,
, , [1][2] -
.

,
X Y. ,
, , :
-
, -

.

X
Z


,
, ,
.
1. , -
/.
#include <stdio.h>
2. main,
matrix.c
.
int main()
{
int matrix[2][3] = {{'A', 'B', 'C'}, {1, 2, 3}};
}
3. , -
.
printf( "Element [0][0] contains %c \n" , matrix[0][0] ) ;
printf( "Element [0][1] contains %c \n" , matrix[0][1] ) ;
printf( "Element [0][2] contains %c \n" , matrix[0][2] ) ;
4. , -

39
.
printf( "Element [1][0] contains %d \n" , matrix[1][0] ) ;
printf( "Element [1][1] contains %d \n" , matrix[1][1] ) ;
printf( "Element [1][2] contains %d \n" , matrix[1][2] ) ;
5. main 0, -
, .
return 0;
6. ,
, ,
. ,

ASCII-

,
%c

.

2.

, C,
-
.
C
.
C char, int, float double.
%d, %f, %c, %s %p
printf(), .
, ,
scanf().

short long.
, -
, unsigned,
.
, ,
sizeof.
40


.
extern ,
, , static -
, .
-
register,
.
, , -
volatile.
, ,
-
.

, .
,
.
, -
.
3

,




,

C.



3.


, -
, ,
, ,
const. , , -
, -
:

const int MILLION = 1000000;

.
.
,
.

-
, 20, -
, -
.
42

const
,
.
1. , -
/.
#include <stdio.h>
2. main, -
constant.c
, .
int main()
{
const float PI = 3.141593;
}
3. main ,
.
float diameter;
float radius;
float circ;
float area;
4. ,
.
printf("Enter the diameter of a circle in millimeters:");
scanf("%f", &diameter);
5. ,

,
. *

circ = PI * diameter;
,
radius = diameter / 2; / .

area = PI * (radius * radius);
-
6. , .
.
printf( "\n\tCircumference is %.2f mm" , circ ) ;
printf( "\n\tAnd the area is %.2f sq.mm\n" , area ) ;
7. main 0, -
.
return 0;

43
8. , ,

,
, -
&

scanf(),
-
.

3.


enum -
.
, enum.
, .

, -
1. ,
0, 1, . . , -
, -
enum {MON,TUE,WED,THU,FRI}. WED
2.


, , -
1, -
. , , -
0, 1, 1 enum
{MON=1,TUE,WED,THU,FRI}. WED -
3.
44


. , 0 -
NIL NONE.

, -
.
, ,
.
1. , -
/.
#include <stdio.h>
2. main, -
enum.c
, .
int main()
{
enum SNOOKER
{ RED=1,YELLOW, GREEN, BROWN, BLUE, PINK, BLACK};
}
3. main , -
.
int total;
4. ,
.
printf( "\nI potted a red worth %d\n" , RED ) ;
printf( "Then a black worth %d\n" , BLACK ) ;
printf( "Followed by another red worth %d\n" , RED ) ;
printf( "And finally a blue worth %d\n" , BLUE ) ;
5. , -
,
. 1
BLUE -
total = RED + BLACK + RED + BLUE;
6. , -
. PINK
2,
printf( "\nAltogether I scored %d\n" , total ) ;
BLACK 3.
7. main 0, -
.

45
return 0;
8. , -
,
. (
SNOOKER)
,
.

3.


, , -
,
.

enum ,

:

- - ;

, , -
SNOOKER,
enum SNOOKER. pair
enum SNOOKER pair;
, .

-
, C int
enum, , :
46

pair = (enum SNOOKER) 7;

, -
, int.

enum ,
. , enum
BOOLEAN {FALSE, TRUE} flag; enum -
flag .


typedef :
,
typedef - ;

-
.
. , -
unsigned short int, -
, , :

typedef unsigned short int USINT;

unsigned short int USINT -


USINT unsigned short int.
1. , -
/.
#include <stdio.h>
2. main,
consttype.c
, .
int main()
{
enum SNOOKER
{ RED=1,YELLOW, GREEN, BROWN, BLUE, PINK, BLACK};
}
3. main -
enum, .
enum SNOOKER pair = RED + BLACK;
printf( Pair value: %d \n , pair ) ;
4. , -
.
typedef unsigned short int USINT;

47
5. -
.
USINT num = 16;
printf( "Unsigned short int value: %d \n" , num ) ;
6. main 0, -
.
-
return 0; -
7. , ,
, , -
, , - .
.

3.

#define -
,
, :

#define - "-"

#include, -
.
-
.

#ifdef -
, .
#define,
. -
. -
#endif.

,
,
. -
48

, Windows -
_WIN32, Linux linux.
#ifdef -
.
1. , -
/.
#include <stdio.h>

define.c 2. ,
.
#define LINE "_____________________"
#define TITLE "C Programming in easy steps"
#define AUTHOR "Mike McGrath"
3. ,
, Windows.
#ifdef _WIN32
#define SYSTEM "Windows"
#endif
4. ,
, Linux.
#ifdef linux
#define SYSTEM "Linux"
#endif
5. main, -
, .
LINE
int main()
{ .

printf("\n \t %s \n \t %s \n", LINE, TITLE);


printf("\t by %S \n \t %s \n", AUTHOR, LINE);
printf("\n Operating System: %s \n", SYSTEM);
}
6. main 0, -
.
return 0;
7. ,

49
, .
-
cpp dM define,

.

3.


-
#if, #else #elif (else if).
.

, #define,
#undef. -
, , -
#ifndef.

,
C,
.
, -
DEBUG.
1. , -
/.
#include <stdio.h>
50

2. ,
debug.c
.
#define DEBUG 1
3. main, -

, .
, ==, -
#if DEBUG == 1
-
, . printf( "Debug status is 1 \n" );
#elif DEBUG == 2
printf( "Debug status is 2 \n" ) ;
#else
#ifdef DEBUG
printf( "Debug is defined! \n" ) ;
#endif
#ifndef DEBUG
printf( "Debug is not defined! \n" ) ;
#endif
#endif
4. main 0, -
.
return 0;
5. ,
, .

6. , ,
, , -
.
#define DEBUG 2

51
7. , , -
, .

#define DEBUG 3
-
#endif

8. -
, ,
, .
#undef DEBUG

3.

, ,
,
const.

.
enum , -
.
-
, -
.

. -
, .

typedef. -
,
.
52

#define -
, -
.
#ifdef ,
.
, -
#endif.
, _WIN32 linux,
.
-
#if, #else #elif.
- -
#undef. #ifndef -
, .
, -
.
4

,


,



.







4.



, ,
C, , ,
, .


+
-
*
/
%
++
--
54

, , -
. , -
, ,
:

a = b * c d % e /f; /* */

a = (b * c) ((d % e) / f); /* */

, %,
.
,
.

, , ++, , --,
1 . -
2 + 3 2 3 . -
. 1, -
1.

-
, .
(),
, () -
, .
1. , -
/.
#include <stdio.h>
2. main,
arithmetic.c
.
int main()
{
int a = 4, b = 8, c = 1, d = 1;
}
3. main
, .
printf( "Addition: %d \n" , a + b ) ;
printf( "Subtraction: %d \n" , b - a ) ;
printf( "Multiplication: %d \n" , a * b ) ;
printf( "Division: %d \n" , b / a ) ;
printf( "Modulus: %d \n" , a % b ) ;
4. main
.

55
printf( "Postfix increment: %d \n" , c++ ) ;
printf( "Postfix now: %d \n" , c ) ;
printf( "Prefix increment: %d \n" , ++d ) ;
printf( "Prefix now: %d \n" , d ) ;
5. main 0, -
.
return 0;
6. , , -

.
,


-
.
-
-

.

4.

, , -
C, , . ,
, =,
, -
.


= a=b a=b
+= a += b a = (a + b)
-= a -= b a = (a - b)
*= a *= b a = (a * b)
/= a /= b a = (a / b)
%= a %= b a = (a % b)

= ,
, , ==.

, , a ,
56

b. a -
b.

+= ,
, a.
, ==, += , -
- a, , b.
. a.
.
, ,
, -
, .

%= a b,
a.
1. ,
/-
.
#include <stdio.h>
assign.c
2. main, -
.
int main()
{
int a, b;
}
3. main
, .
printf( "Assigned: \n" ) ;
printf( "\tVariable a = %d \n" , a = 8 ) ;
printf( "\tVariable b = %d \n" , b = 4 ) ;
printf( "Added & assigned: \n" ) ;
printf( "\tVariable a+=b (8+=4) a= %d \n", a += b ) ;
printf( "Subtracted & assigned: \n" ) ;
printf( "\tVariable a-=b (12-=4) a= %d \n", a -= b ) ;
printf( "Multiplied & assigned: \n" ) ;
printf( "\tVariable a*=b (8*=4) a= %d \n", a *= b ) ;
printf( "Divided & assigned: \n" ) ;

57
printf( "\tVariable a/=b (32/=4) a= %d \n", a /= b ) ;
printf( "Modulated & assigned: \n" ) ;
printf( "\tVariable a%%=b (8%%=4) a= %d \n", a %= b ) ;
4. main 0, -
.
return 0;
5. , , -

.
,
, -
%,
printf()

%%.

4.

, C
, -
.


==
!=
>
<
>=
<=

, ==, 1 (true),
, 0
(false). , , ,
, , -
ASCII-.
58

,!=, 1 (true),
, , -
, ==, 0 (false).


-
- .
ASCII-
. > () 1
(true), , .
,
0 (false).
.

< () ,
1 (true), -
, 0 (false).

= > < -
1, .
1. , -
/.
#include <stdio.h>
2. main, -
comparison.c
.
int main()
{
int zero = 0, nil = 0, one = 1;
char upr = 'A', lwr = 'a';
}
3. main -
.
1
printf( "Equality (0==0): %d \n" , zero == nil ) ; true, 0 -
printf( "Equality (0==1): %d \n" , zero == one ) ; false.

printf( "Equality (A==a): %d \n" , upr == lwr ) ;


printf( "Inequality (A!=a): %d \n" , upr != lwr ) ;
printf( "Greater than (1>0): %d \n" , one > nil ) ;

59
printf( "Less than (1<0): %d \n" , one < nil ) ;
printf( "Greater or equal (0>=0): %d \n" , zero >= nil ) ;
printf( "Less or equal (1<=0): %d \n" , one <= nil ) ;
4. main 0,
.
return 0;
5. ,
, -
. ASCII-
A
65,
a 97,
-
0.

4.

, -
C, .


&&
||
!

, -
true false, ,
true false.

, &&, true
, true. -
, && false.

,
, C,
60

. , -
, -
.

&&, ,
true, , ||,
true,
true. true, || -
false. C ,
, .

, !, ,
. -
-
, ,

var true, !var false.
, -
NOT C, -
-


var = !var. ,
(18151864).
, .

C 0 false,
, , 1 false.
1. , -
/.
#include <stdio.h>
2. main, -
logic.c
.
int main()
{
int yes = 0, no = 1;
}
3. main -
.
printf( "AND (no&&no): %d \n" , no && no ) ;
printf( "AND (yes&&no): %d \n" , yes && no ) ;
printf( "AND (yes&&yes): %d \n" , yes && yes ) ;
printf( "OR (no||no): %d \n" , no || no ) ;
printf( "OR (yes||no): %d \n" , yes || no ) ;
printf( "OR (yes||yes): %d \n" , yes || yes ) ;

61
printf( "NOT (yes !yes): %d %d\n" , yes , !yes ) ;
printf( "NOT (no !no): %d %d\n" , no , !no ) ;
4. main 0,
.
return 0;
5. ,
, -
. ,
0 && 0 -
0 (false),

-
.

4.

, , C,
?:, -
. (true
false), -
.

:
(--) ? -true-- : -false--);

, , , -
-
2, :
(7 % 2 != 0) ? printf (" ") : printf(" ");

7 2 -
, true, ,
, .

-
, , -
62

, -
. printf():
printf(" %d %s", (num == 1 ? "" : "", num );

, true, -
num 1 ,
.



, . :
-
, = (-) ? -true--- : -false-
- --
. , , , -
, ,
a, :
int num, a = 5, b = 2;
num = (a > b) ? a : b;

a -
b, num 5.
1. , -
/.
#include <stdio.h>
2. main,
conditional.c
.
int main()
{
int num = 7;
}
3. main , -
.
(num % 2 != 0) ?
printf("%d is odd\n", num) : printf("%d is even\n", num) ;
4. , -
.
printf("There %s", (num == 1) ? "is" : "are";
printf("%d %s\n", num (num == 1) ? "apple" : "apples";

63
5. ,
,
.
num=1;
printf("There %s", (num == 1) ? "is" : "are";

printf("%d %s\n", num (num == 1) ? "apple" : "apples";


6. main 0, -
.
return 0;
7. , , -
.


,
-
.

4.

C sizeof -
, ,
.

, sizeof, -
, , int, -
, 27.

,
, , -
. -
sizeof , -
.

C char,
. , -
sizeof(char) 1 ( ).

, int float 4
, double 8 ,
. -
64

, . -

sizeof .

, ,
, , -

. , sizeof(int[3]),
, -
, 12 (3 4 ).
sizeof.
sizeof ,
, -
, ,
-
. ,
, .
, , -
int score char grade, 5 (4 + 1), , ,
8 . , (32-
) ,
4 . -
,
, 4.
1. , -
/.
#include <stdio.h>
2. main,
size.c
.
int main()
{
int num = 1234567890;
}
3. main ,
, int (
), .
printf( "Size of int data type is %d bytes\n" , sizeof (int) ) ;
printf( "Size of int variable is %d bytes\n" , sizeof (num) ) ;
4. , -
, .
printf( "Size of an int array is %d bytes\n", sizeof (int[3]) ) ;

65
5. , char
int, -
, , .
struct {int score; char grade; } result;
printf("Size of a structure is %d bytes\n" , sizeof (result) ) ;
6. main 0,
.

return 0;
7. , struct, -
, - -
. .

9,

, -
,


sizeof.

4.


,
1 0. , -
, - 0 255.
C, , 1.
- (Less Significant
, Bit, LSB) (Most Significant Bit, MSB).
, , , 50.
,
.
8 7 6 5 4 3 2 1
MSB LSB
128 64 32 16 8 4 2 1
0 0 1 1 0 0 1 0

char C -
, ,
- -
(4 ). .
66

, -
,
-
, -

.
| 1 ,
1
: 1010 | 0101 = 1111
& 1 ,
1
: 1010 & 1100 = 1000
~ 1 , -
1
: 1010 ~ 0011 = 0100
^ - 1 , -
1
: 1010 ^ 0100 = 1110
<< , 1,

: 0010 << 2 = 1000
>> , 1,

: 1000 >> 2 = 0010
-
, ,
. , XOR (
) -
.
1. , -
/.
#include <stdio.h>
2. main, -
xor.c
.
int main()
{
int x = 10, y = 5;
printf("\nx = %d y = %d\n", x, y);
}
3. main XOR, -
-
.

67
x = x ^ y; /* 1010 ^ 0101 = 1111 ( 15)*/
y = x ^ y; /* 1111 ^ 0101 = 1010 ( 10)*/
x = x ^ y; /* 1111 ^ 1010 = 0101 ( 5)*/
4. , -
.
printf("\nx = %d y = %d\n", x, y);
5. main 0,
.
-
return 0; .
6. , -
, XOR. ,
.

4.

-

, . -
, -
. , char
, ,
char -
.


,
, -
. , 8
1000 (1 8 0 4 0 2 0 1),
, .


(~), , , -
, -
1. , , -
68

,
. 15 -
00001111 (0 128 0 64 0 32 0 16 1 8 1 4 1 2 1 1),
(&).


, 1, -
(<<) (>>).
1. , -
/.
#include <stdio.h>
2. main,
bitflag.c .
int main()
{
int flags = 8; /* 1000 (1 8 0 4 0 2 0 1)*/
}
3. main ,
1 .
flags = flags | 2; /* 1000 | 0010 = 1010 ( 10)*/
4. , -
.
printf( "Flag 1: %s\n" , ( (flags & 1) > 0) ? "ON" : "OFF") ;
printf( "Flag 2: %s\n" , ( (flags & 2) > 0) ? "ON" : "OFF") ;
printf( "Flag 3: %s\n" , ( (flags & 4) > 0) ? "ON" : "OFF") ;
printf( "Flag 4: %s\n\n" , ((flags & 8) > 0) ? "ON" : "OFF") ;
5. ,
,
. -
-
char mask = 15; /* 00001111*/
, -
flags = ~flags & mask; /*~(1010 & 111 = 1010) = 0101*/
6. , - . ,
, - #define FLAG1 1
, .
#define FLAG2 2
printf( "Flag 1: %s\n" , ( (flags & 1) > 0) ? "ON" : "OFF") ;
#define FLAG3 4
printf( "Flag 2: %s\n" , ( (flags & 2) > 0) ? "ON" : "OFF") ;
#define FLAG4 8
printf( "Flag 3: %s\n" , ( (flags & 4) > 0) ? "ON" : "OFF") ;
printf( "Flag 4: %s\n\n" , ((flags & 8) > 0) ? "ON" : "OFF") ;
printf( "Flags decimal value is %d\n" , flags ) ;

69
7. , -
, -
.
flags = flags << 1; /*0101 << 1 = 1010*/
printf( "Flags decimal value is now %d\n" , flags ) ;
8. main 0,
.
return 0;
9. ,
, XOR.


-
,
.
,
int, -
4 ,
32 .

4.


, C -
. , a = 6 + b * 3
, *, - , -
, .
, +, -
a = 6 + b *3
. , , ,
. , , .
, -

. , , ,
, .

-
-> .
() []
; -> .
70

- ! ~ ++ -- +
. sizeof - * & addressof
* / %
+ C -
<< C >> C
< <= > >=

== !=
&
^
|
&&
||
?:
= += -= *= /= %= &= ^= |= <<= >>=
,

1. , -
/.

precedence.c #include <stdio.h>


2. main,
,
, -
.
int main()
{
printf("\nDefault precedence ((2*3)+4)-5 : %d\n", 2*3+4-5 ) ;
printf("Explicit precedence 2* ((3+4)-5 ): %d\n", 2*((3+4)-5) ) ;
}
3. main -
, -
,
.
printf( "\nDefault precedence (7*3) %% 2: %d\n" , 7*3%2 );
printf( "Explicit precedence 7* (3%%2) : %d\n" , 7*(3%2) ) ;
4. - , -
,

.

71
int num = 9;
printf("\nDefault precedence(8/2)*4:%d\n", --num/2*sizeof(int));
num = 9;
printf("Explicit precedence 8/(2*4):%d\n", --num/(2*sizeof(int)));
5. main 0,
.
return 0;
6. ,

, , -
.
%
printf()
%%.

4.

, -
, +, , -, , *,
, /, , %.
, ++, , --,
1.
= -
, ,
.
, -
== !=,
, >, <,
>= <= .
&& || -
, -
true false, NOT ! -
.
?: , -
-
72

.
sizeof
.
,
0 1.
|, &, ~ ^ -
-
, <<
>>
.

, , -
.
, ,
-
, -
.
5

,

switch

,
,


.



5.


if
, .
, ,
,
true. if :
if (-) {-----true}

true,
, , .
,
,
,
,

. .
.
&& , ,
, -
true. :
if ((-) && (-)) {}
, if,
. ,
74

, -
true, :
if (-)
{
if(-) {}
}

, -
if, false, ,
, -
, .
if else, -
if, -
, : if () {} else if () {}.
else, , -
,
false. :
if (-)
{-----true}
else
{-----false}
,
-
. .
1. , -
/.
#include <stdio.h>
2. main, , -
, - ifelse.c
true.
int main()
{
if( 5 > 1 ) { printf( "Yes, 5 is greater than 1\n" ) ; }
}
3. main ,
,
true.
if ( 5 > 1)
{
if(7 > 2)

{ printf("5 is greater than 1 and 7 is greater than 2\n") ; }

}
4.

75
, , -
false.
if( 1 > 2 )
{ printf( "1st Expression is true\n" ) ; }
else if( 1 > 3 )
{ printf( "2nd expression is true\n" ) ; }
else
{ printf( "Both expressions are false\n" ) ; }
5. main 0,
.
return 0;
6. ,
,
. -
C true
1,
false
0.
(5 > 1)
-
(5 > 1 == 1).

5.

switch
, -
if else, -
switch, .
switch . -
, -
case.
, , -
case.

case break,
switch -
default , ,
, .
- case
, default, ,
, case .
. switch :
switch(-)
76

{
case : -----; break;
case : -----; break;
case : -----; break;
default: ------;
}
ANSI C, switch 257
case, case -
.

-
, case -
default . , ,
0, 1 2, :
switch (num)
switch,
{
,
case 0:
-
break. case 1:
case 2 : printf( "Less than 3\n" ) ; break ;
case 3 : printf( "Exactly 3\n" ) ; break ;
default : printf( "Greater than 3 or less than zero\n" ) ;
}
1. , -
/.
#include <stdio.h>
2. main, -
. switch.c
int main()
{
int num = 2; char letter = 'b';
}
3. main switch,
.
switch(num)
{
case 1 : printf( "Number is one\n" ) ; break ;
case 2 : printf( "Number is two\n" ) ; break ;
case 3 : printf( "Number is three\n" ) ; break ;
default : printf( "Number is unrecognized\n" ) ;
}
4. switch, -

77
.
switch(letter)
{
case 'a': case 'b': case 'c':
printf( "Letter is %c\n" , letter ) ; break ;
default : printf( "Letter is unrecognized\n" ) ;
}
5. main 0,
.
return 0;
6. ,
,
switch. switch -
case, ,
,

.

5.


, -
.
.
, .
true, ( ,
false, ).

C -
for, while do while. , -
for, :

for (-; -; ) {}

, -
.
, i.

-
, ,
78

true.
false, -
. ,
.

, -
.
1. , -
/.
#include <stdio.h>
2. main, -
forloop.c ,
.
int main()
{
int i, j;
}
3. main for,
.
for (i = 1; i < 4; i++)
{
printf( "Outer loop iteration %d\n" , i ) ;
}
4. main 0,
.
return 0;
5. ,
,
.

6. -

79
, .
-
.
for (j = 1; j < 4; j++)
{
printf( "\tInner loop iteration %d\n" , j ) ;
}
7. , ,

-
.
for -

i--
i++.

5.


while for, -
. while -
, ,
, for. ,
, -
,
while, -
, ,
:
-
while(-)
{; }

do while do
, while
, :
while
do while -
80

. do {; }
while (-);
while do while ,
false -
. , -
, -
, ,
.

while do while ,
,
, - false. do while
, , -
. -
Windows Linux , do while , -
Ctrl+C, for while .
- , for , -
. , while
, .
,
-
.
1. , -
/
#include <stdio.h>
2. main, -
, dowhile.c

int main()
{
int i, arr[3] = {10, 20, 30};
}
3. main while, -

i = 0;
while(i < 3)
{
printf("While: arr[%d] = %d\n", i, arr[i]); i++;
}
4. do while,

i = 0;

81
do
{
printf("\nDo while: arr[%d] = %d", i, arr[i]); i++;
}
while(i < 3);
5. main 0,

return 0;
6. ,

, -
, -
0
(while(i < 0)) ,

, ,

do
while.

5.


break , -
-
. break -
, .
true, -

. ,
.
1. , -
/
#include <stdio.h>
2. main, -
breakcontinue.c ,

int main()
{
int i, j;
}
82

3. main ,
,

for(i = 1; i < 4; i++)
{
for(j = 1; j < 4; j++)
{
printf( "Running i=%d j=%d\n", i, j ) ;
}
}
4. main 0,

return 0;
5. , ,
-

6. break -
, ,
. -
if(i == 2 && j == 1) break

{
,
printf( "Breaks inner loop when i=%d and j=%d\n" , i, j ) ;

break; .
}

83
continue , -
, .
statement ,
. -
true, .
7. continue ,


, -
if(i == 1 && j == 1) continue -

{
,
printf( "Continues inner loop when i=%d and j=%d\n", i, j ) ;

continue;
.
}

5.

goto -
,
, -. -

.

goto -
, -
, ,
, -
. -
, goto
.

goto
, -
.
, .
1. , -
/
84

#include <stdio.h>
2. main, -
jump.c ,

int main()
{
int i, j;
}
3. main ,
,
,

for(i = 1; i < 4; i++)
goto
. {
. for(j = 1; j < 4; j++)
{
printf( "Running i=%d j=%d\n", i, j ) ;
}
}
4. , -
end

if(i == 2 && j == 1) {goto end;}
5.
} end:
6. main 0,

return 0;
7. , ,

, .
-
C -
1 (true) 0
(false).

goto, -

true (1) .

85
8.
1
int i, j, flag = 1;
9.

,
if (flag)
10. ,
goto , 0
-
if(i == 2 && j == 1) {flag = 0;}

11. end:, - goto.exe, -
, ,
,
Windows goto.

5.

if , -
true false.
else
, -
, if false.

.
,
if else, -
switch.
case switch
break.
switch default, -
, -
.
for , -
, , -
86

.
while ,
, ,
.
while ,
.
do ,
while, -
.
do while , -
.
for while do while
.
break ,
, continue , -
.
, goto -
,
,
.
6

,



,,




.


6.

-
main() , -
, printf() stdio.h. -
, C,
,
.

, -
. -
, ,
. . -
C,
,
.
,
, , , -
, . -
main().
main, -
88

. -
. ,
void.
.
-
,
, . ,
, ,
main.
main , .
, , -

.
1. , -
/.
#include <stdio.h>
2. .
first.c
void first();
int square5();
int cube5();
3. main, -
.
int main()
{
int num;
}
4. main .

void first()
,
{
-
printf( "Hello from the first function\n" ) ;

} .
int square5()
{
int square = 5 * 5;
return square;
}
int cube5()
{
int cube = (5 * 5) * 5;
return cube;

89
}
5. -
main.
first();
num = square5();
printf("5x5= %d\n", num);
printf("5x5x5= %d\n", cube5());
6. main 0,
.
return 0;
7. ,
,
. -


main(), -

, -
main
.

6.

-
. -
.
.

, C -
, . -
- C , Pascal,

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

void action(char c, int I, float f, double d);

,
. -
90

.
1. , -
/.
#include <stdio.h>
2. ,
args.c
.
void display(char str[]);
int square(int x);
int cube(int y);
3. main, -
, -
.
int main()
{
int num;
char msg[50] = "String to be passed to a function" ;
}
4. main .

void display(char str[])
, -
{
,
printf("%s\n", str);
, -
} ,
int square(int x) ,

{ -
return x * x; . , -

}

int cube(int y) .
{
return (y * y) * y;
}
5. -
main.
display(msg);

91
num = square(4);
printf("4x4= %d\n", num);
printf("4x4x4= %d\n", cube(4));
6. main 0,
.
return 0;
7. ,
,
, -
. , -
-
return,
.

,
-
.

6.

, , -
,
printf().

, .
, ,
,
.

recur.c

1. , -
/.
#include <stdio.h>
2. , -
92

.
void count_down_from(int num);
3. main, -
.
int main()
{
int start;
}
4. main ,
.

scanf() - printf( "Enter a positive integer to count down from: " ) ;
scanf("%d", &start);
&, 5. main
2. , , .
count_down_from(start);
6. main , -
,
.
printf( "Lift Off!\n" ) ;
7. main 0,
.
return 0;
8. main
,
, .
void count_down_from(int num)
{
printf("%d\n", num);
}
9. , -
.
--num;
10. -
, main, -
,
.
if(num < 0) return;
else
count_down_from(num);

93
11. , -

, , -
, .

, -
.

6.


, , -
- , , -
, C, -
.
-
. , ,
.
,
, ,
.
.h, C.
, ,
, , -
main(), #include. -
-
( < >,
).
94

1. utils.h, -
.
int square(int num)
{
return (num * num);
utils.h }
2. , -
/.
#include <stdio.h>
#include "utils.h"
3. , -
square.c .
void getnum();
4. main, , -
0, .
int main()
{
getnum();
return 0;
}
5. main ,
.
void getnum()
{
int num;
char again;
}
6. ,
,
. -

printf( "Enter an integer to be squared: " ) ;

scanf("%d", &num); , -
7.
, . -
printf( "%d squared is %d\n" , num, square(num) ) ; -
8. , , -
. #include.
printf( "Square another number? Y or N: " ) ;
scanf("%1s", &again);

95
9. , ,
main.
if((again == 'Y') || (again == 'y')) getnum();
else return;
10. ,
, , -
. ,

%1s -
,
, -
.

6.

static , -
, -
, , .
,
., -
. , square() multiply()
main .
1. , -
/.
#include <stdio.h>
2. , -
.
void menu();
menu.c 3. main,
, 0,
.
int main()
{
menu();
96

return 0;
}
4. main ,

, - .
void menu()
, - {
2.
int option();
printf( "\n\tWhat would you like to do?" ) ;
printf( "\n\t1. Square a number" ) ;
printf( "\n\t2. Multiply two numbers" ) ;
printf( "\n\t3. Exit\n" ) ;
scanf("%d", &option);
action(option);
}
5. -
,
/.
#include <stdio.h>
action.c 6.
static square(int a) {return (a * a); }
static multiply(int a, int b) {return a * b; }
7. , -

main, -
, ,
. , -
void action(int option) , -
{ main(), -
int n1, n2; .
if(option == 1)
{
printf( "Enter an integer to be squared: " ) ;
scanf("%d", &n1);
printf("%d x %d = %d \n", n1, n1, square(n1));
menu();
}
else if (option == 2)
{
printf( "Enter two integers to multiply " ) ;
printf( "separated by a space: " ) ;
scanf("%d", &n1); scanf("%d", &n2);
printf("%d x %d = %d\n", n1, n2, multiply(n1, n2));
menu();

97
}
else return;
}
8. , -
, -
. ,

-
,

.

6.

-
, , ,
. .
,
main -

main().
, ,
, , -
main().
-
,
,
.
, , -
,
.
C -
98

-
.
,
, -
, .
-
.h.
#include
,
.
static
,
, .
-
static, - ,
.
, -
, main().
7

,




.




7.



C
. ,
.

,
-
. , ,
. ,
-, , -
, . - -
, , -
, .

- , -
, *. -
, * ,
*, -
100

.
.
, - , -
&.
- -
, *, , -
-.
- -
, .

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

, ,
-, .
, , -
, , *,
.
1. , -
/
#include <stdio.h>
2. main,
point.c

int main()
{
int num = 8;
int *ptr = &num;
}
3. main -
, ,
printf( "Regular variable contains: %d\n" , num ) ;
printf( "Pointer variable contains: 0x%p\n" , ptr ) ;
printf( "Pointer points to value: %d\n\n" , *ptr ) ;
4. main
, -
,

101
*ptr = 12;
printf( "Regular variable contains: %d\n" , num ) ;
printf( "Pointer variable contains: 0x%p\n" , ptr ) ;
printf( "Pointer points to value: %d\n\n" , *ptr ) ;
5. main 0,



return 0;
,
6. , ,
, , - main().


7.

, ,
,
.

, ++, , --,

, .


+= -=, , -
.

, -
.


.
1 .
1. , -
/.
102

#include <stdio.h>
2. main,
movptr.c
, -
.
int main()
{
int i;
int nums[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
}

3. main - -
, -
*= /= ,
, .
-
. int *ptr = nums;
printf( "\nAt Address: %p is Value: %d\n", ptr , *ptr ) ;
4. main 1,

.
ptr++;
printf( "At Address: %p is Value: %d\n", ptr , *ptr ) ;
ptr++;
printf( "At Address: %p is Value: %d\n", ptr , *ptr ) ;
5. ,

ptr -= 2;
printf( "At Address: %p is Value: %d\n\n", ptr , *ptr ) ;
6. ,

for (i = 0; i < 10; i++)
{
printf( "Element %d Contains Value: %d\n" , i , *ptr ) ;
ptr++;

103
}
7. main 0,

return 0;
8. ,
, , -


.

7.


, C,
.
, ,
.



.
1. , -
/.
#include <stdio.h>
2. ,
passptr.c
.
void twice(int *ptr);
void thrice(int *ptr);
104

3. main,
,
.
int main()
{
int num = 5;
int *ptr = &num;
}

4. main , -
, , .
,
- - printf( "ptr stores address: %p\n" , ptr ) ;
- printf( "*ptr dereferences value: %d\n\n" , *ptr ) ;
.
5.
.
printf( "The num value is %d\n" , num ) ;
6. main -
, , -
. *, -
,
void twice(int *number) ,
{ , -
,
*number = (*number * 2);
-
} .
void thrice(int *number) -

{ .
*number = (*number * 3);
}
7. main ,
,
.
twice(ptr);
printf( "The num value is now %d\n", num ) ;
thrice( ptr ) ;

105
printf( "And now the num value is %d\n", num ) ;
8. main 0,
.
return 0;
9. ,
,
.

7.


, C, -
, -
.

-
. , \0
, -.
,
& ,
-.
1. , -
/.
#include <stdio.h>
2. main, -
arrptr.c
, -
.
106

int main()
{
int i;
int nums[5] = {1, 2, 3, 4, 5};
}
3. main -
-,
.
int *ptr0 = &nums[0];
int *ptr1 = &nums[1];

int *ptr2 = &nums[2];
,
i - int *ptr3 = &nums[3];
int *ptr4 = &nums[4];

4.
.
, -
.
int *ptrs[5] = {ptr0, ptr1, ptr2, ptr3, ptr4};
5. ,
, -
-

char str[9] = { 'C', ' ', 'i', 's', ' ', 'F', 'u', 'n', '\0' } ;
,
char *string = str ; ,
char *strings[3] = { "Alpha", "Bravo", "Charlie" } ;
*.
6. , -
,
.
for(i = 0; i < 5; i++)
{
printf( "The value at %p is: %d\n" , ptrs[i], *ptrs[i] ) ;
}
7. , ,
.
printf( "\nString is: %s\n\n" , string ) ;
8. , , -
.

107
for(i = 0; i < 3; i++)
{
printf( "String %d is: %s\n" , i , strings[i] ) ;
}
9. main 0,
.
return 0;
10. , ,
.

,

' '.
,
'',


.

7.

, ,
, .

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

,
. , -
, ,
.


. , ,
, .
1. , -
/.
108

#include <stdio.h>
2. ,
fcnptr.c , , -
.
int bounce(int a);
int caller((int(*function) (int), int b);
3. main, -
,
- .
int main()
{
int num;
int (*fptr)(int) = bounce;
}
4. main
, -
.
int bounce(int a)
{
printf( "\nReceived Value: %d\n", a ) ;
return((3 * a) + 3);
}
5. , -

-
.
caller()
int caller(int (*function)(int), int b)
{ ,
(function*)(b); -

} ,
6. main -
.
, .
num = (*fptr)(10);
printf( "Returned Value: %d\n" , num ) ;
7. , -

109
, ,
, , -
, .
num = caller(fptr, 5);
printf( "Returned Value: %d\n", num ) ;
8. main 0,
.
return 0;
9. ,
, , -
.
C, -

.

*, -
, -
, -
.

7.

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

*, , .
-
&.

,
.
-
, -
.
.
-
, -
110

.

.

.
/ ,
.
-
*, -
.
,
, .
8




.






8.

C ,
\0. -
, , -
, ,
ASCII. , .
, char letter = 'S',
letter++ 'T'.
ASCII -

, 32
, , -
ASCII 32. ,
. char letter = 'M', letter += 32 'm'.
C
, - -
, - \0,
. , -
implied . -
sizeof ,
112

. -
char, :
char arr[6] = { 'A', 'l', 'p', 'h', 'a' } ;
char *ptr = "Beta" ;
printf("%s", arr); /* "Alpha"*/
printf("%s", ptr); /* "Beta"*/

, -
C, scanf(), -
- 26. ,
C , .
- , scanf() -
, , . ,
. scanf() -
, .
,
stdio.h. -
gets(). , -
. ( ),
, .
- \0 ,
Enter, , -
.
puts(), ,
, .
1. , -
/.
#include <stdio.h>
2. main, .
int main()
{
char str[51];
getsputs.c
}
3. main , -
.
printf( "\nEnter up to 50 characters with spaces:\n" ) ;
gets(str);
4. main , -
.
printf( "fgets() read: " ) ;

113
puts(str);
5. , scanf().
printf( "\nEnter up to 50 characters with spaces:\n" ) ;
scanf("%s", str);
printf( "scanf() read: %s\n" , str ) ;
6. main 0,
.
return 0;
7. ,
, .
, -

,
-
printf(), puts().

8.

C -
string.h, -
. ,
string.h #include, -
.

string.h strlen(), -
, .
, strlen() ,
, -
, - \0.

, - string.h ,
\0. .
strcpy(), .
, ,
, .
:

strcpy(-, -);
114

,
- \0. , -
, \0. ,

.

strncpy().
, strcpy(),
, , -
. :

strncpy(-, -, );

,
,
\0 . ,
, \0,
. ,
,
, , -
, - \0.
1. , -
/
.
#include <stdio.h>
strcpy.c
#include <string.h>
2. main, -
.
int main()
{
char s1[] = "Larger text string", s2[] = "Smaller string" ;
}
3. main ,

.

printf( "\n%s: %d elements" , s1 , sizeof(s1) ) ;
,
printf( ", %d characters\n" , strlen(s1) ) ;
, -
4. main
-
.
. ,

115
strcpy(s1, s2); -
5. 3, , ,
, -
, , - -
\0. ,
.
strncpy( s1, s2, 5 ) ; s1[5] = '\0' ;
6. 3,
, 0, -
.
return 0;
7. ,
, .

s1

s1[5],
-
, .

8.

-
.

string.h
, .
,
string.h #include, -
.

strcat(), -
, .
, ,
, ,
. :

strcat(-, -----);

, , ,
, -
, .
116


strncat(). , strcat(), -
, , -
. :

strncat(-, -----, );

-
,
.
, , -
. :
.
strncat(-, ----- + , );

, strcat(), , -
, -
,
strncat().
1. , -
/
.
#include <stdio.h>
strcat.c
#include <string.h>
2. main, -
.
int main()
{
char s1[100] = "A Place for Everything ";
char s2[] = "and Everything in its Place";
char s3[100] = "The Truth is Rarely Pure ";
char s4[] = "and Never Simple. Oscar Wilde";
}
3. main
.
strcat(s1, s2); printf("\n%s\n", s1);

117
4. 17
.
strncat(s3, s4, 17); printf("\n%s\n", s3);
5. 14
.
strncat(s3, (s4 + 17), 14); printf("\n%s\n", s3);
6. main 0,
.
return 0;
7. ,
, .

,
,

.

8.

, -
,
(). strstr().
string.h,
#include,
.

strstr() . -
, , ,
strstr() - . ,
, NULL. ,
- .
.
, , -
. .
, strstr(), , -
( ),
.
, .
C == !=
118

, NULL,
. -
string.h strcmp(),
. , -
, . -
ASCII
. , ,
strcmp() 0.
.
1. , -
/
.
#include <stdio.h>
strstr.c #include <string.h>
2. main, -
.
int main()
{
char str[] = "No Time Like Present ";
char sub[] = "Time";
}
3. main , -
.
if(strstr(str, sub) == NULL)
{
printf( "Substring \"Time\" Not Found\n" ) ;
}
4. , -

.
,
else
\
{ -
-
printf( "Substring \"Time\" Found at %p\n" , strstr( str, sub) ) ;
,
printf( "Element Index Number %d\n\n", strstr(str,sub) - str ) ; -
}
.
5. , -
.
printf("%s Versus \"Time\": %d\n",sub, strcmp(sub,"Time"));
printf("%s Versus \"time\": %d\n",sub, strcmp(sub,"time"));

119
printf("%s Versus \"TIME\": %d\n" ,sub, strcmp(sub,"TIME"));
6. main 0,
.
return 0;
7. ,

, -
.
string.h
,
.
strchr()
, -
strrchr() .

NULL ,
.

8.

ctype.h -
, .
, -
ctype.h #include, -
.

ctype.h isalpha(), -
, , isdigit(),
, . ,
ispunct() , -
, isspace() .

, ctype.h
isupper() islower(), , -
toupper() tolower(), .

(-
1), , -
0, .


120

, ,
. -
, -,
.
1. , -
/

#include <stdio.h>
isval.c #include <string.h>
2. main, ,
-
true (1)
int main()
{
char str[7];
int i;
int flag = 1;
}
3. main ,
-
puts( "Enter six digits without any spaces..." ) ;
gets(str);
4. main , -

for(i = 0; i < 6; i++) { }
5. - false
(0), -
isdigit() -
if(!isdigit(str[i]))

{ isdigit() == 0.
flag = 0;
}
6. if ,
if(isalpha(str[i]))
{ printf( "Letter %c Found\n" , toupper(str[i]) ) ; }
else if(ispunct(str[i]))
{ printf( "Punctuation Found\n" ) ; }
else if(isspace(str[i]))
{ printf( "Space Found\n" ) ; }
7. ,

121
-
( flag ) ? puts("Entry Valid") : puts("Entry Invalid") ;
8. main 0,

return 0;
9. ,

,
, -
,
.

8.

stdlib.h -
, atoi(). -
. , -
ctype.h
#include, .
atoi() ( alpha-to-integer, --)
,
. -
, atoi() 0.
( ,
) , atoi() -
. atoi() -
, ,
.
itoa() ( integer-to-
alpha, --), -
, int, . ,
ANSI C.
122

itoa() . -
, , -
, ,
. , 2, -
.
ANSI C itoa(), -
sprintf() stdlib.h.
,
. sprintf() -
, ,
, . -
,
.
1. , -
/
.
#include <stdio.h>
conv.c #include <stdlib.h>
2. main,
, -
.
int main()
{
int n1, n2, n3;
char s1[10]= "12eight", s2[10]= "-65.8", s3[10]= "x13" ;
}
3. main

.
itoa() -
n1 = atoi( s1 ) ;
, ,
printf( "\nString %s converts to Integer: %d\n" , s1 , n1 ) ;

n2 = atoi( s2 ) ; ANSI,
printf( "String %s converts to Integer: %d\n" , s2 , n2 ) ; .
n3 = atoi( s3 ) ; GNU C, -
printf( "String %s converts to Integer: %d\n\n" , s3 , n3 ) ;
, -
4. main - .
, -
, .
itoa(n1, s1, 2);
printf( "Decimal %d is Binary: %s\n" , n1 , s1 ) ;
5. , -

123

, -
.
n2 = sprintf( s3, "%o", n1 ) ;
printf( "Decimal %d is Octal: %s chars: %d\n", n1, s3, n2) ;
n3 = sprintf( s3, "%x", n1 ) ;
printf("Decimal %d is Hexadecimal: %s chars: %d\n",n1,s3,n3);
6. main 0,
.
return 0;
7. ,
, .
itoa()
sprintf()

-
, -

.

8.

C -
, -
\0.
ASCII.
.
scanf() -
, , gets() -
- \0 -
.
puts() ,
, .
string.h -
, strlen(),
, strcpy() strncpy(),
.
string.h strcat()
strncat(), .
124

, string.h strstr(),
, strcmp(), -
.
strstr() , -
NULL.
ctype.h , -
, isaplha(), isdigit()
ispunc().
ctype.h islower(),
isupper() tolower(), toupper(),
.
stdlib.h -
atoi(), .
stdlib.h -
sprintf(), .
itoa()
.
9






struct union
union,,
,


,
C,
,



.

9.


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

-
, -
- - . , -
- , ,
- , , . .
.
C -
struct,
, - . -
-
. , ,
x y , , :
struct coords:
126

{
int x;
int y;
};
-

, . -
, - , -
C, - . , point
coords :
main().
struct coords
{
int x;
int y;
} point;
, .
, , point.x.
, ,
. -
. , -
coords top, -
:
struct coords top;
1. , -
/.
#include <stdio.h>
2. , .
struct coords struct.c

{
int x;
int y;
} point;
3. .
struct coords top;
4. main,
.
int main()
{

127
point.x = 5; point.y = 8;
top.x = 15; top.y = 24;
}
5. , .
printf( "\npoint x: %d, point y: %d\n", point.x , point.y ) ;
printf( "\ntop x: %d, top y: %d\n" , top.x , top.y ) ;
6. main 0, y

top
return 0;
7. ,
, , - point
.
x

9.


, , ,

typedef, . -
,
struct .
typedef
, struct
, . -
, ,
, .
, ,
, ,
, ,
.
. -
, ..
128

: -.-..
1. , -
/.
#include <stdio.h>
2. ,
typedef.c .
typedef struct
{
int x;
int y;
} Point;
3.
, -
.
Point top = {15, 24};
Point btm;
4. main,
.
int main()
{
btm.x = 5;
btm.y = 8;
}
5. .
printf( "\nTop x: %d, y: %d\n" , top.x , top.y ) ;
printf( "Bottom x: %d, y: %d\n" , btm.x , btm.y ) ;
6. main , -
-
. ,

typedef struct
,
{ .
Point a;
Point b;
} Box;
7. , -
, .
Box rect ={6, 12, 30, 20};
8. main ,
.

129
printf( "\nPoint a x: %d" , rect.a.x ) ;
printf( "\nPoint a y: %d" , rect.a.y ) ;
printf( "\nPoint b x: %d" , rect.b.x ) ;
printf( "\nPoint b y: %d\n" , rect.b.y ) ;
9. main 0,
.
return 0;
10. , -
, , .

-
,
, -


.

9.


, , -
,
.


. -
L- - , ,
, R- - =.
.
,
=, L-, . ,
=, R-, -
, .
C , -
R- =.
C , L- -
=.
L-,
130

. -
L-, -
.
1. , -
/.
#include <stdio.h>
2. , -
strmbr.c , .
typedef struct
{
char str[5];
} ArrType;
3. , ,
.
typedef struct
{
char *str;
} PrtType;
4.
.
ArrType arr = {'B', 'a', 'd', ' ', '\0'};
PtrType ptr = {"Good"};
5. main,
, .
int main()
{
printf( "\nArray string is a %s" , arr.str ) ;
}

6. main
.
,
arr.str[0] = 'I';

arr.str[1] = 'd';
arr.str[2] = 'e'; ,
arr.str[3] = 'a'; .
arr.str[4] = '\0';
printf("%s\n", arr.str);
7. , - -
.

131
printf( "\nPointer string is a %s" , ptr.str ) ;
8. ,
,
.
ptr.str = "Idea";
printf("%s\n", ptr.str);
9. main 0,

.

return 0;
-
10. ,

, , .

,
.

9.

, ,
, .
, -
.

-
, C,
. .
->, ,
. . -
, ptr->member
(*ptr).member.
1. , -
/.
#include <stdio.h>
2. , -
structptr.c
, -
.
132

typedef struct
{
char *name; char *popn;
} City;
3. main,

-. ,
.
-
int main()
-
. {
City ny, la, ch, *ptr;
}
4. main
, -
., , -
.
ny.name = "New York City" ;
ny.popn = "8,274,527" ;
printf( "\n%s , Population: %s\n", ny.name , ny.popn ) ;
5. .
ptr = &la;
6. , -
, -
, ., - ,
. ,
,
ptr->name = "Los Angeles" ;
ptr->popn = "3,834,340" ; -
.
printf( "\n%s, Population: %s\n" , la.name, la.popn ) ;
7. -.
ptr = &ch;
8. ,
,
.
ptr->name = "Chicago" ;
ptr->popn = "2,836,658" ;
printf( "\n%s, Population: %s\n", ptr->name, ptr->popn ) ;

133
9. main 0,
.
return 0;
10. ,
, , .

9.


, -
. ,
.
-
.

, -

, . -
, ,
- , -
.

- ,
. , ,
. C -
, -
, -
, .
134

1. , -
/.
#include <stdio.h>
2. ,
passstruct.c
.
typedef struct
{
char *name;
int quantity;
} Item;
3. , ,
, .
Item fruits[3] = { { "Apple" , 10 }, { "Orange" , 20 }, { "Pear" , 30 } } ;
4. , -
, ,
, .
void display(Item val, Item *ref);
5. , ,
.
void display(Item val, Item *ref)
{ printf("%s: %d\n", val.name, val.quantity); }
6.

-, .

val.name = "Banana"; val.quantity = 40;
,
{ printf("%s: %d\n", val.name, val.quantity); } -
-
7. , .
,
printf("%s: %d\n", fruits[0].name, fruits[0].quantity);
8. , -
. .

ref->name = "Peach";
ref->quantity = 50;
printf("%s: %d\n", fruits[0].name, fruits[0].quantity);
9. main, -
,

135
.
int main()
{
display(fruits[0], &fruits[0]);
}
10. main 0,
.
return 0;
11. ,
, , -
.

9.


C -


, . -
.

,
union - -
.

, -
, -
, -
. ,
. -
, .
1. , -
/.
136

#include <stdio.h>
2. ,
union.c .
typedef struct
{ int num; char ltr, *str;} Distinct;
3. , -

.
typedef union { int num; char ltr, *str;} Unified;
-
, 4. main,
, ,
.
. int main()
{
Distinct sdata = {10, 'C', "Program"};
}
5. main -
, .
Unified udata;
6.

.
-
printf( "\nStructure:\nNumber: %d", sdata.num ) ;
,
printf( "\tStored at: %p\n" , &sdata.num ) ; -
printf( "Letter: %c" , sdata.ltr ) ; .
printf( "\tStored at: %p\n" , &sdata.ltr ) ;
printf( "String: %s" , sdata.str ) ;
printf( "\tStored at: %p\n" , &sdata.str ) ;
7. , -
.
udata.num = 16;
printf( "\nUnion:\nNumber: %d" , udata.num ) ;
printf( "\tStored at: %p\n" , &udata.num ) ;
8. , -
.
udata.ltr = 'A';
printf( "Letter: %c" , udata.ltr ) ;
printf( "\tStored at: %p\n" , &udata.ltr ) ;

137
9. ,
.
udata.str = "Union" ;
printf( "String: %s", udata.str ) ;
printf( "\tStored at: %p\n" , &udata.str ) ;
10. main 0,
.
return 0;
11. ,
, , -
, .

9.

stdlib.h
,
calloc() . malloc() -
- , ,
, . calloc() ,
malloc() ,
, .
. .
NULL.

, malloc() calloc(), -
realloc().

, , .

NULL .
1. , -
/
.
138

#include <stdio.h>
memory.c
#include <stdlib.h>
2. main, -
.
int main()
{
int size, *mem;
}
3. ,
.
mem = malloc(100 * sizeof(int));
4. -
, , .
, int,
if (mem != NULL)
, 4 -
{ ,
size = _msize(mem);
sizeof(),
printf( "\nSize of block for 100 ints: %d bytes\n", size ) ;
printf( "Beginning at %p\n" , mem ) ; , -
int
}
.
else { printf( "!!! Insufficient memory\n") ; return 1 ; }
5.
, , -
.
mem = realloc(mem, size + (100 * sizeof(int));
if (mem != NULL)
{
size = _msize(mem);
printf( "\nSize of block for 200 ints: %d bytes\n" , size ) ;

139
printf( "Beginning at %p\n" , mem ) ;
}
else { printf( "!!! Insufficient memory\n") ; return 1 ; }
6. main
, 0,
.
free( mem ) ;
return 0;
7. ,
, ,
. , -
,
: calloc(100,
sizeof(int)).

9.


, .
,
..
-
, .
struct
typedef, , .

, -
.
, , -
, , -
.
-
, .
. -
140

->.
-
, -
.
,
.
,
.
, -
.

,
.
stdlib.h -
malloc(), calloc() realloc(), -
, free(),
.
10

,
,

C,

,

,

Windows.






10.

stdio.h ,
.
FILE *fp.
, , . , -
C, file_ptr
FILE *file_ptr; .

, -
stdio.h, .
,
(-
-
).
,
stdio.h. , ,
fopen().
, , ,
. fopen() -
NULL .
,
, ,
fopen().
142

fclose(), -
.
, , -
, -
fopen():


r
, w . -
, ,
- , -
MyPrograms, ,


. - a . -

Windows
C:\MyPrograms, r+
w+
Linux \home\MyPrograms. a+ -

b
, ,
. , rb w+b.
1. , -
/.
#include <stdio.h>
2. main, .
newfile.c
int main()
{
FILE *file_ptr;
}
3. main .

file_ptr = fopen("data.txt", "w");
,
4. , -
, 0,
. -
.
if(file_ptr != NULL)
{
printf("File created\n");
fclose(file_ptr);

143
return 0;
}
5. , -
.
,
else -
{ 1
-
printf( "Unable to create file\n" ) ; return 1 ;

} ,
6. , .
, .

10.


scanf(), ,
fscanf(), -
. ,
.
C stdin -
-
scanf(). scanf()
fscanf(stdin, ).


printf()
fprintf(), -
. , -
.
144

stdout -
printf(). printf()
fprintf(stdout, ).
, -
stdin stdout, , -
.
-
stdin.
, stdout:

fputc() -


. fgetc()

.
stderr, -
- fputs() -
. , fgets()
.
fread() -
, fwrite()
.
fscanf() fprintf() -
.
1. , -
/.
#include <stdio.h>
2. main, ,
- . writechars.c
int main()
{
FILE *file_ptr; int i;
char text[50] = { "Text, one character at a time." } ;
}
3. main .
file_ptr = fopen("chars.txt", "w");
4. ,
, ,
0,
.
if(file_ptr != NULL)
{
printf( "File chars.txt created\n" ) ;
for(i = 0; text[i]; i++) { fputc(text[i], file_ptr); }

145
fclose(file_ptr);
return 0;
}
5. , -

.
else fputc()
ASCII-
{
EOF,
printf( "Unable to create file\n" ) ; return 1 ;
,
}
.

6. ,
, .

10.

fgetc()
,
, -
. fgets(),
.
fgets() .
, .
, -
.
, , .
, fputc(), -
, , fputs(),
. fputs() -
, , ,
, .
fputs() , -
. 0 -
EOF, .
146

1. , -
/
.
#include <stdio.h>
lines.c #include <string.h>
2. main,
.
int main()
{
FILE *file_ptr;
char text[50];
}
3. main
.
file_ptr = fopen("farewell.txt", "r+a");
4. ,
.
if(file_ptr != NULL)
{
printf( "File farewell.txt opened\n" ) ;
}
5. if .

while( fgets(text, 50, file_ptr) ) { printf("%s", text); }
,
6. if , strcpy() -
. -
strcpy( text , "...by Lord Alfred Tennyson" ) ;
.
fputs(text, file_ptr);
7. if
0.
fclose(file_ptr);
return 0;
8. ,

147
.
else { printf( "Unable to open file\n" ) ; return 1 ; }
9. ,
,
.

10.


fread()
fwrite(). -
. ,
.

1. -
, -
, , .
fread() , -
, . ,
fwrite() .
1. , -
/.
#include <stdio.h>
2. main,
readwrite.c , .
int main()
148

{
FILE *orig_ptr;
FILE *copy_ptr;
char buffer[1000];
int num;
}
3. main

.
, - orig_ptr = fopen("original.txt", "r");
,
copy_ptr = fopen("copy.txt", "w");

,
.

4. , .
if((orig_ptr != NULL) && (copy_ptr != NULL))
{
}
5. if -
, , -
.
num = fread(buffer, 1, 1000, orig_ptr);
fwrite(buffer, 1, num, copy_ptr);
6. .
fclose(orig_ptr);

fclose(copy_ptr); ,
7. if , fread() -
, ,
0. fwrite(), ,
printf( "Done: original.txt copied to copy.txt" ) ;
printf( "\n%d objects copied.\n" , num ) ; -
.
return 0;
8. , -
.
else
{
if(orig_ptr == NULL) printf( "Unable to open original.txt\n") ;

149
if(copy_ptr == NULL) printf( "Unable to open copy.txt\n" ) ;
return 1;
}
9. , ,

.
,
fread()
,

.

100,

100 . -
70 -
.

10.


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

, fscanf(),
.
, printf(), -
, fprintf(),
stdout. fprintf()
.
fscanf() fprintf() , -
, .
1. , -
/.
150

#include <stdio.h>
2. main, ,
fscanprint.c .
int main()
{
FILE *nums_ptr, *hint_ptr;
int nums[20], i, j;
}
3. main -
.
nums_ptr = fopen("nums.txt", "r");
hint_ptr = fopen("hint.txt", "w");

4. , .
if((nums_ptr != NULL) && (hint_ptr != NULL))
{
}
5. if .
for(i = 0; !feof(nums_ptr); i++)
,
{ feof() -
fscanf(nums_ptr, "%d", &nums[i]);
} , ,
6. if . ,
,
fprintf( stdout , "\nTotal numbers found: %d\n" , i ) ;
.
for (j = 0; j < i; j++) { fprintf(stdout, '%d", nums[j]); }
7. .
fprintf( hint_ptr , "fscanf and fprintf are flexible\n" ) ;
for (j = 0; j < i; j++) { fprintf(hint_ptr, "%d", nums[j]); }
8. if -
.
fclose(nums_ptr);
fclose(hint_ptr);
9. , -
.
else
{

151
fprintf( stdout , "Unable to open a file\n" ) ; return 1 ;
}
10. ,
, , -
. fscanf() fprintf()
-
, scanf()
printf() -
,
.

10.

C perror(),
stdio.h -
. -
, ,
.

errno.h -
errno,
, . -
strerror(),
string.h,
.
1. , -
/,

.
errno.c #include <stdio.h>
#include <errno.h>
152

#include <string.h>
2. main,
.
int main()
{
FILE *f_ptr;
int i;
}
3. main
.
f_ptr = fopen("nosuch.file", "r");
4. ,
.
if( f_ptr != NULL ) { printf( "File opened\n" ) ; }
else { perror( Error ) ; }
5. , -
, .
for(i = 0; i < 44; i++)
{
printf( "Error %d : %s\n" , i , strerror(i) ) ;
}
6. main 0,
.
return 0;
7. ,

, .
-

.
Linux ,
Windows.

153

10.


,

Unix (00:00:00 GMT 1
tm_isdst - 1970 . -
, .
, , -
0 , , time.h tm,
- , .
.


int tm_sec , , 0-59
int tm_min , 0-59
int tm_hour , 0-23
int tm_mday , 1-31
int tm_mon , , 0-11
int tm_year , 1900
int tm_wday , , 0-6
154

int tm_yday , 1 , 0-365


int tm_isdst

time(NULL)
time_t.
-
localtime()
-
tm.

.
asctime().

strftime().
, ,
, -
, ,
tm.
1. , -
/
.
#include <stdio.h>
datetime.c
#include <time.h>
2. main, ,
time_t tm.
int main()
{
char buffer[100];
time_t elapsed;
struct tm *now;
}
3. main ,
Unix.
elapsed = time(NULL);
4. -
tm.
now = localtime(&elapsed);
5. .
printf("%s\n", asctime(now));
6. .

155
strftime ( buffer, 100, "Today is %A, %B %d.\n", now ) ; ,
printf( "%s", buffer ) ; , &,
-
strftime ( buffer, 100, "The time is %I:%M %p.\n", now ) ;
printf( "%s", buffer ) ; , -

7. main 0,
time_t.
, , -
, -
.
return 0;

10.

- -
, ,
. time.h
difftime(), . -
, time_t.
, -
, double.

clock(),
time.h.
, , -
. ,

, , -
.
1. , -
/
.
#include <stdio.h>
156

write.c #include <time.h>


2. , -
.
void wait(int seconds);
3. main,
time_t .
int main()
{
time_t go, stop;
int i;
}
4. main -
.
go = time(NULL);
printf ( "\nStarting countdown...\n\n" ) ;
5. , , -
.
for(i = 10; i > -1; i--)
{
printf(" - %d", i);
wait(1);
}
6. main ,
, .
stop = time(NULL);
printf("\nRuntime: %.0f seconds\n" , difftime( stop, go ) ) ;
7. main 0, -

.
clock ticks -
return 0;
,
8. main - time.h
clock_t . CLOCKS_PER_SEC.
void wait(int seconds)
{
clock_t end_wait = (clock() + (seconds * CLOCKS_PER_SEC));
}

157
9. wait ,
,
, main.
while(clock() < end_wait) {}
10. ,
, .
-
,
, -
,
clock()
CLOCKS_PER_SEC.

10.


stdlib.h rand(), -
.
0 (
32,767). -
. , 0
9, rand() % 9.

, -
. , -
1 10, (rand() % 9) + 1.

, rand(), -
, -
, , -
. ,
, .
, 1, , -
srand() . ,
158

srand(8). , rand() -
, -
.

-
, , srand(),
- , .
,
srand(time(NULL)).

, -
rand(), ,
rand().
1. , -
/,
, .
#include <stdio.h>
lotto.c #include<stdlib.h>
#include <time.h>
#include<string.h>
2. main,
.
int main()
{
int i, r, temp, nums[50];
char buf[4], str[50]= { "Your Six Lucky Numbers Are: " } ;
}
3. main -
.
srand(time(NULL));
4. 0 49 .

for(i = 0; i < 50; i++) { nums[i] = i; }
nums[0]
5. .
0,
for(i = 1; i < 50; i++)
{ ,
r = (rand() % 49) + 1;
1-49.
temp = nums[i]; nums[i] = nums[r]; nums[r] = temp;
}
6. .
for(i = 1; i < 7; i++)
{

159
sprintf(buf, "%d", nums[i]);
strcat(buf, " "); strcat(str, buf);
}
7. .
printf("\n%s\n\n", str);
8. main 0,
.
return 0;
9. ,

, -
1-49 . ,
sprintf() -


, -


.

10.


Windows , -
C, -
, Windows Application
Programming Interface (WINAPI). -
,
Windows windows.h.

, C,
Windows, main() -
, WinMain():
WinMain()
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
, - int nCmdShow)
main().
, -
. hInstance
, , hPrevInstance, -
Windows,
. lpCmdLine , -
160

,
, nCmdShow
.


, .
, MessageBox():

int MessageBox(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType);

hWnd , , -
,
NULL. lpText , -
MB_ , lpCaption
ICONERROR, MB_ICONQUESTION, MB_ . , uType
ICONINFORMATION. , -
, . , -
MB_OK ,
MB_ICONEXCLAMATION.

, C,
,
.
, , -
. Gnu C mwindows,
,
,
.
1. , lotto.c,
winlotto.c.
2.
,
WINAPI. winlotto.c

#include <windows.h>
3. int main() ,
Windows.
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR
lpCmdLine, int nCmdShow)
4. printf(),
, ,
.
MessageBox(NULL, str, "Lotto Number Picker", MB_OK | MB_ICONEXCLAMATION);
5. , -

161
,
.

10.

FILE *fp -
, , .
fopen(), , -
.
-
fclose().

fgetc() fputs(), fgets() fputs()
.
fread() fwrite()
.
, fscanf() fprintf(),
,
stdin stdout.

perror(), strerror()
errno.
162

time(NULL) , -
1 1970 .
tm struct -
localtime().

asctime(),
strftime().
-
, difftime() clock().
,
rand(),
srand() time(NULL).
windows.h -
Windows API, -
.
Windows WinMain(),
MessageBox() .
+

ASCII-

<stdio.h>
ASCII-
, <ctype.h>

<math.h>

C, <string.h>
<stdlib.h>
,
,
<assert.h>
<stdarg.h>

<time.h>
.
<setjmp.h>
<signal.h>
<limits.h>
<float.h>

ASCII-

ASCII ( American Standard Code for Information


Interchange, ) -
.
, -
, .

ASCII-, -
128255 ( ), -
, .

, , 32, -
. ,
127, delete.


0 NUL null 16 DLE
1 SOH 17 DC1
2 STX 18 DC2
164

3 ETX 19 DC3
4 EOT 20 DC4
5 ENQ 21 NAK

6 ACK 22 SYN
7 BEL , 23 ETB

8 BS 24 CAN

9 TAB 25 EM

10 NL 26 SUB
11 VT 27 ESC

12 FF , 28 FS

13 CR 29 GS
14 SO 30 RS

15 SI - 31 US


-
32 56 8 80 P 104 h
ASCII-
-
33 ! 57 9 81 Q 105 i
www.asciitable.com
34 " 58 : 82 R 106 j
ASCII .
35 # 59 ; 83 S 107 k

36 $ 60 < 84 T 108 l

37 % 61 = 85 U 109 m

38 & 62 > 86 V 110 n

39 ' 63 ? 87 W 111 o

40 ( 64 @ 88 X 112 p

41 ) 65 A 89 Y 113 q

42 * 66 B 90 Z 114 r

165
43 + 67 C 91 [ 115 s

44 , 68 D 92 \ 116 t

45 - 69 E 93 ] 117 u

46 . 70 F 94 ^ 118 v

47 / 71 G 95 _ 119 w

48 0 72 H 96 ` 120 x

49 1 73 I 97 a 121 y

50 2 74 J 98 b 122 z

51 3 75 K 99 c 123 {

52 4 76 L 100 d 124 |
ASCII- -
53 5 77 M 101 e 125 } ,
,
54 6 78 N 102 f 126 ~
-
7 O g del (Notepad)
55 79 103 127
Windows.

stdio.h

, stdio.h -
C.
.

,
\n. -
.
FILE, ,
.

, ,
.


166

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


fopen() FILE NULL,
.
:
r ,
w -
,
a
,
r+ (
),
w+ (
, ),
a+ ,
.
,
b. , wb+.
FILE freopen(const char *filename, const char mode, FILE *stream)
freopen() -
. NULL,
. -
, stdin, stdout stderr.
stdio.h
int fflush(FILE *stream)
, -
, , .
EOF, -
NULL. fflush(NULL) -
.
int fclose(FILE *stream)
fclose()
, . -
EOF , , 0.
int remove(const char *filename)
-
. -
, .
int rename(const char *old-name, const char *new-name)
rename()
, .
FILE *tmpfile(void)
tmpfile() , -
wb+, -
. NULL,

167
.
char *tmpnam(char arr[L_tmpnam])
, , -
,
. arr L-tmpnam -
. tmpnam() .
int setvbuff(FILE *stream, char *buffer, int mode, size_t size)
setvbuff() -
. ,
, ,
.
_IOFBF ( ), _IOLBF (-
) _IONBF ( -
). size . -
, .
void setbuf(FILE *stream, char *buffer)
setbuf() , .
, ,
, .
buffer ,
.

stdio.h

int fprintf(FILE *stream, const char *format, )
fprintf()
.
-
, .
int printf(const char *format, )
printf() stdout.
fprintf(stdout, const char *format).

int sprintf(char *s, const char *format)


sprintf() printf() ,
, -
\0.
vprintf(const char *format va_list arg)
vfprintf(FILE *stream, const char *format, va_list arg)
vsprintf(char *s, const char *format, va_list arg)
printf()
, -
168

va_list type.
stdarg.h ( 180) -
.

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


fscanf()
-
.
EOF
.
int scanf(const char *format, )
scanf()
stdin, fscanf(stdin, const char *format)
int sscanf(char *s, const char *format, )
sscanf() scanf() ,
.
stdio.h

C %
. -
printf(), scanf().

printf()
d, i ,

O , -

x, X , -
. 0
(, 0xff), 0X -
(, 0XFF)
u ,

c , -
char
s

169
,
\0
f double xxx.yyyyyy,
-
.

e, E double xx.yyyyyyezz
xx.yyyyyyEzz,
. -

g, G double, %e, %E
%f,
p
n , -
,
printf()
% , %

stdio.h

scanf()
d ,

i , -
( 0) -
( 0 0)
o , , -
0
u ,

x , ,
0
c , -
. ,
( 1),
\0 . , -

s , , -
170

.
, -
\0
e, f, g ,
. -
. ,
("" ""),
,
p , ,
printf() -
%p
n .
, scanf(),

[] , -
, \0
[^] ASCII ,
, ,
\0
stdio.h

int fgetc(FILE *stream)


-
char EOF,

char *fgets(char *s, int n, FILE *stream)
n-1 ,
\0 .
s NULL,

int fputc(int c, FILE *stream)
c
EOF,
int fputs(const char *s, FILE *stream)
s -
EOF,
int getc(FILE *stream)
getc() fgetc()
int getchar(void)

171
getchar() getc(stdin)
char *gets(char *s)
,
\0.
s NULL,

int putc(int c, FILE *stream)
putc() fputc()
int putchar(int c) putc(c, stdout)
int puts(const char *s)
s stdout.
EOF,

int ungetc(int c, FILE *stream)
c ,
.
, EOF .

EOF,

stdio.h

fread() fwrite(), stdio.h,
:

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


fread() -
ptr. nobj -
size. ,
.
fread()
feof() ferror() -

size_t fwrite(const void *ptr, size_t, size, size_t nobj, FILE *stream)
fwrite() nobj size
ptr.
. ,
, nobj.


172

-
C ,
.
. -
errno, errno.h, -
, -
, .

void clearer(FILE *stream)


clearer()

int feof(FILE *stream)
foef() , -

int ferror(FILE *stream)
ferror() , -

void perror(const char *s)
perror()
, , -
errno.
strerror(), string.h
stdio.h

. ,
, -
.

int fseek(FILE *stream, long offset, int original)


fseek() .

. ,
(offset)
(original).
SEEK_SET ( ), SEEK_CUR ( ) SEEK_END (
).
, , ftell(),
SEEK_SET.
fseek() ,

long ftell(FILE *stream)


ftell()
-1,

173
int fgetpost(FILE *stream, fpos_t *ptr)
fgetpos()
ptr,
fpos_t. , -

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

ctype.h


ctype.h .
.
(true),
, 0 (false) . -
, ,
.
ctype.h, , :


isalpha(c) ?
isalnum(c) ?
iscntrl(c) ?
isdigit(c) ?
isgraph(c) (
)?
islower(c) ?
isprintf(c) (
174

)?
ispunct(c) (
, )?
isspace(c)
, , ,
?
isupper(c) ?
isxdigit(c) ?
int tolower(int c)
int toupper(int c)
math.h

math.h , -
.
. x y double, -
n int. double:


sin(x) x
cos(x) x
tan(x) x
asin(x) x
acos(x) x
atan(x) x
atan2(y, x) ( ) x
y
sinh(x)
x
cosh(x)
x

175
tanh(x)
x
exp(x) (
)
log(x)

log10(x)
pow(x, y) x y
sqrt(x) x
ceil(x) ,
, double
floor(x) ,
, double
fabs(x)
ldexp(x, n) , 2
n
frexp(x, int *exp)
( 0.5 1)
exp
modf(x, double *ip) x -
, ip
fmod(x, y) x y

string.h


string.h ,
-
:


char *strcpy(s1, s2) s1 s2, -
s1
char *strncpy(s1, s2, n) n s1 s2,
s1
char *strcat(s1, s2) s2 s1,
s1
char *strncat(s1, s2, n) n s2 -
s1, s1
char *strncmp(s1, s2) s1 s2,
<0, s1 < s2
0, s1 == s2
176

>0, s1 > s2
char *strncmp(s1, s2, n) n s1 s2,
<0, s1 < s2
0, s1 == s2
>0, s1 > s2
char *strchr(s, c)
c s NULL,

char *strrchr(s, c) -
c s NULL,

size_t strspn(s1, s2) s1, -
s2
size_t strcspn(s1, s2) s1, -
, s2
char *strpbrk(s1, s2)
s2 s1
NULL,
char *strstr(s1, s2)
s2 s1 NULL,

string.h

size_t strlen(s) s
char *strerror(n) -
,
n
char *strok(s1, s2) s1 ,
, s2
void *memcpy(s1, s2, n) n s2 s1,
s1
void *memmove(s1, s2, n) memcpy(), -
,
int memcmp(s1, s2, n) n s1 s2,
, -
, strcmp()
void *memchr(s, c, n) -
c s NULL,
n

void *memset(s, c, n) n s -
c

177

stdlib.h


double atof(const char *s) s double
int atoi(const char *s) s int
long atol(const char *s) s double
double strtod(const char *s, char **endp)
s
double, .
*endp.
long strtol(const char *s, char **endp, int b)
s
long b, .
*endp.
unsigned long strtoul(const char *s, char **endp, int b)
strtol(), ,
unsigned long
int rand(void)
,
178

0 ( 32.767)
void srand(unsigned int seed)

, rand(). 1
void *calloc(size_t nobj, size_t size)
,
nobj, size, NULL,
.

void *malloc(size_t size)
, -
, size, NULL,
. -

void *realloc(void *p, size_t size)
, p,
, size.
NULL, -

void *free(void *p) stdlib.h
free() ,
p. , p
, calloc(), malloc()
realloc().
void abort(void)
void exit(int status)
.
status . -
status EXIT_SUCCESS EXIT_FAILURE
int atexit(voif (*fcn) (void))
, fcn
. atexit() ,

int system(const char *s)
s . -

char *getenv(const char name)
name NULL,
, name . -

179
void *bsearch(const void *key, const void *base, size_t n, size_t size, int
(*cmp), (const void *keyval, const void *datum))
, key,
base[0]base[n-1].
. ,
NULL. base -

void qsort(void *base, size_t n, size_t size, int (*cmp) (const void*, const void
*))
base[0]base[n-1] -
, size, . -
cmp() bsearch()
int abs(int n) int n
long labs(long n) long n
div_t ldiv(long num, long demon)
num denom
, ldiv_t. quot
, rem

assert.h

assert(), assert.h, -
:

void assert(int expression);

expression
assert(expression), stderr,
, :

: , ,

assert()

stdarg.h


stdarg.h , -
, -
180

.
.

,
va_list. , , -
, args va_list:

va_start(va_list args, lastarg)


, -
args, va_list,
lastarg.
va_arg(va_list args, data-type)
, va_list args
va_start,
va_arg() args
data-type
va_end(va_list args)
,
va_list args, ,

time.h


time.h -
. , -
. -
, UNIX (00:00:00 GMT 1
1970 ).

, time.h,
,
.

time_t ,
.

tm ,
:


int tm_sec , 0-60

181
int tm_min , 0-61
int tm_hour , 0-23
int tm_mday , 1-31
int tm_mon , 0-11
int tm_year 1900
int tm_wday , 0-6
int tm_yday 1 , 0-365
int tm_isdst

tm_isdst , ,
, , , -
.

, time.h,
, .

time.h time.h , , -
:

clock_t clock(void)
,
, -1,
time_t time(time_t *tp)
-1,

double difftime(time_t time2, time_t time1)
time2 time1,
time_t mktime(struct tm *tp)
, *tp, -

char *asctime(const struct tm *tp)
, *tp,

char *ctime(const time_t *tp)

struct tm *gmtime(const time_t *tp)
-
182

(UTC GMT)
struct tm *localtime(const time_t *tp)
, *tp,

size_t strftime(char *s, size_t smax, const char *fmt, const struct tm *tp)
, *tp,
*fmt
strftime() tm time.h
.
,
.


%a
%A
%b
%B
%c
%d (01-31)
%H (00-23)
%I (01-12)
%j (001-366)
%m (01-12)
%M (00-59)
%p
%S (00-61, )
%U (
, 00-53)

183
%w (0-6, 0 )
%W (
, 00-53)
%x
%X
%y (00-99)
%Y
%Z ( )

setjmp.h

setjmp.h -
-
.

int setjmp(jmp_buf env)


,
env, 0.
void longjmp(jmp_buf env, int value)
,
env setjmp(), setjmp()
value()

signal.h

signal.h ,
,
:
184

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


signal() ,
. SIG_DFL
, , SIG_IGN,
. sig
:
SIGABRT:
SIGFPE:
SIGILL:
SIGINT:
SIGSEGV: -

SIGTERM:
-
SIG_ERR, . sig
, -
, .
,
,
int raise(int sig)
sig -
,
limits.h

, -
. -
.
, -
, .
, - , -
, .


CHAR_BIT char (8)
CHAR_MAX
char (UCHAR_MAX SCHAR_MAX)
CHAR_MIN
char (0 SCHAR_MIN)
INT_MAX
int (+32,767)
INT_MIN
int (-32,767)

185
LONG_MAX
long (+2,147,483,647)
LONG_MIN
long (-2,147,483,647)
SCHAR_MAX
signed char (+127)
SCHAR_MIN
signed char (-127)
SHRT_MAX
short (+32,767)
SHRT_MIN
short (-32,767)
UCHAR_MAX
unsigned char (+255)
UINT_MAX
unsigned int (+65,535)
ULONG_MAX
unsigned long (+4,294,967,295)
USHRT_MAX
unsigned short (+65,535)

float.h


, -
. -
. , ,
.


FLT_RADIX -
(2)
FLT_ROUNDS
FLT_DIG (5)
FLT_EPSILON , 1.0 + != 1.0 (1-5)
FLT_MANT_DIG FLT_RADIX
FLT_MAX
(1+37)
FLT_MAX_EXP n, -
FLT_RADIXn - 1
186

FLT_MIN
(1-37)
FLT_MIN_EXP n, -
10n
DBL_DIG double (10)
DBL_EPSILON , 1.0 + !=
1.0 (1-9)
DBL_MANT_DIG FLT_RADIX
DBL_MAX double (1E + 37)
DBL_MAX_EXP n, -
FLT_RADIXn - 1
DBL_MIN double (1-37)
DBL_MIN_EXP n, -
10n
-

32 , , -
C.
, C,
.

auto break case char


const continue default do
double else enum extern
float for goto if
int long register return
short signed sizeof static
struct switch typedef union
unsigned void volatile while

14 -
, ,

187
C, , -
.

- -

\n \\
\t \?

\v \'

\b \
\r \xhh

\f \000

\a \0 -

ANSI, 10 stdarg.h, 11, 180


ASCII-, 34, 164 stdio.h, 11, 142, 166
L-, 130 stdlib.h, 11, 122, 138, 158, 178
string.h, 11, 114, 152, 175
R-, 130 time.h, 11, 154, 156, 181
, 100 , 94
, 90
, signed, 28
34 NULL, 118, 138, 142
, 54 Windows Application Program-
+, /, -, *, %, 54 ming Interface (WINAPI), 160
: , , 78
70
, 112
, 8 , 66 , 154
C, 11
auto, 30
&, |, ~, ^, <<, >>, 66 break, 76, 82
: , 69 case, 76
, 26 continue, 83
else, 74
, 88 goto, 84
188

extern, 30 static, 30, 96


return, 15 union, 136
, 19 void, 88
, 32 volatile, 32
, 138 , 23
, 16, 18
, 30
GNU C Compiler (GCC), 12
, 12
tm, 154 -mwindows, 160
, 66 -o, 16
, 160 -save-temps, 19
, 14 , 18
#dene, 48 EOF, 145, 146
#elif, 50
#else, 50 const, 42
#endif, 48 , 185
#if, 50 , 186
#ifdef, 48 :
#ifndef, 50 , 42
#include, 14, 18, 94 :
#undef, 50 enum, 44
, 146 ( ), 14, 34, 64, 108
, 11 General Public License (GPL), 12
assert.h, 11, 180
ctype.h, 11, 120, 174 true (1), false (0), 60
errno.h, 152
oat.h, 11, 186 &&, ||, !, 60
limits.h, 11, 28, 185 false (0), 60
setjmp.h, 11, 184 true (1), 60
signal.h, 11, 184 , 30
, 48 /
, 62
, 38 , 63
, 36 Hello World, 14
, 106 , 64
, 36
, 112 register, 32
, 36 , 100
: , 36 , 92
, 38 , 18
(Most Significant
Bit, MSB), 66 *, 43
(Least Significant , 146
Bit, LSB), 66 - \0, 36, 112
, 30 #, 14
.o, 18 , 158
, 54 ,
22
sizeof, 64 stderr, 144
+, -, *, /, %, 54
&, |, ~, ^, <<, >>, 66 %d, %f, %c, %s, %p, 24
&&, ||, !, 60 long, short, unsigned, 28
, 169
, 70
, 168
=, +=, -=, *=, /=, %=, 56
, 24
==, !=, >, <, >=, <=, 58
string %s, 24
? :, 62
%p, 24

%c, 24
typedef, 46, 128

189

, 38
%f, 24
, 50
%d, 24
-
, 152 , 183
Minimalist GNU for Windows C, 11
(MinGW), 12
, 26 , 120
, 114
, , 90, 134 " ", 15
, 22 (), 116
: , 23 , 114
Linux , 118
windows.h, 160 , 122
linux, 48 , 118
_WIN32, 48 , 36, 106
Windows , 112
_msize(), 138
MessageBox(), 160 tm, 154
WinMain(), 160 tm: , 181
, 46 , 128
, 54 struct, 64, 126
() , 134
, 18 , 156
, 126
, 34 ,
, 54 128
, 34 :
, 70 , 18
, 70 ?:, 62
char stdout (), 144,
char, 23 150
double , 144
double, 23 :
do while, 80 , 150
do, while, 78 .s, 18
enum { }, 14
enum, 46 , 68
FILE, 142
oat , 90
oat, 23 abort(), 179
int abs(), 179
int, 15, 23 acos(), 176
time_t, 154, 156 asctime(), 154, 182
asin(), 176
char, int, oat, double, 23 assert(), 180
int, 15 atan(), 176
- atan2(), 176
, 14 atexit(), 179
< >, 14 atof(), 178
atoi(), 122, 178
, 104 atol(), 178
, 102 bsearch(), 179
, 130 calloc(), 138, 178
, 106 ceil(), 177
, 136 clearerr(), 172
, 132 clock(), 156, 182
, 108 cos(), 176
190

->, 132 cosh(), 176


, 100 ctime(), 182
, 130 difftime(), 156, 182
, 60 exit(), 179
\n , 15 exp(), 176
\t fabs(), 177
( ), 29 fclose(), 142, 165
?:, 62 feof(), 151, 171, 172
ferror(), 171, 172
case, break, default, 76 fush(), 166
if, else, 74 fgetc(), 144, 170
switch, 76 fgetpos(), 173
default fgets(), 144, 146, 170
default, 76 oor(), 177
if else fmod(), 177
if, 74 fopen(), 142, 166
switch fprintf(), 144, 150, 168
switch, 76 fputc(), 144, 170
, 142 fputs(), 144, 146, 170
, , , , fread(), 144, 148, 171
142 free(), 138, 178
r. w, a, r+. w+, a+, 142 freopen(), 166
, 142 frexp(), 177
fscanf(), 144, 150, 168
, 144, 148 fseek(), 173
, 144 fsetpos(), 173
, 144, 146 ftell(), 173
stdin (), 144, fwrite(), 144, 148, 171
150 getc(), 170
getchar(), 170 sscanf(), 168
getenv(), 179 strcat(), 116, 175
gets(), 112, 170 strchr(), 119, 175
gmtime(), 182 strcmp(), 118, 175
isalnum(), 174 strcpy(), 114, 175
isalpha(), 120, 174 strcspn(), 175
iscntrl(), 174 strerror(), 152, 176
isdigit(), 120, 174 strftime(), 154, 182
isgraph(), 174 strlen(), 114, 176
islower(), 120, 174 strncat(), 116, 175
isprint(), 174 strncmp(), 175
ispunc(), 120 strncpy(), 114, 175
ispunct(), 174 strpbrk(), 176
isspace(), 120, 174 strrchr(), 119, 175
isupper(), 120, 174 strspn(), 175
isxdigit(), 174 strstr(), 118, 176
itoa(), 122 strtod(), 178
labs(), 179 strtok(), 176
ldexp(), 177 strtol(), 178
ldiv(), 179 strtoul(), 178
localtime(), 154, 182 system(), 179
log(), 176 tan(), 176
log10(), 176 tanh(), 176
longjmp(), 184 time(), 182
main(), 14 time(NULL), 154
malloc(), 138, 178 tmple(), 167
malloc_usable_size(), 138 tmpnam(), 167

191
memchr(), 175 tolower(), 120, 174
memcmp(), 175 toupper(), 120, 174
memcpy(), 175 ungetc(), 170
memmove(), 175 va_arg(), 180
memset(), 175 va_end(), 180
mktime(), 182 va_start(), 180
modf(), 177 vprintf(), vfprintf(), vsprintf(), 168
perror(), 152, 172 , 14, 90
pow(), 176 , 94
printf(), 15, 24, 168 static, 96
putc(), 170 , 88
putchar(), 170 , 88
puts(), 112, 170 , 92
qsort(), 179 , 14
raise(), 184 , 108
rand(), 158, 178 abs(), 179
realloc(), 138, 178 [], 30
remove(), 167 , 96
rename(), 167
scanf(), 26, 112, 168 for
setbuf(), 167 for, 78
setjmp(), 184 while, 78, 80
setvbuf(), 167 , 79
signal(), 184
sin(), 176 for, while, do while, 78
sinh(), 176 break, 82
sprintf(), 122, 168 continue, 83
sqrt(), 176 goto, 84
srand(), 158, 178 Unix, 154