11. 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
13. คำาสงวนในภาษา 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
17. นิพจน์ (Expression)• นิพจน์ หมายถึง การนำาตัวแปร ค่า
คงที่ มาสัมพันธ์กันโดยใช้เครื่องหมาย
อย่างหนึ่งอย่างใดเป็นตัวเชื่อม เช่น
นิพจน์คณิตศาสตร์ นิพจน์
ภาษา Ca + b
cd
(a+b)/ (c*d)
10x + 3xy + 10y10 * x + 3*x*y + 10*y
18. กฎเกณฑ์ในการเขียน
นิพจน์•1. ห้ามเขียนตัวแปร 2 ตัวติดกัน
โดยไม่มีเครื่องหมาย
เช่น ab ในภาษา C ต้องเขียน
a*b จะเขียนเป็น ab ไม่ได้เพราะ
จะถือเป็นชื่อตัวแปรตัวเดียวชื่อ ab
ไม่ใช่ค่า a คูณ b
•2. ถ้าเขียนนิพจน์โดยมีค่าของ
ตัวแปรหรือค่าคงที่ต่างชนิดกันใน
นิพจน์เดียวกัน ภาษา C จะเปลี่ยน
28. แบบฝึกหัดท้ายบท
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
34. 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. 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. 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.
ผลลัพธ์:
42. 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
43. 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
44. 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
45. 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
62. 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);
ตัวแปรที่ใช้
นับ
ส่วน
กำาหนดค่า
เริ่มต้น
การปรับ
ค่าตัวนับ
เงื่อนไข
ของตัวนับ
คำาสั่งที่ถูก
ทำาซ้ำ้า
79. 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
แทนที่จะส่ง
ตำาแหน่ง
ใช้คำาสั่งกำาหนดค่า
(=) แทนการ
เปรียบเทียบ (==)