Η γλωσσα C σε βαθος

544

description

Χατζηγιαννάκης

Transcript of Η γλωσσα C σε βαθος

............................................................................................................17 1: ......................................................................................19

......................................................................20 C ..................................................................22 C ..................................................................................22C .................................................................................... 23 C .................................................................. 23 C ............................................................................. 23

.................................................................24 ........................................................................................................24 .............................................................................................................26 ..............................................................................................................26 ..................................................................................................27 .............................................................27 .................................................................................................28 2: C ...............................................................37

C .......................................................................38 C ....................................................................39 .............................................................................................. 41 .............................................................................................. 42 ....................................................................................... 44 .............................................................................. 44 ................................................................................................................. 44 ............................................................................................................. 45 .............................................................. 48 ............................................................................................... 49 .................................................................... 51 C ............................................................................................. 52 (compound statement)................................................................. 53

- C.................................................................................54 #include..............................................................................................54 #define................................................................................................54

C

3: ........................................................61

C ................................................................................................62 &............................................................................................................ 63 sizeof ..................................................................................................... 64

............................................................................64 printf()................................................................................................ 64 scanf() ................................................................................................ 67 exit() .................................................................................................. 69

if ............................................................................................70 4: int................................................................79

int.............................................................................................80 int........................................................................................80 (unsigned)........................................................................ 81 .......................................................................................... 81 ............................................................................................... 82 ....................................................................... 84 % ......................................................................................... 85

...............................................................................................85 ........................................................................... 85 ........................................................................... 86 (bitwise) ................................................................................. 87

...................................................................................91 5: char ............................................................99

char ........................................................................................100 !!! .............................................................................. 100 ............................................................................................ 101

char ...................................................................................102 ............................................................ 102 H scanf() ! ............................................................... 104 (character strings) ......................................................................... 107 ; ............................................................................... 107

ASCII ................................................................................................109 6: float ..........................................................115

float........................................................................................116 float...................................................................................116

8

............................................117 printf() ...................................... 117 scanf() float ...................................... 118 pow() sqrt() ............................................................................ 119 7: ......................................................................123

if .......................................................................................................124H if.................................................................................................. 124 if-else ................................................................................................... 125 if - else if.............................................................................................. 126

switch - case.....................................................................................128 ? ....................................................................................................130 8: ..................................141

"" goto.............................................................................142 while ................................................................................................143 O do-while ...........................................................................................146 for.....................................................................................................147 for.................................................................................................. 150

"" (,)....................................................................................153 break.................................................................................................154 continue............................................................................................156 ........................... 157 ............................. 159 9: ................................................................................171

O ...............................................................................173 ........................................................................... 174 ................................................................................. 175

.................................................................178 ..........................................................179 ..................................................................................... 180

.................................................................................183 ................................................................. 183

..................................................................185 ............................................................... 187

(type casting) .....................................................................188 ................................................................................... 189

9

C

10: ..............................................................199

(local variables) ..............................................................200 (global variables)........................................................204 .......................................207 (static local variables).......................................207 11: .......................................................................................219

(Pointer variables)............................................................221 .....................................................................221 & * .........................................................................................223 ... ...............................................................................226 ..................................................................................226 Void..........................................................................................229 NULL .................................................................................................230 ...........................................230 ..................................................................................231 .................................................................... 233 (lvalues)................................................................. 234 12: (arrays)........................................................................245

...................................................................................246 ........................................................248 !!!......................................................................................249 ...........................250 .......................................................................................252 printf() scanf() ................................ 253

..............................................................................254 ............................................... 255

........................................................261 .................................................................262 ...........................................................................265 .................................................................................265 ...............................................................267 ..................................................................................269 .................................. 271 ............................................................. 273

(2).............................275

10

2 ............................................................. 275 2 .............................................................. 276 2 ................ 277 2............................................................... 278 2 ........................................................... 278 2................................... 279 2 .................................. 279 2............................. 280 13: T (User-Defined data types) .....................................................295

(structures) ............................................................................................296 .............................................................................. 300 ................................................................................................. 301

.................................302 ..........................................303 ..............................................................................................307 ............................................................................... 309

( )) ............................................................309 ... ................................................................................................. 310

, , bits (bit fields) ......................................311 (unions)..............................................................................................313 (Enumerations).........................................................................314 typedef ........................................................................................315 14: - ..........................323

() ........................................................................324 ...............................................................................325 .................................................................................................325 ...............................................................................................326 ...............................................................326 ....................................................................327 / ...............................................................................329fopen() .................................................................................................................... 330 fclose().................................................................................................................... 333 ferror() .................................................................................................................... 333 fflush().................................................................................................................... 334 fflushall() ................................................................................................................ 334

11

C

