โครงสร้างภาษาซี1

  • View
    5

  • Download
    0

Embed Size (px)

DESCRIPTION

 

Transcript

  • ! Interactive

    2

  • 3

    PreprocessorDirectives

    Global Declarations

    void main(void) {

    }

    Local Declarations

    Statements

  • Preprocessor Directives 12 34 file 1738917:;:

    13 9 (Header File) 774 Directive A#C

    4

  • Preprocessor Directives

    5

    Directive

    #include Include text from file

    #define Define a macro

    #undef Undefine a macro

    #if Test if a compile-time condition hold

    #ifdef Test if a symbol is defined

    #ifndef Test if a symbol is not defined

    #else Indicate alternatives if a test fails

    #elif Combination of #if and #else

    #endif End a preprocessor conditional

    #line Give a line number for compiler message

    #error Terminate processing early

    #pragma Implementation dependent directive

  • #include

    6

    EF: Compiler 9IJ!7 Compile

    84

    #include Astdio.hC 9IJ! stdio.h

    #include Aconio.hC 9IJ! conio.h

    #include FILE NAME

    49

  • #define

    7

    E389 F!:;7

    #define PI 3.14 389 PI 19 3.14

    #define NAME VALUE

    49

    #define X 3.14*2 389 X 19 3.14*2

  • Global Declarations E917:;:\

    J]!;17:;: 841712 ^ 9F_4:;17

    \I:9I8

    8

  • Main () function 9J]!; 12 ^ F 713F37_8F

    main() 94F84 ; 784 main() 84 { F84 }

    9

  • 9c 4 (Program Comment)

    917:;4c 413

    10

    //3 comment 1

    18

    /*3 comment 1

    18*/

    /*3 comment 9 1

    18fffffffffff.*/

  • ! Interactive

    11

  • #include Astdio.hC

    void main() {

    ffffffffff.;

    ffffffffff.;

    }

    12

  • #include Astdio.hC

    void main() {

    printf(AHello WorldC);

    // !39 Hello World

    }

    13

    Hello World

  • #include Astdio.hC

    void main() {

    printf(AHello\nC);

    printf(AWorldC);

    }

    14

    HelloWorld

  • #include Astdio.hC

    void main() {

    printf(AHello\nC);

    printf(AWorld\nC);

    }

    15

    HelloWorld

  • ! Interactive

    16

  • #include Astdio.hC

    #define PI 3.14

    void main() {

    float r = 20.00, circum = 0.0;

    circum = 2 * PI * r;

    printf(ACircumference is %f Radius is %f\nC, circum,r); }

    17

    20.00r

    0.0area

    125.6

    3.14PI

  • ! Interactive

    18

  • 9

    19

    keyboard

    monitor

    scanf(...)

    printf(...)

    Memory

  • 8n9

    20

    J]!;8n printf()

    putchar()

    puts()

  • 8n9

    21

    J]!; scanf() getchar() getch() getche() gets()

  • 8n9

    22

    J]!;8n printf()

    putchar()

    puts()

  • J]!; printf EJ]!;: !9 ^ 1

    F 13 #include Astdio.hC

    23

    printf(Control String,arg1,arg2,arg3,..);printf(Control String);

  • 24

    printf(%d\n%d,a,b);

    %d\n%d a,b

    22 30a b

    22 -> 30.

    Output Stream

    2230

    Memory

  • J]!; printf : 49

    25

    printf(AComputer Programming 1C);

    printf(AComputer\nProgramming 1C);

    printf(AResult is %fC,area);

    printf(AResult is\n%fC,area);

  • 26

    F478nc! 10 + 3 F

    X = 10 + 3

    PRINT X

    START

    END

    J]!; printf : 49

    38 X E17n(1. 3q9 10 + 3 : X)1. X = 10 +3(2. 89 X 1F)2. PRINT An 10 + 3 CPRINT X

  • J]!; printf : ;

    27

    \

    \

    \a

    cursor !" 1 $!%!&' ( $!%!&'")(%\b

    %(+&,)- " 1 +(\f

    cursor /01$(+2!)\r

    %(+33 6 $!%!&'\t

    670+2!)8\n

    n17I8

  • J]!; printf : 2 !

    28

    8( 6:+,)%o

    8( 6:+&%x

    8( %%%

    8( address 6 pointer%p

    8($!%!&'$!%)% (char)%c

    8(/D)$!%!&' (string) 6(%%s

    8(1, 6FG+%+F 8+1 6&&G !%e

    8(1, 62H+ long double%Lf

    8(1, 62H+ double%lf

    8(1, 62H+ float%f

    8(1, 6FG+%+$K"%u

    8(1, 6FG+%+$K long int%ld

    8(1, 6FG+%+$K:+%d

    :;74

  • J]!; printf : 49

    29

    int j=45, k = -123;float x = 12.34char c = A;char *m = Hello; char mm[6] = Hello ;

    printf(Hello\n);printf(Tax is 10%%\n);printf(%d\n,j);printf(%d\n,k);printf(%f\n,x);printf(%6.2f\n,x);printf(%c\n,c); printf(%d\n,c);

  • J]!; printf : 49

    30

    printf(%d\n,c+1);printf(%c\n,c+1);printf(%s\n,m);printf(%s\n,mm);printf(%e\n,x);printf(%E\n,x);

    int j=45, k = -123;float x = 12.34char c = A;char *m = Hello; char mm[6] = Hello ;

  • J]!; printf : 49

    31

  • J]!; printf : 49

    32

    12345

    1234

    123

    12

    1234512345

    12341234

    123123

    1212%4d%d9

  • #include Astdio.hC

    void main() {

    printf(AYes\nC);

    printf(ANo\nC);

    }

    33

    #include Astdio.hC

    void main() {

    printf(AYes\nC);

    printf(A\nC);

    printf(ANo\nC);

    }

  • #include Astdio.hC

    void main() {

    printf(AYesC);

    printf(ANoC);

    }

    34

    #include Astdio.hC

    void main() {

    printf(AYes\nNoC);

    }

  • #include < stdio.h >void main ( ) {

    //\w7;7 number ; 8EF3int number;// 389 8 : numbernumber = 8; printf (AThe value of number is %d . C, number);

    }

    35

    The value of number is 8 .

  • #include Astdio.hC

    #define PI 3.14

    void main() {

    float r = 20.00, circum = 0.0;

    circum = 2 * PI * r;

    printf(ACircumference is %.1f\nC, circum); }

    36

    Circumference is 125.6

  • #include Astdio.hC#define PI 3.14 void main ( ) {

    float r, area; printf (AThis program computes the area of a circle\nC);r = 12.5; /* 38: \9E 12.5 */area = PI * r * r; /* 17 = r2 */printf (AArea = Pi x radius x radius\nC);printf (A = %.2f x %.2f x %.2f\nC, PI, r, r);printf (A = %f\nC, area);

    37

    This program computes the area of a circleArea = Pi x radius x radius

    = 3.14 x 12.50 x 12.50= 490.873444

  • #include < stdio.h >#define SIXTY 60 /* 4 SIXTY 9E917}q! */void main ( ) {

    float hour; //;7 hour _\:; 8EF3F int minute, second; // 2 _\ 84; 8EF3hour = 1.5; //389 1.5 : hour//q9 hour 84 60 38:E9 minuteminute = hour * SIXTY; //q9 minute 84 60 38:E9 secondsecond = minute * SIXTY;printf (AIn one period : \n %.2f hours\nC, hour);printf (A%d minutes\n%d secondsC, minute, second);

    }

    38

    In one period :1.50 hours90 minutes5400 seconds

  • 8n9

    39

    J]!;8n printf()

    putchar()

    puts()

  • J]!; putchar J ;178n1F 1

    13 #include Astdio.hC

    40

    putchar(variable);

  • J]!; putchar : 49

    41

    #include stdio.hvoid main ( ) {

    char s;s = A;putchar(s);

    }

    A

  • 8n9

    42

    J]!;8n printf()

    putchar()

    puts()

  • J]!; puts J ;178n1F :;7 8n 17

    4 1 variable 4_w 17 8n F

    ; 84 1

    43

    puts(variable);

  • J]!; puts : 49

    44

    #include stdio.h#includevoid main ( ) {

    char str[30];str = C Programming;strcpy(str,C programming);puts(str);printf(\nOutput = %s\n,str);

    } C ProgrammingOutput = C Programming

  • 8n9

    45

    J]!; scanf()

    getchar()

    getch(), getche()

    gets()

  • J]!; scanf EJ]!;:99F84!!8I

    :1738 F8 Enter F_I :

    Fw

    18:9 13 include Astdio.hC

    46

    scanf(Control String,&arg1,&arg2,&arg3,..);

  • 47

    scanf(%d%f,&a,&b);

    %d%f a,b

    22 30a b22 30.

    Input Stream int a;float b;scanf(A%d%fC,&a,&b);

  • J]!; Scanf : 49

    48

    int num;scanf(A%dC,&num);printf(Athe value in num variable is %dC,num);

    char str[80];printf(AEnter a string : C);scanf(A%sC,str);printf(AHere s your string : %sC, str);

  • #include < stdio.h >void main ( ) {

    int years; printf (AHow long have you been here? C);scanf (A%dC, &years);printf (AYouve been here for %d years.C, years);printf (A\tReally?C);

    }

    49

    How long have you been here? 2020Youve been here for 20 years. Really?

  • 8n9

    50

    J]!; scanf()

    getchar()

    getch() ,getche()

    gets()

  • J]!; getchar EJ]!;:; 3n 1

    84F 1 1 8F

    713_w37 F4287: 1 F8 Enter F_I : w7 ; 8 Fw

    18:

    51

    ch = getchar();

  • J]!; getchar : 49

    52

    #include stdio.hvoid main ( ) {

    char ch;ch = getchar();printf(The Character you typed is %c\n, ch);

    }

    d The Character you typed is d

  • 8n9

    53

    J]!; scanf()

    getchar()

    getch(),getche()

    gets()

  • J ;:

    1 84F: : I 8 Enter

    J]!; getch(),getche() J ;:

    1 84I

    : : I 8 Enter

    54

    ch = getch();

    ch = getche(); #include

  • J]!; getch : 49

    55

    #include stdio.h#include conio.hvoid main ( ) {

    char ch;ch = getch();printf(The Character you typed is %c\n, ch);

    }

    The Character you typed is d

  • 8n9

    56

    J]!; scanf()

    getchar()

    getch()

    gets()

  • J]!; gets J ;:

    ; 8

    w7 1 713_w37 F4287:

    F8 Enter 18F_I :17 4 Fw

    18:

    57

    gets(variable);

  • J]!; gets : 49

    58

    #include stdio.hvoid main ( ) {

    char str[51];gets(str);printf(The Message you typed is %s\n, str);

    }

    testThe Message you typed is test

  • ! Interactive

    59

  • Interactive !17134

    17n:;139 2I ;9

    L F473q4178 w7774nn 841917 8n1F

    L 473q17 8438: