01001012 Principle of Computer Programming unsigned char 8

  • Slides: 51
Download presentation

ตารางแสดงขนาดและขอบเขตชนดของขอมล 01001012 Principle of Computer Programming ชนดของตวแป ร ขนาด คาตำส ด unsigned char 8

ตารางแสดงขนาดและขอบเขตชนดของขอมล 01001012 Principle of Computer Programming ชนดของตวแป ร ขนาด คาตำส ด unsigned char 8 bit 0 255 char 8 bit -128 127 unsigned int 16 bit 0 65, 535 short int 16 bit -32, 768 32, 767 unsigned long 32 bit 0 4, 294, 967, 295 long 32 bit -2, 147, 483, 648 2, 147, 483, 647 float 32 bit 3. 4 10 -38 3. 4 1038 double 64 bit 1. 7 10 -308 1. 7 10308 long double 80 bit 3. 4 10 -4932 3. 4 104932 Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ คาสงสด 9 King Mongkut's Institute of Technology

รปแบบการประกาศตวแปรในภาษาซ 01001012 Principle of Computer Programming type var 1; type var 1, var 2,

รปแบบการประกาศตวแปรในภาษาซ 01001012 Principle of Computer Programming type var 1; type var 1, var 2, … , var. N; คอ ชนดของขอมลทจะกำหนดใหกบตวแปร var? คอ ชอของตวแปรทจะตง type Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 10 King Mongkut's Institute of Technology

ตวอยางการประกาศตวแปร int number; int a, b, c; 01001012 Principle of Computer Programming float real;

ตวอยางการประกาศตวแปร int number; int a, b, c; 01001012 Principle of Computer Programming float real; float point 1, point 2; char choice; char ch 1, ch 2; Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 11 King Mongkut's Institute of Technology

ตวอยางการประกาศและกำหนดคาตวแปร 01001012 Principle of Computer Programming int number = 25; int a = 1,

ตวอยางการประกาศและกำหนดคาตวแปร 01001012 Principle of Computer Programming int number = 25; int a = 1, b = 2, c = 3; float real = 99. 99; float point 1 = 45. 2, point 2 = 30; char choice = 'a'; char ch 1 = 'o', ch 2 = 'z'; Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 13 King Mongkut's Institute of Technology

คำสงวน (Reserved word) ในภาษาซ 01001012 Principle of Computer Programming auto break case char const

คำสงวน (Reserved word) ในภาษาซ 01001012 Principle of Computer Programming auto break case char const continue default do double else enum extern float for goto if int long register return short singned sizeof static struct switch typedef union unsigned void volatile while nt : ����� Turbo C++ Reserved Word ����� Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 15 King Mongkut's Institute of Technology

ตวอยางการประกาศและกำหนดคาตวแปร 01001012 Principle of Computer Programming char str[15]; first[20], last[20]; str[15] first[20] last[20] Jirasak

ตวอยางการประกาศและกำหนดคาตวแปร 01001012 Principle of Computer Programming char str[15]; first[20], last[20]; str[15] first[20] last[20] Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 19 King Mongkut's Institute of Technology

ตวอยางการประกาศและกำหนดคาตวแปร 01001012 Principle of Computer Programming char subject[12] = "Programming"; nick[4] = "Com"; nick_1[4]

ตวอยางการประกาศและกำหนดคาตวแปร 01001012 Principle of Computer Programming char subject[12] = "Programming"; nick[4] = "Com"; nick_1[4] = {'C', 'o', 'm', ''}; name[]= "Jirasak"; subject[12] P r o g r a nick[4] m m i r a n g C o m nick_1[4] name[] J i s a Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ k C o m 21 King Mongkut's Institute of Technology

ตวอยางการอางองอกษรในตวแปรขอความ 01001012 Principle of Computer Programming char subject[12] = "Programming"; subject[12] P r o

ตวอยางการอางองอกษรในตวแปรขอความ 01001012 Principle of Computer Programming char subject[12] = "Programming"; subject[12] P r o g r a m m i n g [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] subject[0] subject[1] subject[2] subject[10] Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 'P' 'r' 'o' 'g' 23 King Mongkut's Institute of Technology

การใชงานตวแปรรวมกบคำสง printf 01001012 Principle of Computer Programming • ในคำสง printf มสวนแสดงชนดขอมล ซงตวแปรทใชงานกเปนสวนยอยของขอม ลชนดตาง ๆ

