Transcript C language

องค์ ประกอบภาษา C
Elements of C Language
1
กระบวนการแปล
The compilation process
C program  group characters into tokens
 translate to target code
6 types of tokens in ANSI C
 คำหล ัก (Keywords)
 ค่ำคงที่ (Constants)
ื่ เฉพำะ (Identifiers)
 ชอ
 ต ัวดำเนินกำร (Operators)
 เครือ
่ งหมำยวรรคตอน (Punctuation)
2
องค์ประกอบภาษาซี
Elements of C language
้ ำสง่ ั
สงิ่ ทีก
่ อ
่ ให้เกิดเป็นคำสง่ ั และระเบียบกำรใชค







เซตของต ัวอ ักษร (Character set)
ค่ำคงที่ (Constants)
คำหล ัก (Keywords)
ต ัวแปร (Variables)
ต ัวดำเนินกำร (Operators)
นิพจน์ (Expression)
คำสง่ ั (Statements)
3
เซตของตัวอักขระ
Lowercase letters
a
b c d . . . x y z
Uppercase letters
A
B C D . . .X Y Z
Digits
0
1 2 3 . . .7 8 9
Other characters
+
- * / \ = ( ) [ ]
{ } < > ! ; : ? ‘ “
# , $ % ^ & _ | ~ .
White space characters
 blank, new line, carriage
return, form feed, line feed, tab, vertical tab
4
รหัสควบคุม(Escape sequences)
\a
\b
\f
\n
\r
\t
\0
\’
\”
\\
\ddd
\xxx
Alert (bell)
Backspace
Form feed
New line
Carriage return
Tab
Null character
Single quotation mark
Double quotation mark
backslash
ASCII character in octal notation
ASCII character in hexadecimal notation
5
ค่าคงที่




เลขจำนวนเต็ม (Integer constants)
เลขมีจด
ุ ทศนิยม (Floating-point constants)
อักขระ (Character constants)
สำยอักขระ (String constants)
6
ค่ าคงที่เลขจานวนเต็ม

รูปแบบ
ิ )
1. Digits (เลขฐำนสบ
2. 0octaldigits
3. 0Xhexadecimaldigits
0xhexadecimaldigits

ตัวอย่ำง
ค่ำคงทีใ่ นรูปฐำน 10
10
32719
32768U
ฐำน 8
ฐำน 16
012
0xa หรือ 0XA
077717
0x7fcf หรือ 0X7FCF
0100000U 0x8000U
7
ค่ าคงที่เลขมีจุดทศนิยม

รูปแบบ
[+|-][digits]. [digits][E|e [+|-] digits]

ตัวอย่ำง
15.75
25.
-1.28
1.25L
1.575e1
0.25e2
-.128e1
1575E-2
.0025E4
-128.E-2
8
ค่ าคงที่อักขระ

รูปแบบ
‘อ ักขระ’
‘รห ัสควบคุม’

ตัวอย่ำง
ค่ำคงทีภ
่ ำษำ C
‘a’
‘\a’
‘\\’
‘\x1b’
ค่ำอักขระ
a
ี งบี๊ บ(alert)
กำรสง่ เสย
\ (backslash)
Esc
9
ค่ าคงที่สายอักขระ

รูปแบบ
“ข้อควำม” [“ ข้อควำม ”]
ตัวอย่ำง
ค่ำคงทีภ
่ ำษำ C
“This is a string literal.”
ภำพจำลองหน่วยควำมจำ
T h i s i s a s t r i n g l i t e r a l . \0
“Hello” “world”
“a” ไม่เท่ำกับ ‘a’
H e l l o w o r l d \0
10
คาหลัก
คำทีต
่ วั แปลภำษำกำหนดหน ้ำทีแ
่ ละควำมหมำยเฉพำะ
auto
break
case
char
const
continue default
do
double
else
enum
extern
float
for
goto
if
int
long
register
return
short
signed
sizeof
static
struct
switch
typedef
union
unsigned void
volatile
while

The additional keywords in Turbo C
asm cdecl far huge interrupt near pascal

11
ตัวแปร (variables)

ต ัวแปร ประกอบด ้วย ๒ สว่ นสำคัญ


ื่
ชอ
ข้อมูล ซงึ่ เรียกว่ำเป็น ค่ำของต ัวแปร

ื่ ต ัวแปร  ตัวอักษร ตัวเลข _ (เครือ่ งหมำย underscore)
ชอ
ื่ ต ัวแปร
กฏกำรตงช
ั้ อ

ไม่ตรงหรือซำ้ ก ับคำหล ัก
ื่ ต้องเป็นต ัวอ ักษร หรือ _ เท่ำนน
 อ ักขระต ัวแรกของชอ
ั้
้ ักขระอืน
 ห้ำมใชอ
่ นอกจำก ต ัวอ ักษร ต ัวเลข และ _ เป็น
่ นหนึง่ ของชอ
ื่ ต ัวแปร
สว
ื่ ไม่จำก ัด แต่ถอ
 ควำมยำวของชอ
ื เพียง 31 ต ัวแรกเป็น
น ัยสำค ัญ
acc _ok S_date addr1 ChiangMaiUniversity


 Don’t
#account char 707james
12
ตัวแปร

กำรประกำศต ัวแปร
ื่ ตัวแปร> [=ค่ำเริม
<ชนิดข ้อมูล> <ชอ
่ ต ้น][, ตัวแปร[=ค่ำเริม
่ ต ้น]];


ชนิดข้อมูล
char
signed char
unsigned char
short
int
long
unsigned short unsigned unsigned long
float
double
long double
ต ัวอย่ำง
char
c, msg[] = “Your welcome!”;
int
i, x = 5;
unsigned u=32768;
13
ตัวดาเนินการ (Operators)





ต ัวดำเนินกำรเชงิ คำนวณ (Arithmetic operators)
ต ัวดำเนินกำรเพือ
่ กำหนดค่ำ(Assignment operators)
ต ัวดำเนินกำรเปรียบเทียบ (Relational operators)
ต ัวดำเนินกำรตรรกะ (Logical operators)
ต ัวดำเนินกำรพิเศษ (Special operators)





Increment operator ++
Decrement operator -Address operator
&
Indirection operator *
Conditional operator ? :
14
Arithmetic Operators)
ั
สญล
ักษณ์
*
/
%
+
-
ควำมหมำย
Negation
คูณ
หำร
หำรเก็บเศษ
บวก
ลบ
ต ัวอย่ำง
-x
x*2
x/2
x%2
x+2
x-2
15
ลาดับความสาคัญของตัวดาเนินการ
ตัวดำเนินกำร
ลำดับดำเนินกำร
()
ซ้ ำยไปขวำ
+ (unary) -(unary)
ขวำไปซ้ ำย
* / %
ซ้ ำยไปขวำ
+ -
ซ้ ำยไปขวำ
16
Arithmetic operators
การประกาศตัวแปรและกาหนดค่าเริ่ มต้น
int
นิพจน์
a=1,
นิพจน์เทียบเท่า
b=2,
c=3,
d=4;
ค่าของนิพจน์
a*b/c
(a * b) / c
0
a+b%c*1
a + ((b % c) * 1)
3
(8 - ((-d) / b)) + c
13
8 - -d / b + c
17
Assignment Operators

