งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

ตัวแปรและชนิดของข้อมูล (Variables & Data Types)

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "ตัวแปรและชนิดของข้อมูล (Variables & Data Types)"— ใบสำเนางานนำเสนอ:

1 ตัวแปรและชนิดของข้อมูล (Variables & Data Types)
บทที่ 3 ตัวแปรและชนิดของข้อมูล (Variables & Data Types)

2 หัวข้อ ตัวแปร (Variables) ชนิดของข้อมูล (Data Types) ชนิดของตัวแปร
ตัวแปร Integer ตัวแปร Floating-Point ตัวแปร Character ตัวแปร String ตัวแปร Array ค่าคงที่ (Constant)

3 3.1 ตัวแปร ตัวแปร (Variable) เป็นชื่อที่ใช้แทนการอ้างอิงข้อมูลที่ เก็บอยู่ใน Memory ตัวแปร ที่เก็บค่าต่างๆ ในโปรแกรม (เช่น X, Y, SUM, …) จะต้องถูกจองเนื้อที่ไว้ที่ Address หนึ่งๆ ใน Memory (เช่น , , , …) ค่าของตัวแปร สามารถเปลี่ยนค่าได้ ในขณะประมวลผล . . . Memory ... address การจองเนื้อที่ให้ตัวแปร int X, Y, SUM; X การกำหนดค่าตัวแปร 50 Y 100 X = 50; SUM 150 Y = 100; การคำนวณค่าตัวแปร SUM = X+Y;

4 3.1.1 การตั้งชื่อตัวแปร กฎการตั้งชื่อตัวแปร ตัวอย่าง:
1. ประกอบด้วย a - z, A - Z, หรือ _ 2. อักษรตัวแรกต้องเป็น a - z, A - Z, หรือ _ เท่านั้น 3. ห้ามใช้ชื่อเฉพาะ (Reserved Words) เช่น int, if, float,... 4. ยาวสูงสุด 31 ตัวอักษร ตัวอย่าง: ชื่อที่ใช้ได้ เช่น i, n, _sys, K, SUM, … แต่ชื่อที่ไม่อนุญาต เช่น int, 5j, sum 2 ชื่อที่มีตัวพิมพ์ใหญ่ / เล็ก มีความหมายต่างกัน เช่น ชื่อตัวแปร sum, Sum, SUM ต่างกัน

5 3.1.2 การประกาศตัวแปร การประกาศตัวแปร (Variable declaration)
data_type variable1, variable2, ... ; เช่น int X, Y; เพื่อเป็นการจองเนื้อที่ใน Memory เพื่อเก็บข้อมูล และเรียกใช้ในโปรแกรม (ในรูปแบบของตัวแปร) . .. . 2m-1 Address Memory . . . X Y ชนิดของตัวแปร มี 3 ชนิด (ตามชนิดข้อมูลที่เก็บ) 1. ตัวแปรชนิด Integer (2 bytes) 2. ตัวแปรชนิด Floating Point (4 bytes) 3. ตัวแปรชนิด Character (1 byte)

6 3.2 ชนิดข้อมูล ชนิดข้อมูล (Data Types) ในภาษา C มีหลายชนิด (int, float, char,…) ที่มีขนาด (byte) ต่างกัน ผู้ใช้ต้องเลือกใช้ให้เหมาะสมกับการใช้งาน (ใช้เนื้อที่น้อยๆ โดยไม่เกิดค่า Overflow) -215 +215-1 ชนิดข้อมูล ขนาด (bits) ค่าต่ำสุด ค่าสูงสุด จำนวนเต็ม (Integer) int 16 -32768 +32767 จำนวนจริง(Real) แบบ Floating Point float 32 x1038 x1038 ตัวอักษร(Character) char 8 -128 +127

7 ชนิด Integer ข้อมูลชนิด Integer (16 bits): int เก็บค่าเลขนับได้ทั้งค่า บวกและลบ (ต้องใช้ 1 บิตสำหรับเครื่องหมาย) (max) (min) ค่าทีเป็นไปได้ (Valid Range) Overflow Overflow ข้อมูล Integer (16 bits): unsigned int ที่เก็บค่าเลขนับเฉพาะค่าบวก

