Title: C ?????
1C?????
2?????(variable)?
- ???????????
- ????????????
- ??????????
- ????
- ???????
- ?????
- ??????
??????? ???????????
3Literal Constant
- 20???????
- 20 //???????
- 024 //??????? (???)
- 0x14 //????????
- ???0?????,???0x?????.
example
4Literal Constant
- ?? (123) int
- ????? 20u ? 20U
- ??? (?????????) 222222222L
- ?????? 222222222uL
- ??? (3.14) float ? double
- ???? 3.14E-3 (0.00314) ? 4E2 (400)
- ???float?? 1.0f ? 1.0F
- ??????? 3.14159L
?? unsigned
?? long
5 ???? (Literal Constant)
char (????) short, int ,long (????) float,
double, long double (???)
Literal Constant
?????
?? int x20 // (10??) short
y024 //(???) long z 0x14 //(16??) (1024L)
?? gt int ??
??? float x20.1f // (10??) (3.14f) double
y20.1
?? f ?????? double ?
6????
???????
char ??????? 1 Byte(1 machine byte)
short int ??? 2 Bytes
int ?? 4 Bytes(1 machine word)
long int ??? 4 Bytes (32??? int)
float ?????? 4 Bytes
double ?????? 8 Bytes
long double ????? ? Bytes (?sizeof??)
bool ??? 1 Byte
?? 8 bit
????? short
?? 16 bit
unsigned ??? unsigned int signed ??? signed int
?? 32 bit (???? int)
????? long
Note long double????????????? ??? VC, BCB, g
7????????????
????
?????
??????
?????
????????????
ms-help//MS.MSDNQTR.2004JAN.1033/vclang/html/_plu
slang_fundamental_types.htm
8????(32??????)
- sizeof(Type) ??Type??????,?Byte???.
- ???? DemoTypeSize.cpp link
- ????
- char -gt -128127
- int -gt
- unsigned char -gt 0255
- unsigned int -gt 02?32??-1
- unsigned??????.
9??????
include ltiostreamgt include ltlimitsgt
// for numeric_limits using namespace std void
main() cout ltlt "largest int" ltlt
numeric_limitsltintgtmax() ltlt endl cout ltlt
"largest float" ltlt numeric_limitsltfloatgtmax()lt
lt endl cout ltlt "largest unsigned
int"ltltnumeric_limitsltunsigned intgtmax()ltlt
endl
numeric_limits
ms-help//MS.MSDNQTR.2004JAN.1033/vcstdlib/html/vc
lrf_limits_Numericlimits_class.htm
10 ???? (Literal Constant)
char (????)
???? a 2 ???? ?????? \n ?? ASCII??
\7 (bell) ? cout ltlt \7 \14
(new line) ??? La ?? Hello World \n
null ??????
C-style?? Hello World \n \0
???????
11??
????????
?? ???? (???) Example main() char x
char ya float pi3.14f ???????? int
x1,y2 //???? int x(1) //??x1
?C????,?? Statement????? ??
12???Object????(??)
- ????
- char a
- int day
- unsigned int year
- ????????
- char a,b,c
- int year,day
- unsigned int id,count
- ??
- global?????0.
- local???????.
- ???? VarDemo.cpp link
-
13??
??????
int f(int arg) // f ???
prototype void main() int x ,
y1 xy //??x1 x // xx1 x-- //
xx-1 x4 // xx4 int zf(x) int f
(int arg) return arg1
14???????
void
15????(??)
- outline
- ?????
- ???????
- ????
- Why Pointer
16????(??)-- ?????
ProgramGetAddress
17????(??)-- ??????
- ?????
- ????????????
- int x // ???????????????
- double y //??????double???????
- Ex
- int y5 // ?????? y , ???? 5
- int x // ???????? x, ????????
- xy // ? y ????? x???.
- int pyy // ????? y ????? py ???
- y ? x? py? py?
18????(??)-- Why Pointer?
- ?????
- ???????copy
- ??????
- char A100
- char pAA0
- (pA1)???A1
- ???????????
- Div2(int x)
- pDiv2(int x)
C type?????????? const char strHello
const char kk20Hello
19????
???????? char x // x???????? int y
// y???????? // ??
?????????? int y5 int x //??????????? xy
// ???y???,??x? cout ltlt x coutltlt y
20????
?? int y5 int xy
x???1202????? ??? 5
21??????
int ip1 int ip2 int var32 ip1var ip2ip1
coutltltip2 ??32 ????PointerDemo.cpp
03A9
ip1
03A9
32
var
22????
int A100 // ??????,???A,???100 int
pointerA0 //????????????
1
2
100
(pointer0)
(pointer1)
(pointer99)
???? PointerArrayExample
23 ????????? string ? char
?????????????,???????????.
This is a book \0
24 ????
- ??C ???????--C??????????
- ????
- int lengthstrlen (This is a book)
- ?
- char mesg100this is a book
- int lengthstrlen(msg)
- 2. Assignment???? astrthis is a book
- strcpy(astr,this is a book)
- 3. ????
- strcat(string1,string2)
- ????????????????,????????(buffer overflow)?
- Ex char str201 // \0 ?
-
25String Class
- C Standard Library?????????Class.
- ??? string ????.
- ????StringClassDemo.cpp
- ?????C Standard Library,?????????? using
namespace std ?????.
26String type ????
- string type ??????????????
-
- string str1(This is a book) //????????
- ????
- int lengthstr1.size()
- 2. Assignment
- str2str1 // ?java ??
- 3. ????
- str3str1str2
- 4. ???C-style ??
- const char cstr1str1.c_str()
- 5. ????
- str10 str11 . str1length
-
Or str1.length()
27??
- 01DebugStringClass
- ???ioStreamDemo
- ??????,????????
28String type ????
????? c-style ??????
???cout ??? c-style string
includeltiostream.hgt include ltstringgt using
namespace std void main() string a cout ltlt
"?????" ltlt endl cingtgta
// error string b"StringB" cout ltlt b
// error
29- ?? reference
- ??????(alias),??????object??????.
- ????????object??.
- int a
- int ba
- int c see ReferenceDemo.cpp
30const???
- ??????Object???????,???????????.
- Ex
- void main()
-
- const int a //???????.
- const int b70 //??
- b123 //??const????????.
-
Try ok
31Array type ????
- Definition
- ????????????objects???
- ????object?????index ???????
- ???????
- int a10 char b13 float c3
- (index0 9)
- Access object??
- int ta0 a3a7 coutltlta9
Array type ????
32Array type ????
????????? int a31,2,3 const int
bCount4 int bbCount11,12,13,14 int
c21,22,23,24,25
33Multidemension??
- 2??? Definition
- ???????
- int a23 float b34 char
t313 - Access ?????
- coutltlta00 a12a02 b120.38f
- ???????
- int a230,1,2,
- 3,4,5
Program Array2DDemo.cpp
34????????????
- ??int a5??,????a,???a??????????????. (ex)
- an ???n???????????.
- ??
- a0???a0???,a3???a3???
- (a2)???a2???,?a2??a02
- ????ArrayPointerDemo.cpp
35??(Enumeration)??
- Definition (?????????)
- ???enum
- enum ??
- ??
- ?
- enum MyDataType A1, B2, C3
- Usage
- MyDataType xxyy A
- MyDataType yy(MyDataType)(AB)
- ???????,?????????0????1??.
- ????????.
??? 1,?? A C
??(Enumeration)??
?????,?????
- Program example
- EnumDemo.cpp.
36Typedef
- Definition
- ???typedef
- ???????????????????
- Ex
- typedef int Integer
- void main()
-
- int a10
- Integer b23
- coutltltabltltendl
-
37End
??? debut.cis.nctu.edu.tw/ching
38??
- ?? C ??????????,?????????.
- ?? c-style ?? ?? string ??
- ????, ????, ????, ??????????
int x // ???? xx1 // x ?????,??? x char
strJing cout ltlt str