(text files) .............................................................................335fputc() ..................................................................................................................... 335 fgetc() ..................................................................................................................... 335 fprintf() ................................................................................................................... 336 fscanf() ................................................................................................................... 337 feof()....................................................................................................................... 339 fgets() ..................................................................................................................... 341 fputs() ..................................................................................................................... 342 fseek()..................................................................................................................... 343 rewind() .................................................................................................................. 344 fread() ..................................................................................................................... 344 fwrite().................................................................................................................... 345 ....................................................................... 346 15: .....................................361

(binary files) ...................................343

.........................................................................................................362 .........................................................................364 !!! ..............................................................................367

(inline functions).......................................................372 ...........................................................374

............................................................................... 368 .............................................. 368 ................................. 369 ......................................... 370 ................................................................. 371 ........................................................................... 371

GCC ......................................................................................... 375 ................................................. 376 ...................................................................... 378 ... ..................................................................................................... 381 16: .....................................................393

........................................................................................395 (binary search) ................................................................397 (bubble sort) ..............................................................400 (selection sort)..............................................................403 quick sort.....................................................................................405

12

............................................................410 ...................................................................... 412 17: ..................................................419

............................................................................422 malloc()............................................................................................ 423 calloc()............................................................................................. 424 free() ................................................................................................ 425 realloc()............................................................................................ 425 18: ...................................................431

............................................................................................433 (simple linked list)..................................................433 (circularly linked list)........................................434 (double linked list).................................................434 ..................................................................435 ..............................................................435 ............................................ 435

C..........................................................437 .......................................................................... 441 ............................................................................... 442 .................................................................................. 446

...................................................................................................450 ...................................................................452 ...................................................................................452 ..........................................................................................455 .....................................................................456 (in-order).............................................................................. 456 (pre-order) .................................................................. 457 (post-order) ............................................................... 458

......................................................458 ..................................................... 459 ........................................................... 459 ................................................... 459 ................................................................................... 460

........................................................461

13

C

19: C, C++ ...................................................................477

C++, C...........................................................................478 C++, ............................................................479 ..........................................................480 (classes and objects)....................................................... 482 (Encapsulation)................................................................................. 485 (Inheritance) ............................................................................. 486 (Polymorphism)........................................................................... 487

C++...................................................................489 ................................................................................489 cout................................................................................................ 490 cin.................................................................................................. 492

C C++............................................................................495 C++ .................................. 496 C++................................................... 499 : C ............................................503

C..................................................................................504 .........................................................................................................504int islower(int c); .................................................................................................... 504 int isupper(int c); .................................................................................................... 505 int isprint(int c);...................................................................................................... 505 int tolower(int c);.................................................................................................... 505 int toupper(int c);.................................................................................................... 505

..........................................................................................................505double exp(double x);............................................................................................. 505 double log(double x); ............................................................................................. 506 double log10(double x); ......................................................................................... 506 double pow(double x, double y);............................................................................ 506 double sqrt(double x); ............................................................................................ 507 double fabs(double x);............................................................................................ 507 int abs(int x); .......................................................................................................... 507 double sin(double x);.............................................................................................. 507 double cos(double x); ............................................................................................. 508 double tan(double x);.............................................................................................. 508

..........................................................................................................508EOF ........................................................................................................................ 508 FOPEN_MAX ........................................................................................................ 50814

NULL ..................................................................................................................... 508 SEEK_CUR............................................................................................................ 508 SEEK_END............................................................................................................ 509 SEEK_SET............................................................................................................. 509 stdin ........................................................................................................................ 509 stdout ...................................................................................................................... 509 stderr....................................................................................................................... 510 FILE ....................................................................................................................... 510 FILE *fopen(char *filename, char *mode); ........................................................... 510 FILE *freopen(char *filename, char *mode, FILE *fp); ....................................... 510 int fflush(FILE *fp);............................................................................................... 511 int fclose(FILE *fp);............................................................................................... 511 int remove(char *filename); ................................................................................... 511 int rename(char *oldname, char *newname); ........................................................ 511 int fprintf(FILE *fp, char *format, ,...,...); ........................................... 511 int printf(char *format, ...);..................................................................................... 512 int sprintf(char *s, char *format, ...);...................................................................... 512 int fscanf(FILE *fp, char *format, ,....); .............................................. 512 int scanf(char *format, ...); ..................................................................................... 512 int sscanf(char *s, char *format, ...); ...................................................................... 513 int fgetc(FILE *fp); ................................................................................................ 513 char *fgets(char *s, int n, FILE *fp); ..................................................................... 513 int fputc(int c, FILE *fp); ....................................................................................... 513 char *fputs(char *s, FILE *fp); .............................................................................. 513 int getc(FILE *fp);.................................................................................................. 513 int getch(); .............................................................................................................. 514 char *gets(char *s); ................................................................................................ 514 int putc(int c, FILE *fp); ........................................................................................ 514 int putch(int c); ....................................................................................................... 514 int puts(char *s);..................................................................................................... 514 int fread(void *ptr, int size, int num, FILE *fp);.................................................... 514 int fwrite(void* ptr, int size, int num, FILE *fp); .................................................. 515 int fseek(FILE *fp, int apostasi, int thesi); ............................................................. 515 void rewind(FILE *fp); .......................................................................................... 515 int feof(FILE *fp);.................................................................................................. 515

