Εισαγωγή στη Γλώσσα Προγραμματισμού C

download Εισαγωγή στη Γλώσσα  Προγραμματισμού C

of 81

description

Εισαγωγή στη ΓλώσσαΠρογραμματισμού C

Transcript of Εισαγωγή στη Γλώσσα Προγραμματισμού C

  • 1

    C

    ,

    2009

  • 2

  • 3

    :

    /. C. C. ., .

    Linux, KDEEmacs. Linux . C.

    ,printf()scanf(),.C ifelse,switch,while,fordowhile.

    VV . V .

    .

  • 4

  • 5

    :

    Linux.

    1.1 (OperatingSystem)

    (/):(Hardware)(Software).,/,/.

    (Operating System) (Hardware)/.:

    (Interface). (resources)

    (ResourceAllocation).:

    /. . (Central Process Unit)

    . (RAM)

    , () ().

    . .

    .

    1.2 Linux

    UNIX , .1970..

  • 6

    UNIX /. IBMAIX,SUNSolaris,DECUltrix.

    Linux (Open Source) UNIX .LinusTorvalds Linux 1991, . LinusLinuxInternet,.Linux Internet. Linux,.ScientificLinux.

    1.3 Linux

    To Linux , :

    (,multitasking). To Linux .

    (multiuser).Linux.

    . Linux ( Pentium), 16 , .

    (Portability)..Linux , .Linux (CPU), Intel x86, SPARC,Alpha,PowerPC,MIPS m68k.

    (utilities).Linux.:

    o .

    o . (wordprocessor),(spreadsheet).

    (communications). Linux .

  • 7

    .Linux,Windows,OS/2,UNIX.

    1.3 Linux

    Linux . (kernel).. , ,..,, . C, 1000 (Assembly).

    .C. (Shell). ,. Linux , .

    Linux.:

    Bourne Again bash . Linux.

    CcshBourne.

    Korn ksh UNIX. Bourne.

    Bourne sh 70.

    Ctcsh. Zzsh.Bourne.

    . BourneAgain bash. :

    echo$SHELL

    (FileSystem). (partition) MS

  • 8

    DOS.Linux . , Third Extended(ext3fs),Linux.o.

    Linux , ,:

    (Kernel). (FileSystem). (Shell). .

    1.4 XWindowsKDE.

    XWindows UNIX. . X, .X , . Linux XWindows, . TCP/IP, LinuxXWindows.

    XWindows,MIT,.XWindows Linux XFree86. XFree86 X, X, ,.

    XLinux.X xterm, X.

    K Desktop Environment ( K KDE) Open Source , , UNIX Linux. KDE XWindows. , 1996, . , Qt, C++.KDE ( ) WindowsXP2000.

  • 9

    KDE GNOME .GNOMEKDE.KDE.

    1.4 LinuxKDE.

    /PC.WINDOWSLinux.(poweron).ScientificLinux.

    Linux. :Login:Password.

    . Login Password(userid)(password).Go!KDE(1.1).

    1.1:ToKDE.

  • 10

    KDE., .(panel). , . (1.2):

    1.2:KDE.

    (StartApplication) (1 ). KDE..

    (Shell Terminal emulationwindow) (2 ). Linux.

    Konqueror : Web (Konqueror Web Browser) (3)..

    FireFox : Web (FireFoxWebBrowser) (4). .

    Email (5 ). .

    OpenOffice(6).OfficeWindows. (workspaces)(14).

    (16) .(14). .(1).

    (1.3):

    1.3:KDE.

  • 11

    Klipperclipboardtool. . . .

    .2 1.4. Linux.

    1.4: .

    exit

    x .

    KDE:

  • 12

    .

    (StartApplication)(1)(Logout).

    1.5Logout. KDE LoginPassword

    1.5: (Logout) KDE.

    Linux(poweroff):

    KDE ShutdownOK.

    Linux .

    1.5 Linux.

    Linux(treestructure)(1.6). (rootdirectory),/

  • 13

    (directories)(files).

    1.6:Linux.

    (file)(,CD,.). , , , ((owner)),.

    (directory) . , , .

    (fullpathname). 1.6 file1.c. /home/student1/file1.c /home/student3/file1.c.

    ,.

    !!! Linux .file1.cFile1.c.

  • 14

    1.6 Linux. /home../home.student1,student2,student3.student1/home/student1.

    /home,,.,:

    /bin:Linux,ls. /boot:

    . /etc : ,

    NFS. /var : , (log files)

    . /usr/bin:Linux. /usr/sbin : (superuser)

    . .

    /usr/include : (include files) C,.

    /usr/src:.

    /usr/local : .

    1.6 Linux.

    Linux.Linux..

    pwd:pwd.

    cddirname:dirname.

    ..: cdc_directory cd/usr/bin cd..

  • 15

    . Linux :

    .

    .. .

    mkdirdirname:dirname...mkdirnew_catalogrmdir dirname : dirname. : dirname . ...rmdirnew_catalogls : . ls :

    ls l : , , , , .

    lslt :.(1.7).

    1.7:lslt.

    1.7

    10 .. drwxrxrx

    :d(directory),( file). Desktop,Mail vnc , vnc.tar .

    rwx.Linux:

  • 16

    read().

    write().

    execute(). , .

    chmod. chmod:

    chmod w filename : w(ho) = u (user), g(group), o(other), + : , - : . : r(read), w(write), x(execute).

    .

    cpfilename1filename2: filename1 filename2...cptest1.cnew_test1.crmfilename:filename.

    mv filename1 filename2 : filename1 filename2.

    morefilename:filename.

    tailnfilename:nfilename.

    diff filename1 filename2 : filename1filename2.

    man onlineLinux.

    ..manrm :rm.manmore:more.

    1.7 Emacs.

    UNIX.

  • 17

    UNIX. Linux. .

    , vi Emacs. Emacs...

    C, test1.c. KDE:

    emacstest1.c&

    1.8. .&()..

    Emacs Emacs.Emacs.

    FileSave(CurrentBuffer)1.9.EmacsFileExitEmacs.

  • 18

    1.8:Emacs.

    1.9:Emacs.

  • 19

    I:

    C.

    2.1 C.

    C 70DennisRitchieAT&TBellLabs.ANSI(AmericanNationalStandardsInstitute)ANSIC.:

    :. :. :

    . (compiler) . /. C /.

    2.2 C.

    :

    emacsexample1.c&

    Emacs example1.c. (2.1):

    FileSave(CurrentBuffer)(1.9).(compilation).:

    gccexample1.c

    a.out. , , .Enter.2.2:

  • 20

    2.1:C.

    2.2:.

    .

    2.3 C.

    2.1 :

    .:/*ThisismyfirstCprogram*/

  • 21

    ,/* */. C, /* */ . C .

    #include.:

    #include

    #,include.include ( )/usr/includestdio.h. include stdio.h.

    #include, stdio.h, ..stdio.h,math.h, stdlib.h, string.h . .

    main()C.main(). main(int)(void)..

    printf(). \n . printf() stdio.h.#include.

    return.C.

    main .

  • 22

    2.4 (compiler).

    LinuxC,ccgcc.ccgccGNU (Free Software Foundation). gcc Linux.

    gccexample1.c

    a.out. ,.ex1:

    gccoex1example1.c

    ex1a.out.

    . C, ..c.

    gcc,,:

    . /usr/include.

    (objectfile)..o..:.

    linker (

    ) . , ANSIC ,. , .(executablefile).

    .ANSIC.

    :

  • 23

    gcccfilename.c:filename.o.

    filename.cmain.c.main.cfilename.o.:

    gccopgmmain.cfilemane.o:pgm.

    2.5 C.

    ,,,.

    . . , :

    a=1.35;b=32;

    a=7.5; b=10;

    ab1.3532 . ,.main,return,for.(,;?).

    ,

    . . 100 3.1415 k,u.

    C:

    +

    * / %

    .7%52.

  • 2.6

    a=10;b=5;c=(a+

    (a+5)*b

    c=(a+

    2.3

    2.3:

    .

    :

    ;

    +5)*b;

    b

    +5)*b;

    .c.

    .

    3

    75

    .

    .

    .

    24

    ,

    .

    . .

    ,

    c

    .

    ,.

    75

    .

    (a+

    C

    .

    ,

    5.

    +5)*b

    .

    C

  • 25

    :int.int . int 32bit (4byte).2147483647(2311)2147483648.: inta,b; a=5;

    b=7;

    :doublefloat.doublefloat().float32bit(4byte)double64bit(8byte).float 1.4x1045 3.4x1038 7 . double4.9x103241.8x10308 15 . . . double, . double.: doublex;

    floaty; x=3.14159;

    y=7.;!y7, 7 .

    : char. char . PC 256 . (AZaz),(09),.8bit(1byte).8bit,0(201)255(281)ASCII (AmericanStandardCode for Information Interchange). A, 65 ( 0x41), a 97 ( 0x61). 2.1 ASCII 128.

  • 26

    :void.(main(void)).

    (qualifiers).:

    shortlongint.:

    shortintlongint

    O short int 16 bit (2 byte) long int 32 bit (4 byte). longdouble.

    longdouble

    80bit (10byte) .

    unsignedsigned int.unsignedsigned.:

    unsignedinta;signedintb;

    :unsignedshortinte;signedlongintf;

    2.2.

    2.1ASCII128.

    Non-Printing Characters Printing Characters Name Control Dec Hex Char Dec Hex Char Dec Hex Char Dec Hex Char

    null ctrl-@ 0 00 NUL 32 20 Space 64 40 @ 96 60 `start header ctrl-A 1 01 SOH 33 21 ! 65 41 A 97 61 astart of text ctrl-B 2 02 STX 34 22 " 66 42 B 98 62 bend of text ctrl-C 3 03 ETX 35 23 # 67 43 C 99 63 cend of xmit ctrl-D 4 04 EOT 36 24 $ 68 44 D 100 64 denquiry ctrl-E 5 05 ENQ 37 25 % 69 45 E 101 65 eacknowledge ctrl-F 6 06 ACK 38 26 & 70 46 F 102 66 fbell ctrl-G 7 07 BEL 39 27 ' 71 47 G 103 67 g

  • 27

    backspace ctrl-H 8 08 BS 40 28 ( 72 48 H 104 68 hhorizontal tab ctrl-I 9 09 HT 41 29 ) 73 49 I 105 69 iline feed ctrl-J 10 0A LF 42 2A * 74 4A J 106 6A jvertical tab ctrl-K 11 0B VT 43 2B + 75 4B K 107 6B kform feed ctrl-L 12 0C FF 44 2C , 76 4C L 108 6C lcarriage feed ctrl-M 13 0D CR 45 2D - 77 4D M 109 6D mshift out ctrl-N 14 0E SO 46 2E . 78 4E N 110 6E nshift in ctrl-O 15 0F SI 47 2F / 79 4F O 111 6F o

    data line escape ctrl-P 16 10 DLE 48 30 0 80 50 P 112 70 pdev.cntrl.1 ctrl-Q 17 11 DC1 49 31 1 81 51 Q 113 71 qdev.cntrl.2 ctrl-R 18 12 DC2 50 32 2 82 52 R 114 72 rdev.cntrl.3 ctrl-S 19 13 DC3 51 33 3 83 53 S 115 73 sdev.cntrl.4 ctrl-T 20 14 DC4 52 34 4 84 54 T 116 74 tneg acknowledge ctrl-U 21 15 NAK 53 35 5 85 55 U 117 75 usync.idel ctrl-V 22 16 SYN 54 36 6 86 56 V 118 76 vend of xmit block ctrl-W 23 17 ETB 55 37 7 87 57 W 119 77 w

    cancel ctrl-X 24 18 CAN 56 38 8 88 58 X 120 78 xend of medium ctrl-Y 25 19 EM 57 39 9 89 59 Y 121 79 ysubstitute ctrl-Z 26 1A SUB 58 3A : 90 5A Z 122 7A zescape ctrl-[ 27 1B ESC 59 3B ; 91 5B [ 123 7B {file separator ctrl-\ 28 1C FS 60 3C < 92 5C \ 124 7C |group separator ctrl-] 29 1D GS 61 3D = 93 5D ] 125 7D }record separator ctrl-^ 30 1E RS 62 3E > 94 5E ^ 126 7E ~unit separator ctrl-_ 31 1F US 63 3F ? 95 5F _ 127 7F DEL

    2.2..

    bits char 8 int 32 21474836482147483647shortint 16 3276832767longint 32 intfloat 32 1.4x10453.4x1038double 64 4.9x103241.8x10308

  • 28

    2.7 .

    , . .

    . main

    intmain(void){.

    ..

    C #include. . (globalvariables)..

    . (.0)( ). . ,.

    ,,.

    int. double float. double.

    for.

    .:

    inta,b;doublex;a=5;

  • 29

    b=10;x=a/b;

    x=0x=0.5.a/b.

    double float () (.). o :

    doublex,y; doublex,y;x=4; x=4.;y=9; y=9.;z=x*y; z=x*y;

    ,.:

    doublex,y,z; x=2.; y=10.; z=(1/2)*x*y;

    z=0 (1/2) 0.:

    doublex,y,z; x=2.; y=10.; z=(1./2.)*x*y;

    z=10.

    2.8 (casting).

    . . double float int ( 2 ). doublefloat.:

    a2.inta;doublex; x=2.7; a=(int)x;

  • 30

    a.inta;doublex; x=1.234e24; a=(int)x;

    x25.inta; inta;doublex; floatx; a=25; a=25;x=(double)a; x=(float)a;

    .(casting).

    C . . .(double)..

    C . :

    longdoubledoublefloatintshortint

    2.9 printf().

    printf() ,stdio.h.

    2.3 printf(). :

    Thenumber15isaninteger.

    , .%.2.3

  • 31

    printf(Thenumber:%disaninteger.\n,15);

    2.4printf().

    2.3printf().

    d,i int.o int..x,X int..u unsignedint.c char..s char..f float,double.e,E float,double..g,G float,double.

    .

    \n . \n.%%%.p. 2.5 , .2.3.

  • ThThThThThTh

    .

    2.5

    heargumenthenumber:2heinteger:25heinteger:25henumber:3henumber:3

    %

    %.3f,

    12

    :Aisacharac255isaninteg55inoctalfo55inhexadec3.141590isa3.141590insc

    printf()

    print %12.3f

    cter.ger.rmis:377.cimalformis:realnumber.cientificform

    . .

    tf()

    32

    :ff..

    mis:3.141590

    1

    :

    e+00

    %12f12

    :

    f . %12f.

    . .

    o

    .

    .

  • 33

    doublex;inta;x=3.14159;a=35; printf(x=%f\n,x); => x=3.14159printf(x=%.3f\n,x); => x=3.141printf(x=%10.3f\n,x); => x=3.141printf(x=%d\n,a); => a=35printf(a=%10d\n,a); => a=35

    2.10 C.

    C. math.h #include.

    . double. x,y double n int. .

    .

    ,. sqrt(x) x,x>=0.

    exp(x) ex

    log(x) ln(x),x>0

    log10(x) log10(x),x>0

    pow(x,y) xy

    fabs(x) |x|

    ldexp(x,n) x*2n

  • 34

    .!rad. cos(x) x

    sin(x) x

    tan(x) x

    acos(x) x[0,],x[1,1]

    asin(x) x[/2,/2],x[1,1]

    atan(x) x[/2,/2]

    atan2(y,x) y/x[,]

    cosh(x) x

    sinh(x) x

    tanh(x) x

    : ,60.

    #include#include

    intmain(void){

    doublex; /*x*/x=60.; /*x*/x=x*3.14159/180.; /*rad*/

    printf(cos(60)=%f\n,cos(x)); /**/printf(sin(60)=%f\n,sin(x)); /**/printf(tan(60)=%f\n,tan(x)); /**/

    return0; /*main()*/}

  • 35

    example.c. :

    gccexample.clm

    a.out . !!! lm (linkmathematics) . :

    cos(60)=0.500001sin(60)=0.866025tan(60)=1.732047

    2.11 C#define.

    ,,C:

    :

    >

    >=

  • 36

    :

    && AND()

    || OR()

    ! NEGATION()

    , if, for,while,dowhile. , . 1..:

    inta,b,c,d;a=15;b=7;c=25;d=15;

    a>101 a>b 1 a>c 0 a>=b 1 ab)||(ab)||(a>c) 1 (a>c)||(d>c) 0 !(a>b) 0 !(a==b)1

  • 37

    O:

    ++ 1

    1

    ++ 1.

    ++a; a=a+1;a; a=a1;

    ++

    (. , ++a a) (.,a++a).a 1. ++a a , a++ a .toa5

    b=a++;b5

    b=++a;6.a6.

    :

    =

    = . a=b;ba.

    +=

    =

  • 38

    *= /

    /=

    %=

    :

    a+=b; a=a+b;a=b; a=ab;a*=b; a=a*b;a/=b; a=a/b;a%=b; a=a%b;

    bits:

    & ANDbit

    | ORbit

    ^ XORbit

    ~ NOTbit

    >>

    2;

    b00011010.

    . =3.14159,

  • 39

    g=9.81m/sec2 . . . #define. #define:

    #define__

    . 10cm.

    #include#include

    #definepi3.14159 /*pi=3.14159*/

    intmain(void){

    doubleR,S,E; /**/R=10.; /**/S=2.*pi*R; /*pi*/E=pi*pow(R,2.); /*pi*/

    printf(S=%fcmE=%fcm^2\n,S,E); /**/ return0;

    }

    2.12 scanf().

    scanf() (). scanf() . .scanf()printf()stdio.h. 2.6scanf().

    2.6 a. scanf() 2.4. &, .& . & .

  • 40

    scanf(%d,&a);

    2.6scanf().

    2.4scanf().

    d int.c char.s .lf double.f float.c char.

    2.7 scanf(). intfloat,.

    2.7scanf().

    &

    a

  • 41

    ( ):

    Pleaseinserttwointegers:1234598765Pleaseinsertonerealnumber:987.654321Theintegersyouhavetypedare:1234598765Therealnumberyouhavetypedis:987.654321

    2.13 getchar(),putchar().

    , C . getchar() ,putchar() . scanf()printf().

    getchar() scanf()charc; charc;c=getchar(); scanf(%c,c);

    putchar() printf()

    charc; charc;c=k; c=k;putchar(c); printf(%c,c);

    putchar() ASCII(2.1).putchar(65)A.

    2.14 C.

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

  • 42

    , main(). :

    intinteger_mod(intx,inty)

    {

    intresult; result=x%y; returnresult;

    }

    2.7.

    :

    .intint.doubledouble.void.

    . (AZaz),(09)(_). (+, ,*,/), (), (), (.)#,?,&,@.

    ... void.intmain(void).

    ({)(}).

  • 43

    ., . return.

    2.7.

    intinteger_mod(intx,inty) /*int,:integer_mod xy*/{

    intresult; /*result*/result=x%y; /*resultx%y*/returnresult; /*result*/

    }

    . . ,(ab) ( c), . :

    #include

    intinteger_mod(intx,inty); /**/

    intmain(void){

    inta,b,c; /*a,b,c*/ printf(Eisagetetaakaib:); scanf(%d%d,&a,&b); /**/

    c=integer_mod(a,b); /**/ printf(Ypoloipodiairesis%d\n,c); return0;

    }

    intinteger_mod(intx,inty) {

    intresult;result=x%y;returnresult;

    }

  • 44

    ! #include, . .

    :

    , ,

    ,(x,yz). (a),.:

    #include#include

    doublefx(doublex,doubley,doublez); /**/

    intmain(void){

    doublex,y,z,a; /*a,y,z,a*/

    printf(Dosetox:); scanf(%lf,&x); /*x*/ printf(Dosetoy:); scanf(%lf,&y); /*y*/ printf(Dosetoz:); scanf(%lf,&z); /*z*/

    a=fx(x,y,z); /**/ printf(a=%f\n,a); return0;

    }

    doublefx(doublex,doubley,doublez); {

    doublee; /*e*/e=pow(x,3.)+pow(y,2.)+z; /**/returne; /**/

    }

  • 45

    I:

    .

    3.1 .

    C .:

    ifelseelseif switch for while dowhile breakcontinue goto

    3.2 ifelseelseif.

    Cif.if:

    if(){ _1; _2; . .}

    .,,.

    1..if:

    if() _1;

  • 46

    !if.

    ifelse:

    if(){ _1; _2; .}else{ _A; _B; .}

    , _1, _2 . ,_,_.

    ,II. ( ), ().if:

    doublea,b,c; a=3.4; b=7.8; c=9.2;

    if(a

  • 47

    ifelseelseif:

    if(_1){ _1_1; _1_2; .}elseif(_2){ _2_1; _2_2; .}elseif(_3){ _3_1; _3_2; .}....else{ _A; _B; .}

    _1:_1_1,_1_2._2 : _2_1, _2_2 . _3 : _3_1, _3_2 . :_1,_2,_3.,else,_,_.

    .elseif.

    ..scanf(). if 3.1

  • 48

    3.1if.

    ifelse elseif 3.2

    3.2ifelseelseif.

    3.3

  • 49

    3.33.13.2.

    if(?:).:

    _1?_2:_3

    ifelse,:

    if(_1){ _2; }else{ _3;}

    3.3 switch.

    if .if, . C switch .switch( ) .switch:

    switch(){ case_1: _1_1; _1_2; .

  • 50

    . break; case_2: _2_1; _2_2; . . break; case_2: _2_1; _2_2; . . break; . . . default: _A; _B; . . break; }

    (case) .case. default. case defaultbreakswitch.breakcase.

    .17. 3.4. 3.43.5.

  • 51

    3.4case.

    3.53.4.

  • 52

    3.4 for.

    .().

    for,whiledowhile.,for,:

    for(_1;_2;_3){ _1; _2; . . }

    for (_1,_2.) : _1,_2_3.:

    _1. _2 .

    for.for.

    _3. _2.

    for.for.

    _3. _2.

    for.for.

    _3 _2 ._2for.

    for n ( n ).n :_1,_2_3.

    : for. 110.

  • 53

    #include

    intmain(void){ inti; /*i*/

    for(i=1;i

  • 54

    _2; . . }

    for (if()break).

    3.5 while.

    while for .while:

    while(){ _1; _2; . . }

    . (_1,_2.)..

    :for,while. 110.

    #include

    intmain(void){ inti; /*i*/

    i=1; /*i1*/while(i

  • 55

    i.i1.whilei

  • 56

    do{ /*dowhile*/ printf(%d\n,i); /*i*/ i++; /*i1*/ }while(i

  • 57

    inti; /*i*/ for(i=1;i

  • 58

    V:

    .

    4.1 .

    C, , 1972 Dennis Ritchie AT&T Bell Labs. ( Unix). (hardware)..

    . . C . , . .

    xfloat,3.1415.

    floatx; /*xfloat*/ x=3.1415; /*x*/

    . ;

    (RAM). . ( bytes) ., . , , . .

  • 59

    floatx; /*xfloat*/

    x. bfffdfe0.

    x=3.1415; /*x*/

    bfffdfe03.1415.x . &. x &x. printf()x,.

    printf(%f\n,x);=> 3.1415

    printf(%p\n,&x);=> bfffdfe0

    printf() %p. .:

    floatx; /*xfloat*/ float*point_x; /**point_xfloat*/

    x=3.1415; /*x*/ point_x=&x; /*point_xx*/

    RAM

    bfffdfe4

    3.1415x

    bfffdfdc

    ..

    bfffdfe8

    bfffdfe0

  • 60

    x float. point_x *. , (. x*y), ,.*point_xx. x 3.1415. point_xx.

    .

    printf() x, x , point_x, point_x *point_x.

    printf(%f\n,x); => 3.1415

    printf(%p\n,&x); => bfffdfe0

    printf(%p\n,point_x);=> bfffdfe0

    printf(%p\n,&point_x);=> bfffdfe4

    printf(%f\n,*point_x);=> 3.1415

    *point_x x. x *point_x . :

    *point_x=6.2;

    bfffdfe0

    RAM

    3.1415x

    bfffdfdc

    bfffdfe8

    bfffdfe0

    point_x bfffdfe4*point_x

  • 61

    x6.2.

    (by reference) , (hardware) . internet.

    4.2 .

    . , . . . .

    float2.1,7.2,6.4,9.3, 5.8. .:

    floata[5]; /*a*/ a[0]=2.1; /**/ a[1]=7.2; a[2]=6.4; a[3]=9.3; a[4]=5.8;

    :

    ..

    . []. n1, n

    . a[0] a[4].

    ! a[5] segmentation fault. ,a[7].

  • 62

    . :

    bytes.

    :

    floata[5]={2.1,7.2,6.4,9.3,5.8.}; floata[]={2.1,7.2,6.4,9.3,5.8.};

    ...

    double . 0., 1., 2., .9. for.

    inti; /*i*/ doublex[10]; /*x*/

    for(i=0;i

  • 63

    ,, . doublea[10].

    inti; /*i*/ doublea[10]; /*a*/

    for(i=0;i

  • 64

    chara[]=UniversityofIoannina;printf(%s\n,a); /*UniversityofIoannina*/

    scanf(),:

    charb[20]; /**/ scanf(%s,&b); /*b*/

    C. string.h ( #include). a b.:

    strlen(a):a. strcmp(a,b):ab.

    . >0

  • 65

    doublesum(doublea[]){ /*sum()*/doubles; /*s*/s=0.; /*s*/for(i=0;i

  • 66

    :

    doublea[3][4]={1.2,4.3,7.8,9.3,6.3,8.9,6.7,3.4,1.9,9.8,4.1,6.6};

    doublea[][4]={1.2,4.3,7.8,9.3,6.3,8.9,6.7,3.4,1.9,9.8,4.1,6.6};

    . ..

    . b[3][4][5] 60 .

    ,,. doublea[3][4]..

    doublea[3][4]; /**/inti,j; /*ij*/

    for(i=0;i

  • 67

    printf(\n); /**/ }

    for.....\n(for).

  • 68

    V:

    .

    5.1 .

    . ..

    C . :

    floata; /*float*/intb; /*int*/floatc[3]; /*float*/chard[3]; /**/a=7.1;b=10;c[0]=2.1;c[1]=4.5;c[2]=6.8;d=Hi;

    ,;.C.:

    .:

    structmy_example{floata; intb; floatc[3];chard[3];};

    ( ). my_example..

  • 69

    ().:

    structmy_examples1;()s1 . .

    . :

    s1.a=7.1; s1.b=10; s1.c[0]=2.1;s1.c[1]=4.5;s1.c[2]=6.8;s1.d=Hi;

    , (_._). ., printf(), scanf(),.

    :

    structmy_examples1={7.1,10,2.1,4.5,6.8,Hi};

    . :

    structmy_example{floata; intb; floatc[3];chard[3];}s1;

    C. .

    s1 . .

  • 70

    : . (2,3)(4,5)..

    .:

    structsimeio{ doublex; doubley;

    };

    :

    #include #include

    intmain(void){

    structsimeio{ /**/ doublex; doubley; };

    structsimeioA={2.,3.}; /**/ structsimeioB={4.,5.}; /**/ doublera,rb,rab; /*ra,rb,rab*/

    s1.a

    s1.b

    s1.c[0]

    s1.c[1]

    s1.d[2]

    10

    2.1

    4.5

    s1.d[0]

    s1.d[1]

    s1.c[2]

    \0

    i

    H

    6.8

    RAM

    7.1

  • 71

    ra=sqrt(pow(A.x,2.)+pow(B.y,2.)); /**/ rb=sqrt(pow(B.x,2.)+pow(B.y,2.)); /**/

    rab=sqrt(pow(B.xA.x,2.)+pow(B.yA.y,2.)); /**/

    printf(ApostasisimeiouAapoarxi=%f\n,ra); /**/ printf(ApostasisimeiouBapoarxi=%f\n,rb); printf(ApostasisimeionAB=%f\n,rab);

    return0; }

    : N.a=3+5ib=58i.

    structmigadikos{ /**/ doublere; doubleim;

    }; structmigadikosa={3.,5.}; /*a*/ structmigadikosb={5.,8}; /*b*/

    : N .Anagnostou,Dimitrios3241.

    ,.:

    structfoititis{ /**/ chareponimo[20];

    charonoma[20]; intAM;

    };structfoititisa={Anagnostou,Dimitrios,3241};

    20,int.

    5.4 .

    C . . :

  • 72

    structfoititis{ /**/ chareponimo[20];

    charonoma[20]; intAM;

    };structfoititisA[200];

    A[200]..:

    A[0].eponymoA[0].onomaA[0].AM

    :A[1].eponymoA[2].onomaA[3].AM

    ..

    5.3 .

    . migadikos , ..main().

    #includestructmigadikos{ /**/

    doublere; doubleim;

    };voidfx(structmigadikosb); /**/

    intmain(void){ structmigadikosa={2.,3.}; /*a*/ fx(a); /*fxa*/ return0; }

    voidfx(structmigadikosb){ /**/ printf((%f)+i(%f),b.re,b.im);

    }

  • 73

    . .. main() a.a.:

    voidfx(structmigadikosb)

    .voidprintf().

    5.4 .

    .:

    structvarious{ floata;intb;charc;};structvariouss;

    unionvarious{ floata;intb;charc;};unionvariouss;

    unionstruct..().

    s.a

    s.b

    s.c

    RAM

    s.a

    s.b

    s.c

    RAM

  • 74

    .. .

  • 75

    V:

    .

    5.1 .

    scanf() printf(). stdin (standard input), stdout(standardoutput),5.1(a).

    . , . . , (5.2(b)).

    5.1:a).b).

  • 76

    5.2 .

    .5.2.

    5.2:.

    x=3.14159.:

    #include

    intmain(void){ doublex; /*x*/ FILE*fp; /*fp*/

    x=3.14159; /*x*/fp=fopen(arxeio.data,w); /*arxeio.data*/

    fprintf(fp,%f,x); /**/ fclose(fp); /**/ return0; }

    :

    . : FILE*fp; fp ( ) FILE, .

    : fp=fopen(arxeio.data,w);

  • 77

    fopen():, arxeio.data. , w ( write) .! w arxeio.data...

    fprintf(): fprintf(fp,%f,x); fprintf() printf() ().

    ,fclose(): fclose(fp);

    5.3 .

    .5.3.

    5.3:.

    dedomena.txt 7.689. ,,.:

  • 78

    #include

    intmain(void){ doubley; /*y*/ FILE*pp; /*pp*/

    pp=fopen(dedomena.txt,r); /*dedomena.txt*/ fscanf(pp,%lf,&y); /*y*/ printf(y=%f\n,y); /*y*/

    fclose(pp); /**/ return0; }

    dedomena.txt:

    . : FILE*pp;pp ( ) FILE, .

    dedomena.txt: pp=fopen(dedomena.txt,r);fopen():, dedomena.txt. ,r(read) . ! dedomena.txt .

    dedomena.txt fscanf(): fscanf(pp,%lf,&y); fscanf() scanf() ().

    fclose(): fclose(pp);

    fopen(), :

    w:. r:.

  • 79

    a : .

    5.2 putc()getc().

    C . getc()putc().5.4.

    5.4:getc()putc().

    getc()putc() . scanf() printf().

    getc() scanf()

    charc; charc; c=getc(stdin); scanf(%c,&c);

    putc() printf()

    charc; charc; c=k; c=k; putc(c,stdout); printf(%c,c);

  • 80

    getc() putc() ,stdinstdout . getc() putc() .

    charc; /*char*/ FILE*fp; /*fp*/

    fp=fopen(dedomena.txt,r); /*dedomena.txt*/c=getc(fp); /*c*/fclose(fp); /**/

    charc; /*char*/ FILE*fp; /*fp*/ c=k; /*c*/

    fp=fopen(arxeio.data,w); /*arxeio.data*/putc(c,fp); /**/fclose(fp); /**/

    C fgetc() fputc() getc()putc().

    . , ,.EOF(EndOfFile). :

    charc; /*char*/ FILE*fp; /*fp*/

    fp=fopen(dedomena.txt,r); /*dedomena.txt*/

    while((c=getc(fp))!=EOF){ .}

    fclose(fp); /**/

  • 81

    : while((c=getc(fp))!=EOF){ . } .whileEOF.