การใชงานตวแปรรวมกบคำสง printf 01001012 Principle of Computer Programming • ในคำสง printf มสวนแสดงชนดขอมล ซงตวแปรทใชงานกเปนสวนยอยของขอม ลชนดตาง ๆ printf (format-string, data-list); #include<stdio. h> • จงสามารถใชตวแปรแทนในสวนของ data-list ได int main() เชน { #include<stdio. h> int main() { float g = 3. 5; printf ("GPA : %. 2 f", g); return 0; printf ("GPA : %. 2 f", 3. 5); return 0; } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ } 24 King Mongkut's Institute of Technology

ตวอยางการใชงานคำสง โดยไมใชตวแปร printf 01001012 Principle of Computer Programming ตองการเขยนโปรแกรมแสดงคำวา จำนวน 5 บรรทดทจอคอมพวเตอร #include <stdio.

ตวอยางการใชงานคำสง โดยไมใชตวแปร printf 01001012 Principle of Computer Programming ตองการเขยนโปรแกรมแสดงคำวา จำนวน 5 บรรทดทจอคอมพวเตอร #include <stdio. h> int main() { printf ("%sn", "Programming"); printf ("%sn", "Programming"); return 0; } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ Programming ����� �� Programming ���� Kmitl ����� ? 25 King Mongkut's Institute of Technology

ตวอยางการใชงานคำสง printf โดยใชตวแปร 01001012 Principle of Computer Programming ตองการเขยนโปรแกรมแสดงคำวา จำนวน 5 บรรทดทจอคอมพวเตอร โดยใชตวแปรขอความ #include

ตวอยางการใชงานคำสง printf โดยใชตวแปร 01001012 Principle of Computer Programming ตองการเขยนโปรแกรมแสดงคำวา จำนวน 5 บรรทดทจอคอมพวเตอร โดยใชตวแปรขอความ #include <stdio. h> int main() { char name[20]="Kmitl"; name[20]="Programming"; char printf ("%sn", name); printf ("%sn", name); return 0; } Sittigorn Jirasak http: //www. kmitl. ac. th/~ksjirasa/ Programming ����� �� ����� Programming ���� Kmitl ����� ? 26 King Mongkut's Institute of Technology

ตวอยางการใชคำสง scanf 01001012 Principle of Computer Programming #include<stdio. h> int main() { float point;

ตวอยางการใชคำสง scanf 01001012 Principle of Computer Programming #include<stdio. h> int main() { float point; char name[20]; printf ("Enter your name : "); scanf ("%s", name); printf ("Enter your point : "); scanf ("%f", &point); return 0; } ����� ������� �� & ���������� ������ & �������������� �� ������������ Jirasak Sittigorn 29 http: //www. kmitl. ac. th/~ksjirasa/ King Mongkut's Institute of Technology

ตวอยางการใชคำสง printf และ scanf 01001012 Principle of Computer Programming #include<stdio. h> int main() {

ตวอยางการใชคำสง printf และ scanf 01001012 Principle of Computer Programming #include<stdio. h> int main() { char first[20], last[20]; printf ("Enter your name and surname : "); scanf ("%s %s", first, last); ������� 1 ������ printf ("Hi %s %sn. How are you? ", first, last); return 0; } Enter your name and surname : Jirasak Sittigorn Hi Jirasak Sittigorn �������� How are you? Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 30 �������� King Mongkut's Institute of Technology

ตวอยางการใชคำสง printf และ scanf 01001012 Principle of Computer Programming #include<stdio. h> int main() {

ตวอยางการใชคำสง printf และ scanf 01001012 Principle of Computer Programming #include<stdio. h> int main() { char name[40]; float gpa; printf ("Enter your name : "); scanf ("%s", name); printf ("Enter your GPA : "); scanf ("%f", &gpa); printf ("Name : %sn", name); printf ("Gpa : %f", gpa); return 0; } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ ������ ��� Name ���� "Com eng" ��� GPA ���� 3. 7 32 King Mongkut's Institute of Technology

ตวอยางการใชคำสง printf และ scanf 01001012 Principle of Computer Programming #include<stdio. h> int main() {

ตวอยางการใชคำสง printf และ scanf 01001012 Principle of Computer Programming #include<stdio. h> int main() { char name[40]; float gpa; printf ("Enter your name : "); scanf ("%[^n]", name); printf ("Enter your GPA : "); scanf ("%f", &gpa); printf ("Name : %sn", name); printf ("Gpa : %f", gpa); return 0; } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 34 King Mongkut's Institute of Technology

ตวอยางการใชคำสง printf และ scanf 01001012 Principle of Computer Programming #include<stdio. h> int main() {