8 ชนิด long int ข้อมูลชนิด long Integer (32 bits): long เก็บค่าเลขนับได้ ทั้งค่าบวกและลบ (ต้องใช้ 1 บิตสำหรับเครื่องหมาย) (max) (min) ค่าทีเป็นไปได้ (Valid Range) Overflow Overflow ข้อมูล long Integer (32 bits): unsigned long ที่เก็บค่าเลขนับเฉพาะค่าบวก

9 ชนิด Float ข้อมูลชนิด Floating Point (32 bits)
~ x 1038 ค่าบวก ถึง (2-2-23)2128 (max) ค่าลบ -(2-2-23)2128 (min) ถึง ค่า (-) ที่เป็นไปได้ ค่า (+) ที่เป็นไปได้ Overflow Overflow -(2-2-23)x (2-2-23)x2128

10 ชนิด double ค่า double Fl-Pt (64 bits) จะมีค่าในช่วง (+, -) คือ
ข้อมูลชนิด double precision Floating Point (64 bits) ค่า double Fl-Pt (64 bits) จะมีค่าในช่วง (+, -) คือ ~ x 10308 ค่าบวก ถึง (2-2-52)22048 (max) ค่าลบ -(2-2-52)22048 (min) ถึง

11 ชนิด Character ข้อมูลชนิด Character (8 bits) ในภาษา C จะใช้รหัส ASCII (American Standard Code for Information Interchange) มีสัญลักษณ์ 28 = 256 ในช่วง

12 ASCII A - Z

13 3.3 ชนิดตัวแปร ตัวแปรขั้นสูง
ตัวแปรในภาษา C มีหลายชนิด ที่กำหนด ตามชนิดของข้อมูล ตัวแปรพื้นฐาน 3 ชนิด ตัวแปร Integer (2 bytes) เก็บค่าเลขจำนวนเต็ม ตัวแปร Floating Point (4 bytes) เก็บค่าเลขจำนวนจริง ตัวแปร Character (1 byte) เก็บตัวอักษรรหัส ASCII ตัวแปรขั้นสูง ตัวแปร String (m bytes) เก็บหลายตัวอักษร ตัวแปร Array (n bytes) เก็บค่าเลขหลายสมาชิก

14 3.3.1 ตัวแปร Integer ตัวแปรชนิด Integer (เลขจำนวนเต็ม) ที่สามารถแสดง ได้ 3 เลขฐาน เลขฐาน 10 (0,1,2,3,4,5,6,7,8,9) เลขฐาน 8 (0,1,2,3,4,5,6,7) เลขฐาน 16 (0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F) ตัวแปร Integer (ขนาด 16, 32 บิต) int (16 bits): unsigned int (16 bits): long (32 bits): unsigned long (32 bits): ตัวอย่างเช่น int X; // 2 bytes long N; // 4 bytes

15 ตัวอย่าง 3.1 เขียนโปรแกรม รับค่าตัวแปร N เป็นเลขฐาน 10 (%d)
(Decimal Number) แล้วแสดงผลลัพธ์ เป็นเลขฐาน 8, 10, 16 ด้วย %o, %d, %X #include <stdio.h> void main() { unsigned int N; printf(“Enter N (base 10): ”); scanf(“%d”, &N); ผลลัพธ์ Enter N (base 10): 10 N = 12 (base 8) N = 10 (base 10) N = A (base 16) printf(“N = %o (base 8)\n”, N); printf(“N = %d (base 10)\n”, N); printf(“N = %X (base 16)\n”, N); } 15

16 ตัวอย่าง 3.2 เขียนโปรแกรม รับค่าตัวแปร N เป็นเลขฐาน 16 (%X)
(Hexadecimal Number) แล้วแสดงผลลัพธ์ เป็นเลขฐาน 8, 10, 16 ด้วย %o, %d, %X #include <stdio.h> void main() { unsigned int N; printf(“Enter N (base 16): ”); scanf(“%X”, &N); ผลลัพธ์ Enter N (base 16): 20 N = 40 (base 8) N = 32 (base 10) N = 20 (base 16) printf(“N = %o (base 8)\n”, N); printf(“N = %d (base 10)\n”, N); printf(“N = %X (base 16)\n”, N); } 16

