SlideShare ist ein Scribd-Unternehmen logo
1 von 79
Downloaden Sie, um offline zu lesen
บทท 3
    พนฐาน
โปรแกรมภาษา C

                  อ.วราวฒ แขงขน
มหาวทยาลยราชภฏสวนดสต ศนยพษณโลก
เนอหา

  1.ประวตความเปนมา
  2.ข!อดของภาษา C
  3.กระบวนการแปลโปรแกรมภาษา C
  4.ตดตงโปรแกรม
  5.โครงสร!างโปรแกรมภาษา C
  6.กฎเกณฑ(ของโปรแกรมภาษา C

K.Warawut         บทท 3 พนฐานของโปรแกรมภาษา C   2
เนอหา (ตอ)

  7.ตวแปรในภาษา C
  8.ตวด*าเนนการในภาษา C
  9.นพจน(ในภาษา C
  10.การเปลยนชนดข!อม.ลของตวแปรในภาษา C
  11.การแสดงผลในภาษา C
  12.การรบข!อม.ลในภาษา C
  13.หมายเหต1 (Comment)
K.Warawut       บทท 3 พนฐานของโปรแกรมภาษา C   3
1. ประวตความเปนมา

    ภาษา               ภาษา                       ภาษา
    BCPL                B                          C
Basic Combined       บนเครอง                      พ.ศ. 2515
 Programming          PDP-7                    โดย เดนนช รทช
   Language           (Unix)
                     พ.ศ. 2513




K.Warawut        บทท 3 พนฐานของโปรแกรมภาษา C                   4
2. ขอดของภาษา C

  ●
      เปนภาษาระดบส.ง (High Level Language)
  ●
      ไม3ข4นกบระบบปฏบตการ (OS Independent)
  ●
      ไม3ข4นกบชนดของเครองคอมพวเตอร( (Hardware
      Independent)
  ●
      เปนภาษาโครงสร!าง (Structural Language)



K.Warawut          บทท 3 พนฐานของโปรแกรมภาษา C   5
3. กระบวนการแปลโปรแกรมภาษา C



  Source Code                                 Program



                      Compile

K.Warawut       บทท 3 พนฐานของโปรแกรมภาษา C             6
4. ตดต!งโปรแกรม

  ●
      โปรแกรม Editor ต3างๆ เช3น Notepad,
      Notepad++, EditPlus2, Turbo C, Turbo C++
      เปนต!น



  ●
      โปรแกรมทใช!ในการจดการเรยนการสอน คอ
      Bloodshed Dev-C++ Version 4.9.9.2 เปน
      Freeware (http://www.bloodshed.net/)
K.Warawut          บทท 3 พนฐานของโปรแกรมภาษา C   7
5. โครงสรางโปรแกรมภาษา C

#include <stdio.h>             /* Comment */


… Data Declarations ...;


int main ( ) {
    … Executable Statements ...;
    return 0;
}                บทท 3 พนฐานของโปรแกรมภาษา C   8
5. โครงสรางโปรแกรมภาษา C

#include <stdio.h>             /* Comment */


… Data Declarations ...;                       Comment


int main ( ) {
    … Executable Statements ...;
    return 0;
}                บทท 3 พนฐานของโปรแกรมภาษา C             9
5. โครงสรางโปรแกรมภาษา C

#include <stdio.h>             /* Comment */

                Header File
… Data Declarations ...; Directive
          Preprocessor

int main ( ) {
    … Executable Statements ...;
    return 0;
}                บทท 3 พนฐานของโปรแกรมภาษา C   10
5. โครงสรางโปรแกรมภาษา C

#include <stdio.h>             /* Comment */


… Data Declarations ...;

                              int n;
int main ( ) {               char c;
    … Executable Statements ...;
    return 0;
}                บทท 3 พนฐานของโปรแกรมภาษา C   11
5. โครงสรางโปรแกรมภาษา C

#include <stdio.h>         /* Comment */


… Data Declarations ...;
                              Start Program

int main ( ) {
    … Executable Statements ...;
               [
    return 0;
}                                             12
6. กฎเกณฑ'ของโปรแกรมภาษา C

  ●
      ฟ8งก(ชนแรกต!องเปน main( ) เสมอ
  ●
      ใช!ป9กกา ({ }) เปนตวก*าหนดขอบเขตการท*างาน
  ●
      ใช!เครองหมาย ; (semi colon) ป:ดท!ายค*าสงเสมอ
  ●
      ใช!เครองหมาย , (comma) เปนตวคนตวแปรและ
      พารามเตอร(
  ●
      ใช!เครองหมาย /* … */ เปนการอธบายการท*างาน
      ของโปรแกรม
K.Warawut           บทท 3 พนฐานของโปรแกรมภาษา C      13
Example 1 (ex0301_1.c)
 #include <stdio.h> /* #include “stdio.h” */
 #include <conio.h>


 int main( ) {
       printf(“Hello World!!!n”);
       getch();
       return 0;
 }


K.Warawut             บทท 3 พนฐานของโปรแกรมภาษา C   14
Example 1 (ex0301_2.c)
 #include <stdio.h> /* #include “stdio.h” */
 #include <stdlib.h>


 int main( ) {
       printf(“Hello World!!!n”);
       system(“PAUSE”);
       return 0;
 }


K.Warawut             บทท 3 พนฐานของโปรแกรมภาษา C   15
Example 2 (ex0302.c)
 #include “stdio.h”
 #include “conio.h”

 int main( ) {
       printf(“Hello “);
       printf(“World!!!”);
       printf(“n”);
       getch();
       return 0;
 }
K.Warawut              บทท 3 พนฐานของโปรแกรมภาษา C   16
Example 3 (ex0303.c)
 #include <stdio.h>
 #include <stdlib.h>


 int main( ) {
       printf(“Hello n”);
       printf(“World”);
       system(“PAUSE”);
       return 0;
 }

K.Warawut            บทท 3 พนฐานของโปรแกรมภาษา C   17
7. ต!วแปรในภาษา C (Variable in C)

  ●
      ต!วแปร (Variable) คอ การจองพนทในหน3วยความ
      จ*า เพอใช!ในการเก<บข!อม.ล เปรยบเสมอนการ
      ก*าหนดชอให!สงของหรอห!องพก ในการเรยกใช!
      ข!อม.ลก<จะท*าการเรยกผ3านตวแปรทเราก*าหนดข4น




K.Warawut          บทท 3 พนฐานของโปรแกรมภาษา C     18
ชนดของต!วแปรในภาษา C

  ●
      ต!วแปรพนฐาน (Scalar) หมายถ4ง ตวแปรทเก<บ
      ข!อม.ลได!เพยงค3าเดยว ภายในตวแปรตวเดยว
  ●
      ต!วแปรช*ด (Array) หมายถ4ง ตวแปรทเก<บข!อม.ลได!
      หลายค3า ภายในตวแปรตวเดยว และมชนดข!อม.ล
      เปนชนดเดยวกน
  ●
      ต!วแปรโครงสราง (Structure) หมายถ4ง ตวแปรท
      เก<บข!อม.ลได!หลายค3า ภายในตวแปรตวเดยว และม
      ชนดข!อม.ลหลายชนด
K.Warawut           บทท 3 พนฐานของโปรแกรมภาษา C    19
ต!วแปรพนในฐานในภาษา C

 แบ3งออกเปน 2 ชนด ดงน
  ●   Integer Types
  ●   Real Types




K.Warawut             บทท 3 พนฐานของโปรแกรมภาษา C   20
K.Warawut   บทท 3 พนฐานของโปรแกรมภาษา C   21
K.Warawut   บทท 3 พนฐานของโปรแกรมภาษา C   22
Example 4 (ex0104.c)
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>


int main( ){
     printf("Minimum char = %d, ", CHAR_MIN);
     printf("Maximum char = %dn", CHAR_MAX);
     printf("Minimum int = %i, ", INT_MIN);
     printf("Maximum int = %in", INT_MAX);
     system(“PAUSE”);
     return 0;
}
Minimum char = -128, Maximum char = 127               23

Minimum int = -2147483648, Maximum int = 2147483647
การต!งชอต!วแปรในภาษา C

  ●
      ต!องข4นต!นด!วยตวอกษร A-Z หรอ a-z หรอ
      เครองหมาย _
  ●
      ภายในชอตวแปรสามารถใช!ตวอกษร A-Z หรอ a-z
      หรอ 0-9 หรอเครองหมาย _
  ●
      ภายในชอตวแปรห!ามเว!นช3องว3าง หรอใช!สญลกษณ(
      อนนอกเหนอจากทระบ1ไว!
  ●
      ตวอกษรตวเล<กและตวใหญ3มความหมายแตกต3างกน

K.Warawut          บทท 3 พนฐานของโปรแกรมภาษา C     24
การต!งชอต!วแปรในภาษา C (ตอ)

  ●
      ห!ามตงชอซ*ากบค*าสงวน (Reserved Word) หรอ
      ค*าสงทใช!ในภาษา C
      auto break         case          char       const
      continue default   do            double     else
      enum extern        float         for        goto
      it       int       long          register   return
      short signed       sizeof        static     struct
      switch typedef     union         unsigned   void
      volatile while     etc.
K.Warawut           บทท 3 พนฐานของโปรแกรมภาษา C            25
ต.ย. การต!งชอต!วแปรในภาษา C

 Correct                       Incorrect
 average     pi                3rd_entry       all$done
 number_of_students            the end         int
 entry_total entryTotal
 all_total   allTotal




K.Warawut        บทท 3 พนฐานของโปรแกรมภาษา C              26
การประกาศต!วแปร

 ร-ปแบบ:
      type name;
                                                 Example:
                                                 int n;
 โดยท:                                           float f;
      type คอ ชนดของตวแปร                        char c;
      name คอ ชอของตวแปร                         double d;

K.Warawut          บทท 3 พนฐานของโปรแกรมภาษา C               27
การก.าหนดคาใหก!บต!วแปร

 ร-ปแบบ:
      type name = value;
                                  Example:
                       int n = 10;
 โดยท:                 float f = 10.5;
   type คอ ชนดของตวแปร char c = 'A';
   name คอ ชอของตวแปร double d = 250.00;
      value คอ ค3าทก*าหนดให!กบตวแปร
K.Warawut          บทท 3 พนฐานของโปรแกรมภาษา C   28
8. ต!วด.าเนนงานในภาษา C
  ●   Assignment Statement
  ●   Arithmetic Operators
  ●   Increment/Decrement Operators
  ●   Other Assignment Operators
  ●   Comparison Operators
  ●   Logical Operators

K.Warawut           บทท 3 พนฐานของโปรแกรมภาษา C   29
Assignment Statement

  ●
      ใช!เครองหมาย =
  ●
      ใช!ในการก*าหนดค3าให!กบตวแปร
 ร-ปแบบ:
      variable = expression;
 โดยท:
      variable คอ ชอของตวแปร
      expression คอ นพจน(หรอค3าทต!องการก*าหนด
K.Warawut           บทท 3 พนฐานของโปรแกรมภาษา C   30
Example Assignment Statement




K.Warawut   บทท 3 พนฐานของโปรแกรมภาษา C   31
Example 5 (ex0305.c)
#include <stdio.h>
#include <stdlib.h>


int main( ) {
      int term;
      int term_2;
      int term_3;                        term = 15
                                         term_2 = 30
      term = 3 * 5;                      term_3 = 45
      term_2 = 2 * term;
      term_3 = 3 * term;
      printf("term = %dn", term);
      printf("term_2 = %dn", term_2);
      printf("term_3 = %dn", term_3);
      system(“PAUSE”);
      return 0;                                        32
  }
K.Warawut   บทท 3 พนฐานของโปรแกรมภาษา C   33
Example 6 (ex0306.c)
#include <stdio.h>
#include <stdlib.h>

int main( ) {
    int a = 5, b = 2;
    int z;                       a+b=7
                                 a-b=3
    z = a + b;
                                 a * b = 10
    printf("a + b = %dn", z);
    z = a – b;
    printf("a - b = %dn", z);
    z = a * b;
    printf("a * b = %dn", z);
    system(“PAUSE”);
    return 0;                                 34

}
Example 7 (ex0307.c)
#include <stdio.h>
#include <stdlib.h>

int main( ) {
    int a = 5, b = 2;
    int z;
                                                      div = 2
    z = a / b;                                        mod = 1
    printf(“div = %dn", z);
    z = a % b;
    printf(“mod = %dn", z);
    // printf(“a %% b = %dn”, z);
    system(“PAUSE”);
    return 0;
}                       บทท 3 พนฐานของโปรแกรมภาษา C             35
Increment/Decrement Operators

  ●
      เปนการเพมค3า/ลดค3าทละหน4ง
  ●
      การเพมค3าทละหน4ง ใช!เครองหมาย ++
  ●
      การลดค3าทละหน4ง ใช!เครองหมาย –
 int i = 5, j = 4;

 i++;       // i = i + 1;        result = 6
 --j;       // j = j – 1;        result = 3
 ++i;       // i = i + 1;        result = 7
K.Warawut              บทท 3 พนฐานของโปรแกรมภาษา C   36
K.Warawut   บทท 3 พนฐานของโปรแกรมภาษา C   37
Example 8 (ex0308.c)
 #include <stdio.h>
 #include <stdlib.h>
                                                  n=5
 int main( ) {                                    n=6
      int n = 5;                                  n=6
                                                  n=7
      printf(“n = %dn”, n);
      printf(“++n = %dn”, ++n);
      printf(“n++ = %dn”, n++);
      printf(“n = %dn”, n);

      system(“PAUSE”);
      return 0;
 }
K.Warawut               บทท 3 พนฐานของโปรแกรมภาษา C     38
Example 9 (ex0309.c)
 #include <stdio.h>
 #include <stdlib.h>

 int main( ) {
      int value = 1, result;                            result = 8
                                                        value = 3
      result = (value++ * 5) + (value++ * 3);

      printf(“result = %dn”, result);
      printf(“value = %dn”, value);

      system(“PAUSE”);
      return 0;
 }
K.Warawut                 บทท 3 พนฐานของโปรแกรมภาษา C                39
ต!วด.าเนนการทางคณตศาสตร'
                ประเภทลดร-ป




K.Warawut      บทท 3 พนฐานของโปรแกรมภาษา C   40
Example 10 (ex0310.c)
#include <stdio.h>
#include <stdlib.h>

int main( ) {
    int a = 5, b = 1;                                 Before b = 1
                                                      After b = 6
    printf(“Before b = %dn”, b);

    b += a;       // b = b + a;

    printf(“After b = %dn”, b);

    system(“PAUSE”);
    return 0;
}                       บทท 3 พนฐานของโปรแกรมภาษา C                  41
Comparison Operators




K.Warawut        บทท 3 พนฐานของโปรแกรมภาษา C   42
Logical Operators




K.Warawut       บทท 3 พนฐานของโปรแกรมภาษา C   43
Table Logical Operators

       A       B      A && B A || B                  !A      !B
   TRUE       TRUE    TRUE           TRUE           FALSE   FALSE
   TRUE       FALSE   FALSE          TRUE           FALSE   TRUE
   FALSE      TRUE    FALSE          TRUE           TRUE    FALSE
   FALSE      FALSE   FALSE          FALSE          TRUE    TRUE




K.Warawut             บทท 3 พนฐานของโปรแกรมภาษา C                   44
9. นพจน'ในภาษา C

  ●
      นพจน' (Expression) คอ การน*าข!อม.ล ซ4งอาจจะ
      อย.3ในร.ปของค3าคงทหรอตวแปรมาด*าเนนการ โดยใช!
      เครองหมายต3างๆ เปนตวสงงาน ส*าหรบนพจน(ทเรา
      พบเห<นกนทวไปในชวตประจ*าวน เช3น y = 2x + 5,
      2xy – 5 เปนต!น




K.Warawut          บทท 3 พนฐานของโปรแกรมภาษา C   45
9. นพจน'ในภาษา C (ตอ)

  ●
      การเขยนนพจน'ในภาษา C
      คอ การน*าข!อม.ลและตวแปรในภาษา C มาด*าเนน
      การด!วยเครองหมายทางคณตศาสตร( ตรรกศาสตร(
      หรอ เครองหมายเปรยบเทยบในภาษา C เปนตวสง
      งาน




K.Warawut          บทท 3 พนฐานของโปรแกรมภาษา C   46
9. นพจน'ในภาษา C (ตอ)

  ●
      นพจน'ทางคณตศาสตร'
      การเขยนจะเหมอนกบการเขยนนพจน(ทาง
      คณตศาสตร(ตามปกต เพยงแต3เปลยนมาใช!
      เครองหมายทางคณตศาสตร(ของภาษา C แทน ซ4ง
      ต!องระวงเครองหมายบางตวทใช!ไม3เหมอนกน เช3น
      การค.ณจะใช!เครองหมาย * แทน x หรอหารจะใช!
      เครองหมาย / แทน


K.Warawut          บทท 3 พนฐานของโปรแกรมภาษา C    47
9. นพจน'ในภาษา C (ตอ)

  ●
      นพจน'ทางตรรกศาสตร'
      คอ การเขยนนพจน(โดยใช!เครองหมายการด*าเนน
      การทางตรรกศาสตร(ในภาษา C (&&, ||, !) เปนตวสง
      งาน ซ4งส3วนใหญ3แล!วนพจน(ทางตรรกศาสตร(จะอย.3
      รวมกบนพจน(ประเภทอนๆ เช3น
      c && (a <= b), (b >= c) || (c <= a)



K.Warawut          บทท 3 พนฐานของโปรแกรมภาษา C   48
9. นพจน'ในภาษา C (ตอ)

  ●
      ล.าด!บเครองหมายในการค.านวณ
      ส3วนใหญ3นพจน(ทเขยนข4นในโปรแกรมมกจะมความ
      ซบซ!อน มการด*าเนนการหลายอย3างปะปนอย.3
      ภายในนพจน(เดยวกน เช3น
      a / b + 15 * c หรอ (a – b) * 10 / c && d + 5
      ซ4งผลลพธ(จะออกมาเปนอย3างนน ต!องพจารณา
      จากล*าดบความส*าคญก3อนหลงของเครองหมายท
      ภาษา C ก*าหนดไว!

K.Warawut           บทท 3 พนฐานของโปรแกรมภาษา C      49
ล.าด!บของเครองหมายในการค.านวณ

     ล.าด!บความส.าค!ญ              เครองหมาย
            1                              ()
            2                 !, ++, --, (typecast)
            3                           *, /, %
            4                             +, -
            5                      <, <=, >, >=

K.Warawut               บทท 3 พนฐานของโปรแกรมภาษา C   50
10. การเปลยนชนดขอม-ลของต!วแปร
           ในภาษา C
 ถ!าน*าตวแปรต3างชนดมาด*าเนนการร3วมกน เช3น int +
 float หรอ int – char การทจะด*าเนนการตาม
 เครองหมายได!นนจะต!องเปลยนชนดของตวแปรให!เปน
 ชนดเดยวกนก3อน โดยวธการเปลยนตวแปรในภาษา C
 เรยกว3า Casting ซ4งมอย.3 2 ร.ปแบบ คอ
  ●
      การเปลยนชนดข!อม.ลของตวแปรอตโนมต
  ●
      การเปลยนชนดข!อม.ลของตวแปรโดยใช!ค*าสง

K.Warawut          บทท 3 พนฐานของโปรแกรมภาษา C    51
การเปลยนชนดขอม-ลของต!วแปร
               อ!ตโนม!ต
  ●
      เราไม3ต!องท*าอะไร ตวแปลภาษา C จะจดการให!
      ทงหมด โดยใช!หลกการเปลยนชนดของตวแปร
      ขนาดเล<กกว3าไปตามชนดของตวแปรทมขนาดใหญ3
      กว3า




K.Warawut          บทท 3 พนฐานของโปรแกรมภาษา C   52
ตารางการเปลยนชนดขอม-ลของ
         ต!วแปรแบบอ!ตโนม!ต
  Var.1     Var. 2            Var. 3
   char        int          char → int
     int     long           int → long
     int unsinged int int → unsinged int
     int     float          int → float
     int    double        int → double
   float    double      float → double
   long     double      long → double
  double long double double → long double
K.Warawut       บทท 3 พนฐานของโปรแกรมภาษา C   53
Example 11 (ex0311.c)
 #include <stdio.h>
 #include <stdlib.h>

 int main( ) {
      int n = 5;
      float f = 3.5, r;                                  r = 8.500000

      r = n + f;

      printf(“r = %fn”, r);

      system(“PAUSE”);
      return 0;
 }
K.Warawut                  บทท 3 พนฐานของโปรแกรมภาษา C                  54
ตารางรห!สควบค*มร-ปแบบการแสดง
              ผล
  รห!สควบค*ม                     น.าไปใช
  %d หรอ %i    แสดงตวเลขจ*านวนเต<ม
      %u       แสดงตวเลขจ*านวนเต<มบวก
      %0       แสดงตวเลขฐานแปด
      %x       แสดงตวเลขฐานสบหก
      %f       แสดงตวเลขจ*านวนทศนยม (6 หลก)
      %e       แสดงตวเลขทศนยมร.ปแบบของ E หรอ e ยกก*าลง
      %c       แสดงอกขระ 1 ตว (char)
      %s       แสดงข!อความ (string)
      %p       แสดงตวชต*าแหน3ง (pointer)
K.Warawut            บทท 3 พนฐานของโปรแกรมภาษา C         55
ตารางอ!กขระควบค*มการแสดงผล

  อกขระควบค1ม                       ความหมาย
      n        ข4นบรรทดใหม3
       t       เว!นช3องว3างเปนระยะ 1 tab (เท3ากบ 6 ตวอกษร)
       r       ก*าหนดให! cursor ไปอย.ต!นบรรทด
                                       3
       f       เว!นช3องว3างเปนระยะ 1 หน!าจอ
      b        ลบอกขระส1ดท!ายออก 1 ตว




K.Warawut             บทท 3 พนฐานของโปรแกรมภาษา C             56
การเปลยนชนดขอม-ลของต!วแปร
              โดยใชค.าส!ง
  ●
      เปนการใช!ค*าสงเพอเปลยนชนดของตวแปร เรา
      สามารถเลอกชนดของตวแปรทต!องการจะเปลยนไป
      ใช!ได!
  ●
      ร-ปแบบ:
         (type) expression;
      โดยท:
         type คอ ชนดของตวแปรทต!องการจะเปลยน
         expression คอ ตวแปรหรอข!อม.ลทต!องการจะ
      เปลยน
K.Warawut          บทท 3 พนฐานของโปรแกรมภาษา C    57
Example 12 (ex0312.c)
 #include <stdio.h>
 #include <stdlib.h>

 int main( ) {
      int n = 5;                                        n = 5.000000
      float f = 3.5;                                    f=3
                                                        n+f=8
      printf(“n = %fn”, (float)n);
      printf(“f = %dn”, (int)f);
      printf(“n + f = %dn”, (n + (int)f));

      system(“PAUSE”);
      return 0;
 }
K.Warawut                 บทท 3 พนฐานของโปรแกรมภาษา C                  58
11. การแสดงผลในภาษา C

  ●
      สามารถท*าได!หลายวธ แต3วธทนยมใช!กนมากทส1ด
      คอ การเรยกใช!ฟ8งก(ชน printf( ) ซ4งเปนฟ8งก(ชน
      มาตรฐานทใช!ในการแสดงผลข!อม.ลท1กชนดออก
      ทางหน!าจอ (Monitor) ไม3วาจะเปนจ*านวนเต<ม
                              3
      ทศนยม อกขระ หรอข!อความ




K.Warawut           บทท 3 พนฐานของโปรแกรมภาษา C      59
11. การแสดงผลในภาษา C (ตอ)

 ร-ปแบบ:
      printf(format, expression-1, expression-2);
 โดยท:
   format คอ ส3วนทใช!ควบค1มร.ปแบบการแสดงผล
   expression-1, expression-2 คอ นพจน(หรอ
 ตวแปรทต!องการแสดงผล


K.Warawut            บทท 3 พนฐานของโปรแกรมภาษา C    60
11. การแสดงผลในภาษา C (ตอ)




K.Warawut    บทท 3 พนฐานของโปรแกรมภาษา C   61
Example 13 (ex0313.c)
 #include <stdio.h>
 #include <stdlib.h>

 int main( ) {
      int a = 5, b = 10, c;                             5    10
                                                        15
      printf(“%dt%dn”, a, b);
      c = a + b;
      printf(“%d”, c);

      system(“PAUSE”);
      return 0;
 }

K.Warawut                 บทท 3 พนฐานของโปรแกรมภาษา C             62
แสดงผลทละอ!กขระดวยค.าส!ง
              putchar( )
 ร-ปแบบ:
      putchar(expression);
 โดยท:
   expression คอ ตวแปรหรอข!อม.ลทเปนชนดข!อม.ล
 เปนอกขระ เขยนอย.3ในเครองหมาย ' ' เช3น 'A'



K.Warawut           บทท 3 พนฐานของโปรแกรมภาษา C   63
Example 14 (ex0314.c)
 #include <stdio.h>
 #include <stdlib.h>
                                       Function printf( ) : A
 int main( ) {
                                       Function putchar( ) : A
      char ch = 'A';

      printf(“Function printf( ) : %cn”, ch);
      printf(“Function putchar( ) : “);
      putchar(ch);

      system(“PAUSE”);
      return 0;
 }

K.Warawut                 บทท 3 พนฐานของโปรแกรมภาษา C            64
แสดงผลเป3นขอความดวยค.าส!ง
                puts( )
 ร-ปแบบ:
      puts(expression);
 โดยท:
   expression คอ ตวแปรหรอข!อม.ลทเปนชนดข!อม.ล
 เปนอกขระ เขยนอย.3ในเครองหมาย “ “ เช3น “ABC”



K.Warawut           บทท 3 พนฐานของโปรแกรมภาษา C   65
Example 15 (ex0315.c)
 #include <stdio.h>
 #include <stdlib.h>
                                       Function printf( ) : ABC
 int main( ) {
                                       Function puts( ) : ABC
      char str[ ] = “ABC”;

      printf(“Function printf( ) : %sn”, str);
      printf(“Function puts( ) :);
      puts(str);

      system(“PAUSE”);
      return 0;
 }

K.Warawut                 บทท 3 พนฐานของโปรแกรมภาษา C             66
12. การร!บขอม-ลในภาษา C

  ●
      รบข!อม.ลจากแปDนพมพ(ในภาษา C สามารถเรยกใช!
      ฟ8งก(ชน scanf( ) ซ4งเปนฟ8งก(ชนมาตรฐานส*าหรบรบ
      ข!อม.ลจากแปDนพมพ( โดยสามารถรบข!อม.ลได!ท1ก
      ประเภท ไม3ว3าจะเปนจ*านวนเต<ม ทศนยม อกขระ
      หรอข!อความ




K.Warawut           บทท 3 พนฐานของโปรแกรมภาษา C   67
12. การร!บขอม-ลในภาษา C (ตอ)

 ร-ปแบบ:
      scanf(format, &variable-1, &variable-2);
 โดยท:
    format คอ ส3วนทใช!ควบค1มร.ปแบบการรบข!อม.ล
    variable-1, variable-2 คอ ตวแปรทใช!ในการเก<บ
 ค3า และจ*าเปนต!องมเครองหมาย & น*าหน!าตวแปร
 ยกเว!นตวแปรชนด string ทไม3จ*าเปนต!องมเครองหมาย
 & น*าหน!าตวแปร
K.Warawut           บทท 3 พนฐานของโปรแกรมภาษา C   68
ร!บขอม-ลมากกวาหน4งคาดวยค.าส!ง
             scanf( )
  ●   scanf(%d %f %d”, &var1, &var2, &var3);
  ●   scanf(%d/%f/%d”, &var1, &var2, &var3);
  ●   scanf(%d,%f,%d”, &var1, &var2, &var3);
  ●   scanf(%s %s %s”, var1, var2, var3);




K.Warawut           บทท 3 พนฐานของโปรแกรมภาษา C   69
Example 16 (ex0316.c)
 #include <stdio.h>
 #include <stdlib.h>
                                Input number to 2 value : 3 6
 int main( ) {
                                a+b=9
      int a, b, c;

      printf(“Input number to 2 value : “);
      scanf(“%d %d”, &a, &b);
      c = a + b;
      printf(“a + b = %dn”, c);

      system(“PAUSE”);
      return 0;
 }
K.Warawut                บทท 3 พนฐานของโปรแกรมภาษา C            70
Example 17 (ex0317.c)
#include <stdio.h>
#include <stdlib.h>
                             Input number to a : 3
int main( ) {                Input number to b : 6
    int a, b, c;             a+b=9

    printf(“Input number to a : “);
    scanf(“%d”, &a);
    printf(“Input number to b : “);
    scanf(“%d”, &b);
    c = a + b;
    printf(“a + b = %dn”, c);

    system(“PAUSE”);
    return 0;                                        71

}
ร!บขอม-ลทละอ!กขระดวย getchar( )
           และ getch( )
 ร-ปแบบ:
      variable = getchar( );
      variable = getch( );
 โดยท:
    variable คอ ตวแปรทเปนชนดข!อม.ลเปนช1ดอกขระ
 (ข!อความ) เขยนอย.3ในเครองหมาย ' ' เช3น 'A'


K.Warawut            บทท 3 พนฐานของโปรแกรมภาษา C   72
ร!บขอม-ลทละอ!กขระดวย getchar( )
         และ getch( ) (ตอ)
  ●
      getchar( ) ปDอนข!อม.ลจากแปDมพมพ( 1 ค3า (1
      อกขระ) ตามด!วย Enter
  ●
      getch( ) ปDอนข!อม.ลจากแปDนพมพ( 1 ค3า (1 อกขระ)
      ไม3ต!องกด Enter และไม3สามารถค3าทปDอนพมพ(ขณะ
      ทพมพ(




K.Warawut           บทท 3 พนฐานของโปรแกรมภาษา C        73
Example 18 (ex0318.c)
 #include <stdio.h>
 #include <stdlib.h>

 int main( ) {
      char ch;

      printf(“Enter character : “);
      scanf(“%c”, &ch);
      printf(“Result ch : %cn“, ch);

      system(“PAUSE”);
      return 0;
 }

K.Warawut                บทท 3 พนฐานของโปรแกรมภาษา C   74
Example 19 (ex0319.c)
#include <stdio.h>
#include <stdlib.h>

int main( ) {
    char ch1, ch2;

    printf(“Enter character 1 : “);
    ch1 = getchar( );
    printf(“Result ch1 : %cn“, ch1);
    printf(“Enter character 2: “);
    ch2 = getch( );

    system(“PAUSE”);
    return 0;
}                      บทท 3 พนฐานของโปรแกรมภาษา C   75
ร!บขอม-ลเป3นขอความดวย gets( )

 ร-ปแบบ:
      gets(expression);
 โดยท:
    variable คอ ตวแปรหรอข!อม.ลทเปนชนดข!อม.ลเปน
 ช1ดอกขระ (ข!อความ) เขยนอย.3ในเครองหมาย “ “ เช3น
 “ABC”


K.Warawut           บทท 3 พนฐานของโปรแกรมภาษา C    76
Example 20 (ex0320.c)
 #include <stdio.h>
 #include <stdlib.h>

 int main( ) {
      char str[ ] = “”;

      printf(“Input string : “);
      scanf(“%s”, str);
      printf(“String : %sn“, str);

      system(“PAUSE”);
      return 0;
 }

K.Warawut                 บทท 3 พนฐานของโปรแกรมภาษา C   77
Example 21 (ex0321.c)
 #include <stdio.h>
 #include <stdlib.h>

 int main( ) {
      char str[ ] = “”;

      printf(“Input string : “);
      gets(str);
      printf(“String : %sn“, str);

      system(“PAUSE”);
      return 0;
 }

K.Warawut                 บทท 3 พนฐานของโปรแกรมภาษา C   78
13. หมายเหต* (Comment)

  ●
      ช3วยอธบายการท*างานของโปรแกรม เพอให!เกด
      ความเข!าใจในสงทเขยน
  ●
      ไม3มผลกบโปรแกรมทเขยนข4น
  ●
      ร.ปแบบหมายเหต1
            –   /*   */ → อธบายได!หลายบรรทด
            –   // → อธบายในบรรทดเดยว เรมตงแต3ม
                   เครองหมาย // ก*ากบ

K.Warawut                 บทท 3 พนฐานของโปรแกรมภาษา C   79

Weitere ähnliche Inhalte

Was ist angesagt?

ภาษา C เบื้องต้น
ภาษา C เบื้องต้นภาษา C เบื้องต้น
ภาษา C เบื้องต้นApinyaphorn
 
การเขียนโปรแกรม Dev c++
การเขียนโปรแกรม Dev c++การเขียนโปรแกรม Dev c++
การเขียนโปรแกรม Dev c++Naowarat Jaikaroon
 
การใช้สูตรหาพื้นที่ ด้วย Dev++
การใช้สูตรหาพื้นที่ ด้วย Dev++การใช้สูตรหาพื้นที่ ด้วย Dev++
การใช้สูตรหาพื้นที่ ด้วย Dev++Ooy's Patchaya
 
การเข้าใช้โปรแกรมซี Dev C++
การเข้าใช้โปรแกรมซี Dev C++การเข้าใช้โปรแกรมซี Dev C++
การเข้าใช้โปรแกรมซี Dev C++dechathon
 
Pbl2 นะแนนxปิ้น
Pbl2 นะแนนxปิ้นPbl2 นะแนนxปิ้น
Pbl2 นะแนนxปิ้นDararat Worasut
 
2. โครงสร้างภาษาซี
2. โครงสร้างภาษาซี2. โครงสร้างภาษาซี
2. โครงสร้างภาษาซีmansuang1978
 
ภาษา C
ภาษา Cภาษา C
ภาษา C0872671746
 
บทที่4 เมธอด (METHOD)
บทที่4 เมธอด (METHOD)บทที่4 เมธอด (METHOD)
บทที่4 เมธอด (METHOD)N'Name Phuthiphong
 
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม.6.1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม.6.1ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม.6.1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม.6.1Little Tukta Lita
 
ประวัติภาษา C
ประวัติภาษา Cประวัติภาษา C
ประวัติภาษา CFair Kung Nattaput
 
ฟังก์ชันในภาษา
ฟังก์ชันในภาษาฟังก์ชันในภาษา
ฟังก์ชันในภาษาSedthawoot Pitapo
 
2 การรับข้อมูลและการแสดงผลข้อมูล 8-10-2555
2 การรับข้อมูลและการแสดงผลข้อมูล 8-10-25552 การรับข้อมูลและการแสดงผลข้อมูล 8-10-2555
2 การรับข้อมูลและการแสดงผลข้อมูล 8-10-2555อภิญญา คำเหลือ
 

Was ist angesagt? (20)

ภาษา C เบื้องต้น
ภาษา C เบื้องต้นภาษา C เบื้องต้น
ภาษา C เบื้องต้น
 
Chapter1
Chapter1Chapter1
Chapter1
 
การเขียนโปรแกรม Dev c++
การเขียนโปรแกรม Dev c++การเขียนโปรแกรม Dev c++
การเขียนโปรแกรม Dev c++
 
การใช้สูตรหาพื้นที่ ด้วย Dev++
การใช้สูตรหาพื้นที่ ด้วย Dev++การใช้สูตรหาพื้นที่ ด้วย Dev++
การใช้สูตรหาพื้นที่ ด้วย Dev++
 
การเข้าใช้โปรแกรมซี Dev C++
การเข้าใช้โปรแกรมซี Dev C++การเข้าใช้โปรแกรมซี Dev C++
การเข้าใช้โปรแกรมซี Dev C++
 
C slide
C slideC slide
C slide
 
Pbl2 docx
Pbl2 docxPbl2 docx
Pbl2 docx
 
Pbl2 นะแนนxปิ้น
Pbl2 นะแนนxปิ้นPbl2 นะแนนxปิ้น
Pbl2 นะแนนxปิ้น
 
2. โครงสร้างภาษาซี
2. โครงสร้างภาษาซี2. โครงสร้างภาษาซี
2. โครงสร้างภาษาซี
 
ภาษา C
ภาษา Cภาษา C
ภาษา C
 
Intro c
Intro cIntro c
Intro c
 
บทที่4 เมธอด (METHOD)
บทที่4 เมธอด (METHOD)บทที่4 เมธอด (METHOD)
บทที่4 เมธอด (METHOD)
 
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม.6.1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม.6.1ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม.6.1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม.6.1
 
ประวัติภาษา C
ประวัติภาษา Cประวัติภาษา C
ประวัติภาษา C
 
ฟังก์ชันในภาษา
ฟังก์ชันในภาษาฟังก์ชันในภาษา
ฟังก์ชันในภาษา
 
โครงสร้างของภาษา C
โครงสร้างของภาษา Cโครงสร้างของภาษา C
โครงสร้างของภาษา C
 
บทที่ 1
บทที่ 1บทที่ 1
บทที่ 1
 
Learn 2
Learn 2Learn 2
Learn 2
 
2 การรับข้อมูลและการแสดงผลข้อมูล 8-10-2555
2 การรับข้อมูลและการแสดงผลข้อมูล 8-10-25552 การรับข้อมูลและการแสดงผลข้อมูล 8-10-2555
2 การรับข้อมูลและการแสดงผลข้อมูล 8-10-2555
 
12
1212
12
 

Andere mochten auch

โครงสร้างโปรแกรมภาษาซี
โครงสร้างโปรแกรมภาษาซีโครงสร้างโปรแกรมภาษาซี
โครงสร้างโปรแกรมภาษาซีFair Kung Nattaput
 
การใช้ Turbo C ชุดที่ 1 Turbo C
การใช้ Turbo C ชุดที่ 1 Turbo Cการใช้ Turbo C ชุดที่ 1 Turbo C
การใช้ Turbo C ชุดที่ 1 Turbo CKnow Mastikate
 
การใช้ Turbo C ชุดที่ 2 variable
การใช้ Turbo C ชุดที่ 2 variableการใช้ Turbo C ชุดที่ 2 variable
การใช้ Turbo C ชุดที่ 2 variableKnow Mastikate
 
การใช้ Turbo C ชุดที่ 5 IO
การใช้ Turbo C ชุดที่ 5 IOการใช้ Turbo C ชุดที่ 5 IO
การใช้ Turbo C ชุดที่ 5 IOKnow Mastikate
 
การใช้ Turbo C ชุดที่ 7 Loop
การใช้ Turbo C ชุดที่ 7 Loopการใช้ Turbo C ชุดที่ 7 Loop
การใช้ Turbo C ชุดที่ 7 LoopKnow Mastikate
 
4121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 4/7
4121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 4/74121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 4/7
4121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 4/7Know Mastikate
 
Cpwk14 screen and sound
Cpwk14 screen and soundCpwk14 screen and sound
Cpwk14 screen and soundKnow Mastikate
 
4121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 2/7
4121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 2/74121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 2/7
4121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 2/7Know Mastikate
 
การใช้ Turbo C ชุดที่ 6 condition
การใช้ Turbo C ชุดที่ 6 conditionการใช้ Turbo C ชุดที่ 6 condition
การใช้ Turbo C ชุดที่ 6 conditionKnow Mastikate
 
4121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 5/7
4121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 5/74121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 5/7
4121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 5/7Know Mastikate
 
4121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 1/7
4121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 1/74121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 1/7
4121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 1/7Know Mastikate
 
การใช้ Turbo C ชุดที่ 8 Array
การใช้ Turbo C ชุดที่ 8 Arrayการใช้ Turbo C ชุดที่ 8 Array
การใช้ Turbo C ชุดที่ 8 ArrayKnow Mastikate
 
4121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 7/7
4121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 7/74121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 7/7
4121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 7/7Know Mastikate
 
4121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 3/7
4121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 3/74121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 3/7
4121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 3/7Know Mastikate
 
การใช้ Turbo C ชุดที่ 11 function
การใช้ Turbo C ชุดที่ 11 functionการใช้ Turbo C ชุดที่ 11 function
การใช้ Turbo C ชุดที่ 11 functionKnow Mastikate
 
การใช้ Turbo C ชุดที่ 13 File IO
การใช้ Turbo C ชุดที่ 13 File IOการใช้ Turbo C ชุดที่ 13 File IO
การใช้ Turbo C ชุดที่ 13 File IOKnow Mastikate
 

Andere mochten auch (20)

งานPPT
งานPPTงานPPT
งานPPT
 
โครงสร้างโปรแกรมภาษาซี
โครงสร้างโปรแกรมภาษาซีโครงสร้างโปรแกรมภาษาซี
โครงสร้างโปรแกรมภาษาซี
 
การใช้ Turbo C ชุดที่ 1 Turbo C
การใช้ Turbo C ชุดที่ 1 Turbo Cการใช้ Turbo C ชุดที่ 1 Turbo C
การใช้ Turbo C ชุดที่ 1 Turbo C
 
Introduction of C++
Introduction of C++Introduction of C++
Introduction of C++
 
Lesson1
Lesson1Lesson1
Lesson1
 
การใช้ Turbo C ชุดที่ 2 variable
การใช้ Turbo C ชุดที่ 2 variableการใช้ Turbo C ชุดที่ 2 variable
การใช้ Turbo C ชุดที่ 2 variable
 
การใช้ Turbo C ชุดที่ 5 IO
การใช้ Turbo C ชุดที่ 5 IOการใช้ Turbo C ชุดที่ 5 IO
การใช้ Turbo C ชุดที่ 5 IO
 
การใช้ Turbo C ชุดที่ 7 Loop
การใช้ Turbo C ชุดที่ 7 Loopการใช้ Turbo C ชุดที่ 7 Loop
การใช้ Turbo C ชุดที่ 7 Loop
 
4121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 4/7
4121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 4/74121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 4/7
4121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 4/7
 
Cpwk14 screen and sound
Cpwk14 screen and soundCpwk14 screen and sound
Cpwk14 screen and sound
 
4121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 2/7
4121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 2/74121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 2/7
4121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 2/7
 
การใช้ Turbo C ชุดที่ 6 condition
การใช้ Turbo C ชุดที่ 6 conditionการใช้ Turbo C ชุดที่ 6 condition
การใช้ Turbo C ชุดที่ 6 condition
 
4121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 5/7
4121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 5/74121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 5/7
4121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 5/7
 
4121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 1/7
4121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 1/74121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 1/7
4121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 1/7
 
การใช้ Turbo C ชุดที่ 8 Array
การใช้ Turbo C ชุดที่ 8 Arrayการใช้ Turbo C ชุดที่ 8 Array
การใช้ Turbo C ชุดที่ 8 Array
 
4121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 7/7
4121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 7/74121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 7/7
4121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 7/7
 
4121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 3/7
4121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 3/74121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 3/7
4121103 การเขียนโปรแกรมและอัลกอริทึ่ม SLIDE 3/7
 
การใช้ Turbo C ชุดที่ 11 function
การใช้ Turbo C ชุดที่ 11 functionการใช้ Turbo C ชุดที่ 11 function
การใช้ Turbo C ชุดที่ 11 function
 
การใช้ Turbo C ชุดที่ 13 File IO
การใช้ Turbo C ชุดที่ 13 File IOการใช้ Turbo C ชุดที่ 13 File IO
การใช้ Turbo C ชุดที่ 13 File IO
 
Plan3
Plan3Plan3
Plan3
 

Ähnlich wie C Programming

การเขียนฟังก์ชั่นในภาษา C
การเขียนฟังก์ชั่นในภาษา Cการเขียนฟังก์ชั่นในภาษา C
การเขียนฟังก์ชั่นในภาษา CWarawut
 
Multimedia of introducation to programming c++
Multimedia of introducation to programming c++Multimedia of introducation to programming c++
Multimedia of introducation to programming c++จู ลิ
 
คอมพิวเตอร์โอลิมปิก
คอมพิวเตอร์โอลิมปิกคอมพิวเตอร์โอลิมปิก
คอมพิวเตอร์โอลิมปิกSumalee Sonamthiang
 
ภาษาC++
ภาษาC++ภาษาC++
ภาษาC++sonsukda
 

Ähnlich wie C Programming (20)

การเขียนฟังก์ชั่นในภาษา C
การเขียนฟังก์ชั่นในภาษา Cการเขียนฟังก์ชั่นในภาษา C
การเขียนฟังก์ชั่นในภาษา C
 
Pbl2
Pbl2Pbl2
Pbl2
 
Lab Computer Programming 1
Lab Computer Programming 1Lab Computer Programming 1
Lab Computer Programming 1
 
Introduction toc
Introduction tocIntroduction toc
Introduction toc
 
Know 1 1
Know 1 1Know 1 1
Know 1 1
 
โครงสร้างโปรแกรมภาษาซี
โครงสร้างโปรแกรมภาษาซีโครงสร้างโปรแกรมภาษาซี
โครงสร้างโปรแกรมภาษาซี
 
Lesson2
Lesson2Lesson2
Lesson2
 
7 2โครงสร้าง
7 2โครงสร้าง7 2โครงสร้าง
7 2โครงสร้าง
 
Multimedia of introducation to programming c++
Multimedia of introducation to programming c++Multimedia of introducation to programming c++
Multimedia of introducation to programming c++
 
C lang
C langC lang
C lang
 
C language
C languageC language
C language
 
C language
C languageC language
C language
 
คอมพิวเตอร์โอลิมปิก
คอมพิวเตอร์โอลิมปิกคอมพิวเตอร์โอลิมปิก
คอมพิวเตอร์โอลิมปิก
 
ภาษาC++
ภาษาC++ภาษาC++
ภาษาC++
 
3.8 การทำงานแบบลำดับ
3.8 การทำงานแบบลำดับ3.8 การทำงานแบบลำดับ
3.8 การทำงานแบบลำดับ
 
3.6 การเขียนโปรแกรมคำนวณ
3.6 การเขียนโปรแกรมคำนวณ3.6 การเขียนโปรแกรมคำนวณ
3.6 การเขียนโปรแกรมคำนวณ
 
3.8 การเขียนโปรแกรมคำนวณ
3.8 การเขียนโปรแกรมคำนวณ3.8 การเขียนโปรแกรมคำนวณ
3.8 การเขียนโปรแกรมคำนวณ
 
Pbl2 docx
Pbl2 docxPbl2 docx
Pbl2 docx
 
เริ่มต้นกับภาษาซี
เริ่มต้นกับภาษาซีเริ่มต้นกับภาษาซี
เริ่มต้นกับภาษาซี
 
02 basic
02 basic02 basic
02 basic
 

Mehr von Warawut

Database design
Database designDatabase design
Database designWarawut
 
Business Computer Project 4
Business Computer Project 4Business Computer Project 4
Business Computer Project 4Warawut
 
Object-Oriented Programming 10
Object-Oriented Programming 10Object-Oriented Programming 10
Object-Oriented Programming 10Warawut
 
Object-Oriented Programming 9
Object-Oriented Programming 9Object-Oriented Programming 9
Object-Oriented Programming 9Warawut
 
Object-Oriented Programming 8
Object-Oriented Programming 8Object-Oriented Programming 8
Object-Oriented Programming 8Warawut
 
Object-Oriented Programming 7
Object-Oriented Programming 7Object-Oriented Programming 7
Object-Oriented Programming 7Warawut
 
Object-Oriented Programming 6
Object-Oriented Programming 6Object-Oriented Programming 6
Object-Oriented Programming 6Warawut
 
Management Information System 6
Management Information System 6Management Information System 6
Management Information System 6Warawut
 
Management Information System 5
Management Information System 5Management Information System 5
Management Information System 5Warawut
 
Management Information System 4
Management Information System 4Management Information System 4
Management Information System 4Warawut
 
Object-Oriented Programming 5
Object-Oriented Programming 5Object-Oriented Programming 5
Object-Oriented Programming 5Warawut
 
Business Computer Project 3
Business Computer Project 3Business Computer Project 3
Business Computer Project 3Warawut
 
Management Information System 3
Management Information System 3Management Information System 3
Management Information System 3Warawut
 
Business Computer Project 2
Business Computer Project 2Business Computer Project 2
Business Computer Project 2Warawut
 
Chapter 2 Strategy & Information System
Chapter 2 Strategy & Information SystemChapter 2 Strategy & Information System
Chapter 2 Strategy & Information SystemWarawut
 
Object-Oriented Programming 4
Object-Oriented Programming 4Object-Oriented Programming 4
Object-Oriented Programming 4Warawut
 
Business Computer Project 1
Business Computer Project 1Business Computer Project 1
Business Computer Project 1Warawut
 
Chapter 1 Organization & MIS
Chapter 1 Organization & MISChapter 1 Organization & MIS
Chapter 1 Organization & MISWarawut
 
Object-Oriented Programming 3
Object-Oriented Programming 3Object-Oriented Programming 3
Object-Oriented Programming 3Warawut
 
Object-Oriented Programming 2
Object-Oriented Programming 2Object-Oriented Programming 2
Object-Oriented Programming 2Warawut
 

Mehr von Warawut (20)

Database design
Database designDatabase design
Database design
 
Business Computer Project 4
Business Computer Project 4Business Computer Project 4
Business Computer Project 4
 
Object-Oriented Programming 10
Object-Oriented Programming 10Object-Oriented Programming 10
Object-Oriented Programming 10
 
Object-Oriented Programming 9
Object-Oriented Programming 9Object-Oriented Programming 9
Object-Oriented Programming 9
 
Object-Oriented Programming 8
Object-Oriented Programming 8Object-Oriented Programming 8
Object-Oriented Programming 8
 
Object-Oriented Programming 7
Object-Oriented Programming 7Object-Oriented Programming 7
Object-Oriented Programming 7
 
Object-Oriented Programming 6
Object-Oriented Programming 6Object-Oriented Programming 6
Object-Oriented Programming 6
 
Management Information System 6
Management Information System 6Management Information System 6
Management Information System 6
 
Management Information System 5
Management Information System 5Management Information System 5
Management Information System 5
 
Management Information System 4
Management Information System 4Management Information System 4
Management Information System 4
 
Object-Oriented Programming 5
Object-Oriented Programming 5Object-Oriented Programming 5
Object-Oriented Programming 5
 
Business Computer Project 3
Business Computer Project 3Business Computer Project 3
Business Computer Project 3
 
Management Information System 3
Management Information System 3Management Information System 3
Management Information System 3
 
Business Computer Project 2
Business Computer Project 2Business Computer Project 2
Business Computer Project 2
 
Chapter 2 Strategy & Information System
Chapter 2 Strategy & Information SystemChapter 2 Strategy & Information System
Chapter 2 Strategy & Information System
 
Object-Oriented Programming 4
Object-Oriented Programming 4Object-Oriented Programming 4
Object-Oriented Programming 4
 
Business Computer Project 1
Business Computer Project 1Business Computer Project 1
Business Computer Project 1
 
Chapter 1 Organization & MIS
Chapter 1 Organization & MISChapter 1 Organization & MIS
Chapter 1 Organization & MIS
 
Object-Oriented Programming 3
Object-Oriented Programming 3Object-Oriented Programming 3
Object-Oriented Programming 3
 
Object-Oriented Programming 2
Object-Oriented Programming 2Object-Oriented Programming 2
Object-Oriented Programming 2
 

C Programming

  • 1. บทท 3 พนฐาน โปรแกรมภาษา C อ.วราวฒ แขงขน มหาวทยาลยราชภฏสวนดสต ศนยพษณโลก
  • 2. เนอหา 1.ประวตความเปนมา 2.ข!อดของภาษา C 3.กระบวนการแปลโปรแกรมภาษา C 4.ตดตงโปรแกรม 5.โครงสร!างโปรแกรมภาษา C 6.กฎเกณฑ(ของโปรแกรมภาษา C K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 2
  • 3. เนอหา (ตอ) 7.ตวแปรในภาษา C 8.ตวด*าเนนการในภาษา C 9.นพจน(ในภาษา C 10.การเปลยนชนดข!อม.ลของตวแปรในภาษา C 11.การแสดงผลในภาษา C 12.การรบข!อม.ลในภาษา C 13.หมายเหต1 (Comment) K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 3
  • 4. 1. ประวตความเปนมา ภาษา ภาษา ภาษา BCPL B C Basic Combined บนเครอง พ.ศ. 2515 Programming PDP-7 โดย เดนนช รทช Language (Unix) พ.ศ. 2513 K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 4
  • 5. 2. ขอดของภาษา C ● เปนภาษาระดบส.ง (High Level Language) ● ไม3ข4นกบระบบปฏบตการ (OS Independent) ● ไม3ข4นกบชนดของเครองคอมพวเตอร( (Hardware Independent) ● เปนภาษาโครงสร!าง (Structural Language) K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 5
  • 6. 3. กระบวนการแปลโปรแกรมภาษา C Source Code Program Compile K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 6
  • 7. 4. ตดต!งโปรแกรม ● โปรแกรม Editor ต3างๆ เช3น Notepad, Notepad++, EditPlus2, Turbo C, Turbo C++ เปนต!น ● โปรแกรมทใช!ในการจดการเรยนการสอน คอ Bloodshed Dev-C++ Version 4.9.9.2 เปน Freeware (http://www.bloodshed.net/) K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 7
  • 8. 5. โครงสรางโปรแกรมภาษา C #include <stdio.h> /* Comment */ … Data Declarations ...; int main ( ) { … Executable Statements ...; return 0; } บทท 3 พนฐานของโปรแกรมภาษา C 8
  • 9. 5. โครงสรางโปรแกรมภาษา C #include <stdio.h> /* Comment */ … Data Declarations ...; Comment int main ( ) { … Executable Statements ...; return 0; } บทท 3 พนฐานของโปรแกรมภาษา C 9
  • 10. 5. โครงสรางโปรแกรมภาษา C #include <stdio.h> /* Comment */ Header File … Data Declarations ...; Directive Preprocessor int main ( ) { … Executable Statements ...; return 0; } บทท 3 พนฐานของโปรแกรมภาษา C 10
  • 11. 5. โครงสรางโปรแกรมภาษา C #include <stdio.h> /* Comment */ … Data Declarations ...; int n; int main ( ) { char c; … Executable Statements ...; return 0; } บทท 3 พนฐานของโปรแกรมภาษา C 11
  • 12. 5. โครงสรางโปรแกรมภาษา C #include <stdio.h> /* Comment */ … Data Declarations ...; Start Program int main ( ) { … Executable Statements ...; [ return 0; } 12
  • 13. 6. กฎเกณฑ'ของโปรแกรมภาษา C ● ฟ8งก(ชนแรกต!องเปน main( ) เสมอ ● ใช!ป9กกา ({ }) เปนตวก*าหนดขอบเขตการท*างาน ● ใช!เครองหมาย ; (semi colon) ป:ดท!ายค*าสงเสมอ ● ใช!เครองหมาย , (comma) เปนตวคนตวแปรและ พารามเตอร( ● ใช!เครองหมาย /* … */ เปนการอธบายการท*างาน ของโปรแกรม K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 13
  • 14. Example 1 (ex0301_1.c) #include <stdio.h> /* #include “stdio.h” */ #include <conio.h> int main( ) { printf(“Hello World!!!n”); getch(); return 0; } K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 14
  • 15. Example 1 (ex0301_2.c) #include <stdio.h> /* #include “stdio.h” */ #include <stdlib.h> int main( ) { printf(“Hello World!!!n”); system(“PAUSE”); return 0; } K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 15
  • 16. Example 2 (ex0302.c) #include “stdio.h” #include “conio.h” int main( ) { printf(“Hello “); printf(“World!!!”); printf(“n”); getch(); return 0; } K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 16
  • 17. Example 3 (ex0303.c) #include <stdio.h> #include <stdlib.h> int main( ) { printf(“Hello n”); printf(“World”); system(“PAUSE”); return 0; } K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 17
  • 18. 7. ต!วแปรในภาษา C (Variable in C) ● ต!วแปร (Variable) คอ การจองพนทในหน3วยความ จ*า เพอใช!ในการเก<บข!อม.ล เปรยบเสมอนการ ก*าหนดชอให!สงของหรอห!องพก ในการเรยกใช! ข!อม.ลก<จะท*าการเรยกผ3านตวแปรทเราก*าหนดข4น K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 18
  • 19. ชนดของต!วแปรในภาษา C ● ต!วแปรพนฐาน (Scalar) หมายถ4ง ตวแปรทเก<บ ข!อม.ลได!เพยงค3าเดยว ภายในตวแปรตวเดยว ● ต!วแปรช*ด (Array) หมายถ4ง ตวแปรทเก<บข!อม.ลได! หลายค3า ภายในตวแปรตวเดยว และมชนดข!อม.ล เปนชนดเดยวกน ● ต!วแปรโครงสราง (Structure) หมายถ4ง ตวแปรท เก<บข!อม.ลได!หลายค3า ภายในตวแปรตวเดยว และม ชนดข!อม.ลหลายชนด K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 19
  • 20. ต!วแปรพนในฐานในภาษา C แบ3งออกเปน 2 ชนด ดงน ● Integer Types ● Real Types K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 20
  • 21. K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 21
  • 22. K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 22
  • 23. Example 4 (ex0104.c) #include <stdio.h> #include <stdlib.h> #include <limits.h> int main( ){ printf("Minimum char = %d, ", CHAR_MIN); printf("Maximum char = %dn", CHAR_MAX); printf("Minimum int = %i, ", INT_MIN); printf("Maximum int = %in", INT_MAX); system(“PAUSE”); return 0; } Minimum char = -128, Maximum char = 127 23 Minimum int = -2147483648, Maximum int = 2147483647
  • 24. การต!งชอต!วแปรในภาษา C ● ต!องข4นต!นด!วยตวอกษร A-Z หรอ a-z หรอ เครองหมาย _ ● ภายในชอตวแปรสามารถใช!ตวอกษร A-Z หรอ a-z หรอ 0-9 หรอเครองหมาย _ ● ภายในชอตวแปรห!ามเว!นช3องว3าง หรอใช!สญลกษณ( อนนอกเหนอจากทระบ1ไว! ● ตวอกษรตวเล<กและตวใหญ3มความหมายแตกต3างกน K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 24
  • 25. การต!งชอต!วแปรในภาษา C (ตอ) ● ห!ามตงชอซ*ากบค*าสงวน (Reserved Word) หรอ ค*าสงทใช!ในภาษา C auto break case char const continue default do double else enum extern float for goto it int long register return short signed sizeof static struct switch typedef union unsigned void volatile while etc. K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 25
  • 26. ต.ย. การต!งชอต!วแปรในภาษา C Correct Incorrect average pi 3rd_entry all$done number_of_students the end int entry_total entryTotal all_total allTotal K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 26
  • 27. การประกาศต!วแปร ร-ปแบบ: type name; Example: int n; โดยท: float f; type คอ ชนดของตวแปร char c; name คอ ชอของตวแปร double d; K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 27
  • 28. การก.าหนดคาใหก!บต!วแปร ร-ปแบบ: type name = value; Example: int n = 10; โดยท: float f = 10.5; type คอ ชนดของตวแปร char c = 'A'; name คอ ชอของตวแปร double d = 250.00; value คอ ค3าทก*าหนดให!กบตวแปร K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 28
  • 29. 8. ต!วด.าเนนงานในภาษา C ● Assignment Statement ● Arithmetic Operators ● Increment/Decrement Operators ● Other Assignment Operators ● Comparison Operators ● Logical Operators K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 29
  • 30. Assignment Statement ● ใช!เครองหมาย = ● ใช!ในการก*าหนดค3าให!กบตวแปร ร-ปแบบ: variable = expression; โดยท: variable คอ ชอของตวแปร expression คอ นพจน(หรอค3าทต!องการก*าหนด K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 30
  • 31. Example Assignment Statement K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 31
  • 32. Example 5 (ex0305.c) #include <stdio.h> #include <stdlib.h> int main( ) { int term; int term_2; int term_3; term = 15 term_2 = 30 term = 3 * 5; term_3 = 45 term_2 = 2 * term; term_3 = 3 * term; printf("term = %dn", term); printf("term_2 = %dn", term_2); printf("term_3 = %dn", term_3); system(“PAUSE”); return 0; 32 }
  • 33. K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 33
  • 34. Example 6 (ex0306.c) #include <stdio.h> #include <stdlib.h> int main( ) { int a = 5, b = 2; int z; a+b=7 a-b=3 z = a + b; a * b = 10 printf("a + b = %dn", z); z = a – b; printf("a - b = %dn", z); z = a * b; printf("a * b = %dn", z); system(“PAUSE”); return 0; 34 }
  • 35. Example 7 (ex0307.c) #include <stdio.h> #include <stdlib.h> int main( ) { int a = 5, b = 2; int z; div = 2 z = a / b; mod = 1 printf(“div = %dn", z); z = a % b; printf(“mod = %dn", z); // printf(“a %% b = %dn”, z); system(“PAUSE”); return 0; } บทท 3 พนฐานของโปรแกรมภาษา C 35
  • 36. Increment/Decrement Operators ● เปนการเพมค3า/ลดค3าทละหน4ง ● การเพมค3าทละหน4ง ใช!เครองหมาย ++ ● การลดค3าทละหน4ง ใช!เครองหมาย – int i = 5, j = 4; i++; // i = i + 1; result = 6 --j; // j = j – 1; result = 3 ++i; // i = i + 1; result = 7 K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 36
  • 37. K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 37
  • 38. Example 8 (ex0308.c) #include <stdio.h> #include <stdlib.h> n=5 int main( ) { n=6 int n = 5; n=6 n=7 printf(“n = %dn”, n); printf(“++n = %dn”, ++n); printf(“n++ = %dn”, n++); printf(“n = %dn”, n); system(“PAUSE”); return 0; } K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 38
  • 39. Example 9 (ex0309.c) #include <stdio.h> #include <stdlib.h> int main( ) { int value = 1, result; result = 8 value = 3 result = (value++ * 5) + (value++ * 3); printf(“result = %dn”, result); printf(“value = %dn”, value); system(“PAUSE”); return 0; } K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 39
  • 40. ต!วด.าเนนการทางคณตศาสตร' ประเภทลดร-ป K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 40
  • 41. Example 10 (ex0310.c) #include <stdio.h> #include <stdlib.h> int main( ) { int a = 5, b = 1; Before b = 1 After b = 6 printf(“Before b = %dn”, b); b += a; // b = b + a; printf(“After b = %dn”, b); system(“PAUSE”); return 0; } บทท 3 พนฐานของโปรแกรมภาษา C 41
  • 42. Comparison Operators K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 42
  • 43. Logical Operators K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 43
  • 44. Table Logical Operators A B A && B A || B !A !B TRUE TRUE TRUE TRUE FALSE FALSE TRUE FALSE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 44
  • 45. 9. นพจน'ในภาษา C ● นพจน' (Expression) คอ การน*าข!อม.ล ซ4งอาจจะ อย.3ในร.ปของค3าคงทหรอตวแปรมาด*าเนนการ โดยใช! เครองหมายต3างๆ เปนตวสงงาน ส*าหรบนพจน(ทเรา พบเห<นกนทวไปในชวตประจ*าวน เช3น y = 2x + 5, 2xy – 5 เปนต!น K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 45
  • 46. 9. นพจน'ในภาษา C (ตอ) ● การเขยนนพจน'ในภาษา C คอ การน*าข!อม.ลและตวแปรในภาษา C มาด*าเนน การด!วยเครองหมายทางคณตศาสตร( ตรรกศาสตร( หรอ เครองหมายเปรยบเทยบในภาษา C เปนตวสง งาน K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 46
  • 47. 9. นพจน'ในภาษา C (ตอ) ● นพจน'ทางคณตศาสตร' การเขยนจะเหมอนกบการเขยนนพจน(ทาง คณตศาสตร(ตามปกต เพยงแต3เปลยนมาใช! เครองหมายทางคณตศาสตร(ของภาษา C แทน ซ4ง ต!องระวงเครองหมายบางตวทใช!ไม3เหมอนกน เช3น การค.ณจะใช!เครองหมาย * แทน x หรอหารจะใช! เครองหมาย / แทน K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 47
  • 48. 9. นพจน'ในภาษา C (ตอ) ● นพจน'ทางตรรกศาสตร' คอ การเขยนนพจน(โดยใช!เครองหมายการด*าเนน การทางตรรกศาสตร(ในภาษา C (&&, ||, !) เปนตวสง งาน ซ4งส3วนใหญ3แล!วนพจน(ทางตรรกศาสตร(จะอย.3 รวมกบนพจน(ประเภทอนๆ เช3น c && (a <= b), (b >= c) || (c <= a) K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 48
  • 49. 9. นพจน'ในภาษา C (ตอ) ● ล.าด!บเครองหมายในการค.านวณ ส3วนใหญ3นพจน(ทเขยนข4นในโปรแกรมมกจะมความ ซบซ!อน มการด*าเนนการหลายอย3างปะปนอย.3 ภายในนพจน(เดยวกน เช3น a / b + 15 * c หรอ (a – b) * 10 / c && d + 5 ซ4งผลลพธ(จะออกมาเปนอย3างนน ต!องพจารณา จากล*าดบความส*าคญก3อนหลงของเครองหมายท ภาษา C ก*าหนดไว! K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 49
  • 50. ล.าด!บของเครองหมายในการค.านวณ ล.าด!บความส.าค!ญ เครองหมาย 1 () 2 !, ++, --, (typecast) 3 *, /, % 4 +, - 5 <, <=, >, >= K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 50
  • 51. 10. การเปลยนชนดขอม-ลของต!วแปร ในภาษา C ถ!าน*าตวแปรต3างชนดมาด*าเนนการร3วมกน เช3น int + float หรอ int – char การทจะด*าเนนการตาม เครองหมายได!นนจะต!องเปลยนชนดของตวแปรให!เปน ชนดเดยวกนก3อน โดยวธการเปลยนตวแปรในภาษา C เรยกว3า Casting ซ4งมอย.3 2 ร.ปแบบ คอ ● การเปลยนชนดข!อม.ลของตวแปรอตโนมต ● การเปลยนชนดข!อม.ลของตวแปรโดยใช!ค*าสง K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 51
  • 52. การเปลยนชนดขอม-ลของต!วแปร อ!ตโนม!ต ● เราไม3ต!องท*าอะไร ตวแปลภาษา C จะจดการให! ทงหมด โดยใช!หลกการเปลยนชนดของตวแปร ขนาดเล<กกว3าไปตามชนดของตวแปรทมขนาดใหญ3 กว3า K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 52
  • 53. ตารางการเปลยนชนดขอม-ลของ ต!วแปรแบบอ!ตโนม!ต Var.1 Var. 2 Var. 3 char int char → int int long int → long int unsinged int int → unsinged int int float int → float int double int → double float double float → double long double long → double double long double double → long double K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 53
  • 54. Example 11 (ex0311.c) #include <stdio.h> #include <stdlib.h> int main( ) { int n = 5; float f = 3.5, r; r = 8.500000 r = n + f; printf(“r = %fn”, r); system(“PAUSE”); return 0; } K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 54
  • 55. ตารางรห!สควบค*มร-ปแบบการแสดง ผล รห!สควบค*ม น.าไปใช %d หรอ %i แสดงตวเลขจ*านวนเต<ม %u แสดงตวเลขจ*านวนเต<มบวก %0 แสดงตวเลขฐานแปด %x แสดงตวเลขฐานสบหก %f แสดงตวเลขจ*านวนทศนยม (6 หลก) %e แสดงตวเลขทศนยมร.ปแบบของ E หรอ e ยกก*าลง %c แสดงอกขระ 1 ตว (char) %s แสดงข!อความ (string) %p แสดงตวชต*าแหน3ง (pointer) K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 55
  • 56. ตารางอ!กขระควบค*มการแสดงผล อกขระควบค1ม ความหมาย n ข4นบรรทดใหม3 t เว!นช3องว3างเปนระยะ 1 tab (เท3ากบ 6 ตวอกษร) r ก*าหนดให! cursor ไปอย.ต!นบรรทด 3 f เว!นช3องว3างเปนระยะ 1 หน!าจอ b ลบอกขระส1ดท!ายออก 1 ตว K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 56
  • 57. การเปลยนชนดขอม-ลของต!วแปร โดยใชค.าส!ง ● เปนการใช!ค*าสงเพอเปลยนชนดของตวแปร เรา สามารถเลอกชนดของตวแปรทต!องการจะเปลยนไป ใช!ได! ● ร-ปแบบ: (type) expression; โดยท: type คอ ชนดของตวแปรทต!องการจะเปลยน expression คอ ตวแปรหรอข!อม.ลทต!องการจะ เปลยน K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 57
  • 58. Example 12 (ex0312.c) #include <stdio.h> #include <stdlib.h> int main( ) { int n = 5; n = 5.000000 float f = 3.5; f=3 n+f=8 printf(“n = %fn”, (float)n); printf(“f = %dn”, (int)f); printf(“n + f = %dn”, (n + (int)f)); system(“PAUSE”); return 0; } K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 58
  • 59. 11. การแสดงผลในภาษา C ● สามารถท*าได!หลายวธ แต3วธทนยมใช!กนมากทส1ด คอ การเรยกใช!ฟ8งก(ชน printf( ) ซ4งเปนฟ8งก(ชน มาตรฐานทใช!ในการแสดงผลข!อม.ลท1กชนดออก ทางหน!าจอ (Monitor) ไม3วาจะเปนจ*านวนเต<ม 3 ทศนยม อกขระ หรอข!อความ K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 59
  • 60. 11. การแสดงผลในภาษา C (ตอ) ร-ปแบบ: printf(format, expression-1, expression-2); โดยท: format คอ ส3วนทใช!ควบค1มร.ปแบบการแสดงผล expression-1, expression-2 คอ นพจน(หรอ ตวแปรทต!องการแสดงผล K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 60
  • 61. 11. การแสดงผลในภาษา C (ตอ) K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 61
  • 62. Example 13 (ex0313.c) #include <stdio.h> #include <stdlib.h> int main( ) { int a = 5, b = 10, c; 5 10 15 printf(“%dt%dn”, a, b); c = a + b; printf(“%d”, c); system(“PAUSE”); return 0; } K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 62
  • 63. แสดงผลทละอ!กขระดวยค.าส!ง putchar( ) ร-ปแบบ: putchar(expression); โดยท: expression คอ ตวแปรหรอข!อม.ลทเปนชนดข!อม.ล เปนอกขระ เขยนอย.3ในเครองหมาย ' ' เช3น 'A' K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 63
  • 64. Example 14 (ex0314.c) #include <stdio.h> #include <stdlib.h> Function printf( ) : A int main( ) { Function putchar( ) : A char ch = 'A'; printf(“Function printf( ) : %cn”, ch); printf(“Function putchar( ) : “); putchar(ch); system(“PAUSE”); return 0; } K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 64
  • 65. แสดงผลเป3นขอความดวยค.าส!ง puts( ) ร-ปแบบ: puts(expression); โดยท: expression คอ ตวแปรหรอข!อม.ลทเปนชนดข!อม.ล เปนอกขระ เขยนอย.3ในเครองหมาย “ “ เช3น “ABC” K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 65
  • 66. Example 15 (ex0315.c) #include <stdio.h> #include <stdlib.h> Function printf( ) : ABC int main( ) { Function puts( ) : ABC char str[ ] = “ABC”; printf(“Function printf( ) : %sn”, str); printf(“Function puts( ) :); puts(str); system(“PAUSE”); return 0; } K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 66
  • 67. 12. การร!บขอม-ลในภาษา C ● รบข!อม.ลจากแปDนพมพ(ในภาษา C สามารถเรยกใช! ฟ8งก(ชน scanf( ) ซ4งเปนฟ8งก(ชนมาตรฐานส*าหรบรบ ข!อม.ลจากแปDนพมพ( โดยสามารถรบข!อม.ลได!ท1ก ประเภท ไม3ว3าจะเปนจ*านวนเต<ม ทศนยม อกขระ หรอข!อความ K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 67
  • 68. 12. การร!บขอม-ลในภาษา C (ตอ) ร-ปแบบ: scanf(format, &variable-1, &variable-2); โดยท: format คอ ส3วนทใช!ควบค1มร.ปแบบการรบข!อม.ล variable-1, variable-2 คอ ตวแปรทใช!ในการเก<บ ค3า และจ*าเปนต!องมเครองหมาย & น*าหน!าตวแปร ยกเว!นตวแปรชนด string ทไม3จ*าเปนต!องมเครองหมาย & น*าหน!าตวแปร K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 68
  • 69. ร!บขอม-ลมากกวาหน4งคาดวยค.าส!ง scanf( ) ● scanf(%d %f %d”, &var1, &var2, &var3); ● scanf(%d/%f/%d”, &var1, &var2, &var3); ● scanf(%d,%f,%d”, &var1, &var2, &var3); ● scanf(%s %s %s”, var1, var2, var3); K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 69
  • 70. Example 16 (ex0316.c) #include <stdio.h> #include <stdlib.h> Input number to 2 value : 3 6 int main( ) { a+b=9 int a, b, c; printf(“Input number to 2 value : “); scanf(“%d %d”, &a, &b); c = a + b; printf(“a + b = %dn”, c); system(“PAUSE”); return 0; } K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 70
  • 71. Example 17 (ex0317.c) #include <stdio.h> #include <stdlib.h> Input number to a : 3 int main( ) { Input number to b : 6 int a, b, c; a+b=9 printf(“Input number to a : “); scanf(“%d”, &a); printf(“Input number to b : “); scanf(“%d”, &b); c = a + b; printf(“a + b = %dn”, c); system(“PAUSE”); return 0; 71 }
  • 72. ร!บขอม-ลทละอ!กขระดวย getchar( ) และ getch( ) ร-ปแบบ: variable = getchar( ); variable = getch( ); โดยท: variable คอ ตวแปรทเปนชนดข!อม.ลเปนช1ดอกขระ (ข!อความ) เขยนอย.3ในเครองหมาย ' ' เช3น 'A' K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 72
  • 73. ร!บขอม-ลทละอ!กขระดวย getchar( ) และ getch( ) (ตอ) ● getchar( ) ปDอนข!อม.ลจากแปDมพมพ( 1 ค3า (1 อกขระ) ตามด!วย Enter ● getch( ) ปDอนข!อม.ลจากแปDนพมพ( 1 ค3า (1 อกขระ) ไม3ต!องกด Enter และไม3สามารถค3าทปDอนพมพ(ขณะ ทพมพ( K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 73
  • 74. Example 18 (ex0318.c) #include <stdio.h> #include <stdlib.h> int main( ) { char ch; printf(“Enter character : “); scanf(“%c”, &ch); printf(“Result ch : %cn“, ch); system(“PAUSE”); return 0; } K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 74
  • 75. Example 19 (ex0319.c) #include <stdio.h> #include <stdlib.h> int main( ) { char ch1, ch2; printf(“Enter character 1 : “); ch1 = getchar( ); printf(“Result ch1 : %cn“, ch1); printf(“Enter character 2: “); ch2 = getch( ); system(“PAUSE”); return 0; } บทท 3 พนฐานของโปรแกรมภาษา C 75
  • 76. ร!บขอม-ลเป3นขอความดวย gets( ) ร-ปแบบ: gets(expression); โดยท: variable คอ ตวแปรหรอข!อม.ลทเปนชนดข!อม.ลเปน ช1ดอกขระ (ข!อความ) เขยนอย.3ในเครองหมาย “ “ เช3น “ABC” K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 76
  • 77. Example 20 (ex0320.c) #include <stdio.h> #include <stdlib.h> int main( ) { char str[ ] = “”; printf(“Input string : “); scanf(“%s”, str); printf(“String : %sn“, str); system(“PAUSE”); return 0; } K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 77
  • 78. Example 21 (ex0321.c) #include <stdio.h> #include <stdlib.h> int main( ) { char str[ ] = “”; printf(“Input string : “); gets(str); printf(“String : %sn“, str); system(“PAUSE”); return 0; } K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 78
  • 79. 13. หมายเหต* (Comment) ● ช3วยอธบายการท*างานของโปรแกรม เพอให!เกด ความเข!าใจในสงทเขยน ● ไม3มผลกบโปรแกรมทเขยนข4น ● ร.ปแบบหมายเหต1 – /* */ → อธบายได!หลายบรรทด – // → อธบายในบรรทดเดยว เรมตงแต3ม เครองหมาย // ก*ากบ K.Warawut บทท 3 พนฐานของโปรแกรมภาษา C 79