242 101 241 101 Introduction to Computer Programming

  • Slides: 88
Download presentation
242 -101, 241 -101 Introduction to Computer Programming บทท 2 แนะนำภาษา C Basic C

242 -101, 241 -101 Introduction to Computer Programming บทท 2 แนะนำภาษา C Basic C Program คณาจารยภาควชาวศวกรรมคอมพวเต อร introcom@coe. psu. ac. th Department of Computer Engineering, PSU 1

242 -101, 241 -101 Introduction to Computer Programming ขนตอนการพฒนาโปรแกรมภาษาซ Compiler (c-free, gcc, dev-c++) Source

242 -101, 241 -101 Introduction to Computer Programming ขนตอนการพฒนาโปรแกรมภาษาซ Compiler (c-free, gcc, dev-c++) Source Code ( file. c ) Preprocessing & Compilation Linker Object code )file. o) Linking �������� (������ Machine code / Program / executable file ( file. exe ) Department of Computer Engineering, PSU 5

242 -101, 241 -101 Introduction to Computer Programming ขนตอนการพฒนาโปรแกรม Source Code -------------------------- Compiler Object

242 -101, 241 -101 Introduction to Computer Programming ขนตอนการพฒนาโปรแกรม Source Code -------------------------- Compiler Object File --. obj Library --------------------- Linker Executable File --. exe OS Hardware Department of Computer Engineering, PSU • ���� Source Code ����� Object File • ����� Code ������� (Library) • ����� machine language (Executable 9

242 -101, 241 -101 Introduction to Computer Programming โปรแกรมแรกกบภาษา C Pre-processor # include <stdio.

242 -101, 241 -101 Introduction to Computer Programming โปรแกรมแรกกบภาษา C Pre-processor # include <stdio. h> int main (void) { ���� main // My first program printf(“Hello World!!!”); system(“PAUSE”); return 0; } เปนโปรแกรมสงพมพขอความ Department of Computer Engineering, PSU comment ����� �� ����� �main Hello World!!! 10

242 -101, 241 -101 Introduction to Computer Programming สวนตางๆ ในโปรแกรมภาษาซ Comment ส วนหว และ/หรอสวนอนๆ

242 -101, 241 -101 Introduction to Computer Programming สวนตางๆ ในโปรแกรมภาษาซ Comment ส วนหว และ/หรอสวนอนๆ ของโปรแกรม สวนของ preprocessors int main ( void ) { สวนของโปรแกรม return 0 ; } Department of Computer Engineering, PSU 11

242 -101, 241 -101 Introduction to Computer Programming ตวอยางโปรแกรม จงแสดงผลลพธของโปรแก รม # include <stdio.

242 -101, 241 -101 Introduction to Computer Programming ตวอยางโปรแกรม จงแสดงผลลพธของโปรแก รม # include <stdio. h> int main (void) { printf(“My name is Tommy n”); return 0; } ผลการรนโปรแกรม My name is Tommy Department of Computer Engineering, PSU 21

242 -101, 241 -101 Introduction to Computer Programming ทดสอบความเขาใจ จงเขยนโปรแกรมแสดงชอ พรอมกบรหสนกศกษา # include <stdio.

242 -101, 241 -101 Introduction to Computer Programming ทดสอบความเขาใจ จงเขยนโปรแกรมแสดงชอ พรอมกบรหสนกศกษา # include <stdio. h> นามสกล int main (void) { printf(“Tommy Kimn 53102999n”); return 0; } ผลการรนโปรแกรม : Tommy Kim 53102999 Department of Computer Engineering, PSU 22

242 -101, 241 -101 Introduction to Computer Programming ตวแปรและตวดำเนนการ คณาจารยภาควชาวศวกรรมคอมพวเต อร introcom@coe. psu. ac.

242 -101, 241 -101 Introduction to Computer Programming ตวแปรและตวดำเนนการ คณาจารยภาควชาวศวกรรมคอมพวเต อร introcom@coe. psu. ac. th Department of Computer Engineering, PSU 23

242 -101, 241 -101 Introduction to Computer Programming การตงชอตวแปร • หามตงชอซำกบคำสงวน ซงเปนคำทมอยแลวในภาษา (Keywords) C

242 -101, 241 -101 Introduction to Computer Programming การตงชอตวแปร • หามตงชอซำกบคำสงวน ซงเปนคำทมอยแลวในภาษา (Keywords) C auto volatile switch short signed sizeof static register for double else enum extern default break while typedef union unsigned void Department of Computer Engineering, PSU float do case char const continue struct goto if int long 32

242 -101, 241 -101 Introduction to Computer Programming ตวอยาง คำตอไปนสามารถนำมาตงเปนชอตวแปรได หรอไม number 2 value

242 -101, 241 -101 Introduction to Computer Programming ตวอยาง คำตอไปนสามารถนำมาตงเปนชอตวแปรได หรอไม number 2 value grade 1 student_id float _score $age Number-person Department of Computer Engineering, PSU 33

242 -101, 241 -101 Introduction to Computer Programming ตวอยาง จากคำสงตอไปน int a, b, c;

242 -101, 241 -101 Introduction to Computer Programming ตวอยาง จากคำสงตอไปน int a, b, c; a = 2; b = 3; c = a + b; จงหาคาของตวแปร Department of Computer Engineering, PSU c C = 5 36

242 -101, 241 -101 Introduction to Computer Programming ตวอยาง จากคำสงตอไ ปน b=6 int a,

242 -101, 241 -101 Introduction to Computer Programming ตวอยาง จากคำสงตอไ ปน b=6 int a, b; or a = 2; a =b=9 5; b = a + 4; จงหาคาของตวแป รb Department of Computer Engineering, PSU จากคำสงตอไ ปน a=2 int a; a = 2; or a = a + 3; a=5 จงหาคาของตวแป รa 37

ตวดำเนนการทางคณตศาสตร Operator) 242 -101, 241 -101 Introduction to Computer Programming (Arithmetic ������ + *

ตวดำเนนการทางคณตศาสตร Operator) 242 -101, 241 -101 Introduction to Computer Programming (Arithmetic ������ + * / % ++ -Department of Computer Engineering, PSU ��� ��� ����� (Modulo) ������� 1 38

242 -101, 241 -101 Introduction to Computer Programming ลำดบของตวดำเนนการ ����������� () ����� * /

242 -101, 241 -101 Introduction to Computer Programming ลำดบของตวดำเนนการ ����������� () ����� * / % ����� + ����� Department of Computer Engineering, PSU 39

242 -101, 241 -101 Introduction to Computer Programming ตวอยาง จงหาคาของนพจนตอไปน 10/5 3/9 7/2 7.

242 -101, 241 -101 Introduction to Computer Programming ตวอยาง จงหาคาของนพจนตอไปน 10/5 3/9 7/2 7. 0/2 21%4 10%7 10%2 3%7 Department of Computer Engineering, PSU 2 _______ 0 _____________ 3. 5 _______ 1 _______ 3 _______ 0 _______ 3 _______ 41

242 -101, 241 -101 Introduction to Computer Programming ตวอยาง. จงหาคาของนพจนตอไปน 10 + 2 *

242 -101, 241 -101 Introduction to Computer Programming ตวอยาง. จงหาคาของนพจนตอไปน 10 + 2 * 3 * -1 4 _______ 2 * 5 % 8 + -7 -5 _______ (3 + 5/2)*(7 - 3 - 5%2) 5 * Department of Computer Engineering, PSU _______ 15 3 42

242 -101, 241 -101 Introduction to Computer Programming ตวอยาง จงเขยนผลลพธของโปรแกรมตอไปน b= 3 a= 3

242 -101, 241 -101 Introduction to Computer Programming ตวอยาง จงเขยนผลลพธของโปรแกรมตอไปน b= 3 a= 3 //increment. c #include <stdio. h> int main() { int a = 2, b, c; b = ++a; printf(“b= %d a= %dn”, b, a); c = b++; printf(“b= %d c= %dn”, b, c); return 0; } b= 4 c=3 Department of Computer Engineering, PSU 46

242 -101, 241 -101 Introduction to Computer Programming //cast. c #include <stdio. h> int

242 -101, 241 -101 Introduction to Computer Programming //cast. c #include <stdio. h> int main() { int a =10, b=4; double x 1, x 2, x 3; printf("a/b= %d n", (a/b)); x 1=a/b; printf("x 1= %f n", x 1); x 2= (double)(a/b); printf("x 2= %f n", x 2); x 3= (double)a/b; printf("x 3= %f n", x 3); return 0; } Department of Computer Engineering, PSU 48

242 -101, 241 -101 Introduction to Computer Programming ตวอยาง จากคำสงตอไปน int num 1 =

242 -101, 241 -101 Introduction to Computer Programming ตวอยาง จากคำสงตอไปน int num 1 = 3, num 2 = 1; num 1 *= 4; num 2 += num 1 - 2; จงหาคาของตวแปร Department of Computer Engineering, PSU num 2=11 50

242 -101, 241 -101 Introduction to Computer Programming ตวอยางการใชตวแปรภายใน // internal. c #include <stdio.

242 -101, 241 -101 Introduction to Computer Programming ตวอยางการใชตวแปรภายใน // internal. c #include <stdio. h> void my_func(); int main() { double x = 1. 1; my_func(); printf(“In main x = %fn”, x); return 0; } void my_func() { double x; x = 2. 5; printf(“In my_func x = %fn”, x); } Department of Computer Engineering, PSU 52

242 -101, 241 -101 Introduction to Computer Programming ตวอยางการใชตวแปรภายนอก //external. c #include <iostream. h>

242 -101, 241 -101 Introduction to Computer Programming ตวอยางการใชตวแปรภายนอก //external. c #include <iostream. h> double x; void my_func(); int main() { x = 1. 1; my_func(); printf(“In main x = %fn”, x); return 0; } void my_func() { x = 2. 5; printf(“In my_func x = %fn”, x); } Department of Computer Engineering, PSU 53

242 -101, 241 -101 Introduction to Computer Programming ตวแปรภายในทมชอเดยวกบตวแปรภายนอก // variable. c #include <stdio.

242 -101, 241 -101 Introduction to Computer Programming ตวแปรภายในทมชอเดยวกบตวแปรภายนอก // variable. c #include <stdio. h> double x; void my_func(); int main() { double x = 1. 1; my_func(); printf(“In main x = %fn”, x); return 0; } void my_func() { x = 2. 5; printf(“In my_func x = %fn”, x); } Department of Computer Engineering, PSU 54

242 -101, 241 -101 Introduction to Computer Programming Escape sequence ���������� a ����� beep

242 -101, 241 -101 Introduction to Computer Programming Escape sequence ���������� a ����� beep t ������ (tab) n ������� (new line) ” Double Quote “ ’//print. c Single Quote ‘ #include<stdio. h> int main(){ printf("Welcomettto my programn"); printf("C language isteasynn"); printf(""Good bye"n"); return 0; } Department of Computer Engineering, PSU 61

242 -101, 241 -101 Introduction to Computer Programming ตวอยาง //input. c #include<stdio. h> int

242 -101, 241 -101 Introduction to Computer Programming ตวอยาง //input. c #include<stdio. h> int main(){ char name[100]; int salary; printf("Please enter your name: "); scanf("%s", name); printf("Please enter your salary: "); scanf("%d", &salary); printf("%s gets %dn", name, salary); } ����������� (weight)�������� Department of Computer Engineering, PSU 62

242 -101, 241 -101 Introduction to Computer Programming Format Code ทควรร • • •

242 -101, 241 -101 Introduction to Computer Programming Format Code ทควรร • • • %c = อกขระ หรอ ตวอกษรหนงตว (character) %d = ตวเลขจำนวนเตม (decimal integer) %f = เลขทศนยม (floating-point number) %. 2 f = เลขทศนยม 2 ตำแหนง %o = เลขฐานแปด (octal number) %s = ขอความ (string) Department of Computer Engineering, PSU 63

#include<stdio. h> Example 242 -101, 241 -101 Introduction to Computer Programming int main() {

#include<stdio. h> Example 242 -101, 241 -101 Introduction to Computer Programming int main() { int a = 14, b = 3; int i = 10, j = 2, k = 5, x = 4, y = 5; /*Difference between / and % */ printf("Difference between / and %n"); printf("a = %3 d , b = %3 d n", a, b); printf("a/b = %3 d n", a/b); printf("a%%b = %3 d nn", a%b); return 0; } Department of Computer Engineering, PSU 64

242 -101, 241 -101 Introduction to Computer Programming Example #include<stdio. h> int main() {

242 -101, 241 -101 Introduction to Computer Programming Example #include<stdio. h> int main() { int a = 14, b = 3; int i = 10, j = 2, k = 5, x = 4, y = 5; /*Using casting*/ printf("Using castingn"); printf("(float) a/b = %fnn", (float)a/b); return 0; } Department of Computer Engineering, PSU 65

Example 242 -101, 241 -101 Introduction to Computer Programming #include<stdio. h> int main() {

Example 242 -101, 241 -101 Introduction to Computer Programming #include<stdio. h> int main() { int a = 14, b = 3; int i = 10, j = 2, k = 5, x = 4, y = 5; /*Precedence of arithmetic operators*/ printf("Precedence of arithmetic operatorsn"); printf("i = 10, j = 2, k = 5, x = 4, y = 5n"); printf("i+j*k = %dn", i+j*k); printf("(i+j)*k = %dn", (i+j)*k); printf("i+(j*k) = %dn", i+(j*k)); printf("x*y+i/10*k-8 = %dn", x*y+i/10*k-8); return 0; } Department of Computer Engineering, PSU 66

242 -101, 241 -101 Introduction to Computer Programming #include <stdio. h> int main ()

242 -101, 241 -101 Introduction to Computer Programming #include <stdio. h> int main () { int i, j; printf("Enter the value of i: "); scanf("%d", &i); printf("n. Enter the value of j: "); scanf("%d", &j); Example } /* Logical Operator */ printf("n. Using Logical Operatorn"); printf(" i&&j is %dn", i&&j); printf(" i||j is %dn", i||j); printf(" !i is %dn", !i); printf(" !j is %dn", !j); All integer numbers except 0 return 0; mean “true” (1) Department of Computer Engineering, PSU 67

242 -101, 241 -101 Introduction to Computer Programming Case 1: ลมปดปกกาของ main 1. #include<stdio.

242 -101, 241 -101 Introduction to Computer Programming Case 1: ลมปดปกกาของ main 1. #include<stdio. h> 2. main(){ • ขอความแสดงขอผดพลาด In function `main': syntax error at end of input • การตความ รปแบบมขอผดพลาดตอนทายของโปรแกรม Department of Computer Engineering, PSU 70

242 -101, 241 -101 Introduction to Computer Programming Case 2: พมพคำวา #include ผด 1.

242 -101, 241 -101 Introduction to Computer Programming Case 2: พมพคำวา #include ผด 1. #inlude<stdio. h> 2. main(){ 3. } • ขอความแสดงขอผดพลาด Line 1: invalid preprocessing directive #inlude • การตความ ������� preprocessor ����� #inlude Department of Computer Engineering, PSU 71

242 -101, 241 -101 Introduction to Computer Programming Case 3: พมพ stdio. h ผด

242 -101, 241 -101 Introduction to Computer Programming Case 3: พมพ stdio. h ผด 1. #include<stio. h> 2. main(){ 3. } • ขอความแสดงขอผดพลาด Line 1: stio. h: No such file or directory. • การตความ ไมมไฟลทกำหนด Department of Computer Engineering, PSU ใหตรวจสอบชอไฟลใหถกตอง 72

242 -101, 241 -101 Introduction to Computer Programming Case 4: พมพชอฟงกชน main ผด 1.

242 -101, 241 -101 Introduction to Computer Programming Case 4: พมพชอฟงกชน main ผด 1. #include<stdio. h> 2. man(){ 3. } • ไมมขอผดพลาดในการคอมไพล • ผดพลาดในขณะลงค [Linker error] undefined reference to `Win. Main@16' ld returned 1 exit status ������ main ������ Department of Computer Engineering, PSU 73

242 -101, 241 -101 Introduction to Computer Programming Case 5: ลม ; 1. #inlude<stdio.

242 -101, 241 -101 Introduction to Computer Programming Case 5: ลม ; 1. #inlude<stdio. h> 2. main(){ 3. int x 4. } • ขอความแสดงขอผดพลาด In function main: Line 4: syntax error before '}' token • การตความ −ไปยงบรรทดท 4 และมองหาคำสงในบรรทดกอนหนาวาตกหลน Department of Computer Engineering, PSU ; หรอไม 74

242 -101, 241 -101 Introduction to Computer Programming Case 6: ลม ; อกครง 1.

242 -101, 241 -101 Introduction to Computer Programming Case 6: ลม ; อกครง 1. #inlude<stdio. h> 2. main(){ 3. int x 4. x = 2; 5. } • ขอความแสดงขอผดพลาด In function main: Line 4: syntax error before "x" • การตความ −ไปยงบรรทดท 4 และมองหาคำสงในบรรทดกอนหนาวาตกหลน Department of Computer Engineering, PSU ; หรอไม 75

242 -101, 241 -101 Introduction to Computer Programming Case 7: ลม ; หลาย ๆ

242 -101, 241 -101 Introduction to Computer Programming Case 7: ลม ; หลาย ๆ จด 1. #inlude<stdio. h> 2. main(){ 3. int x 4. x = 2 5. } • ขอความแสดงขอผดพลาด In function main: Line 4: syntax error before "x" • คอมไพเลอรบอกแคอนเดยว Department of Computer Engineering, PSU ใหแกทละจด 76

Case 9: ลมปดวงเลบเวลาใช 242 -101, 241 -101 Introduction to Computer Programming printf หรอ scanf

Case 9: ลมปดวงเลบเวลาใช 242 -101, 241 -101 Introduction to Computer Programming printf หรอ scanf 1. #inlude<stdio. h> 2. main(){ 3. printf("Hin"; 4. } • ����������� In function main: Line 3: syntax error before '; ' token • ��������������������� 3 ���������� (syntax error befor)�������� (token) ; Department of Computer Engineering, PSU 78

Case 10: ลมเปดวงเลบเวลาใช 242 -101, 241 -101 Introduction to Computer Programming printf หรอ scanf

Case 10: ลมเปดวงเลบเวลาใช 242 -101, 241 -101 Introduction to Computer Programming printf หรอ scanf 1. #inlude<stdio. h> 2. main(){ 3. printf"Hin"); 4. } • ����������� In function main: Line 3: syntax error before string constant • ��������� 3 ����������������� “…” Department of Computer Engineering, PSU 79

242 -101, 241 -101 Introduction to Computer Programming Case 13: ลมเปดวงเลบเครองหมายคำพด 1. #include<stdio. h>

242 -101, 241 -101 Introduction to Computer Programming Case 13: ลมเปดวงเลบเครองหมายคำพด 1. #include<stdio. h> 2. main(){ 3. printf. Hin"); 4. } • In function main: −Line 3: stray '' in program 3: `printf. Hi' undeclared (first use in this function) 3: syntax error before "n" 3: missing terminating " character • ตความ เชนเดยวกบการลมเครองหมายคำพดแตยงผดพลาดมากกวาเพราะทำใหคำส ง printf ผดพลาดไปดวย นนคอเปนคำสงทไมรจก Department of Computer Engineering, PSU 82

242 -101, 241 -101 Introduction to Computer Programming Case 14: เอาตวแปรไปใสคาคงท 1. #include<stdio. h>

242 -101, 241 -101 Introduction to Computer Programming Case 14: เอาตวแปรไปใสคาคงท 1. #include<stdio. h> 2. main(){ 3. int x; 4. 2 = x; 5. } • In function main: −Line 4: invalid lvalue in assignment • ��������� 4 �������������� (lvalue-left value)������� Department of Computer Engineering, PSU 83

ขอผดพลาดบางอยาง 242 -101, 241 -101 Introduction to Computer Programming คอมไพเลอรเตอนเราไมได 1. int x; 2.

ขอผดพลาดบางอยาง 242 -101, 241 -101 Introduction to Computer Programming คอมไพเลอรเตอนเราไมได 1. int x; 2. scanf("%d"); //very dangerous!! 3. scanf("%d", x); //forgot & 4. printf("%d"); 5. printf("Hi", x); Department of Computer Engineering, PSU 84