2

                                        C

       C                                    Denis Ritchie

1970

                                                               UNIX)

                                                                   C




                                C                    Middle –lever

language)

           C                                 compiled       Language)

                   (Compiler)                                Compile)

                                               Machine Language)



               C

                   C
Int        main       (void)

{

                          Header          Files)

                                              C



#include<             >

#include                              “        ”



#include<stdio.h>
h

                         1

                                         stdio.h



    Global            (Global         Variables)




                                      Functions)

                 C                            1

             Main()                           1

             1

    Local             (Local          Variables)




                                    (Statements)



C                                              ;
C

                                           C

                      Case Sensitive

Test, test                                 C




    ;




             Return                    Value)




                                Comment)
2-2                         Comment

                     2       –       1




           Identifier)



1.                                              _

2.                       2                              _

3.                                                  _

4.                                         31

5.

6.                               C
A                                    $sum
            Student_name                      Student Name
            _SystemName                           2names
                A1                                     int


                                                  C



                                       C



                           C       4

                                           Void              Type        (Void)




                                             Integer            Type       (int)

                                                                               C

                               3              short int,int            long int

                                                                         2-1




Short int            2         16            -32,768            32,768
                                                  0             65,535
Int                      2       16              -32,768             32,768
(16                                                  0               65,535
Int                      4       32                  -           2,147,486,643
(32                                           2,147,486,643 4,294,967,295
                                                     0
Long int                 4       32                  -           2,147,486,643
                                              2,147,486,643 4,294,967,295
                                                     0
                                            Character            Type         (char)

                                      A-Z            0-9

                  ACSII (American Standard Code Information Interchange)

                                                                 1

                                               127

1           8

                                      Floating           Point       Type      (flat)

                                                                 3             float,

double           long double

                      2-2

           2-2



float                4                32                         -38           38
3.4-10             3.4-10
double             8               64         -
                                        308                 308
                                        1.7*10             1.7*10
long double        10              80         -
                                        4932                4932
                                        3.4*10             1.1*10




                               C         2

                                                     Global



                                                              Local



#include<stdio.h>

int       total;          /*                      Global            */

main()

{

int        price,money;            /*                        Local*/
…

}




        2-3




int                   total                =          0;



int                            total             =0,sum



int                           total            =0,sum=0;




total                                  =             50;



total                            =             total+sum



scanf(“%d”,&total);
2-2

#include<stdio.h>

/*                                                                   Global*/

int                   sum                            =                     0;

int                                                                main(void)

{

/*                                              Local                       */

int                                                                        a;

int                                                                        b;

int                                                                         c;




/*

printf(“nWelcome.             This             Program             addsn”);

printf(“threenumbers.Enter                   three               numbersn”);

printf(“in   the      form:         nnn        nnn       nnn      <retur>n”);

scanf(“%d                           %d                         %d”,&a,&b,&c);

/*                      a,b              c                             sum*/

sum=a+b+c;
printf(“The                total              is:                %dn”,sum);

printf(“Thant      you.            Have      a            good      day.n”);

return                                                                    0;

}



Welcome.                   This               Program                  adds

Three           numbers.             Enter            three          number

In       the       form:           nnn       nnn          nnn       <return>

11                                    22                                  23

The                    total                        is:                   56

Thank you. Have a good day.




([
(float)a

(int)a

                              2-3

#include<stdio.h>

int                                                                  main(void)

{

float                                a=                                25.3658;

printf(“Value                 of            a              :            %n”,a);

printf(“Value   of        a   when    set       is   integer   :   %dn”,(int)a);

return                                                                         0;

}



Value                of              a                 :             25.365801

Value of a when change is integer : 25

                                                                     Constants)



                                                                      Constants
Const[



const                 folat                      a                 =          5.23;

const int b = a%2;

                              2-4

#include<stdio.h>

imt                                                                     main(void)

{

const                float                  pi             =              3.14159;

float                                                                       radius;

radius                                           =                                3;

printf(“Value                 of                 pi            :          %fn”,pi);

printf(“Value         of            area              :   %fn”,pi*(radius*radius));

return                                                                            0;

}



Value                 of                   pi              :              3.141590

Value of area : 28.274311
constant

Integer Constants

const int a = 5;

Floating-Point        Constants

const float b = 5.6394;

Character Constants

‘’

const char b = ‘t’;

String   Constants

“”

“”

“h”

“Hello                                    worldn”

“HOW                              ARE       YOU”

“Good                                   Morning!”

                   2-5

#includ<stdio.h>

int                                     main(void)
{

const           int              a            =       3;            /*Integer        Constats*/

const    flat         b     =         3.14159;    /*Floating          –     Point   Constants*/

const       cahr                 c        =       ‘P’;         /*Character          Constants*/

printf(“Value                             of                     a:                   %dn”,a);

printf(“Value                             of                     b:                   %dn”,b);

printf(“Value                             of                     c:                   %dn”,c);

printf(“Good                          Bye”);                 /*String               Constants*/

return                                                                                       0;

}



Value                            of                      a                      :            3

Value                       of                    b                     :             3.141590

Value                            of                      c                    :              P

Good Bye

Statements

                          statements                            c
c                   6

Expression Statement           Compound Statement                   2


   1. Expression Statement                          Single Statement

      Statement                             ;           statement

      C                    ;

      Statement


                           a                    =                          2;



printf(“x contains %d, y contains %dn”,x,y);

Compound Statement                                                      Block

                       {                            }

                                Statement                    Main

           C Compound Statement




Flowchart)
1.

                                    Terminator




                      2.

                          Process




                      3.




Decision




                     4.




Data




           5.

Manual                                   Input
6.




Display




                   7.




Predefined                  Process




8.

Connect




              9.

                             Arrow



                        3

1.
2.
if…else




3.

               do….while
บทที่ 2

บทที่ 2

  • 1.
    2 C C Denis Ritchie 1970 UNIX) C C Middle –lever language) C compiled Language) (Compiler) Compile) Machine Language) C C
  • 2.
    Int main (void) { Header Files) C #include< > #include “ ” #include<stdio.h>
  • 3.
    h 1 stdio.h Global (Global Variables) Functions) C 1 Main() 1 1 Local (Local Variables) (Statements) C ;
  • 4.
    C C Case Sensitive Test, test C ; Return Value) Comment)
  • 5.
    2-2 Comment 2 – 1 Identifier) 1. _ 2. 2 _ 3. _ 4. 31 5. 6. C
  • 6.
    A $sum Student_name Student Name _SystemName 2names A1 int C C C 4 Void Type (Void) Integer Type (int) C 3 short int,int long int 2-1 Short int 2 16 -32,768 32,768 0 65,535
  • 7.
    Int 2 16 -32,768 32,768 (16 0 65,535 Int 4 32 - 2,147,486,643 (32 2,147,486,643 4,294,967,295 0 Long int 4 32 - 2,147,486,643 2,147,486,643 4,294,967,295 0 Character Type (char) A-Z 0-9 ACSII (American Standard Code Information Interchange) 1 127 1 8 Floating Point Type (flat) 3 float, double long double 2-2 2-2 float 4 32 -38 38
  • 8.
    3.4-10 3.4-10 double 8 64 - 308 308 1.7*10 1.7*10 long double 10 80 - 4932 4932 3.4*10 1.1*10 C 2 Global Local #include<stdio.h> int total; /* Global */ main() { int price,money; /* Local*/
  • 9.
    … } 2-3 int total = 0; int total =0,sum int total =0,sum=0; total = 50; total = total+sum scanf(“%d”,&total);
  • 10.
    2-2 #include<stdio.h> /* Global*/ int sum = 0; int main(void) { /* Local */ int a; int b; int c; /* printf(“nWelcome. This Program addsn”); printf(“threenumbers.Enter three numbersn”); printf(“in the form: nnn nnn nnn <retur>n”); scanf(“%d %d %d”,&a,&b,&c); /* a,b c sum*/ sum=a+b+c;
  • 11.
    printf(“The total is: %dn”,sum); printf(“Thant you. Have a good day.n”); return 0; } Welcome. This Program adds Three numbers. Enter three number In the form: nnn nnn nnn <return> 11 22 23 The total is: 56 Thank you. Have a good day. ([
  • 12.
    (float)a (int)a 2-3 #include<stdio.h> int main(void) { float a= 25.3658; printf(“Value of a : %n”,a); printf(“Value of a when set is integer : %dn”,(int)a); return 0; } Value of a : 25.365801 Value of a when change is integer : 25 Constants) Constants
  • 13.
    Const[ const folat a = 5.23; const int b = a%2; 2-4 #include<stdio.h> imt main(void) { const float pi = 3.14159; float radius; radius = 3; printf(“Value of pi : %fn”,pi); printf(“Value of area : %fn”,pi*(radius*radius)); return 0; } Value of pi : 3.141590 Value of area : 28.274311
  • 14.
    constant Integer Constants const inta = 5; Floating-Point Constants const float b = 5.6394; Character Constants ‘’ const char b = ‘t’; String Constants “” “” “h” “Hello worldn” “HOW ARE YOU” “Good Morning!” 2-5 #includ<stdio.h> int main(void)
  • 15.
    { const int a = 3; /*Integer Constats*/ const flat b = 3.14159; /*Floating – Point Constants*/ const cahr c = ‘P’; /*Character Constants*/ printf(“Value of a: %dn”,a); printf(“Value of b: %dn”,b); printf(“Value of c: %dn”,c); printf(“Good Bye”); /*String Constants*/ return 0; } Value of a : 3 Value of b : 3.141590 Value of c : P Good Bye Statements statements c
  • 16.
    c 6 Expression Statement Compound Statement 2 1. Expression Statement Single Statement Statement ; statement C ; Statement a = 2; printf(“x contains %d, y contains %dn”,x,y); Compound Statement Block { } Statement Main C Compound Statement Flowchart)
  • 17.
    1. Terminator 2. Process 3. Decision 4. Data 5. Manual Input
  • 18.
    6. Display 7. Predefined Process 8. Connect 9. Arrow 3 1.
  • 19.
  • 20.
    if…else 3. do….while