C 2η διάλεξη

download C 2η διάλεξη

of 19

description

Εισαγωγή στη C

Transcript of C 2η διάλεξη

  • 1 () 101 () 101 / & / &

    . .

    . .

    **********--mail: mail: [email protected]@ced.tuc.gr**********Web: Web: http://www.music.tuc.gr/kmaniahttp://www.music.tuc.gr/kmania

    Office:Office: 145.14145.14Tel: 28210 37222Tel: 28210 37222

    -- ::http://http://courses.ced.tuc.grcourses.ced.tuc.gr

    ( ([email protected]@ced.tuc.gr)) ( ([email protected]@ced.tuc.gr)) ([email protected])([email protected])

    ::http://http://courses.ced.tuc.grcourses.ced.tuc.gr

    : : 1155:00:00 -- 1188:00 :00 145.145.4242

    113 003 00 16 0016 00 : : 113.00 3.00 -- 16.0016.00

    : : 09.00 09.00 --1111.00.00, 145., 145.4242

  • 2

    C, Aitken, C, Aitken, Jones Jones ISBN 9605124912ISBN 9605124912 ISBN 9605124912, ISBN 9605124912,

    C, EC, E , , Aitken & Jones, ISBN 0Aitken & Jones, ISBN 0--672672--3106931069--44

    CC

    ::http://courses.ced.tuc.grhttp://courses.ced.tuc.gr

    ..40% : 40% : 22 . . onlineonline online online ..

    , , online online (, ) (, )

    60% : .60% : . . .

    6, ( 6, ( ) .) . coursescourses ( ).( ).

    , , . .

  • 3 . . . . . . . . . .

    . . / / . .

    17:00 17:00 ( , 141.35). ( , 141.35).

    /. /. /. /. .. . .

    2200

  • 4 ( (problem solving) problem solving) , , , , , , . .

    (( ) ) ( (Top Down)Top Down) ( (Bottom Up) Bottom Up)

    ( (top down problem solvingtop down problem solving) ) :: ::

    1.1. -- ((subsub goalsgoals))..2.2. . . --, ., .3.3. -- . .

    - 1

    - 2

    - 3

    - 1-1

    - 1-2

    - 2-1

    - 3-1

    - 3-2

    ( (bottom up problem solvingbottom up problem solving) ) ::

    :: ::1.1. , ,

    . .2.2. , ,

    . .3.3. , ,

    . .

  • 5

    1 2 3

    1-1 1-2 2-1 3-1 3-2

    ( (mixed problem mixed problem solving methodology) solving methodology) . .

    , , --/ / , , ..

    ::

    . . , , , , ..

    . .

    ;; (Algorithm)(Algorithm) () () .. .. ( (time complexitytime complexity)), , ((space complexityspace complexity)), , ((parameterizationparameterization)), , ((reliabilityreliability)), , ( (robustnessrobustness) ) .. ( ( ), ), ( ). ( ).

  • 6

    /Domain Knowledge

    /Compiler/Assembler/Linker /Data

    - / /

    -

    /Problem Definition

    /Algorithm

    (Source Code) .. C

    /Result

    (C, C++, Java, Pascal, Fortran, Cobol .) :

    1. . (functional

    requirements analysis).. - (non-

    functional requirements analysis).

    .................. ............................... .............

    2. ( (design specifications).3.

    (implementation).4. -

    (testing).5. , , (maintenance).

    ( ( !!!) !!!)

    :: (( ) 6000 ) 6000

    ( .. ).( .. ). 50 ( 50 ( 50 ( 50 ( ) ) . .

    11200. 11200.

  • 7 0 1 ( 0 1 ( ) ).. . .

    .. . . ( (assemblerassembler)).. :: MOV A, [6000]MOV A, [6000] SUB A, 50SUB A, 50 JG 11200JG 11200

    ;;

    6000 6000 . . 50 50 . . 50, 50, 11200 11200 . .

    ( (mnemonicmnemonicnamesnames) ) . . ::

    MOV A, [FinalMark]MOV A, [FinalMark]SUB A, 50SUB A, 50JG HandleFinalMarkErrorJG HandleFinalMarkError

    . .

  • 8 .. .. CC ..

    ::if (if (FinalMarkFinalMark > 50) > 50)

    gotogoto HandleFinalMarkErrorHandleFinalMarkError

    ::

    ( (Level of Abstraction)Level of Abstraction)(( )) ( (Programmatic paradigm)Programmatic paradigm) ( (Programmatic structure)Programmatic structure)

    (programmatic variable types)(programmatic variable types)

    ((C, Fortran, Cobol, C, Fortran, Cobol, Pascal, Pascal, AdaAda, Java ), Java )..

    (8086, (8086, P3, Motorola 6000) P3, Motorola 6000) ..

    0 1. 0 1.

    ( (C, ModulaC, Modula--22 .). .).

    ((imperative/algorithmic/procedure oriented)imperative/algorithmic/procedure oriented) proceduresprocedures.. ((functional languages) functional languages) . . ((object oriented languages) object oriented languages) (encapsulate) (encapsulate) (methods) (methods) objects objects instances instances of classes of classes ( (data fields) data fields) . . (logic programming languages) (logic programming languages) , , . .

  • 9

    . .

    ( (Strongly Typed).Strongly Typed). . . . . ( (Weakly Typed).Weakly Typed). . . (Static typing).(Static typing). . . ( (Dynamic typingDynamic typing).). . .

    C C . .

    :: ( ) ( ) . . (.. , (.. , .). .). ( (librarylibrary)) ((compiledcompiled) ) , , ( (linkedlinked) ) . . , , . , . , ( (includeinclude) ) . .

    LIB 1 LIB 2 LIB 3

    Include

    & &

    CCCC

  • 10

    CC Dennis Ritchie Dennis Ritchie Bell Labs USA Bell Labs USA to 1972to 1972.. UNIX.UNIX. EE . . American American National Standards Institute (ANSI)National Standards Institute (ANSI) CC ANSI standard C.ANSI standard C.

    CCH C H C .. .. C C .. C C . .

    CCH C H C . . C C . . ..

    CC C C .. CC C C Java, C++, Java, C++, ..

  • 11

    (source code)(source code) . . . . . . . .c.c.

    C:C:

    ##includeinclude

    /

    -

    ##include include mainmain()(){{

    printfprintf(Hello World!)(Hello World!);;}}

    main

    printf stdio (standard output)

    AKOMH ENA AKOMH ENA CC ((hello.chello.c):):

    ##include include mainmain()()mainmain()(){{

    printfprintf(Hello World(Hello World!!));;}}

    Hello world! Hello world! . .

    . . .. compiler.compiler.

  • 12

    C C compiler compiler . . 1.1. -- ((hello.chello.c):): ( (

    compiler)compiler) -- ( (prepre--processor). processor). -- : : ,, ,, ((source codesource code)). . -- #. #. #include stdio.hstdio.h #include. . -- ##..

    2.2. (hello.obj (hello.obj hello.ohello.o)):: TT object codeobject code ( ( ) ) 3.3. ( (linking linking hello.exehello.exe)):: ( ) ( ) (.. . (.. .exe)exe) ..

    . .

    1.1. . .2.2. , , ( (compiler) compiler) . .3.3. . .

    1 1 (, , (, , CC). ).

    1.1. O O ( ( ) . ) . . .

    11 ##include include 22 mainmain()()33 {{44 printfprintf(Hello World(Hello World!!))55 }}

    C .

    1 111 ##include include 22 mainmain()()33 {{44 printfprintf(Hello World(Hello World!!))55 }}

    5

    hello.c (5) : Error: ; expected

    C . 5 4 C 5. .

  • 13

    1 111 ##include include 22 mainmain()()33 {{44 printfprintf(Hello World(Hello World!!));;pp (( ));;55 /* /* at the end at the end */*/55 }}

    hello.c (4): Error: undefined identifier Hello

    hello.c (5): Lexical error: unterminated string

    Lexical error: unterminated string

    Fatal error: premature end of source file , .

    CC

    directivesdirectivesmain()main()

    { { variable declarationsvariable declarationsstatementsstatements

    }}

    CompilerCompiler

    OO .. C C . .

    #include

    intint radius, area;radius, area;

    main()main()main()main(){{

    printfprintf( Enter radius );( Enter radius );scanfscanf( %d, &radius );( %d, &radius );area = (area = (intint) (3.14 * radius * radius);) (3.14 * radius * radius);printfprintf( ( \\nn\\nAreanArea = %d= %d\\n, area );n, area );return 0; return 0;

    }}YY . .

  • 14

    ? ?#include

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

    printfprintf( Keep looking( Keep looking\\n );n );printfprintf( You will find it );( You will find it );return 0; return 0;

    }}

    ? ?#include

    main()main()()(){{

    printfprintf( This is a program with a );( This is a program with a );do_itdo_it( problem );( problem );return 0; return 0;

    }}

    ? ? (editor) (editor) ?? AA , , ( (.c).c)?? ??

    :: / / / / () () () ()

    CC

  • 15

    .. :: ( (assignment statement)assignment statement) ( ( g )g ) ( (conditional statements)conditional statements) ( (iterative statements)iterative statements) ( (expression statements)expression statements) ( (;) ;) semicolon semicolon directives directives . .

    DIRECTIVESDIRECTIVES --..

    ::

    .. ..##include #include #include myBestLibrary.hmyBestLibrary.h

    .. ..#define FREEZING_POINT_CELCIUS 0.0#define FREEZING_POINT_CELCIUS 0.0#define FREEZING_POINT_FARHENEIT 32.0#define FREEZING_POINT_FARHENEIT 32.0##define HALFOF(value) ((value)/2)define HALFOF(value) ((value)/2)

    //-- ( (routines, subroutines, routines, subroutines, procedures)procedures) . . . . ((high cohesion) high cohesion) ((low coupling).low coupling).

    :: library functions library functions ( )( ) application functions application functions ( ( application functions application functions ( ( )) C C ( ) .. ( ) ..

    int i = 0;i = addOne(i);

    int addOne(int j){

    j = j+1;return (j);

    }

    mainmain main main C C .. main main C C . . . . main main , , . .

  • 16

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

    /*/* this is a comment in C */this is a comment in C *//* /* this is a comment in C */this is a comment in C */

    . . ( ) .. ( ) ..

    /***********************************************/********************************************************************** * Name: Name: main.cmain.c *** Author: John Smith ** Author: John Smith ** Purpose: Translate Celsius degrees to Fahrenheit ** Purpose: Translate Celsius degrees to Fahrenheit ** Date/Version: 12/12/02 v1.1 ** Date/Version: 12/12/02 v1.1 *************************************************************/************************************************************/

    C++ C++ Java Java //. //. CC /*/* ..

  • 17

    :: -- mainmain

    main (main ( 8 8--23)23):: CC. . main () { }main () { } ..

    :: -- #include#include

    H H include include C C . . . .

    ::

    .. C C . . ..

    :: (6) (6) . . . . . .

  • 18

    :: //printf(): printf(): . .scanf(): scanf(): . .

    tt return: return: x x y y product(). product(). return return 22 22 0 0 . .

    ! ! , , , , . .

    Enter a number between 1 and 100: 35Enter a number between 1 and 100: 35Enter another number between 1 and 100: 23Enter another number between 1 and 100: 23

    35 times 23 = 80535 times 23 = 80535 times 23 = 80535 times 23 = 805

    #include

    void void display_linedisplay_line (void);(void);

    main()main(){{

    display_linedisplay_line();();printfprintf( ( \\n Teach n Teach YouselfYouself C in 21 daysC in 21 days\\n);n);display_linedisplay_line();();return 0 ;return 0 ;

    }}

    void void display_linedisplay_line (void)(void){{

    intint counter;counter;for(counter = 0; counter

  • 19

    #include

    main()main()

    {{intint ctrctr;;for( for( ctrctr = 65; = 65; ctrctr < 91; < 91; ctrctr++)++)

    printfprintf(%c, (%c, ctrctr););

    return 0;return 0;

    }}

    ::

    / / / / ( )( )()()

    CC

    CC: : Directives, Directives, , , , , , , MM

    ?? ? ? ? ? ? ?