รูปแบบ



ต ัวแปร = นิพจน์
ต ัวแปร op= นิพจน์
ต ัวแปร = (ต ัวแปร op (นิพจน์))
ั ลักษณ์
สญ
ตัวอย่ำง ควำมหมำย
=
Simple assignment
x=2
x=2
*= Addition assignment
x *= 2
x = x*2
/= Division assignment
x /= 2
x = x/2
%= Remainder assignment x %= 2 x = x%2
+=
Addition assignment x += 2 x = x+2
-=
Subtraction assignment x -= 2 x = x-218
ลาดับความสาคัญของตัวดาเนินการ
ตัวดำเนินกำร
ลำดับดำเนินกำร
()
ซ้ ำยไปขวำ
+ (unary) -(unary)
ขวำไปซ้ ำย
* / %
ซ้ ำยไปขวำ
+ -
ซ้ ำยไปขวำ
= += -= *= /= %=
ขวำไปซ้ ำย
19
Assignment operators
การประกาศตัวแปรและกาหนดค่าเริ่ มต้น
int
นิพจน์
a=1,
นิพจน์เทียบเท่า
a += b + c
b *= c = d + 5
b=2,
c=3,
d=4;
ค่าของนิพจน์
a = (a + (b + c))
6
b = (b * (c = (d + 5))) 18
20
Math library
#include <stdlib.h>
 Function prototype
description
double sqrt (double)
หำค่ำรำกทีส
่ อง
double exp (double)
หำค่ำ ex
double log (double)
หำค่ำ log ฐำน e
double sin (double)
Sine
double cos (double)
Cosine
double tan (double)
Tangent
double pow (double, double) หำค่ำ xy
่
int
rand ()
หำค่ำเลขสุม

21


รูปแบบ
[ค่ำคงที]่
[ค่ำคงที]่
ตัวแปร
ตัวแปร
ชนิดข ้อมูล
นิพจน์
[ตัวดำนินกำร]
[ตัวดำนินกำร]
ตัวดำนินกำร
ตัวดำนินกำร
ค่ำคงที่
ตัวแปร
ค่ำคงที่
ตัวแปร
ชนิดข้อมูลของนิพจน์ขน
ึ้ อยูก
่ ับชนิดข้อมูลของค่ำคงที่
หรือต ัวแปรในนิพจน์ หำกค่ำคงทีห
่ รือต ัวแปรมีชนิดข้อมูล
ต่ำงก ัน ค่ำของนิพจน์จะมีชนิดข้อมูลตำมชนิดทีม
่ ข
ี นำด
ใหญ่

ขนำดของชนิดข ้อมูล
char < short < int < long < float < double < long double
22
ตัวอย่ าง
#include <stdio.h>
void main() {
int a = 1, b = 2, c = 3;
x = a + b;
print(“x = %d\n”, x);
}
้ ะมีขอ
เมือ
่ ทำกำร compile โปรแกรมนีจ
้ ผิดพลำดปรำกฎด ังนี้
Error EX1.C 7: Undefined symbol ‘x’ in function main
Warning EX1.C 9: ‘c’ is assigned a value that is never used
23