SlideShare a Scribd company logo
1 of 80
การเขียนโปรแกรมการเขียนโปรแกรม
เบื้องต้นด้วยภาษาซีเบื้องต้นด้วยภาษาซี
2
เนื้อหาเนื้อหา
• Dev-C++: ซอฟต์แวร์ช่วยพัฒนา
โปรแกรม
• โครงสร้างโปรแกรมภาษาซี
• ชนิดของข้อมูล ตัวแปร
• คำาสั่งควบคุมการไหลของโปรแกรม
• ฟังก์ชัน
• ข้อผิดพลาดที่พบบ่อย
3
เริ่มต้นใช้งานเริ่มต้นใช้งาน Dev-C++Dev-C++
• สร้างไฟล์ใหม่จากเมนู
File> New > SourceFile
• ทดลองกรอกโปรแกรม
ตามตัวอย่างด้านขวา
• เซฟไฟล์โดยใช้นามสกุล .c

เช่น first.c
4
เริ่มต้นใช้งานเริ่มต้นใช้งาน Dev-C++Dev-C++
• ทดลองคอมไพล์และรันโปรแกรม

โดยคลิ้กปุ่ม

หรือกด F9
• หากดูผลลัพธ์ไม่ทัน:

เพิ่มคำาสั่ง getch() ด้านท้ายของโปรแกรม
ทำาใบงานที่ 1.1
6
ตัวอย่างโปรแกรมตัวอย่างโปรแกรม
ภาษาซีภาษาซี
#include <stdio.h>
int main()
{
int a, b, sum;
printf("Enter A: ");
scanf("%d", &a);
printf("Enter B: ");
scanf("%d", &b);
sum = a+b;
printf("Sum = %dn", sum);
getch();
return 0;
}
#include <stdio.h>
int main()
{
int a, b, sum;
printf("Enter A: ");
scanf("%d", &a);
printf("Enter B: ");
scanf("%d", &b);
sum = a+b;
printf("Sum = %dn", sum);
getch();
return 0;
}
คืนค่า 0 เพื่อระบุ
ว่าโปรแกรมจบ
การทำางานโดย
อ่านไฟล์ stdio.h
เพื่อเอาฟังก์ชัน
มาตรฐานมาใช้
ฟังก์ชัน main
ระบุจุดเริ่มต้น
ของโปรแกรม
คืนค่ากลับเป็น
int
ประกาศตัวแปร
แบบ int สามตัว
คือ a, b และ sum
ฟังก์ชัน
มาตรฐาน
printf() ใช้พิมพ์
ข้อความออกทาง
หน้าจอ
ฟังก์ชันมาตรฐาน
scanf() ใช้รับอิน
พุทจากแป้นพิมพ์
เมื่อจบในแต่ละคำา
สั่งให้ปิดท้าย
ด้วย ;
•เป็นการเขียนอธิบายการทำางานเพื่อ
ให้เข้าใจและอ่านโปรแกรมได้ง่าย
ขึ้นและจะไม่ถูกประมวลผล
•รูปแบบการเขียนหมายเหตุ
การเขียนหมายเหตุ (Comment)
/* … ข้อความที่ต้องการเขียน
อธิบาย(หลายบรรทัด)… */
//… ข้อความที่ต้องการเขียนอธิบาย
(หนึ่งบรรทัด) …
/* arithmetic
expression */
เช่น
// create
ค่าคงที่อักขระพิเศษ
a ตัวอักษรเรียกความสนใจ
(Beep)
n ขึ้นบรรทัดใหม่ (newline)
 backslash