17 การกำหนดค่า รูปแบบ (เลขฐาน 10): variable = value;
Memory . . . . .. . 2m-1 Address รูปแบบ (เลขฐาน 10): variable = value; รูปแบบ (เลขฐาน 8): variable = 0value; รูปแบบ (เลขฐาน 16): variable = 0Xvalue; X1 10 X2 010 0X10 X3 ตัวอย่าง int X1, X2, X3; // ประกาศ ตัวแปร ใช้เก็บข้อมูลชนิด integer // กำหนดค่าตัวแปรเป็นเลขฐาน 10 X1 = 10; X2 = 010; X3 = 0X10; // กำหนดค่าตัวแปรเป็นเลขฐาน 8 // กำหนดค่าตัวแปรเป็นเลขฐาน 16

18 %format ใน scanf, printf
ในคำสั่ง scanf(“%format”, &variable); %format %d สำหรับรับข้อมูลของตัวแปร int %u สำหรับรับข้อมูลของตัวแปร unsigned int %ld สำหรับรับข้อมูลของตัวแปร long %lu สำหรับรับข้อมูลของตัวแปร unsigned long %o, %lo สำหรับรับข้อมูลของตัวแปร int, long, unsigned (ฐาน 8) %X, %lX สำหรับรับข้อมูลของตัวแปร int, long, unsigned (ฐาน 16) ในคำสั่ง printf(“%format”, variable); %format %d,%u,%ld,%lu กำหนดใช้สำหรับข้อมูลเช่นเดียวกับใน scanf %o, %#o สำหรับพิมพ์ข้อมูลเลขฐาน 8 (กรณี # จะมี 0 นำหน้าค่าเลขฐาน 8) %X, %#X สำหรับพิมพ์ข้อมูลเลขฐาน 16 (กรณี # จะมี 0X นำหน้าค่าเลขฐาน 16) %.2d สำหรับพิมพ์ข้อมูล 2 ตำแหน่งรวมเลข 0 นำหน้า %10d สำหรับพิมพ์ข้อมูล integer ชิดขวาภายใน 10 ตำแหน่ง

19 ตัวอย่าง 3.3 เขียนโปรแกรมรับข้อมูล วัน (dd) เดือน (mm) ปี (yyyy) เช่น 05, 09, 2010 (เมื่อ dd=05, mm = 09, yyyy = 2010) และ พิมพ์ผลลัพธ์ในรูปแบบ dd/mm/yyyy แบบ ชิดซ้าย (ปกติ) และชิดขวา (ภายใน 20 ตำแหน่ง) ผลลัพธ์ #include <stdio.h> void main() { unsigned int dd,mm,yyyy; printf(“Enter day (dd):”); scanf(“%d”, &dd); printf(“Enter month (mm):”); scanf(“%d”, &mm); printf(“Enter year (yyyy):”); scanf(“%d”, &yy); printf(“%d/%d/%d\n”, dd,mm,yyyy); printf(“%.2d/%.2d/%d\n”, dd,mm,yyyy); printf(“%12.2d/%.2d/%d\n”, dd,mm,yyyy); } 5 / 9 / 0 5 / / 19

20 3.3.2 ตัวแปร Floating Point ตัวแปร Floating Point (เลขจำนวนจริง)
มี 2 ขนาด (32, 64 บิต) float (32 bits หรือ 4 bytes) Double (64 bits หรือ 8 bytes) Memory . . . X Y ตัวอย่างเช่น float X; // 4 bytes (single precision) double Y; // 8 bytes (double precision) x 1038 x 1038 x 10308 x 10308 20

21 ตัวอย่าง 3.4 เขียนโปรแกรมคำนวณปริมาตรของรูปทรงเหลี่ยม (Volume) = Width x Length x Height (กว้าง x ยาว x สูง) และแสดงผลลัพธ์จากการคำนวณ start Input W,L,H Volume=W*L*H Print Volume end #include <stdio.h> void main() { int W, L, H; float Volume; printf(“Enter width: ”); scanf(“%d”, &W); printf(“Enter length: ”); scanf(“%d”, &L); ผลลัพธ์ printf(“Enter height: ”); scanf(“%d”, &H); Enter width: _ 5 Volume = W * L * H; printf(“Volume = %f\n”, Volume); } Enter length: _ 10 Enter height: _ 2 Volume = 100.0