.........................................................................................................515NULL ..................................................................................................................... 515 void *calloc(int num, int size);............................................................................... 515 void *malloc(int size);............................................................................................ 516

15

C

void *realloc(void *p, int size);.............................................................................. 516 void free(void *p);.................................................................................................. 516 void exit(int status);................................................................................................ 516 int system(char *s); ................................................................................................ 516 int rand();................................................................................................................ 517 void srand(unsigned int seed);................................................................................ 518 int atoi(char *str); ................................................................................................... 518 double atof(char *str); ............................................................................................ 519

.........................................................................................................520NULL ..................................................................................................................... 520 char *strcpy(char *str1, char *str2);....................................................................... 520 char *strncpy(char *str1, char *str2, n);................................................................. 520 char *strcat(char *str1, char *str2); ........................................................................ 520 char *strncat(char *str1, char *str2, n); .................................................................. 520 int strcmp(char *str1, char *str2); .......................................................................... 520 int strncmp(char *str1, char *str2, n); .................................................................... 521 char *strchr(char *str, int c); .................................................................................. 521 char *strrchr(char *str, int c); ................................................................................. 522 char *strstr(char *str1, char *str2);......................................................................... 522 int strlen(char *str); ................................................................................................ 522 : DEV C++ ......................523

DEV C++ ...........................................................................524 DEV C++ ............................................................525 ....................................... 525 ....................................................................................... 525 ............................................................................................... 526 ................................................... 526 ( )...................................... 527 ................................................................................ 527 , ................... 527 ..................................................................................... 528 DEV C++ ........................................................... 528 ....................................................................................................529

......................................................................................530 ..................................................................................530 .........................................................................................................533

16

C

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

20

1 . 2 "" . , -

1:

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

C C

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

C :

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

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

C

BASIC .. . C . (compiler).

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

(source code) ( ).

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

24

, , (RAM) /. .

C

1.1

, , .32

. . . .

, . .

1:

1.2

100 . 1

, . 0.

C . C "" , . . C : int, char float . float. , . . , , . .

33

C

C

C "" C. , . .

C C, "Hello world".#include main() { printf("Hello world"); } , main() Hello world.

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

(IDE) . .

38

2: C

main(). , main() . { main(). printf("Hello world"); printf() . "Hello world". } main(). , , #include main(). , main().

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

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

39

C

; ; }

: C . ( ). , int a,b; a b.

, (;). : ( ) .

(;). main():main() { main(); main(); } function1() { function1(); function1(); } function2() { function2(); function2(); }

40

2: C

. (local variables) . 10 . . , /* */. main() ./* C 24/10/1983 */ main() { /* printf()*/ printf("Hello world\n"); /* */ }

C: , . C C++, DEV C++, " " (//). :// // // C 24/10/1983

main() {

41

C

printf("Hello world\n"); // Hello world } (compiler) C /* */. (//) .

, . C . C : type 1,2, ... ; type 1, 2, ... . C : int char float . . . . ( ).

, , . , C (.. , ) : , , "_". . .

42

2: C

, , .., . . , .. :a teliko_apotelesma a23

:_ 23a number of students

: int number_of _students, a2; number_of_students a2. char apantisi; apantisi. float mesos_oros; mesos_oros. C . a A. , C .

43

C

. :int a=4,b; float c=4.6,m; char ch='A';

a, b, c, m ch a, c ch 4, 4.6, '' . C . , const.const int a=8; const float c=4.6;

a c, 8 4.6 . . .

.

C : T T (, ) , , , 44

2: C

. , , (int, char, float). , . ,main() { int x,y; x=5; y=x+4*20; }

. , . C. , , , (.. +, -, /) (.. >= < 2;

. a 1 3>2, , 1. . , . :50

2: C

(a>b && a==c) || a:\n"); ch=getch(); switch(ch) { case '0': exit(0); 462

node .

C, C++

C

C, C++ C, . C, C++ 17.

C++, C17

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

478

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

T " C++ ", , 2008.

19: C, C++

19.1

C C++

C++,

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

479

C

(classes and objects)482

. , , . . . , .

, , .

( ) () , . ; ; , , , . . C, , . : .

. .

19: C, C++

(classes) (objects) .

19.2

(class) .

, "" .

- ""

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

19: C, C++

C++ , .

#include #include 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); }

19_first.cpp

; C++ C! ; .cpp; C++. C++ C, C C++! C++, " " " .

. .

489

C

cout T cout . cout . cout , .

490

cout