? เครื่องหมายคำาถาม
’ ฝนทอง
9
โครงสร้างหน่วยความโครงสร้างหน่วยความ
จำาจำา
1 ช่อง = 1 ไบต์ (8 บิต
ตำาแหน่งที่อยู่
(address)
ข้อมูลที่เก็บ
(data)
0000 32
0001 67
0002 255
0003 0
0004 121
: :
10
ชนิดข้อมูลชนิดข้อมูล
การ
ประกาศ
ขนาด
(ไบต์)
ค่าตำ่าสุด ค่าสูงสุด
char 1 -128 +127
short 2 -32,768 +32,767
int 2 หรือ
4
-32,768 หรือ
-2,147,483,648
+32,767 หรือ
+2,147,483,647
long 4 -2,147,483,648 +2,147,483,647
float 4 3.4 * 10-38
3.4 * 1038
double 8 1.7 * 10-308
1.7 * 10-308
11
• ใช้แทนการอ้างถึงหน่วยความจำา ณ
ตำาแหน่งหนึ่ง ๆ

เพื่อใช้ในการเก็บข้อมูล (ที่เปลี่ยนค่าได้)
ในขณะประมวลผล
ตัวแปรตัวแปร (Variable)(Variable)
short a, b, sum;
a = 10;
b = 5;
sum = a+b;
short a, b, sum;
a = 10;
b = 5;
sum = a+b;
ตำาแหน่ง ค่า
: :
1000 2
1001 30
1002 211
1003 5
1005 23
: :
1004 8
10
0
5
0
0
15
b มีค่า 5 (อ้างถึงค่า)
b มีค่า 1002 (อ้างถึงตำาแหน่ง)
a
b
sum
กฏในการตั้งชื่อตัวแปรกฏในการตั้งชื่อตัวแปร
(Variable)(Variable)
ประกอบด้วยอักษร ตัวเลข และ
เครื่องหมาย underscore ( _ )

ตัวแรกต้องเป็นตัวอักษรหรือ
เครื่องหมาย underscore ( _ )

ความยาว 1-32 ตัวอักษร

ห้ามมีเครื่องหมายอื่นยกเว้น $

Case sensitive

ห้ามตั้งชื่อซำ้ากับคำาสงวน
(Reserve words)
คำาสงวนในภาษา C
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof while do
รูปแบบการประกาศชนิด
ตัวแปร
• variable-list หมายถึงชื่อตัวแปรที่
ต้องการ
ประกาศ ถ้ามีมากกว่า 1 ตัว แยกกัน
ด้วยเครื่องหมาย
คอมม่า ( , )

type หมายถึงชนิดของตัวแปร

value หมายถึงค่าเริ่มต้นที่ต้องการ
type variable-list [=value] ;
ตัวอย่าง การประกาศ
ชนิดตัวแปร
int a=1;
int lower;
float man,ratio;
double point;
char ch,c=‘5’,name;
ค่าคงที่
#include<stdio.h>
#definePI 3.414
int main()
{
printf("%.3f",PI);
getch();
return 0;
}
#include<stdio.h>
#definePI 3.414
int main()
{
printf("%.3f",PI);
getch();
return 0;
}
const.cconst.c
3.414
นิพจน์ (Expression)• นิพจน์ หมายถึง การนำาตัวแปร ค่า
คงที่ มาสัมพันธ์กันโดยใช้เครื่องหมาย
อย่างหนึ่งอย่างใดเป็นตัวเชื่อม เช่น
นิพจน์คณิตศาสตร์ นิพจน์
ภาษา Ca + b
cd
(a+b)/ (c*d)
10x + 3xy + 10y10 * x + 3*x*y + 10*y
กฎเกณฑ์ในการเขียน
นิพจน์•1. ห้ามเขียนตัวแปร 2 ตัวติดกัน
โดยไม่มีเครื่องหมาย
เช่น ab ในภาษา C ต้องเขียน
a*b จะเขียนเป็น ab ไม่ได้เพราะ
จะถือเป็นชื่อตัวแปรตัวเดียวชื่อ ab
ไม่ใช่ค่า a คูณ b
•2. ถ้าเขียนนิพจน์โดยมีค่าของ
ตัวแปรหรือค่าคงที่ต่างชนิดกันใน
นิพจน์เดียวกัน ภาษา C จะเปลี่ยน
ถ้า
ใ
ช้
cha
r
กับ int จะเปลี่ยน
เป็น
int
“
“
“
“
“
“
“
“
flo
a
t
int
int
int
int
lon
g
อะไ
กับ
กับ
กับ
กับ
กับ
กับ
กับ
กับ
double
float
long
double
unsign
ed
double
long
long
doub
“
“
“
“
“
“
“
“
double
float
long
double
unsign
ed
double
long
long
doub
ข้อมูลชนิดตัวอักษรข้อมูลชนิดตัวอักษร
•ตัวอักษร A-Z, 0-9 และสัญลักษณ์ต่าง ๆ
ข้อมูล ชนิดตัวอักษรจะใช้จำานวน 8
บิตในการเก็บตัวอักษร 1 ตัว ซึ่งในภาษา
ซีไม่มีข้อมูลชนิดข้อความ หรือ สตริง
(String) ข้อมูลชนิดข้อความจะประกอบ
ด้วยตัวอักษรหลาย ๆ ตัวเรียงติดกันเป็น
ข้อความ โดยใช้อาร์เรย์ (Array) ในการ
จัดเก็บ ซึ่งเราเรียกว่า Array of
Character อาร์เรย์ที่ใช้เก็บข้อมูลชนิด
ข้อความ จะต้องมีจำานวนมากกว่าความ
ยาวของข้อความ หรือสตริงอย่างน้อย
1 ตัวอักษร เพื่อใช้เก็บสตริงศูนย์ (Null
เครื่องหมายเครื่องหมาย
คณิตศาสตร์คณิตศาสตร์• เครื่องหมายคณิตศาสตร์ (Arithmetic Operators)
เครื่องหมาย ความหมาย ตัวอย่าง
+ การบวก A + B
- การลบ A - B
* การคูณ A * B
/ การหาร A / B
% การหารเอาแต่เศษไว้ 5%3 = 2
(Modulus)
-- การลดค่าลงครั้งละ 1 A-- มีค่าเท่ากับ
A = A-1
++ การเพิ่มค่าขึ้นครั้งละ 1 A++ มีค่าเท่ากับ
A = A+1
นิพจน์คณิตศาสตร์นิพจน์คณิตศาสตร์
(Expression)(Expression)•นิพจน์คณิตศาสตร์ประกอบด้วย ตัวแปร
หรือ ค่าคงที่ ที่เชื่อมกันด้วยเครื่องหมาย
คณิตศาสตร์ การเขียนนิพจน์คณิตศาสตร์
จะคล้ายกับสมการคณิตศาสตร์ เช่น
x = (n1 + n2) x10
•เมื่อเขียนเป็นนิพจน์คณิตศาสตร์จะได้ดังนี้
x = (n1 + n2) * 10
•นิพจน์ที่อยู่ชั้นในสุดจะถูกประมวลผลก่อน
เครื่องหมายคณิตศาสตร์ที่มีลำาดับเดียวกัน
จะถูกประมวลผลจากซ้ายไปขวา
2. ตัวดำาเนินการเลขคณิต
(arithmetic operators)
เป็นตัวดำาเนินการที่ทำาให้เกิดการกระทำา
ทางคณิตศาสตร์ ประกอบด้วย
operat
or
ความหมาย
+ การบวก
- การลบ และ unary operator
* การคูณ
/ การหาร ซึ่งจะเป็นการหาผลลัพธ
จากการหาร
%
( mod
การหาเศษ(remainder) ที่เป็น
จำานวนเต็มจากการหาร (operand
ตัวอย่าง แสดงขั้นตอน
การทำางานของตัว
ดำาเนินการ
• 4 + 3 * 2
4 + 6
• 9 * 2 - 15/3 + 7
18 - 5 + 7
13 + 7
20
จงหาค่านิพจน์ต่อไปนี้
-(-5+(2*4-1))+((6+2)*5+8)/4
-(-5+(8-1))+(8*5+8)/4
-(-5+7)+(40+8)/4
-(2)+48/4
-2+12
10
ลำาดับการประมวลผลลำาดับการประมวลผล
เครื่องหมายคณิตศาสตร์เครื่องหมายคณิตศาสตร์
เครื่องหมายทาง
คณิตศาสตร์
ลำาดับการ
ประมวลผล
++, -- 1
- (เครื่องหมายลบ
หน้าตัวเลข)
2
* / % 3
+ - 4
ตัวอย่าง เช่น
•2+8*2 = 18 นำา 8 คูณ 2 ได้ 16
แล้วบวกด้วย 2
•(2+8)*2 = 20 นำา 8 บวก 2 ได้
10 แล้วคูณด้วย 2
•4/2*3 = 6 นำา 4 หารด้วย 2 ได้ 2
แล้วคูณด้วย 3
•++ n หมายถึง เพิ่มค่า n
อีก 1
•-- n หมายถึง ลดค่า n ลง 1
•y = x+1 หมายถึง การเพิ่มค่า
แบบฝึกหัดท้ายบท
1. จงหาผลลัพธ์ของนิพจน์ต่อไปนี้ เมื่อกำาหนด
ให้ n1=5 และ n2=10
1.1 x = (n1 + n2) / 3
1.2 x = n1 + n2 / 3
1.3 x = n2%n1
1.4 x = n1--
1.5 x = n2++2. จงหาค่าของนิพจน์ต่อไปนี้ ถ้ากำาหนดให้ a = 2, b
= 3, c = 4,
d = 5, e = 6 และ f = 8
2.1 a + e / f -- * c
2.2 (f - e) * (c / a)
2.3 a * d / a + e / b
2.4 a * (d / (a + e)) / b
แบบฝึกหัดท้ายบท
ทำาใบงานที่ 2.1
31
อาร์เรย์และสตริงอาร์เรย์และสตริง
• ข้อมูลแบบอาร์เรย์เป็นการรวมกลุ่ม
ข้อมูลชนิดเดียวกันไว้เป็นแถวลำาดับ
• จัดเป็นตัวแปรแบบตัวชี้ประเภทหนึ่ง
short arr[3];
arr[0] = 50;
arr[2] = 89;
short arr[3];
arr[0] = 50;
arr[2] = 89;
arr มีค่า 20
arr[0] มีค่า 50
arr[1] มีค่า 211
arr[2] มีค่า 89
23
8
ตำาแหน่ง ค่า
: :
0020 2
0021 30
0022 211
0023 5
0027 12
: :
0026 33
0025
0024
50
0
89
0
arr
32
ข้อมูลประเภทสายข้อมูลประเภทสาย
อักขระอักขระ ((สตริงสตริง))• “สตริง” คืออาร์เรย์ของอักขระโดด
(array of char)
char s[5] = "Hello";
printf("%c %c %cn", s[0], s[1], s[2]);
char s[5] = "Hello";
printf("%c %c %cn", s[0], s[1], s[2]);
H e lH e l
ผลลัพธ์:
H e l l o
0 1 2 3 4
S
33
การรับการรับ//แสดงผลแสดงผล
• ฟังก์ชันมาตรฐาน (เพื่อการรับ/แสดง
ผลข้อมูล) ถูกเตรียมไว้ในเฮดเดอร์
ไฟล์ “stdio.h”
• ฟังก์ชันรับข้อมูล

scanf สำาหรับตัวแปรทุกชนิด (ระบุด้วย
%format)
• ฟังก์ชันแสดงผลข้อมูล (Output
Function)

printf สำาหรับตัวแปรทุกชนิด (ระบุด้วย
%format)
34
การแสดงผลข้อมูลการแสดงผลข้อมูล
• ทุกอย่างในหน่วยความจำาล้วนเป็น
ตัวเลข
• การแสดงผลลัพธ์ด้วยฟังก์ชัน printf
จึงต้องระบุรูปแบบตามที่เราต้องการจะ
เห็น
int c = 65;
printf("c (as a number) = %dn", c);
printf("c (as a character) = %cn", c);
int c = 65;
printf("c (as a number) = %dn", c);
printf("c (as a character) = %cn", c);
หน่วยความจำา
:
2
30
65
5
23
:
8
c
c (as a number) = 65
c (as a character) = A
c (as a number) = 65
c (as a character) = A
ผลลัพธ์:
35
%format%format ในใน scanfscanf และและ
printfprintf
ชนิดข้อมูล ชนิดตัวแปร %for
mat
จำานวนเต็ม
int %d
long %ld
long long %lld
unsigned int %u
unsigned long %lu
unsigned long
long
%llu
จำานวนมี
ทศนิยม
float %f
double %lf
อักขระโดด char %c
สตริง char array[] %s
36
ตัวอย่างการใช้ตัวอย่างการใช้ scanfscanf
char name[20];
int age;
printf("Enter your name and age: ");
scanf("%s %d", name, &age);
printf("Hello %s. You are %d years old.n",
name, age);
char name[20];
int age;
printf("Enter your name and age: ");
scanf("%s %d", name, &age);
printf("Hello %s. You are %d years old.n",
name, age);
Enter your name and age: Tony 38
Hello Tony. You are 38 years old.
Enter your name and age: Tony 38
Hello Tony. You are 38 years old.
ผลลัพธ์:
37
การไหลของโปรแกรมการไหลของโปรแกรม
• โปรแกรมแบบง่าย
ทำางานรวดเดียวจาก
บนลงล่าง
STARTSTARTSTARTSTART
ENDENDENDEND
StatementStatementStatementStatement
โปรแกรมที่มีคำาโปรแกรมที่มีคำา
สั่งเดียวสั่งเดียว
โปรแกรมที่มีโปรแกรมที่มี
หลายคำาสั่ง
StatementStatement11
StatementStatement11
StatementStatement22
StatementStatement22
StatementStatementnn
StatementStatementnn
ENDENDENDEND
STARTSTARTSTARTSTART
StatementStatement33
StatementStatement33
Relational Operators
• ใช้สำาหรับเปรียบเทียบความสัมพันธ์ของค่า
คงที่หรือตัวแปร(Operand) ที่กำาหนดให้
Operator คำาอธิบาย
< เครื่องหมาย น้อยกว่า
<= เครื่องหมาย น้อยกว่าหรือเท่ากับ
> เครื่องหมาย มากกว่า
>= เครื่องหมาย มากกว่าหรือเท่ากับ
== เครื่องหมาย เท่ากับ
!= เครื่องหมาย ไม่เท่ากับ
Expression
• ใช้เรียกแทนการนำาค่าคงที่ หรือ
ตัวแปร(Operand) เขียนร่วมกับตัวดำาเนินการ
(Operator)
A = 10; B = 3; C = 20;
Expression ผลลัพธ์
A + B 13
(A + B) – C -7
(C*B)+(A*B) 90
Relational Operators
• ตัวอย่างการประมวลผลของ Relational
Operators
i = 1; j = 2; k = 3;
Expression ผลลัพธ์ที่ได้ ค่าที่ได้
i < j true 1
(i + j) >= k true 1
(j + k) > (i + 5) false 0
k != 3 false 0
j == 2 true 1
Logical (Bitwise) Operators
• ใช้สำำหรับเปรียบเทียบ ค่ำคงที่หรือ
ตัวแปร(Operand) เขียนร่วมกับตัวดำำเนินกำร
ทำงตรรกะ (Logical Operator)
Operator คำำอธิบำย
&& เครื่องหมำย AND แปลว่ำ และ
|| เครื่องหมำย OR แปลว่ำ หรือ
ANDOperators
• ตัวดำำเนินกำร และ จะเป็นจริงก็ต่อเมื่อ
ตัวแปรทุกตัวมีค่ำเป็นจริง นอกจำกนั้นเป็น
เท็จทั้งหมด
A B A && B ผลลัพธ์ ค่ำที่
ได้
True True T && T True 1
True False T && F False 0
False True F && T False 0
False False F && F False 0
ANDOperators
• ตัวอย่ำงกำรประมวลผลของ AND Operators
i = 8; j = 4.5; k = ‘z’; // ascii ของ z เท่ำกับ 1 22
Expression ผลลัพธ์ที่ได้ ค่ำที่
ได้
(i >= 6) && (k == ‘z’) T && T == T 1
(i >= 6) && (k == 122) T && T == T 1
(j >= 0) && (j != 4.5) T && F == F 0
(k > 0) && (j != 5) T && T == T 1
(i < 0) && (j > 0) && (k > 0) F && T && T ==
F
0
OROperators
• ตัวดำำเนินกำร หรือ จะเป็นเท็จก็ต่อเมื่อ
ตัวแปรทุกตัวมีค่ำเป็น เท็จ นอกจำกนั้นเป็น
จริงทั้งหมด
A B A || B ผลลัพธ์ ค่ำที่
ได้
True True T || T True 1
True False T || F True 1
False True F || T True 1
False False F || F False 0
OROperators
• ตัวอย่ำงกำรประมวลผลของ OR Operators
i = 8; j = 4.5; k = ‘z’; // ascii ของ z เท่ำกับ 1 22
Expression ผลลัพธ์ที่ได้ ค่ำที่
ได้
(i >= 6) || (k == ‘z’) T || T == T 1
(i >= 6) || (k != 122) T || F == T 1
(j < 0) || (j != 4.5) F || F == F 0
(k > 0) || (j != 5) T || T == T 1
ตัวอย่ำงกำรเปรียบ
เทียบ• ตัวอย่ำงกำรเปรียบเทียบ
i = 7; f = 4.5;
Expression ผลลัพธ์ที่ได้ ค่ำที่ได้
f > 5 False 0
!(f > 5) True 1
i <= 3 False 0
!(i <=3) True 1
i > (f + 1) True 1
!(i > (f + 1)) False 0
ลำำดับควำมสำำคัญ
(Precedence)
ของตัวดำำเนินกำร
• แสดงลำำดับควำมสำำคัญ จำกมำกไปหำ
น้อย ในกำรประมวลผล ตัวดำำเนินกำร
ต่ำงๆ
ประเภท Operators ทิศทำง
unary operators - , ++ , -- , ! , sizeof() R->L
multiply, divideand
remainder
* , / , % L->R
add and subtract + , - L->R
relational < , <= , > , >= L->R
equality == , != L->R
ลำำดับควำมสำำคัญ
(Precedence)
ของตัวดำำเนินกำร (ต่อ)
• แสดงลำำดับควำมสำำคัญ จำกมำกไปหำ
น้อย ในกำรประมวลผล ตัวดำำเนินกำร
ต่ำงๆ
ประเภท Operators ทิศทำ
ง
and && L->R
or || L->R
assignment
operators
=, +=, -=, *=, /=,
%=
R->L
ตัวอย่ำงกำรประมวลผล
โดยพิจำรณำลำำดับควำม
สำำคัญ
i = 8; j = 4.5; k = ‘z’; // ascii ของ z เท่ำกับ 1 22
Expression ผลลัพธ์ที่ได้ ค่ำที่
ได้
i + j <= 10 False 0
i >= 8 && k == ‘z’ True&& True== True 1
k != ‘a’ || i + j <= 10 True|| False== True 1
k != ‘a’ || i < j && k < i
(ตัวอย่ำงที่ไม่ถูกต้อง)
True|| False&& False
== True&& False
== False
0
k != ‘a’ || i < j && k < i
(ตัวอย่ำงที่ถูกต้อง)
True|| False&& False
== True|| False
== True
1
50
กำรควบคุมกำรไหลกำรควบคุมกำรไหล
ของโปรแกรมของโปรแกรม• คำำสั่งกำำหนดเงื่อนไข

โครงสร้ำง if

โครงสร้ำง if…else

โครงสร้ำง if แบบหลำยเงื่อนไข

โครงสร้ำง switch-case
• คำำสั่งวนซ้ำ้ำ

โครงสร้ำง while loop

โครงสร้ำง do…while loop

โครงสร้ำง for loop
• โปรแกรมย่อย (ฟังก์ชัน)
51
โครงสร้ำงโครงสร้ำง ifif
if (if (conditioncondition))
{{
statement1;statement1;
::
statementN;statementN;
}}
if (if (conditioncondition))
{{
statement1;statement1;
::
statementN;statementN;
}}
C SyntaxC Syntax
STARTSTARTSTARTSTART
ENDENDENDEND
StatementStatementStatementStatement
conditionconditionconditioncondition
true
false
StatementStatementStatementStatement
FlowchartFlowchart
• ส่วนของ conditioncondition
ตีควำมเป็นข้อมูลแบบ
int
• ทำำคำำสั่งใน {} หำก
conditioncondition เป็นจริง (ไม่
เป็นศูนย์)
• หำกมีคำำสั่งเดียวไม่
52
ตัวอย่ำงตัวอย่ำง ifif
#include <stdio.h>
int main()
{
int i=101,j=100;
if(i>j)
printf("I> J");
getch();
return 0;
}
#include <stdio.h>
int main()
{
int i=101,j=100;
if(i>j)
printf("I> J");
getch();
return 0;
}
I > J
if1.cif1.c
53
โครงสร้ำงโครงสร้ำง ifif……elseelse
if (if (conditioncondition))
{{
statementstatementt1t1;;
statementstatementt2t2;;
}}
elseelse
{{
statementstatementf1f1;;
statementstatementf2f2;;
}}
if (if (conditioncondition))
{{
statementstatementt1t1;;
statementstatementt2t2;;
}}
elseelse
{{
statementstatementf1f1;;
statementstatementf2f2;;
}}
C SyntaxC SyntaxFlowchartFlowchart
STARTSTARTSTARTSTART
ENDENDENDEND
StatementStatementf1f1
StatementStatementf1f1
conditionconditionconditioncondition
true false
StatementStatementt1t1
StatementStatementt1t1
StatementStatementf2f2
StatementStatementf2f2StatementStatementt2t2
StatementStatementt2t2
54
ตัวอย่ำงตัวอย่ำง ifif……elseelse
#include <stdio.h>
int main()
{
int i=101,j=102;
if(i>j)
printf("I> J");
else
printf("I<= J");
getch();
return 0;
}
#include <stdio.h>
int main()
{
int i=101,j=102;
if(i>j)
printf("I> J");
else
printf("I<= J");
getch();
return 0;
}
I<= J
ifelse1.cifelse1.c
55
โครงสร้ำงโครงสร้ำง ifif แบบแบบ
หลำยเงื่อนไขหลำยเงื่อนไขif (x==1)
Action1;
else if (x==2)
Action2;
else if (x==3)
Action3;
else if (x==4)
Action4;
else
Default_Action;
if (x==1)
Action1;
else if (x==2)
Action2;
else if (x==3)
Action3;
else if (x==4)
Action4;
else
Default_Action;
false
Action1;Action1;x==1x==1
Action2;Action2;x==2x==2
Action3;Action3;x==3x==3
Action4;Action4;x==4x==4
true
true
true
true
false
false
false
Default_Action;Default_Action;
56
ตัวอย่ำงตัวอย่ำง ifif แบบหลำยแบบหลำย
เงื่อนไขเงื่อนไข#include <stdio.h>
int main()
{
int i=7;
if(i>7) printf("> 7");
else if(i>6) printf("> 6");
else if(i>5) printf("i> 5");
else printf("1 , 2 , 3");
getch();
return 0;
}
#include <stdio.h>
int main()
{
int i=7;
if(i>7) printf("> 7");
else if(i>6) printf("> 6");
else if(i>5) printf("i> 5");
else printf("1 , 2 , 3");
getch();
return 0;
}
> 6
ifelse2.cifelse2.c
57
โครงสร้ำงโครงสร้ำง switch-caseswitch-case
switch (x)
{
case 1: Action1;
break;
case 2: Action2;
break;
case 3: Action3;
break;
case 4: Action4;
break;
default: Default_Action;
break;
}
switch (x)
{
case 1: Action1;
break;
case 2: Action2;
break;
case 3: Action3;
break;
case 4: Action4;
break;
default: Default_Action;
break;
}
false
Action1;Action1;x==1x==1
Action2;Action2;x==2x==2
Action3;Action3;x==3x==3
Action4;Action4;x==4x==4
true
true
true
true
false
false
false
Default_Action;Default_Action;
58
ตัวอย่ำงตัวอย่ำง switch-caseswitch-case
#include <stdio.h>
int main()
{
int i=2;
switch(i)
{
case 2 : printf("2");
break;
case 1 : printf("1");
break;
default : printf("NO MATCH");
break;
}
getch();
return 0;
#include <stdio.h>
int main()
{
int i=2;
switch(i)
{
case 2 : printf("2");
break;
case 1 : printf("1");
break;
default : printf("NO MATCH");
break;
}
getch();
return 0;
}
2
switch1.cswitch1.c
ทำำใบงำนที่ 3.1-3.5
กำรกำำหนดค่ำตัวนับ
i++ = i
= i+1i-- = i = i-1
i+=5 = i = i+5
i-=5 = i = i-5
ตัวอย่
ำง
61
โครงสร้างโครงสร้าง whilewhile ลูปลูป
• วนทำาคำาสั่ง stmt1 ถึง
stmtN
ตราบเท่าที่ condition
เป็นจริง
conditionconditionconditioncondition
ENDENDENDEND
STARTSTARTSTARTSTART
true
StatementStatementStatementStatement
StatementStatementStatementStatement
false
while (condition)
{
stmt1;
stmt2;
:
stmtN;
}
while (condition)
{
stmt1;
stmt2;
:
stmtN;
}
62
ลูปวนนับลูปวนนับ ((CountingCounting
Loop)Loop)
• หากพิจารณาโครงสร้างของลูปที่ใช้
ในโปรแกรมส่วนใหญ่ มักจะเป็นลูป
แบบวนนับ
• ลูปวนนับจะมีส่วนประกอบดังตัวอย่าง
ต่อไปนี้เสมอint i, sum = 0;
i = 1;
while (i <= 10)
{
sum = sum + i;
i = i + 1;
}
printf("Sum = %dn", sum);
int i, sum = 0;
i = 1;
while (i <= 10)
{
sum = sum + i;
i = i + 1;
}
printf("Sum = %dn", sum);
ตัวแปรที่ใช้
นับ
ส่วน
กำาหนดค่า
เริ่มต้น
การปรับ
ค่าตัวนับ
เงื่อนไข
ของตัวนับ
คำาสั่งที่ถูก
ทำาซ้ำ้า
63
ตัวอย่างโครงสร้างตัวอย่างโครงสร้าง
whilewhile ลูปลูป#include <stdio.h>
int main()
{
int i=1;
while(i<=10)
{
printf("Hello %dn",i);
i++;
}
getch();
return 0;
}
#include <stdio.h>
int main()
{
int i=1;
while(i<=10)
{
printf("Hello %dn",i);
i++;
}
getch();
return 0;
}
Hello 1
Hello 2
Hello 3
Hello 4
Hello 5
Hello 6
Hello 7
Hello 8
Hello 9
Hello 10
while1.cwhile1.c
64
โครงสร้างโครงสร้าง whilewhile
ลูปลูป(INFINITY LOOP)(INFINITY LOOP)#include <stdio.h>
int main()
{
int i=1;
while(1)
{
printf("Hello %dn",i);
if(i==10)
break;
i++;
}
getch();
return 0;
#include <stdio.h>
int main()
{
int i=1;
while(1)
{
printf("Hello %dn",i);
if(i==10)
break;
i++;
}
getch();
return 0;
}
Hello 1
Hello 2
Hello 3
Hello 4
Hello 5
Hello 6
Hello 7
Hello 8
Hello 9
Hello 10
while2.cwhile2.c
65
โครงสร้างโครงสร้าง dodo……whilewhile
ลูปลูป
• ทำาคำาสั่ง stmt1...stmtN
และวนทำาซ้ำ้าอีกตราบ
เท่าที่ condition ยังคง
เป็นจริง
• นั่นคือ stmt1...stmtN จะ
ถูกกระทำาอย่างน้อยห
ENDENDENDEND
conditionconditionconditioncondition
false
STARTSTARTSTARTSTART
StatementStatement11
StatementStatement11
StatementStatementNN
StatementStatementNN
true
do
{
stmt1;
stmt2;
:
stmtN;
} while (condition);
do
{
stmt1;
stmt2;
:
stmtN;
} while (condition);
66
ตัวอย่างตัวอย่าง dodo……whilewhile ลูปลูป
#include <stdio.h>
int main()
{
int i=1;
do
{
printf("Hello %dn",i);
i++;
}
while(i<=10);
getch();
return 0;
}
#include <stdio.h>
int main()
{
int i=1;
do
{
printf("Hello %dn",i);
i++;
}
while(i<=10);
getch();
return 0;
}
Hello 1
Hello 2
Hello 3
Hello 4
Hello 5
Hello 6
Hello 7
Hello 8
Hello 9
Hello 10
dowhile1.cdowhile1.c
67
โครงสร้างโครงสร้าง forfor ลูปลูป
• เป็นโครงสร้างที่ให้ความสะดวกในการเขียน
ลูปวนนับ
• การทำางาน
1. ทำาคำาสั่ง init_stmt หนึ่งครั้ง
2. ถ้า condition เป็นจริง ทำาคำาสั่ง
statement1...statementN
for (init_stmt; condition; update_stmt)
{
statement1;
statement2;
:
statementN;
}
for (init_stmt; condition; update_stmt)
{
statement1;
statement2;
:
statementN;
}
68
การทำางานของการทำางานของ forfor ลูปลูป
STARTSTARTSTARTSTART
ENDENDENDEND
false
conditionconditionconditioncondition
true
Statement1Statement1Statement1Statement1
StatementNStatementNStatementNStatementN
init_stmtinit_stmtinit_stmtinit_stmt
update_stmtupdate_stmtupdate_stmtupdate_stmt
for (init_stmt; condition; update_stmt)
{
statement1;
statement2;
:
statementN;
}
for (init_stmt; condition; update_stmt)
{
statement1;
statement2;
:
statementN;
}
69
ตัวอย่างตัวอย่าง forfor ลูปลูป
#include <stdio.h>
int main()
{
int i;
for(i=1;i<=10;i++)
{
printf("Hello %d",i);
printf("n");
}
getch();
return 0;
}
#include <stdio.h>
int main()
{
int i;
for(i=1;i<=10;i++)
{
printf("Hello %d",i);
printf("n");
}
getch();
return 0;
}
Hello 1
Hello 2
Hello 3
Hello 4
Hello 5
Hello 6
Hello 7
Hello 8
Hello 9
Hello 10
for1.cfor1.c
70
โปรแกรมย่อยโปรแกรมย่อย
(Subroutine)(Subroutine)• ในภาษาซ้ีเรียกว่า "ฟังก์ชัน" (Function)
• เป็นส่วนของโปรแกรมที่มีหน้าที่การ
ทำางานชัดเจนในตัวเอง ซ้ึ่งถูกเรียกใช้ใน
โปรแกรมหลักอีกทีหนึ่ง
• การเขียนโปรแกรมโดยแยกเป็นฟังก์ชันมี
ข้อดีหลายประการ

ช่วยแบ่งงานที่ซ้ับซ้้อนเป็นงานย่อยหลายงาน

ลดการเขียนโค้ดที่ซ้ำ้าซ้้อน

ซ้่อนรายละเอียดไว้ในส่วนอื่น ทำาให้
โปรแกรมเข้าใจได้ง่ายขึ้น

ฟังก์ชันที่เขียนขึ้นมาสามารถนำาไปใช้ใน
71
ชนิดของฟังก์ชันชนิดของฟังก์ชัน
• ฟังก์ชันมาตรฐานฟังก์ชันมาตรฐาน (Standard(Standard
Functions)Functions)

เป็นฟังก์ชันที่อยู่ในชุดไลบรารีของ
ภาษาซ้ี เรียกใช้ได้ทันที

เช่น printf(), scanf(), ...
• ฟังก์ชันที่ผู้ใช้กำาหนดฟังก์ชันที่ผู้ใช้กำาหนด (User Defined(User Defined
Functions)Functions)

เป็นฟังก์ชันที่ผู้ใช้สร้างขึ้นเอง

เรียกใช้ใน main() หรือจากฟังก์ชันอื่นๆ
ได้เหมือนฟังก์ชันมาตรฐาน
72
ตัวอย่างฟังก์ชันตัวอย่างฟังก์ชัน
มาตรฐานมาตรฐาน
กลุ่มฟังก์ชัน เฮดเดอร์ไฟล์ ตัวอย่าง
ฟังก์ชัน
จัดการอินพุท/เอา
ท์พุท
stdio.h scanf, printf,
gets, puts
คณิตศาสตร์ math.h sin, cos, exp, pow
แยกประเภทข้อมูล
อักขระ
ctype.h isalpha, isdigit,
islower, isupper
จัดการกับสตริง string.h strlen, strcpy,
strcmp
อื่น ๆ stdlib.h rand, atoi, atof
73
ฟังก์ชันที่ผู้ใช้กำาหนดฟังก์ชันที่ผู้ใช้กำาหนด
เองเอง
• แบบไม่ส่งค่ากลับ

ระบุชนิดข้อมูล
void

ไม่ต้องมีคำาสั่ง
return
• แบบส่งค่ากลับ

ระบุชนิดข้อมูลที่
ต้องการ

ใช้คำาสั่ง return ส่ง
void say_hi(char *name)
{
printf("Hi, %sn", name);
}
void say_hi(char *name)
{
printf("Hi, %sn", name);
}
int max(int a, int b)
{
if (a > b)
return a;
else
return b;
}
int max(int a, int b)
{
if (a > b)
return a;
else
return b;
}
74
การไหลของโปรแกรมการไหลของโปรแกรม
เมื่อใช้ฟังก์ชันเมื่อใช้ฟังก์ชัน
#include <stdio.h>
int incr(int i)
{
int j;
j = i + 1;
return j;
}
int main()
{
int k, m = 4;
k = incr(m);
printf ("k = %d, m = %dn", k, m);
getch();
return 0;
}
#include <stdio.h>
int incr(int i)
{
int j;
j = i + 1;
return j;
}
int main()
{
int k, m = 4;
k = incr(m);
printf ("k = %d, m = %dn", k, m);
getch();
return 0;
}
function1.cfunction1.c
75
โปรแกรมแสดงข้อความโปรแกรมแสดงข้อความ
(Function)(Function) แบบไม่ส่งค่า
กลับ
#include <stdio.h>
void print1()
{
printf(“Hello”);
printf(“n”);
}
int main()
{
print1();
print1();
getch();
return 0;
}
#include <stdio.h>
void print1()
{
printf(“Hello”);
printf(“n”);
}
int main()
{
print1();
print1();
getch();
return 0;
}
function2.cfunction2.c
76
โปรแกรมคำานวณภาษีโปรแกรมคำานวณภาษี
(Function)(Function)
แบบส่งค่ากลับ#include <stdio.h>
float cal_tax(float i)
{
float ctax;
ctax = i*0.07;
return ctax;
}
int main()
{
float money=7290,ff;
ff = cal_tax(money);
printf(“%f”,ff);
getch();
return 0;
}
#include <stdio.h>
float cal_tax(float i)
{
float ctax;
ctax = i*0.07;
return ctax;
}
int main()
{
float money=7290,ff;
ff = cal_tax(money);
printf(“%f”,ff);
getch();
return 0;
}
function3.cfunction3.c
77
ขั้นตอนวิธีกับการขั้นตอนวิธีกับการ
โปรแกรมโปรแกรม
• การออกแบบขั้นตอนวิธีเป็นส่วนสำาคัญ
ในการพัฒนาโปรแกรม
• การ "เขียนโปรแกรม" เป็นเพียงการ
แปลงขั้นตอนวิธีให้อยู่ในรูปที่ยอมรับ
ได้โดยตัวภาษา
78
ตัวอย่างตัวอย่าง
• เขียนโปรแกรมเพื่อรับตัวเลขระบุ
ขนาด และพิมพ์รูปสามเหลี่ยมที่มี
ขนาดตามที่กำาหนดEnter N: 3
*
**
***
Enter N: 3
*
**
***
Enter N: 5
*
**
***
****
*****
Enter N: 5
*
**
***
****
*****
79
ข้อผิดพลาดที่พบบ่อยข้อผิดพลาดที่พบบ่อย
#include <stdio.h>
int main ()
{
int i;
scanf("%d", i);
if (i = 0)
puts("false");
else
puts("true");
return 0;
}
#include <stdio.h>
int main ()
{
int i;
scanf("%d", i);
if (i = 0)
puts("false");
else
puts("true");
return 0;
}
ส่งค่า i ให้ scanf
แทนที่จะส่ง
ตำาแหน่ง
ใช้คำาสั่งกำาหนดค่า
(=) แทนการ
เปรียบเทียบ (==)
THANK YOU
QUESTION ?

More Related Content

What's hot

59170284 สาวิกา
59170284 สาวิกา59170284 สาวิกา
59170284 สาวิกาBeam Suna
 
Java Programming: โครงสร้างควบคุม
Java Programming: โครงสร้างควบคุมJava Programming: โครงสร้างควบคุม
Java Programming: โครงสร้างควบคุมThanachart Numnonda
 
Java Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่นJava Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่นThanachart Numnonda
 
หน่วยที่ 4 การสร้างแบบสอบถาม
หน่วยที่ 4 การสร้างแบบสอบถามหน่วยที่ 4 การสร้างแบบสอบถาม
หน่วยที่ 4 การสร้างแบบสอบถามkruthanyaporn
 
Java Programming [4/12] : Object Oriented Concept
Java Programming [4/12] : Object Oriented ConceptJava Programming [4/12] : Object Oriented Concept
Java Programming [4/12] : Object Oriented ConceptIMC Institute
 
(Big One) C Language - 02 ฟังก์ชันส่งผ่านสตริง
(Big One) C Language - 02 ฟังก์ชันส่งผ่านสตริง(Big One) C Language - 02 ฟังก์ชันส่งผ่านสตริง
(Big One) C Language - 02 ฟังก์ชันส่งผ่านสตริงKittinan Noimanee
 
Java Programming: การสร้างส่วนต่อประสานกราฟิกกับผู้ใช้ (Java GUI)
Java Programming: การสร้างส่วนต่อประสานกราฟิกกับผู้ใช้ (Java GUI)Java Programming: การสร้างส่วนต่อประสานกราฟิกกับผู้ใช้ (Java GUI)
Java Programming: การสร้างส่วนต่อประสานกราฟิกกับผู้ใช้ (Java GUI)Thanachart Numnonda
 

What's hot (18)

59170284 สาวิกา
59170284 สาวิกา59170284 สาวิกา
59170284 สาวิกา
 
Tec4
Tec4Tec4
Tec4
 
Java Programming: โครงสร้างควบคุม
Java Programming: โครงสร้างควบคุมJava Programming: โครงสร้างควบคุม
Java Programming: โครงสร้างควบคุม
 
Java Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่นJava Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่น
 
C slide
C slideC slide
C slide
 
หน่วยที่ 4 การสร้างแบบสอบถาม
หน่วยที่ 4 การสร้างแบบสอบถามหน่วยที่ 4 การสร้างแบบสอบถาม
หน่วยที่ 4 การสร้างแบบสอบถาม
 
Java Programming [4/12] : Object Oriented Concept
Java Programming [4/12] : Object Oriented ConceptJava Programming [4/12] : Object Oriented Concept
Java Programming [4/12] : Object Oriented Concept
 
ภาษาซีเบื้องต้น
ภาษาซีเบื้องต้นภาษาซีเบื้องต้น
ภาษาซีเบื้องต้น
 
Chapter 05 Loop
Chapter 05 LoopChapter 05 Loop
Chapter 05 Loop
 
Week4
Week4Week4
Week4
 
Intro c
Intro cIntro c
Intro c
 
Midterm
MidtermMidterm
Midterm
 
4 4
4 44 4
4 4
 
(Big One) C Language - 02 ฟังก์ชันส่งผ่านสตริง
(Big One) C Language - 02 ฟังก์ชันส่งผ่านสตริง(Big One) C Language - 02 ฟังก์ชันส่งผ่านสตริง
(Big One) C Language - 02 ฟังก์ชันส่งผ่านสตริง
 
Array 2
Array 2Array 2
Array 2
 
Java Programming: การสร้างส่วนต่อประสานกราฟิกกับผู้ใช้ (Java GUI)
Java Programming: การสร้างส่วนต่อประสานกราฟิกกับผู้ใช้ (Java GUI)Java Programming: การสร้างส่วนต่อประสานกราฟิกกับผู้ใช้ (Java GUI)
Java Programming: การสร้างส่วนต่อประสานกราฟิกกับผู้ใช้ (Java GUI)
 
แนวคิดในการเขียนโปรแกรม
แนวคิดในการเขียนโปรแกรมแนวคิดในการเขียนโปรแกรม
แนวคิดในการเขียนโปรแกรม
 
Java AWT
Java AWTJava AWT
Java AWT
 

Similar to C language

การเขียนคำสั่งควบคุมขั้นพื้นฐาน
การเขียนคำสั่งควบคุมขั้นพื้นฐานการเขียนคำสั่งควบคุมขั้นพื้นฐาน
การเขียนคำสั่งควบคุมขั้นพื้นฐานNookky Anapat
 
Computer programming
Computer programmingComputer programming
Computer programmingJariyaa
 
1. ประวัติภาษาซี
1. ประวัติภาษาซี1. ประวัติภาษาซี
1. ประวัติภาษาซีmansuang1978
 
งานนำเสนอ1
งานนำเสนอ1งานนำเสนอ1
งานนำเสนอ1Thank Chiro
 
C Programming
C ProgrammingC Programming
C ProgrammingWarawut
 
2. โครงสร้างภาษาซี
2. โครงสร้างภาษาซี2. โครงสร้างภาษาซี
2. โครงสร้างภาษาซีmansuang1978
 
Multimedia of introducation to programming c++
Multimedia of introducation to programming c++Multimedia of introducation to programming c++
Multimedia of introducation to programming c++จู ลิ
 
โปรแกรม ภาษาซี
โปรแกรม ภาษาซีโปรแกรม ภาษาซี
โปรแกรม ภาษาซีfinverok
 

Similar to C language (20)

การเขียนคำสั่งควบคุมขั้นพื้นฐาน
การเขียนคำสั่งควบคุมขั้นพื้นฐานการเขียนคำสั่งควบคุมขั้นพื้นฐาน
การเขียนคำสั่งควบคุมขั้นพื้นฐาน
 
Computer programming
Computer programmingComputer programming
Computer programming
 
Introduction toc
Introduction tocIntroduction toc
Introduction toc
 
1. ประวัติภาษาซี
1. ประวัติภาษาซี1. ประวัติภาษาซี
1. ประวัติภาษาซี
 
งานนำเสนอ1
งานนำเสนอ1งานนำเสนอ1
งานนำเสนอ1
 
Chapter 02 Flowchart
Chapter 02 FlowchartChapter 02 Flowchart
Chapter 02 Flowchart
 
Chapter 05 Loop
Chapter 05 LoopChapter 05 Loop
Chapter 05 Loop
 
12
1212
12
 
C Programming
C ProgrammingC Programming
C Programming
 
โครงสร้างการเขียนโปรแกรมภาษาไพธอน
โครงสร้างการเขียนโปรแกรมภาษาไพธอนโครงสร้างการเขียนโปรแกรมภาษาไพธอน
โครงสร้างการเขียนโปรแกรมภาษาไพธอน
 
2. โครงสร้างภาษาซี
2. โครงสร้างภาษาซี2. โครงสร้างภาษาซี
2. โครงสร้างภาษาซี
 
Python101
Python101Python101
Python101
 
3.8 การทำงานแบบลำดับ
3.8 การทำงานแบบลำดับ3.8 การทำงานแบบลำดับ
3.8 การทำงานแบบลำดับ
 
3.8 การทำงานตามลำดับ
3.8 การทำงานตามลำดับ3.8 การทำงานตามลำดับ
3.8 การทำงานตามลำดับ
 
3.8 การทำงานแบบลำดับ
3.8 การทำงานแบบลำดับ3.8 การทำงานแบบลำดับ
3.8 การทำงานแบบลำดับ
 
Multimedia of introducation to programming c++
Multimedia of introducation to programming c++Multimedia of introducation to programming c++
Multimedia of introducation to programming c++
 
โปรแกรม ภาษาซี
โปรแกรม ภาษาซีโปรแกรม ภาษาซี
โปรแกรม ภาษาซี
 
Chapter1
Chapter1Chapter1
Chapter1
 
3.8 การทำงานตามลำดับ
3.8 การทำงานตามลำดับ3.8 การทำงานตามลำดับ
3.8 การทำงานตามลำดับ
 
207
207207
207
 

C language