You are on page 1of 121

.

,
.

1983. ,
BCPL. H . .
. ,
C. , ,
. , . , , , ,
C. ... !
, , C .

, C.


""
. ,

.

1 .
2 "" .
, -

20

1:

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

C
(modularity) ,
""
. C ()
. (function)
(local variables), ""
.

C :




C . , , ,
.
100
105!!!
, .

C
( ),
, (interpreter's),
(compiler's) .
23

BASIC ..
. C .
(compiler).
(source code)

(
).
(compile time),
.
,
(executable code),
, /.
,
,
.

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

, , (RAM) /.
.
24





.

.

.
.

1.1


,
, .

32

,


. .

1:


,

.
0.

1.2



100 .

 C .
 C "" ,
.

.
 C : int, char
float.

 ,
.
 .
 , , .
 .

33

"" C.
,
.
.

C,
"Hello world".
#include <stdio.h>
main()
{
printf("Hello world");
}

, main() Hello world.

- .

#include <stdio.h>
C
stdio.h . / C. / (.. printf () ), 3 (#include) .
main()
main() C.
,

38

(IDE) .
.

2: C

main().
, main() .
{


main().

printf("Hello world");
printf()
. "Hello
world".


main().

, ,
#include <stdio.h>
main(). ,
main().

, C. , , :
#include <stdio.h> main() {
printf("Hello world");}

C 3 :
main():
. main(). main(), C,
:
main()
{

39

2: C

a = rand() a.

rand(); rand() , .
. ,
C:
printf("T")
5 (
).

 10 ( 5+5) a 10.
 T 14 ( 10+4) b.

14, .

4 ( 91).

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

.
 C char, int, float.

57


.


( 1) ( 0).
 =
, .. a=b+3

.

2
2.1

a, b, c
:
main()
{
int a,b,c=3;
a=b=2;
a=c+b;
}

2.2

58

a, b, c
:
#define MM 23
main()
{
const int c=3;
int a,b;
a=4+(b=2);
b=c+b+MM;
}

C
, C
: int, float, char.
.
RAM /, RAM
1 byte.

/; .
(byte), / .

/
32bit
INTEL, int bytes, float , char byte.
(
) byte.

,

:
char a,gramma;
float pososto;
int b;

62

3:

( ) .
byte ( ) "" .
, . a
( 1), gramma ( 8),
pososto ( 3,4,5, 6), b ( 350000,
350001, 350002, 350003).
,
, :
.
byte (
) .

, :
a
b
pososto
gramma

000001
 350000
 000003
 000008


bytes .

C ,
.

&

& , . , , &a 1 &gramma 8.


, / 32bit INTEL, 4 bytes .

63

3:

scanf()

C ,
. .

scanf()
.
, . scanf()
printf():
scanf(" ", 1, 2, 3,....., N); ,

(format string)
. :

 , (1~N).
%c
%d
%f
.
 . scanf()

, . , :
"%d %d"
, ( ).
 . scanf()
. , scanf() . , :
"%d,%d"

67

. 10,
" ##", ""
( ## ).

3.1

#include <stdio.h>
main()
{
int b1,b2,b3;
float mo;
printf(" :");
scanf("%d %d %d",&b1,&b2,&b3);
mo=(b1+b2+b3)/3.0;
if(mo>=10)
printf(" %f\n",mo);
else
printf("\n");
}

3.2

72

3_p3_1.c

scanf()
b1,b2 b3.
.

:
main()
{
int a,b,c;
float d;
scanf("%d %d",a,b);
int k;
scanf("%c",&c);
if(c==1)
printf("NAI\n");
else
printf("OXI\n");
}

scanf() .
&a,&b.

, %d.

3:

3
,
, . 

3.1

3.2

; 
main()
{
int a=4,b=5;
char ch;
ch='A';
printf("%d %d %c",a,b,ch);
printf("%d %d %d\n",a,b,ch);
printf("%d\n%d \n%c\n",a,b,ch);
printf("\n");
}

3.3

3.4

3
, ,
. : 6
: 2
: 10
6,2,10 18
6,2,10 120
6,2,10 6
.

; 

main()
{
int a,b;
float f;
char ch;
printf("%d %d %d\n",sizeof a,sizeof f,sizeof ch);
75


int

int


(integer) C. , .
.

int

.
.
int 4 bytes.

4 bytes -2.147.483.648
+2.147.483.647. bit ' 31 ( 32 bits 0
31) 31
.

int

C , int , : long int short int.


int :
int _1, _2, ....;
long int :
long int _1, _2, ....;

80

4: int

long _1, _2, ....;


int ,
.

short int :

short int _1, _2, ....;


short _1, _2, ....;

int long int 4 bytes5 (32 bits)


-2.147.483.648 +2.147.483.647.
short int 2 bytes (16 bits) -32.768 +32.767.

(unsigned)

int (int, long int short int)


, unsigned
, :
unsigned int _1, _2, ....;

unsigned long int _1, _2, ....;


unsigned short int _1, _2, ....;

unsigned bytes
. ,
bit ,
. , unsigned
, (
).


, . , :
5

16bit, int 2 bytes, long int 4.

81

int a=4,b=8;

a b,
4 8 .

"" , C
.

C.


++ -- C.

++ --

.
..

++a;

a++;

--a;

+
*
/

++
--

1
1

a--;

++ 1,
-- 1.
( ).

main()
{
int a,b;
b=a=5;
++a;
--b;
printf("a=%d b=%d\n",a,b);
}

++a a 1
(6) --b b
1 (4). printf()
: a=6 b=4

, ++ -- . (
).

82


char

char


(characters) C. C
char. , .

char

char . , :
''
'1'
'*'

char.

!!!

( )
char . ASCII7 . , :
printf("%c %d\n",'A','A');

65 ( ASCII ).

C .
.

, :
5 + '*'  47 (5 + 42 ASCII '*').

printf("%d %c\n",'A'+1,'A'+1);  66 .
7

.
ASCII .

100

5: char

66 ''+1 %d "" printf() .


%c "" printf()
''+1 ( 66) . , ASCII 66.

(escape characters) (escape sequences). , .


C :
\n

new line ( )

\b

backspace ( )

\f

form feed ( )

\r

carriage return ( )

\t

tab ( ))

\\

backslash (o " ", \)

\'

(')

\"

(")

\0

null character (byte 00000000)

,
printf()
:

" "
9

printf("\n\" \"\n 9\n");


(")
printf(), .
\".
101

, tab,
(whitespace characters).
, .

char

char :

char _1, _2, ....;

char 1 byte (8 bits). ASCII , .


C , .

, :
int a,b;
char ch;
ch='A';
a=ch+2;

// ch ASCII '' 65

a 67 (65+2).

( ) ( ) getch(), getchar() putch().


getch()
getch() .
<Enter>. -

102


float

float


(floating point) C. C float.
( ).

float

float .
float,
(.), :
123.45
123.0
123.
0.1

float.

float

(floating point), float double. float 4


bytes, double 8. double
.
float :

float _1, _2, ....;

double _1, _2, ....;


long float _1, _2, ....;
long float double.

116

6: float

(floating point)

int
*

char.
/

float ( double) .

%, ++ -- float. C ( gcc DEV C++) float


++ --.

printf()


:
21.234
5467.1

main()
{
float a,b;
a=21.234;
b=5467.1;
printf("%f\n",a);
printf("%f\n",b);
}

,
,
...
() , %f printf() :
%n.mf

117

n (
) m .
, %8.3f 8
3 .
, :

main()
{
float a,b;
a=21.234;
b=5467.1;
printf("%8.3f\n",a);
printf("%8.3f\n",b);
}

21.234
5467.100

scanf() float

scanf() . , .

6_float_scanf.c
#include <stdio.h>
main()
{
float a;
double b;
printf(" :");
scanf("%f",&a); // float a
printf(" :");
scanf("%lf",&b); // double b
printf(" %f %f\n",a,b);
}

(
double), scanf(), %lf %f.

118

H C (" "). if switch-case.

if

if
( ), ( ) .
if :

 if
 if - else
 if - else if

H if

To if :
if ( ) -A;

if , (), - .
(),
if.

C
: 1 0 .

, C
( 7.3).

if. - (compound statement).


if:

124

7:

if(a==5) printf("a=5\n");

if(a==5)
printf("a=5\n");

if(a==5)
{
printf("a=5\n");
}

, - :
if(a==5)
{
printf("a=5\n");
printf(" \n");
}

if-else

if ( ) -; else -;

, if , (),
- . (), -.
if .
if-else.
.
if-else:

if(a==5) printf(""); else printf("");


if(a==5)
printf("");

125

else
printf("");
if(a==5)
{
printf("\n");
printf(" - A\n");
}
else
{
printf("\n");
printf(" - B\n");
}

if - else if

else
if.

if-else if
. - . ..:

if(_-1)
{
-A1;
}
else if(_-2)
{
-A2;
}
...
else if(_-)
{
-A;
}

126

_-1

_-2

_-3


"" goto

C, ,
goto . C ,
(labels) "" .
(label)
(:), :
telos:


.
goto (
).

goto :

goto ;

1
1.

main()
{
1
int a;
2
3
a=0;
4
pali:
printf("%d\n",++a); // a 1
goto pali;
// pali
}

142

8:

pali (;). , (;). goto


(goto pali;)

main()
{
int a;
a=0;
pali: printf("%d\n",++a);
goto pali;
}

goto .
goto, .
.
C
goto.

while

while C.
while .
. , while a 0.
a=5;
while (a>0)
{
printf("a=%d\n",a);
--a;
}

a 0. ,
.

143

( ) while (
) .
while :

while (_)
;

while (_) ;

while (_)
{
1;
2;
}
while.


C ,
.
0,
.

while " C ",


1 .
''. .
while (1) printf(" C \n");

, a,
. ,
"".
144

8:

8_while1.c

a=5
a=4
a=3
a=2
a=1

#include <stdio.h>
main()
{
int a,b;
a=5;
while (a>0)
{
printf("a=%d\n",a);
--a;
}
printf("\n");
}


(ASCII 32).

#include <stdio.h>
main()
{
char ch;
ch='';
while (ch!=32) ch=getch();
printf(" \n");
}

8_while2.c

while
. C, .

#include <stdio.h>
main()
{
char ch='';
while ((ch=getch()) != 32);
printf(" \n");
}

8_while3.c

: while
.

145

continue

continue break.
( break), . .
// while
while (1)
{
ch=getch();
if (ch==27) continue;
putch(ch);
.............
}
printf(" while\n");

// do-while
do
{
ch=getch();
if (ch==27) continue;
putch(ch);
.............
}
while (1);
printf(" do-while\n");
// for
for (i=1;i<=100;++i)
{
ch=getch();
if (ch==27) continue;
putch(ch);
.............
}
printf(" for\n");

156

continue

( ). (
)
continue .

while (1)
{
ch=getch();
if (ch==27) goto pali;
putch(ch);
pali:
}
printf(" while\n");

,
Esc (ASCII 27), continue

.

8:


goto .



( 10),
.
0, , 1.
,
0, , .
-
.
cnt sum. ,
scanf() a.

#include <stdio.h>
main()
{
int a,cnt,sum;
cnt=sum=0;
while (cnt<12)
{
printf(" :");
scanf("%d",&a);
cnt++;
sum=sum+a;
}
printf("=%d\n",sum);
}

8_sum.c

cnt
sum 0.
12 ,
.


a.
(a),
cnt 1.
(a) sum.

.

157

C.
main(),
C. : printf(), scanf(), getch() ..
""
, .
, "" .
, C
:

main()
{
main();
main();
}

function-1()
{
function-1();
function-1();
}
function-2()
{
function-2();
function-2();
}
function-N()
{
function-N();

172

9:

function-N();
}

. main () .

C ,
. (int, float ..) .
,
,
(void).
, "" .
C .
, .

_ _ ( 1, 2, ...)

, void.
, C
int.

173

( ) , ,
.



.

.



,

.

, 5 10. void.
, ,
.

void display_numbers()
{
int i;
for (i=5;i<=10;i++) printf("%d\n",i);
}

main()
{
printf(" \n");
display_numbers();

174


5, 6, 7,
8, 9, 10.

9:

printf(" \n");
display_numbers();
}

main() display_numbers() . . A
.


.
. . ,
.


func(1,2)
.

func(), func(1,2).
(), .


display_numbers() ,
.

175

9:

, ,
.

int add(int x, int y)


{
............
............
}

,
, :
int add(x,y)
int x,y;
{
............
............
}

.
.

add()
main().
main()
{
int a,b,c;

183

.
.

a=10;
b=20;
c=add(a,b);
printf("%d\n",c);
}

int add(int x, int y)


{
int ss;
ss=x+y;
return(ss);
}

.
.

add()
x y.
,
( 10
20) x y .
, .


' (call by
value),
.



( x y) (formal
parameters) .

184

9:

,
, .

9.1

int reverse(int ar)


{
int y,p,cnt=0;
do
{
y=ar % 10;
p=ar/10;
printf("%d",y);
cnt++;
ar=p;
} while (p!=0);
return cnt;
}

9.2

9_p9_1.c


8.5
:
10.
.

0.
cnt "" .
cnt.

1 , 2 , 3 (0~9), 0 .
int isgreek(char ch)
{
int ap;
if (ch>='A' && ch<='') // ''=
ap=1;
else if (ch>='' && ch<='')
ap=2;
else if (ch>='0' && ch<='9')
ap=3;
else
ap=0;
return ap;
}

9_p9_2.c

191

9.9

() /2, .
:

18.5
18.5 25
25 30
30


,
(.. ). 0 .
. . 

9.10 f(x)=x4-10x2+2,
x 0 1 0.05. x,
f(x) . f(x) . 

9.11 :

198

1
2
3
4


0.00
0.06
0.13
0.19

,
10 .

.

. 

C . .

.

,
(local), (global), (static).

(local variables)

, ({).
, ,
. .
, ( ), (local variables)
, .

.
#include <stdio.h>

main()
{
int a,b,sum;
printf(" :");
scanf("%d %d",&a,&b);
sum=add(a,b);
printf("=%d\n",sum);
}

200


main().

10:

add(), ss
add().

x y
.

int add(int x, int y)


{
int ss;
ss=x+y;
return(ss);
}


.
ss.

add()
ss.

,
.

""
, .
, .
#include <stdio.h>

main()
{
int sum;
float a,b;
printf(" :");
scanf("%f %f,&a,&b);
sum=add(a,b);
printf("ss=%d\n",ss);
printf("=%d\n",sum);
}


add() int float.
main() ss
add().

201

(global variables)

C
. (global variable).

.

, a b .
int a,b;
main()
{
a=8;
b=10;
printf("=%d\n",add(a,b));
}

a b main().

x y.

a b add().

int add(int x, int y)


{
int ss;
ss=x+y;
printf("a=%d b=%d\n",a,b);
return(ss);
}

a b.

add()
{
int ss;
ss=a+b;
return(ss);
}

204



a b add() ,
a b.

10:

. ,
(
main().


,
.

10.1

205

(pointers)
"" .

,
.
, ,
.

,
, .
.
. (1,2,3 ....)
.

()
, .. 5
1 ( / 5).
( / 5). 1.


(1). , ,
1. , ,
:
 1 ( ) .

220

11:

 ,
/ 5 .
.
- / .
-
, / .

.
C. H
, ,
C.

(Pointer variables)


.

.


. b
a.


,
*. , :
int a,*b;

221

...

 ,
.

.
 & .
 *
.

, .


ptr,
a. a 6000.
int a,*ptr;

ptr 6000

ptr=ptr+1;

ptr 6004

++ptr;

ptr 6008

ptr=ptr+3;

ptr 6020

ptr=&a;

ptr 1, 4 3 12!
.

226

11:

11.1 'A'

"".

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

11_p11_1a.c

main()
{
char *ptr,ch;
int c=0;
ptr="";
while(*ptr != '\0')
{
if(*ptr == '') c++;
ptr++;
}
printf("H %d A\n",c);
}

ptr "" ( , 35500). H *ptr ptr. ptr++ . ptr '', '',


'' ... ptr '\0', . c ''.

.

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

11_p11_1b.c

int count(char *p, char ch);

235

(arrays)

. .
. C
.


, .
0 ( 0, 1 ..).
C
:
_ _[]; ..
int a[10];

a 10 .

C
0. , int a[10]
10 a[0] a[9].

.

.
, :

a[5]=45;

246

12:

45 a (a[0]
, a[5] ).

,
. .

, :
i=5;
a[i]=45;
i=8;
a[i]=64;

a[5] a 45 a[8]
64, :
for (i=0;i<=9;i++) a[i]=45;

a 45.

a 10
1000:
#include <stdio.h>
#include <stdlib.h>

main()
{
int i,a[10];
for(i=0;i<10;i++)
{
a[i]=rand();
}
for(i=0;i<10;i++)
{
if (a[i]>1000) printf("%d\n",a[i]);
}
}

12_random1.c

247

, .




.
, a,
int a.


:
(a[5]), :


*a=12

a[1]=15

*(a+1)=15

a[2]=15

*(a+2)=20

a[9]=34

*(a+9)=34

a[0]=12

, *(a+9) 5036,
a ( 5000) 9
5036! a int, -

248

, for,
.
100 .
:

void fill_it(int a[])


{
int i;
for (i=0;i<100;i++)
{
a[i]=rand();
}
}

100 i 0
99. ,
:
a[0], a[1], ..

void print_it(int a[])


{
int i;
for (i=0;i<100;i++)
{
printf("%d\n",a[i]);
}
}

100 i 0
99. ,
:
a[0], a[1], ..

int athroisma(int a[])


{
int i,sum;
sum=0;
for (i=0;i<100;i++)
{

262

100 :
12_arrays_1d.c

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

void minmax(int a[]);


void fill_it(int a[]);
void print_it(int a[]);

main()
{
int pin[100],timi;
fill_it(pin);
print_it(pin);
printf("\n =%d\n",athroisma(pin));
minmax(pin);
printf(" :");
scanf("%d",&timi);
if (find_it(pin,timi))
printf(" \n");
else
printf(" \n");
}

 fill_it() pin .
 print_it() .
 H athroisma()
pin.
 minmax() .
 , find_it()
pin, .
1 () , 0 ().

264

12:

,
,
.
,
, .

, a (a[4][3][3]) , ,
.

12.5

,
, .
,
. ;
, ;

273

12:

(2)
2

a,
. ,
.
for (i),
(j). "" .
...
#include <stdio.h>
#include <stdlib.h>

main()
{
int i,j,a[10][5];
for(i=0;i<10;i++)
for(j=0;j<5;j++)
a[i][j]=rand();
}

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

main()
{
int i,j,a[10][5];
for(j=0;j<5;j++)
for(i=0;i<10;i++)
a[i][j]=rand();
}

275

for
(j), (i).
...

(
)
.
,
.. .
a[10][5] ,
2.
a.


10x5.

.

ss
.

int sum(int p[][5])


{
int i,j,ss=0;
for(i=0;i<10;i++)
for(j=0;j<5;j++)
ss=ss+p[i][j];
return ss;
}

sum main() a.
int a[10][5],ath;
.....
ath=sum(a);
printf(" a %d\n",ath);
.....

276

 . , , *
: char *func() func()
char.
 , . ,
.
, a[]
a[][4][5] .

12

12.1 lexi1 lexi2


gets() : 

286

main()
{
char lexi1[80], lexi2[80];
gets(lexi1);
gets(lexi2);
.....
.....
}


.
 lexi1 lexi2.
 lexi2
lexi1. 0 , ,
lexi1
lexi2.

T
(User-Defined data types)

H C
( ).
(user defined data types) :
 (Structures)
,
.
 bits (Bit fields)
bits
byte.
 (Unions)
, , .
 (Enumerations)
.
 Typedef
typedef
. typedef .

(structures)

C ( ) ,
. .
, .


. , , , ,
:
296

13:

,
,
,






struct.
stoixeia :

struct stoixeia
{
char onoma[15];
char address[20];
char thl[13];
int ilikia;
};

, .
stoixeia. : stoixeia , char, int ..

struct (;).

stoixeia :
. :
struct stoixeia pelatis,filos;

stoixeia, pelatis
filos .
. :

struct stoixeia
{
char onoma[15];
char address[20];
char thl[13];
int ilikia;
} pelatis,filos;

297

pelatis filos.
. :

struct
{
char onoma[15];
char address[20];
char thl[13];
int ilikia;
} pelatis,filos;

,
( o stoixeia).


.

, pelatis
filos 52 bytes (15+20+13+4).
sizeof ( byte)
.
:

struct .
{
.;
.;
.;
..............
..............
..............
.;
} [.];

298

13.1

struct stoixeia

. {

char eponymo[30];
char taxi[5];
100 float mesos_oros;
.
int ilikia;
- } mathites[100];
. () .
NULL.

struct stoixeia *find_onom(struct stoixeia math[],char onoma[])


{
int i;
strcmp()
eponymo onoma .

for(i=0;i<100;i++)
{
if(strcmp(math[i].eponymo,onoma)==0)
return &math[i];

}
return NULL;

, NULL.

316

mathites :
.............
struct stoixeia *ptr;
char lex[30];
.............
printf(" :");
gets(lex);
ptr=find_onom(mathites,lex);

13_p13_1&2.c



ptr.

()

C
/ (streams).

/. C
bytes.
() bytes, .

bytes , ..
, .
bytes
, , .. /
(.. , modem .).
C "" .

14.1

324

() C

14: -

""
,
. /. C "" /, .

C ()
(standard) /.
stdio.h :
stdout

()

stdin

()

stderr

()

C (default) 13.

/, ()
.. ,
, modem, ...

C :
(low-level) (high level). , , bytes,
bytes . , , -

13

/ .

325

, " /" (formatted I/O), bytes .

" " (binary


streams), " " (text streams).
stdout, stdin, stderr, .
.
C , .
,
.

bytes ( , , CD,
...). C
: .


:
 ,
.
 bytes
.
,
12. :
1 2, bytes.
, 12

326

fputs()
H fputs() puts(), . :

fputs(char *str, FILE *fp)

str
( char) fp . ,
.

in.txt -
'', out.txt.
out.txt.

14_fputs.c
#include <stdio.h>
#include <stdlib.h>
main()
{
FILE *fp1,*fp2;
char grammi[80];
int ar=0;
80
fp1=fopen("in.txt","r");
in.txt
grammi.
fp2=fopen("out.txt","w");
while(!feof(fp1))
{
'A'.
fgets(grammi,80,fp1);
if(grammi[0]=='A')
{
' '
out.txt.
fputs(grammi,fp2);
ar++;
ar
}
out.txt.
}
printf(" out.txt %d \n",ar);
fclose(fp1);
fclose(fp2);
}

342

14: -

(binary files)

fopen() (binary mode).


bytes
.
,
, :

fseek()

(.. 125 byte)

rewind()

( byte 0)

fread()

bytes (.. 10
25 bytes)

fwrite()

bytes (.. 10
25 bytes)

fseek()

fseek() .
:
int fseek(FILE *fp, long int apostasi, int thesi)

fp apostasi ( bytes) thesi. H


thesi :
0
1
2


SEEK_SET
SEEK_CUR
SEEK_END

SEEK_SET, SEEK_CUR, SEEK_END stdio.h


0, 1, 2 .
343

,
apostasi ,
byte .

fseek() fread() fwrite() C

byte .

fseek() .

rewind()

H rewind() :
viod rewind(FILE *fp)

fread()

H fread() bytes (buffer).


int fread(void *buffer, int arbytes, int fores, FILE *fp)

buffer
bytes . H arbytes,
bytes fores
. fp .

bytes .
bytes .

344

. , :
"
2008" : " 5.000
2007". : " 2007" , .. "4.000 2006" ...
,
, (.. 100) .

(
) , ( 100) ,
2008 .

15.1


. 2004: 100.
- .

362

15:

C . (recursive).

, .

, n (1*2*3 .....* n). , 3


6 (1*2*3) 8 40320 (1*2*3*4*5*6*7*8).

int par(int n)
{
int i,p;
p=1;
for(i=1;i<=n;i++)
p=p*i;
return p;
}

( )
n.

(n!) n 1 o n. , (n!) n n n! = n * (n-1)!, 6 6 * 5!.


.

int par(int n)
{
int p;
if(n==1)
return 1;
p=n*par(n-1);
return p;
}

363

15:

15_space.c,
C
15_space.exe.

,
:
15_space data.txt dataout.txt

data.txt dataout.txt .
data.txt dataout.txt, .

!!!


/.

.
, ,
.
.

C ! .

func1()
func2() 000341 000481 .
ptr1 ptr2,
367

(inline functions)


,

.


,

,
.

,
. , .
,


.
,
,


,
.

372

, .

gcc.exe
c:\dev-cpp\bin.
, path
.
:
c:\>path %path%;c:\dev-cpp\bin

. T into.c book().
" C ".
// intro.c
#include <stdio.h>
#include <stdlib.h>

15_separate/intro.c

void book()
{
printf (" C \n");
}

T bye.c thanks(). " ##!" ## .


// bye.c
#include <stdio.h>
#include <stdlib.h>

void thanks(char *name)


{
printf (" %s!\n", name);
}

376

15_separate/bye.c

15:

T main.c main() book() thanks().


15_separate/main.c

// main.c
#include <stdio.h>
#include <stdlib.h>
#include "myfunc.h"
int main()
{
book();
thanks("");
return 0;
}

H (""), <>,
.

C !
!

main() #include
"myfunc.h". ,
, ,
myfunc.h.
book() thanks():
// myfunc.h
void book();
void thanks(char *name);

, main()
main.c
.


.
. GCC :
gcc Wall c _.c

H -c
( ) .

Wall (warnings) .

377

""
.
,
.

/,
15.
.
(
/) , ,
., ,
.
, ,
.
.
.
. .
"" .

.
.
,
,
.

15

18.
C .

394

16:

.
- (
) (
) .
(.. found),
0.
, 1 .
found. 1,
,

.


100
.

printf(" :");
scanf("%d",&ar);
found=0;
for(i=0;i<100;i++)
{
if(a[i]==ar)

found
0.
,
1 found
.

395

16.1

402

"" (bubble sort)

16:

x[i]=x[maxpos];
x[maxpos]=temp;
}
}

: .

quick sort

quick sort (" ")


" "
.
:

 ,
( ) .
 :

.
 ,
. .

16.3

quicksort

405

16:

16.1 100 -

4
. .

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

16_p16_1.c

void bubblesort2d();

main()
{
int a[100][4];
int i,j;
for(i=0;i<100;i++)
for(j=0;j<4;j++)
a[i][j]=rand();
bubblesort2d(a,3);
for(i=0;i<100;i++)
{
for(j=0;j<4;j++)
{
printf("%10d",a[i][j]);
}
putch('\n');
}
}

a
.
bubblesort2d() , 3
( ).

4 .

void bubblesort2d(int x[][4],int m)


{
int i,j,k,temp;
for(i=0;i<100;i++)
{

413

C
, .
, :





.
.

( ) .
, ,
. ,
.

( ) .

,
char a,b;
int num[100];

bytes, a b, 400 bytes (4 x 100)


num. .
420

17:

.. ,
.

(.. 100 )
. , ( ) (.. 1000 )
.

.
.


,
. ,
.
C .

:
int num[a];

C.
C ,
. , .

421

malloc().
malloc(),
.

calloc()

#include <stdlib.h>

void *calloc(unsigned int num, unsigned int size);


calloc()
num size ( byte) .

num size: num*size byte. H
calloc()

.

,
calloc() NULL.
.

, calloc() -

(.. ) num
size.

malloc() calloc()
. malloc() byte, calloc()
(num*size).

calloc() unsigned int void .

424

. :
, ( ) .
. , .
, , ( ) ,
.
,
.
, ,
, .


:
 (linked lists)


. .
, .
( ).
432

18:

,

(
). .



.

"".

(simple linked list)

(, nodes) .
. ,
:


 , ()
.

18.1

433


(root node), .
,
.

18.9


.
.

, ,
( tree_root), (root node).


:

.
450

( ) .

(left most node) ( ) -

( ).

(right most node) ( )


( ).


, , .
, .

18.11

452

18:

, ""

.


.


( root),

.


.
(insert), (rm), (find),
(display).
insert()

rm()

,
, .

find()


display() (in-order traversal)
.

.
.
461

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

node
.

struct node
{
int data;
struct node *left;
struct node *right;
};

18_binary_tree.c

struct node *root;


struct node *newnode(int num);
struct node *find(int key);
struct node *insert(int num);
void display(struct node *ptr);
struct node *find_left_most();
struct node *find_right_most();

main()
{
char ch;
int a;
struct node *new;
root=NULL;
while(1)
{
printf("0-> 1-> 2-> ");
printf("3-> 4->:\n");

462

ch=getch();
switch(ch)
{
case '0':
exit(0);

C,
C++

C, C++

C,
.
C, C++
17.

C, C++ Bjarne Stroustrup


Bell, 1983-1985. 1983, Bjarne Stroustrup
Simula ( ) , C,
C "C with Classes". "" C++, 1983. 1997, (ANSI, ISO ..).
C++ UNIX.
. C++
.

C++, C

C++ C,
.
"" C.
C C++.
C, ,
C++.

17

T " C++
", , 2008.

478

19: C, C++

19.1

C C++

C++,

C++ Java
(Object Oriented Programming - OOP).
.
, ( )
. , , . ()

479

.

, , .
 .
.
 ,
.
,
.

(classes and objects)

(
) () , .

; , , ,
.

.
C,
,
. :
.

. .
482

19: C, C++

(classes) (objects) .

(class)
.

, ""
.

19.2

- ""


. .. , , . ,
. -
(, , ...) , !
483

19: C, C++

C++
,
.
19_first.cpp





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

main()
{
int a,b;
float c;
printf(" 2 :");
scanf("%d %d",&a,&b);
c=(a+b)/2.0;
printf(" %d %d %f\n",a,b,c);
}

; C++ C! ; .cpp;

C++.
C++ C, C
C++!
C++,
"" .


. .

489

cout

T cout
. cout .
cout , .

cout << .
<< , cout .

, "C++" cout, , .

cout,
.
. , , , , .

490

19: C, C++

, cout
( ).
, .

.
cout.

cout:

main()
{
int a;
a+2=17 b=7.5 c=A
char c;

float b;
a=15;
b=a/2.0;
c='A';
cout <<"a+2="<<a+2<<" b="<<b<<" c="<<c<<endl;
cout <<"\n";
}

:
491

cout <<"a+2="<<a+2<<" b="<<b<<" c="<<c<<endl;

cout . ,
. endl
.
endl cout. cout , ,
. endl .
endl cout .

cout <<"\n";

\n
.
'\n' . cout <<"\n" cout <<""<<endl .

cin

T cin
. cin . .

492

C

(header files). . ,
#include .
( DEV C++), , ANSI, stdlib.h stdio.h.
, ,
.

<ctype.h>

int islower(int c);

islower() c
, 0.

504

#include <ctype.h>
main()
{
char ch;
do
{
ch=getch();
putch(ch);
} while(islower(ch));
}

: C

int isupper(int c);


isupper() c
, 0.

int isprint(int c);

isprint() c
( ), 0.

int tolower(int c);

tolower() c. .

tolower('A')

'a'.

int toupper(int c);

toupper()
c. .

toupper('a')

'A'.

<math.h>

double exp(double x);


exp() e
x (ex).

f e4.

505

DEV C++

DEV C++

DEV C++
C C++. GCC (Win32) (console applications)
Windows (GUI applications). DEV C++
GNU (GNU GENERAL PUBLIC LICENSE).
CD,
http://c.bytes.gr,
DEV C++.

DEV C++.

DEV C++

DEV
C++, devcpp
_setup.exe
CD.

 .
 Full.

C:\Dev-Cpp
.

. , .
DEV C++ . , code completion feature .

524

: DEV C++

DEV C++


.
DEV C++
() .

: --


C.

525

You might also like