ตวอยางการใชคำสง printf และ scanf 01001012 Principle of Computer Programming #include<stdio. h> int main() { char name[40]; int date, month, year; printf ("Enter your name & surname : "); scanf("%[^n]", name); printf ("Enter your Birthday [d/m/y] : "); scanf("%d/%d/%d", &date, &month, &year); printf ("%s birthday is %d %d %d", name, date, month, year); return 0; } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ ����������� scanf ������ %d/%d/%d ������ 35 King Mongkut's ���� [^n] Institute of Technology

เครองหมายคำนวณทางคณตศาสตร เครองห มาย + * / % 01001012 Principle of Computer Programming การทำงาน บวก

เครองหมายคำนวณทางคณตศาสตร เครองห มาย + * / % 01001012 Principle of Computer Programming การทำงาน บวก ลบ คณ หาร โมดลส (modulo) Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ ตวอยาง ans ans = = a a + * / b; b; ans = a % b; 36 King Mongkut's Institute of Technology

เครองหมายแบบลดรป เครองห มาย ตวอยางการใชงาน 01001012 Principle of Computer Programming ตวอยางรปแบบเตม += y += x;

เครองหมายแบบลดรป เครองห มาย ตวอยางการใชงาน 01001012 Principle of Computer Programming ตวอยางรปแบบเตม += y += x; y = y + x; -= y -= x; y = y - x; *= y *= x; y = y * x; /= y /= x; y = y / x; %= y %= x; y = y % x; Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 39 King Mongkut's Institute of Technology

ตวดำเนนการ Type 1 int & ตวถกดำเนนการ 01001012 Principle of Computer Programming Operator +, -,

ตวดำเนนการ Type 1 int & ตวถกดำเนนการ 01001012 Principle of Computer Programming Operator +, -, *, / Type 2 int Result int +, -, *, / float +, -, *, / int float +, -, *, / float int % Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ int Exam 3*3 9 19/2 9 3*3. 0 9. 000000 19/2. 0 9. 500000 3. 0*3 9. 000000 19. 0/2 9. 500000 3. 0*3. 0 9. 000000 19. 0/2. 0 9. 500000 7%4 3 8%4 0 40 King Mongkut's Institute of Technology

นพจนในภาษาซ 01001012 Principle of Computer Programming ลำดบความสำค ญ เครองหมาย 1 () 2 3 4

นพจนในภาษาซ 01001012 Principle of Computer Programming ลำดบความสำค ญ เครองหมาย 1 () 2 3 4 5 6 7 8 9 !, ++, --, (type) *, /, % +, <, <=, >, >= ==, != && || *=, /=, %=, +=, -= Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 41 King Mongkut's Institute of Technology

ตวอยางการเปลยนชนดของขอมล 01001012 Principle of Computer Programming float gpa = 3. 9; int num =

ตวอยางการเปลยนชนดของขอมล 01001012 Principle of Computer Programming float gpa = 3. 9; int num = 3; printf ("gpa(f) : %-10 f num(d) : %-10 dn", gpa, num); printf ("gpa(d) : %-10 d num(f) : %-10 fn", gpa, num); printf ("(int)gpa(d) : %-10 d", (int)gpa); printf ("(float)num(f) : %-10 f", (float)num); Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 43 King Mongkut's Institute of Technology

คำสงแสดงผล และรบขอมลอนๆ 01001012 Principle of Computer Programming putchar(ch); puts(str); ch = getchar(); ch =

คำสงแสดงผล และรบขอมลอนๆ 01001012 Principle of Computer Programming putchar(ch); puts(str); ch = getchar(); ch = getch(); gets(str); Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 44 King Mongkut's Institute of Technology

ตวอยางคำสงแสดงผล และรบขอมล 01001012 Principle of Computer Programming char ch = 'A', str[] = "Computer";

ตวอยางคำสงแสดงผล และรบขอมล 01001012 Principle of Computer Programming char ch = 'A', str[] = "Computer"; putchar (ch); putchar (' '); putchar (str[1]); putchar ('n'); puts (str); A o Computer Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 47 King Mongkut's Institute of Technology

ตวอยางคำสงแสดงผล และรบขอมล 01001012 Principle of Computer Programming char ch 1, ch 2; printf ("Enter

ตวอยางคำสงแสดงผล และรบขอมล 01001012 Principle of Computer Programming char ch 1, ch 2; printf ("Enter Character 1 : "); ch 1 = getchar(); printf ("Enter Character 2 : "); ch 2 = getch(); puts ("n**** Output ****"); printf ("Char 1 = %cn. Char 2 = %c", ch 1, ch 2); Enter Character 1 : J Enter Character 2 : **** Output **** Char 1 = J Char 2 = O Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ ����� O ������� �� 51 King Mongkut's Institute of Technology