22 %format ใน scanf, printf
ในคำสั่ง scanf(“%format”, &variable); %format %f สำหรับรับข้อมูลของตัวแปร float %e สำหรับรับข้อมูลของตัวแปร float ในรูปแบบ ±m.dddddde±xx %lf, %le สำหรับรับข้อมูลของตัวแปร double ในคำสั่ง printf(“%format”, variable); %format %f สำหรับพิมพ์ข้อมูลของตัวแปร float, double %e สำหรับพิมพ์ข้อมูลของตัวแปร float ในรูปแบบ ±m.dddddde±xx %.2f สำหรับพิมพ์ข้อมูลของตัวแปร float, double (พิมพ์ทศนิยม 2 ตำแหน่ง) %.0f สำหรับพิมพ์ข้อมูลของตัวแปร float, double (พิมพ์ทศนิยม 0 ตำแหน่ง) %20f สำหรับพิมพ์ข้อมูล float, double ชิดขวาภายใน 20 ตำแหน่ง %20.2f สำหรับพิมพ์ข้อมูลของตัวแปร float, double ชิดขวาภายใน 20 ตำแหน่ง (พิมพ์ทศนิยม 2 ตำแหน่ง) %20.0f สำหรับพิมพ์ข้อมูลของตัวแปร float, double ชิดขวาภายใน 20 ตำแหน่ง (พิมพ์ทศนิยม 0 ตำแหน่ง)

23 ตัวอย่าง การพิมพ์เลขทศนิยมให้ชิดขวา (ภายใน 10 ตำแหน่ง) และระบุทศนิยม 2 ตำแหน่ง #include <stdio.h> void main() { float x; x= ; printf("%10.2f\n", x); x= ; printf("%10.2f\n", x); x= ; printf("%10.2f\n", x); } ผลลัพธ์ 23

24 ตัวอย่าง 3.5 เขียนโปรแกรมคำนวณคะแนนสอบรวม = Mid-term 40% + Final 60% และแสดงผลลัพธ์จากการคำนวณ start Input ID,M,F Total=M*0.4+F*0.6 Print ID, Total end #include <stdio.h> void main() { int ID, M, F; float Total; printf(“Enter ID: ”); scanf(“%d”, &ID); printf(“Enter mid-term (0-100): ”); scanf(“%d”, &M); printf(“Enter final (0-100): ”); scanf(“%d”, &F); ผลลัพธ์ Enter ID: _ Total = (M*0.4) + (F*0.6); printf(“ID=%d, Total=%.2f\n”, ID, Total); } Enter mid-term: _ 81 Enter final: _ 72 ID = , Total = 75.60

25 ตัวอย่าง 3.6 เขียนโปรแกรมคำนวณเงินฝากพร้อมดอกเบี้ย เมื่อเวลาผ่านไป 1 ปี และ 2 ปี เมื่อรายได้เงินฝาก (Income)=เงินต้น (M) x(1+อัตราดอกเบี้ย (R))ปี = Mx(1+R)Y start Input M, R Income=M*(1+R) Print Income end #include <stdio.h> void main() { float M, R, Income, Income2; printf(“Enter Invested Money (M): ”); scanf(“%f”, &M); Income2 = M*(1+R)*(1+R) Income2 printf(“Enter %Interest Rate (R): ”); scanf(“%f”, &R); R = R/100; Income = M * (1+R); Income2 = M * (1+R)*(1+R); ผลลัพธ์ Enter Invested Money (M): _ 10000 printf(“Income after 1 year : %20.2f\n”, Income); printf(“Income after 2 years:%20.2f\n”,Income2); } Enter %Interest Rate (R): _ 5 Income after 1 year : Income after 2 years:

26 ในคำสั่ง scanf และ printf
3.3.3 ตัวแปร Character . .. . 2m-1 Address Memory . . . ตัวแปร Character (อักขระ): char เก็บใน Memory เป็นรหัส ASCII (8 bits หรือ 1 byte) ตัวแปรชนิดนี้ สามารถแสดงผลได้ทั้งแบบตัวอักษร (%c) และรหัส ASCII ฐาน 10 (%d), ฐาน 8 (%o), ฐาน 16 (%X) ตัวอย่างเช่น char C; // ประกาศตัวแปร C ใช้เก็บค่าเป็นตัวอักษร (Character) scanf(“%c”, &C) a C ในคำสั่ง scanf และ printf %format %c สำหรับข้อมูลของตัวแปร char เป็นตัวอักษร %d, %o, %X สำหรับข้อมูลของตัวแปร char เป็นรหัส ASCII ฐาน 10, 8, หรือ 16

