ตัวแปร ประเภทตัวแปร ตัวด...
TRANSCRIPT
อ. เอก ตงคนานนท อ. เอก ตงคนานนท
2 November 2010
เอกสารประกอบการสอนวชา CP111
มหาวทยาลยศรนครนทรวโรฒ
ตวแปร ประเภทตวแปร ตวด าเนนการ Variables and Operators
The content in this slide is adapted from Introduction to Computing Systems 2nd edition. Yale N. Patt, Sanjay J. Patel and its supplementary slides.
อ. เอก ตงคนานนท
เนอหา
องคประกอบพนฐานของภาษาซ
ตวแปร
ตวด าเนนการ
โครงโปรแกรมภาษาซ (ส าหรบทบทวนความเขาใจ)
Agenda
2
อ. เอก ตงคนานนท อ. เอก ตงคนานนท 3
องคประกอบพนฐานของภาษาซ Basic C Elements
อ. เอก ตงคนานนท
องคประกอบพนฐานของภาษาซ
ตวแปร (Variables)
เปนชอท ใชในการอางองถงขอมลท ถกก าหนดประเภท
มลกษณะเชนเดยวกนกบตวแปรใดๆ ในคณตศาสตร แตม
ประเภทของตวแปรก ากบอยดวย
ตวด าเนนการ (Operators)
เปนการด าเนนการทก าหนดไวแลวท ใชกระท ากบขอมล
มลกษณะเชนเดยวกบการบวก ลบ คณ หาร ในคณตศาสตร
4
Basic C Elements
อ. เอก ตงคนานนท อ. เอก ตงคนานนท 5
ตวแปร Variables
อ. เอก ตงคนานนท
ประเภทขอมลพนฐาน
int
จ านวนเตม (integer)
double
จ านวนทศนยม (floating point)
char
ตวอกษร (character)
ขนาดของตวแปรจะขนอยกบคอมไพเลอรและสถาปตยกรรมของ
คอมพวเตอร
6
C Data Type
Citation: http://msdn.microsoft.com/en-us/library/a86zba5c%28VS.80%29.aspx
อ. เอก ตงคนานนท
ประเภทขอมล
7
Data Type
Citation: http://msdn.microsoft.com/en-us/library/a86zba5c%28VS.80%29.aspx
Integer
short 2 bytes
int 4 bytes
long 4 bytes
Floating point
float 4 bytes
double 8 bytes
Character
char 1 byte
Others
void
Optional keywords
signed
unsigned
Example
unsigned short
unsigned int
signed long int
อ. เอก ตงคนานนท
ชอตวแปร (1/2)
การตงชอตวแปรในภาษาซจะตองสอดคลองกบ context free
grammar ดงน
identifier: nondigit | identifier nondigit | identifier digit
nondigit: one of
_ a b c d e f g h i j k l m n o p q r s t u v w x y z
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
digit: one of
0 1 2 3 4 5 6 7 8 9
สรปส นๆ คอประกอบดวยตวอกษร a – z, A – Z, 0 – 9 และ
สญลกษณ _ โดยหามขนตนดวยตวเลข 0 – 9
ตวเลกตวใหญมความแตกตางกน (case sensitive)
8
Variable Names, Identifiers
Citation: http://msdn.microsoft.com/en-us/library/e7f8y25b%28VS.80%29.aspx
อ. เอก ตงคนานนท
ชอตวแปร (2/2)
การตงชอท ถกตอง
ticksPerSecond
_ticksPerSecond
x1
ตวอยางการตงชอท ไมถกตอง
123num
sur-name
finished?
9
Variable Names, Identifiers
Citation: http://msdn.microsoft.com/en-us/library/e7f8y25b%28VS.80%29.aspx
ตวพมพเลกและใหญแตกตางกน นนคอตว
แปรตอไปนหมายถงคนละตวกน
surname
SurName
surName
Surname
อ. เอก ตงคนานนท
การประกาศตวแปร
สามารถท าไดโดยการเขยน statement ตอไปน
data-type variable-name [, variable-name, …];
ตวอยาง
int numStudents;
double x, y;
int a = 2;
10
Variable Declaration
numStudents
x
y
2 a
อ. เอก ตงคนานนท
C Literals
Integer
12345
-12345
0x2A1F
Floating point
1.02
9.18e20
Character
'r'
'\n‘
'\xA'
11
อ. เอก ตงคนานนท อ. เอก ตงคนานนท 12
ตวด าเนนการ Operators
อ. เอก ตงคนานนท
ตวด าเนนการ
สวนหนงเปนตวด าเนนการทวๆ ไปท รจกกนดในคณตศาสตร
เชนการบวก ลบ คณ หาร
ใชในการจดการตวแปร เชน ก าหนดคา เพมคา เปนตน
13
Operators
x = a + 3;
Expression
Statement
Literal Identifier Operator
อ. เอก ตงคนานนท
Expression
เกดจากการประกอบกนของตวแปร (variables), คาคงท
(constants), ตวด าเนนการ (operators), และฟงกชน (function
calls)
มผลลพธของการด าเนนการเปนขอมลประเภทใดประเภทหนง
ตวอยาง
y + 2
1 – sqrt(10)
count > LIMIT
14
อ. เอก ตงคนานนท
Statement
ใชแสดงการท างานชนหนง
ตวอยาง
a = y + 2;
b = 1 – sqrt(10);
c = 1;
;
15
อ. เอก ตงคนานนท
ประเภทของตวด าเนนการ
Assignment Operators
Arithmetic Operators (Additive, Multiplicative)
Relational and Equality Operators
Logical Operators
Bitwise Operators
Cast Operators
16
Classification of Operators
ถาดตามจ านวนของตวถกกระท า อาจแบงเปน
o Unary operator
o Binary operator
ถาดตามต าแหนงของตวด าเนนการ อาจแบงเปน
o Infix operator
o Prefix operator
o Postfix operator
อ. เอก ตงคนานนท
ตวด าเนนการก าหนดคา
ใชในการก าหนดคาหรอเปลยนแปลงคาของตวแปร
17
Assignment Operators
a = 5;
x = a + 3;
1. ท ำกำรค ำนวนแลวใหผลลพออกมำ
2. น ำคำท ไดจำกกำรค ำนวนใน 1 มำใส x
อ. เอก ตงคนานนท
ตวด าเนนการทางคณตศาสตร (1/2)
การบวก
+
การลบ
-
การคณ
*
การหาร
/
การมอดดโล
%
18
Arithmetic Operators
ตวอยาง
5 + 3
5 * 3
5 % 3
3 + 4 + 5
5 * 2 + 3
• ท าจากซายไปขวา (Left to right
association)
• ท า * / กอน + - % (Precedence)
Citation: http://msdn.microsoft.com/en-us/library/y0c1cfax%28VS.80%29.aspx
อ. เอก ตงคนานนท
ตวด าเนนการทางคณตศาสตร (2/2)
ผลลพธจากการกระท าระหวาง integer กบ floating point
จะไดเปนขอมลประเภพ floating point
a = 1 + 1.5;
เศษท เหลอจากการหารจ านวนเตมจะถกตดทงไป
a = 9 / 4;
ตวด าเนนการ % หมายถงเศษท เหลอจากการหาร
a = 9 % 4;
19
Arithmetic Operators
Citation: http://msdn.microsoft.com/en-us/library/y0c1cfax%28VS.80%29.aspx
อ. เอก ตงคนานนท
ตวด าเนนการเปรยบเทยบ (1/2)
มากกวา
>
มากกวาหรอเทากบ
>=
นอยกวา
<
นอยกวาหรอเทากบ
<=
เทากบ
==
ไมเทากบ
!=
20
Relational Operators
ตวอยาง
5 > 3
5 < 3
5 == 3
5 != 3
Citation: http://msdn.microsoft.com/en-us/library/y0c1cfax%28VS.80%29.aspx
อ. เอก ตงคนานนท
ตวด าเนนการเปรยบเทยบ (2/2)
ผลลพธจากการกระท าเปนตวแปรประเภท int โดยม
ความหมายดงน
1 TRUE
0 FALSE
21
Relational Operators
Citation: http://msdn.microsoft.com/en-us/library/y0c1cfax%28VS.80%29.aspx
อ. เอก ตงคนานนท
ตวด าเนนการตรรกกะ (1/2)
นเสธ (NOT)
!
และ (AND)
&&
หรอ (OR)
||
22
Logical Operators
ตวอยาง
!TRUE
TRUE && FALSE
TRUE || FALSE
!!!TRUE
!TRUE && FALSE || TRUE
• NOT ท าจากขวาไปซาย
AND, OR ท าจากซายไปขวา
• ท า NOT กอน AND และท า AND
กอน OR
อ. เอก ตงคนานนท
ตวด าเนนการตรรกกะ (2/2)
ภาษาซไมมตวแปรประเภทตรรกกะ (Boolean data type)
ภาษาซจะมการตความหมายตรรกกะจากตวแปรประเภท
จ านวนเตมดงน
FALSE ส าหรบจ านวนเตมศนย
TRUE ส าหรบจ านวนเตมท ไมใชศนย
23
Logical Operators
1 -2 -1.5 -1.5e10
' a' 0
อ. เอก ตงคนานนท
ตวด าเนนการ
24
Operators
a = !(5 > 3) && 1 + 2 != 3
อ. เอก ตงคนานนท
C++ Operator Precedence
Symbol Type of Operation Associativity
[ ] ( ) . –> postfix ++ and postfix –– Expression Left to right
prefix ++ and prefix –– sizeof & * + – ~ ! Unary Right to left
typecasts Unary Right to left
* / % Multiplicative Left to right
+ – Additive Left to right
<< >> Bitwise shift Left to right
< > <= >= Relational Left to right
== != Equality Left to right
& Bitwise-AND Left to right
^ Bitwise-exclusive-OR Left to right
| Bitwise-inclusive-OR Left to right
&& Logical-AND Left to right
|| Logical-OR Left to right
? : Conditional-expression Right to left
= *= /= %= += –= <<= >>=&= ^= |=
Simple and compound assignment2 Right to left
, Sequential evaluation Left to right
25 Citation: http://msdn.microsoft.com/en-us/library/2bxt6kc4%28VS.80%29.aspx
อ. เอก ตงคนานนท อ. เอก ตงคนานนท 26
โครงโปรแกรมภาษาซ (ส าหรบทบทวนความเขาใจ) C Source Code Structure (for Practical Lecture Revision)
อ. เอก ตงคนานนท
ประกาศตวแปร
(var. declaration)
โครงโปรแกรมภาษาซ (1/3)
27
C Source Code Structure
#include<stdio.h> int main() { int a; a = 5; printf("a = %d\n", a); }
ex02_01.c
ค าส ง
(statements)
อ. เอก ตงคนานนท
โครงโปรแกรมภาษาซ (2/3)
28
C Source Code Structure
#include<stdio.h> int main() { int a; int b; float f; double x, y; char c1, c2, c3; a = 5; printf("a = %d\n", a); b = a > 1; printf("b = %d\n", b); f = a + 1.2; printf("f = %f\n", f); x = y = f; printf("(x, y) = (%f, %f)\n", x, y); c1 = c2 = c3 = 'a'; printf("c1 c2 c3 = %c %c %c\n", c1, c2, c3); return 0; }
ex02_02.c
อ. เอก ตงคนานนท
โครงโปรแกรมภาษาซ (3/3)
29
C Source Code Structure
#include<stdio.h> #define TRUE 1 #define FALSE 0 int main() { printf("TRUE = %d\n", TRUE); printf("FALSE = %d\n", FALSE); printf("test1 = %d\n", TRUE && FALSE); printf("test2 = %d\n", TRUE || FALSE); printf("test3 = %d\n", !TRUE); printf("test4 = %d\n", (1 > 2) != TRUE); return 0; }
ex02_03.c ประกาศมาโคร
(macro declaration)
อ. เอก ตงคนานนท
การบาน (1/2)
ใหท าความเขาใจกบ operator ตอไปน
Assignment =
Arithmetic +, -, *, /, %
Logical &&, ||, !
Relational <, <=, >, >=, ==, !=
Postfix Operator ++, --
ใหท าความเขาใจกบค าส ง printf ในขนพนฐาน
30
Homework
อ. เอก ตงคนานนท
การบาน (2/2)
ส าหรบ operator นสตสามารถศกษาไดจากเวปไซตดงตอไปน
http://www.cplusplus.com/doc/tutorial/operators
http://msdn.microsoft.com/en-us/library/y0c1cfax(VS.80).aspx
นสตควรใชโครงโปรแกรมกอนหนานในการท าความเขาใจดวย
การปฏบตจรง ส าหรบฟงกชน printf สามารถศกษาไดจาก
http://www.cplusplus.com/reference/clibrary/cstdio/printf
http://msdn.microsoft.com/en-us/library/wc7014hz.aspx
31
Homework
อ. เอก ตงคนานนท อ. เอก ตงคนานนท 32
ค าถามทายบท
จะขนสไลดทายชวโมงท ท าการสอน
อ. เอก ตงคนานนท อ. เอก ตงคนานนท 33
ส าหรบสปดาหตอไป
จะใหเวลา 30 นาททายชวโมงในการถามตอบ
ถาไมมคนถามค าถามภายในเวลา 1 นาท จะท าการสอบ
อ. เอก ตงคนานนท อ. เอก ตงคนานนท
2 November 2010
บทท 3 การรบและการแสดงขอมล
การรบขอมลทางแปนพมพและการแสดงผลขอมลออกทางจอคอมพวเตอร
จบบทท 2 The End of Chapter 2