Title: ??? C ??????
1??? C ??????
2????
- C????
- ????
- ??????(????????)
- ??
- ??
- ??
????
- ???,C ????,???
- B.W. Kernighan and D.M. Ritchie,The C
Programming Language (C??????) , ??? - H. Schildt,C????, ???
3?????????
CPU????,?0?1?????????????????????????
????
??? ???
????????????? ADD, SUB????????,????????
????
????
????
????
????
4?????????
Ada
Modula-2
Pascal
ALGOL60
ALGOL68
C
BCPL
CPL
B
C
Smalltalk 80
Java
Simula 67
QBASIC
VB
BASIC
ANSI-BASIC
FORTRAN90
FORTRAN77
FORTRAN
PL/1
COBOL
LISP
PROLOG
5?????????
- FORTRANFormula Translation
1956?,?????????,????,??????????
- BASICBeginner's All-purpose Symbolic
Instruction Code
1964?,????,??????
70??,???????,????????
70??,????????????,?????,?????????
6C ?????
- 1969-1973,? Dennis M. Ritchie ?????
- 1973,UNIX??????C????
- 1978,B. Kernighan ? D. RitchieC??????
- 1983-1989,ANSI C ???? (C89)
- 1999, ?????? C99
- GNU C Comipler (Linux) gcc
- Microsoft Visual C Comipler
- Borland Turbo C
- Intel C Comipler (Linux/Windows)
- PGI C Comipler (Linux/Windows)
7?????????
/ example calculate the sum of a and b
/ include ltstdio.hgt / the main program
/ main() int a, b, sum a10 b24
sumadd(a,b) printf("sumd\n", sum) /
function sum of two integers / int add(int
x,int y) int z zxy return(z)
8C ??????
- ?? C ??????????????
- ????????????????
- ????????????main ??,????
- ????? main ??,? main ???
- ????????????,????????? ???????
9C ????
- ??????????????? ??, ??????,???????
??????? - ???,??????????????????, ?????????,??????????
- ?????????,???????????
- ???????,?????
- ??/ / ????, ????
- ?????,??????
- ?????????
- ????? C ???????
- ??
- ????????,???????
- ?? TAB ??
- ??????
- ??????
10C ?????
- ??(???? 52 ?)
- ??
- ???(???????????)
- ???????
11C ????
- ????????????,??????????????
- ?????,??????
- ?? C ?????????,??????????
- ?????????????
- ??? (32?)??????????,????????
- ???
- ????????
- ??????????????????????
- ???? / ???? / ??
12????? (Linux)
- ?????,? .c ????(??????????)
gcc o ???? ???
-o ???????,??? a.out -c ??????,????????(.o
??) -Ipath ?????????(? .h)????? -Lpath
??(??)???????? -lname???? libname.a ?? -O, -O1,
-O2, -O3 ???? -g ???????????,??????
13??
?? ?? ????
?????? ???????
??????????????????
?????????????
- ?????????????????????,???????????????
- ?????????????????,???????????,?????????
- ???????????????????,?????????????,????????
14????
- C????
- ????
- ??????(????????)
- ??
- ??
- ??
15????
?? (int) ??? (short) ??? (long)
??? (unsigned) ???
??
??? (float) ??? (double)
??
????
??? (char)
C ????
??? (enum)
??
??? (struct)
????
??? (union)
?????? 1. ????????2. ??????3. ?????????
????
???(void)
????(typedef)
16??????
?? ??? ????? ????
?? int 2 -215 215 -1
?? short 2 -215 215 -1
?? long 4 -231 231 -1
?? unsigned int 2 0 216 -1
?? unsigned short 2 0 216 -1
?? unsigned long 4 0 232 -1
?? float 4 (6-7) 10-38 1038
?? double 8 (15-16) 10-308 10308
?? long double 16 (18-19) 10-4932 104932
??? char 1
?example_datatype.c (long double?????????)
17??
- ??????,??? l ? L ?????
- ?????????,??? f ? F ?????
- ?????????????????
- ?????????????????
define PRICE 30
- ????????
- ?????????,?? C ??
18??
- ???????????
- ?????????????
- ???????,???
????? ?????
int i, j, k0 double a,b3.1415,c char c
19????
- ???????????,?????????
- ??????????????,????????
- ????????????????
- char ?? short ??????,?????? int ?
- ?????????????,????????????
char, short ? int ? unsigned ? long ? double ?
float
(?????) ???
20???
- ??????-??/?? (??)?-- (??)
- ???????????,gt?lt? ?gt?lt?!
- ???????????,??!
- ???????????????, ??? (??)?ltlt (??)?gtgt
(??) - ????? ??-??/? ????gtgt?ltlt
- ?????????????,?????? ( ? )
- ?????,(??????????????)
- ????? (???)? (???)
- ???????sizeof (????????????)
- ??????? (),?? ,?? (?,.) ???
21?????
???????C??????
() gt . ! (type)
sizeof / ltlt gtgt lt lt gt gt
! ? / ? ,
?
?
22????
????? ????????
int a10 double b1221, c16 char ch34
????????
?example03.c
23????
????? ????????1?????2
double A88, B88
????????1?????2
?example02.c
24????
char c1, c210, c86
- ??????????????
- ???? ASCII ???????????
- ????????????????
- ????????????
- ???????\0????????
- ?????????(???? string.h ?)
puts?gets?strcat?strcpy?strcmp?strlen
?example04.c
25????
- C????
- ????
- ??????(????????)
- ??
- ??
- ??
26C ????
C ??
????? 1
????? 2
????? n
?????
????
?? 1
?? n
????
???
??????
????
27??
- ?????
- ??????
- ????
- ????
- ???
???
???(?????)
??????
?????? ??????????
????
28????
?? ???
- ????????
- ???????????????????,??????????????????????
- ??????(??????????)
xyz0
29??printf
C ??????????????? stdio.h ?
printf(???????,????)
- ????????,????? ????
- ?????????
printf(kd, af\n,k, a)
30?????
????????.??????
31?????
????????.??????
- d?o?x?X?u ( ?? )
- e (??????????? )
- f (?????????)
- g ( ???????????????)
- c ( ??????)
- s ( ?????)
\n (??) \t (???) \b (??) \r (??) \f
(????)\ (???) \ (???) \\ (???) (???)
\a (?? )
?example_printf.c
32??scanf
scanf(???????,????)
- ??????
- ??????????????,?????
??????????
- ????????
- ???????,??????????????,???????????????????,?????
??
scanf(df,k,a)
scanf(d,f,k,a)
33?????????
lt lt gt gt !
- ?????????????,????????
- ?????? 1 ? ? 0
!
- ???! ??????,????????
- ?????? 1 ? ? 0
- ?????,?????????
34if ??
if(???) ??
if(???) ??1else ??2
- ??????????? (????????????)
- ?????????
- else if ????????
- if ??????
- else ????
if(???1) ??1else if(???2) ??2 else
if(???n) ??n else ??n1
35switch ??
????????????,???? case ???????,??????????,
????????,????????,????????case??????????????case
????????????,??? default ????
switch(???) case ??1 ??1 case ??2 ??2
case ??n ??n default ??n1
- ?????????????
- ?????????
- switch ??????
- ?????????? break
36for ??
for(???1???2???3) ???
???? 1) ??????1 2) ?????2,?????(?0),????
?????? ?????(0),????? 3)
??????3,??????? 2) ?
- ??????????,????????
- ???1?2?3????,??????
- 3????????,??????
for(?????????????????) ???
37while ??
while(???) ???
38????
return ???
return(???)
39????
- C????
- ????
- ??????(????????)
- ??
- ??
- ??
40????
- ??????????????????
- ?????????????????????
- ????????????????
????????????????????
??????
41????
int abs(int i) double
fabs(double x) long
labs(long n) double
exp(double x) double
log(double x) double
log10(double x) double
pow(double x,double y) double pow10(int
p) double sqrt(double x)
double acos(double x)
double asin(double x)
double atan(double x) double
cos(double x) double
sin(double x) double
tan(double x) double
cosh(double x) double
sinh(double x) double
tanh(double x) double
ceil(double x) double
floor(double x) int rand()
double modf(double
x,double iptr) double fmod(double x,double
y)
- ??? math.h
- ???? lm ??
- ??????????
?example_math.c
42?????
????? ???(??????) ???? ??
- ??????????????,?????????
- ?????????,??????,?????
- ????????,?????????,??????
43????
main() int max(int a, int b) int
x,y,z printf("input two numbers\n")
scanf("dd",x,y) zmax(x,y)
printf("maxmumd",z) int max(int a, int
b) if(agtb) return a else return b
44?????
- ???????????????,????????
- ???????????
- ??????????????????,???????????,???????????
- ?????????,???,????????
- ??????,??????,?????????
45????
- ????????????
- ?????,??????????(????)
- ?????????????
- ??????????????
- ?????,????????????,??????????????????
void nzp(int a) void nzp(int a, int n) void
nzp(int A5, int m)
46?????
- ??????? return ????????
- ???????? return ??, ??????????return ?????
- ??????????????????, ?????????????,????????
- ????????,??????????????
- ????????,????????(void)
47????
???(????)
zmax(x,y)
scanf("d",a)
printf("d",max(x,y))
48????
????? ?????(?? ??,?? ??, )
????? ?????(??,??, )
- ??????????????,??????
- ???????????????,??????
- ????????,??????????????
???????????,???????????????,????????,??????
49????
50????
????????,??????????????
51????
- ??????????,??????????
- ??????????,?????????? (extern)
- ????????????,?????
- ?????????????,???????
52??????
?????????????????
- ????,???????????
- ??????????????
- ????,????????,??????
- ?????????????,???????
- ??????,?????????0???? ?????????
- ????????CPU?????,??????
- ??????????????,??????
53????
- C????
- ????
- ??????(????????)
- ??
- ??
- ??
54??
- ????????C?????
- ???????C??????????
- ??????????????????
- ????C???????????????,????
- ???C?????????,??????????
- ??????????????C???????
55???????
- ????????????????????, ????????????
- ???????
????? ?????
?????? ?????(?????? ???)
a3 pa p5
56????
int a,pa,b5,pb paa pbb
?example_pointer1.c
int a,paa,b5,pbb
int b5,pb0b,pb3 pb3pb03
?example_pointer2.c
57????
- C???,???????????????????
- ????????????????
pbb b3 (pb3) (b3)
- ??????????????????
- ?????????????????
int a34 a a0 a a00 a1 a1
a1
?example_pointer3.c
58????
????? (?????)??
int (p)4, A34 pa
(pi)j
?example_pointer4.c
59?????
- ????????????????????
- ???????????????????,??????
char c,pc
char s"Hello world"
char ps, str20 ps"Hello world" str"Hello
world"
60?????
????? (?????)()
?(?????????)
61????
- C????
- ????
- ??????(????????)
- ??
- ??
- ??
62????
FILE ?????
63????
- ??????fgetc ? fputc
- ???????fgets ? fputs
- ???????freed ? fwrite
- ???????fscanf ? fprinf
?(?????????)
64??
include ltstdio.hgt include math.h
65????
double x x clock()/CLOCKS_PER_SEC /
????????? / x clock()/CLOCKS_PER_SEC - x
time_t t0, t1 double t t0time(NULL) /
????????? / t1time(NULL) tdifftime(t1,t0)
66????
CA mn BnL
(1) ?????????????
???i-j-k ??
for(i0 iltm i) for(j0 jltn j)
for(k0 kltL k) CijCijAi
kBkj
???j-k-i ??
for(j0 jltn j) for(k0 kltL k)
for(i0 iltm i) CijCijAi
kBkj
for(i0 iltm i) for(k0 kltL k)
for(j0 jltn j) CijCijAi
kBkj
???i-k-j ??
? mnL1024, ??????20?,????????
67????
(2) ? Jacobi ? GS ?????????? Ax b, ??
- ??????????
- ???????? 1000 ?
- ?????????? 10-6