27 ตัวอย่าง 3.7 เขียนโปรแกรมรับค่าเป็นตัวอักษร (%c) แล้วหาตัวอักษรอีก 3 ตัวถัดจากนั้น พร้อมค่า ASCII (ฐาน 10) %d #include <stdio.h> void main() { char c, c1, c2, c3; printf(“Enter a character:”); scanf(“%c”, &c); . .. . 2m-1 Addr Memory . . . c1 = c+1; c2 = c+2; c3 = c+3; a c c1 b c d printf(“ASCII of %c is %d\n”, c, c); printf(“ASCII of %c is %d\n”, c1, c1); printf(“ASCII of %c is %d\n”, c2, c2); printf(“ASCII of %c is %d\n”, c3, c3); } c2 c3 27

28 ในคำสั่ง scanf และ printf
3.3.4 ตัวแปร String . .. . 2m-1 Address Memory . . . ตัวแปร String (สายอักษร): char var[size] เก็บค่าหลายตัวอักษร (ตัวละ 1 byte) %format ใน scanf และ printf คือ %s ตัวอย่างเช่น char Name[10]; // ประกาศตัวแปร Name เก็บค่าได้ 10 ตัวอักษร scanf(“%s”, &Name); Name No v e mb e r ในคำสั่ง scanf และ printf %format %s สำหรับข้อมูลของตัวแปร string เป็นหลายตัวอักษร

29 ตัวอย่าง 3.8 เขียนโปรแกรมรับข้อมูล วัน (Day) เดือน (Month) ปี (Year) เช่น 1, November, 2010 และแสดงผล Month day, year และอักษรย่อของเดือน ผลลัพธ์ Enter Day : _ 1 #include <stdio.h> void main() { int Day, Year; char Month[20], c1, c2, c3; printf("Enter Day: "); scanf("%d", &Day); printf("Enter Month: "); scanf("%s", &Month); printf("Enter Year : "); scanf("%d", &Year) Enter Month (in string) : _ November Enter Year : _ 2010 Date is November 1, 2010 November => Nov c1 = Month[0]; c2 = Month[1]; c3 = Month[2]; printf("Date is %s %d, %d\n", Month, Day, Year); printf(“%s => %c%c%c\n”, Month, c1, c2, c3); } 29

30 3.4 ค่าคงที่ ค่าคงที่ (Constant) คล้ายตัวแปร
แต่ค่าคงที่ เมื่อกำหนดแล้วจะเปลี่ยนค่าไม่ได้ ส่วนค่าที่เก็บในตัวแปรสามารถเปลี่ยนแปลงได้ตลอดเวลา การประกาศค่าคงที่ ทำได้ 2 วิธี 1. ประกาศคล้ายกับตัวแปรแต่มี const นำหน้า const data_type constant_name = constant; เช่น const float pi = ; 2. ประกาศไว้ในส่วน Preprocessor Directive #define constant_name constant เช่น #define PI 30

31 ตัวอย่าง 3.9 เขียนโปรแกรมเพื่อคำนวณพื้นที่วงกลม ผลลัพธ์
Area = pr2 เมื่อกำหนดค่า p = (เป็นค่าคงที่) #include <stdio.h> void main() { int r; float Area; const float Pi = ; printf(“Enter radius (r): ”); scanf(“%d”, &r); start Input r Area = PI*r*r Print Area end PI= ผลลัพธ์ Area = Pi * r * r; printf(“Circular area = %f \n”, Area); } Enter radius (r):_ 2 Area = 31

32 ตัวอย่าง 3.10 เขียนโปรแกรมคำนวณเส้นรอบรูปของวงกลม
เมื่อ เส้นรอบรูป (Circumference) =2pr, p = (เป็นค่าคงที่) ผลลัพธ์ #include <stdio.h> #define PI void main() { int r; float Circ; printf("Enter radius (r): "); scanf("%d", &r); Enter radius _ 4 Circular Circumference = start Input r Circ = 2*PI*r Print Circ end PI= Circ = 2 * PI * r; printf(“Circular circumference = %f\n", Circ); } 32


ดาวน์โหลด ppt ตัวแปรและชนิดของข้อมูล (Variables & Data Types)

งานนำเสนอที่คล้ายกัน


Ads by Google