函数大全(c开头)
) F; j0 m1 _. {
3 d& L1 w3 _( J+ L函数名: cabs 3 e7 [" s7 y2 z: u; {, s
功 能: 计算复数的绝对值 0 y. M6 N9 v7 T( R1 X2 f. t8 s
用 法: double cabs(struct complex z); i' t% u, J' n$ u5 m3 N% v. R
程序例: + K. N A" f! G. V. N; c' Q
#include
6 T* O# v7 _$ j! N6 Q( H7 ?1 S#include 5 b5 ^, K* U \- T
int main(void)
+ c' I* ]) i: C1 V7 ]" I{
0 M$ U& U* n8 Lstruct complex z; 6 k3 p4 A' n4 N& G! k* ?
double val;
! B b: M; z1 ` X$ Xz.x = 2.0;
7 v- c, W8 ]5 x$ K# qz.y = 1.0; ' @& R6 _6 M5 u" [2 Z
val = cabs(z); , Z4 s" \! y6 a) K& m4 G
printf("The absolute value of %.2lfi %.2lfj is %.2lf", z.x, z.y, val);
" D- D7 r" X1 E7 q, Ereturn 0;
( \1 d& _# h, A% I, n} : _3 s* H3 U% ^( x
9 K, y- I w8 T7 G( x0 N2 T% {9 a6 [: T, }1 s- \0 p+ w
( l+ r( n- z6 G Q- [8 S, J函数名: calloc 9 W/ k7 B0 Q- B
功 能: 分配主存储器 - Y; D3 B; [; U; n+ ~& a* T8 X
用 法: void *calloc(size_t nelem, size_t elsize); 7 ~" `" c$ R( d4 l" E3 Z
程序例: 8 j' f6 P8 ?+ {6 L) C+ g+ L
#include 7 P5 a- J0 v& c: N$ M
#include / T4 w7 j9 k* j, f$ a1 F
int main(void) 3 S4 x0 _$ {8 v% T5 P, K5 U
{ % l6 o; H! X/ C2 ~5 m3 y# `) _
char *str = NULL;
) J" ]8 o: X% G/* allocate memory for string */
2 `/ `, w0 k( Z/ `: J; Astr = calloc(10, sizeof(char)); 7 M8 L- t6 B/ g' T# ?
/* copy "Hello" into string */ , d: i K- M2 J9 a
strcpy(str, "Hello"); 2 J( [* p7 L5 \, @# |
/* display string */
- E: E0 n y+ h; U' m& K# u, ^- Tprintf("String is %s\n", str);
4 q& D! F: Z# ^" ]+ P/* free memory */ ; z0 ^2 P$ O' k) S n% I0 U
free(str); ( N% a9 O4 [6 _, H. N
return 0; % Z5 J. c3 W3 G
}
- P( B1 [, ~2 `' I' N+ c$ Y- c7 `* S2 M j6 h
% J3 z% P: `5 s . x$ q7 `" z8 c. K; z! E" D
函数名: ceil 4 y0 o2 Y+ U @1 N: W
功 能: 向上舍入 : Y! g$ d9 z6 Y( Q
用 法: double ceil(double x);
; U: Q5 j$ B' }程序例:
- ]% ^5 ~( u2 F! o' B) A#include * ?% I! v; i6 `# ^0 Y# s8 x
#include
3 Y F: ~) F2 c X- `int main(void) ! }/ N3 `/ ]; c8 c0 G* B
{
g+ z% D) F- P$ U( L3 mdouble number = 123.54; 0 Y. W9 ?$ c- _8 M# o5 ~# M5 q
double down, up; 9 P* F; K- y' s3 z; r) T
down = floor(number);
% ~$ i: \% v J# `5 _6 R/ o% mup = ceil(number); 2 `$ ~$ d5 c1 A6 V
printf("original number %5.2lf\n", number);
9 F4 Q2 }2 t' F p2 Cprintf("number rounded down %5.2lf\n", down);
6 S- {. ?5 K- g+ y! R. wprintf("number rounded up %5.2lf\n", up);
# E+ `6 N0 t$ P% M4 V+ C1 ?return 0; - g. V2 `9 m |5 F
} , G/ A4 V1 u1 f5 r4 v# q
* b' m, V; ]% y* ~, X; O: H9 O
. `9 K: [) G/ D# b8 h # b7 h+ k1 l! R& Q9 z4 ?# r+ {
函数名: cgets
4 T& T! B& d" p功 能: 从控制台读字符串
4 e1 d1 r1 y* G, s用 法: char *cgets(char *str); " @ }* @8 P- x
程序例: ) m7 k C) w+ d9 L4 F/ }
#include
1 F( s% U; H* i#include
& Y2 b5 Y9 c/ a+ k; Y7 O6 n0 Cint main(void) 0 }( X3 G7 |: |) A$ f: i
{ & r* u; H2 y h' y$ D* \
char buffer[83]; + t& P8 N+ A# p& k$ w4 e5 Q% q
char *p; 1 {+ Y' R- g$ P0 }9 A& t5 G# }
/* There's space for 80 characters plus the NULL terminator */
* J2 q8 v7 N) V0 w: @0 ibuffer[0] = 81;
& o# Z, `; ?3 _2 vprintf("Input some chars:");
- n; @0 [6 i; tp = cgets(buffer);
3 B/ D/ Y! d) R- J. w7 Cprintf("\ncgets read %d characters: \"%s\"\n", buffer[1], p);
5 j( }- Q/ ~' ~! Q$ ~% O/ ?printf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer);
% q7 e9 }7 [% J% C! N/* Leave room for 5 characters plus the NULL terminator */ ' H; B( K" E4 \" ^6 e, i' [
buffer[0] = 6;
?- e# n! `- q" S3 s/ F$ b) Uprintf("Input some chars:"); ) Y. Z/ M. s6 e4 D
p = cgets(buffer); . ]8 ?, q' N0 @- n2 c5 J
printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p); 4 l9 ~" a$ r4 Q( }8 A9 a' n
printf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer); 8 b! \, m2 d$ ?4 {/ A3 G% n
return 0;
7 f: H, F. I; f6 M# P2 D} 1 s0 M z5 ^. u c9 e0 V B! j* q
; s9 x, G; ?1 m5 z% G. m" l2 ~8 E6 F6 O- {: i$ H
3 \, K! ]5 G. u函数名: chdir
6 z d. m& j* g4 o' J0 Q) `3 j" }功 能: 改变工作目录
* p# N3 F ?, S用 法: int chdir(const char *path);
6 ]2 o$ T# G' B% r' F3 J6 Y程序例: ! V S+ O0 K* j- _
#include
4 P4 |3 J5 {) H#include 1 u" L" P, Y+ \: r3 }! |* s
#include - H$ Z+ M. V) ?; o( J1 f& N6 I5 z& f
char old_dir[MAXDIR];
" z5 W7 s* j; U3 s1 D2 Echar new_dir[MAXDIR]; ) E. h0 l- f& x5 P6 x" j, X
int main(void) ( u% I) E! N. N s% Q. Z# x
{ ) @& a/ n9 M0 Q2 \
if (getcurdir(0, old_dir)) . g5 v, J F" P
{
5 @4 r( E+ Y7 C+ Wperror("getcurdir()"); , o2 R0 b, `2 I$ ~6 s3 p
exit(1); ) J+ c: \( J: G3 H4 F& y( G8 x
} : o; h/ J; A6 T$ N! \! V
printf("Current directory is: \\%s\n", old_dir); $ B8 T3 @! V K
if (chdir("\\"))
) x, H7 d: j3 M2 Z# I) P9 q/ i{
8 c% ^" f6 S& Z1 Lperror("chdir()"); " w [: }' M, }( ^/ ]' M1 g' D
exit(1); - ], Z" s ?4 W7 Y) B1 K6 i* D
} . l2 B+ B7 d; q+ h+ q Z1 {
if (getcurdir(0, new_dir))
# P$ \5 Z$ R! C3 q7 n$ p" y{ ) M2 m* z: G- [
perror("getcurdir()"); " l% ~) E$ C6 }* @. Q$ r+ s$ P" J
exit(1); . m" e/ y0 Z' q, b) O
} 5 W, u4 Z2 Z% f! T
printf("Current directory is now: \\%s\n", new_dir);
# X4 q, ~1 u& Z/ C- I$ Uprintf("\nChanging back to orignal directory: \\%s\n", old_dir);
! h T9 k' G3 `" o% }1 Bif (chdir(old_dir))
7 K0 Y1 p$ s& }" O% c6 b' |{
9 n6 g" `; i+ v e. p @perror("chdir()");
! C+ _& E8 W W3 U4 ?9 F( ]exit(1);
: A7 T- o, E2 `* y/ l3 e. [2 O} 0 G: ?# w+ M6 Y+ Z& C
return 0; 1 j4 U0 i% ?. ?! U( i
} , o/ B2 F1 D! }
1 Y' U+ }3 p: N2 b. H' p" N* X+ i - N$ m' {7 v: \# T5 j$ W0 C
函数名: _chmod, chmod
+ o) r2 u+ c5 N# a6 }功 能: 改变文件的访问方式 ' e1 o: w6 k V, I2 m0 Z. g9 \
用 法: int chmod(const char *filename, int permiss);
7 H7 N: O: D* e0 |* H程序例: ) t+ h% J$ ]5 |# o& |# s
#include
3 C( I$ J+ j1 u% s4 E1 O7 t#include 5 q/ l6 `+ ~% }5 O! e3 K
#include 2 d4 B7 O0 G0 J b l! r9 O
void make_read_only(char *filename); + B% H" b% ^" j9 K% m0 Q# N
int main(void)
2 Z: m8 ]* L& [8 ?; T{ V- l& ^- e7 e' b3 q8 F* x' f+ o' G
make_read_only("NOTEXIST.FIL"); ! ]" s" ?1 m: e
make_read_only("MYFILE.FIL");
! j3 O" i6 ^3 G H- Nreturn 0; ( T7 K H& U8 h8 w* p" c
}
' n/ L$ k7 M/ Z3 J5 T$ ivoid make_read_only(char *filename)
7 h& b1 @! P- R& q{
; i: E% ~) T2 W; m" `int stat; 5 O8 j7 ]$ {# o' ?( F
stat = chmod(filename, S_IREAD); 3 ~) Z! a! C' ^/ q
if (stat)
x+ w' e v- Wprintf("Couldn't make %s read-only\n", filename);
1 s" x) T' c' a' aelse / L8 p6 F. {: r& y+ e- f; `6 L
printf("Made %s read-only\n", filename);
( h/ A' E' O' X3 B9 P! |}
) y) A2 _3 s e5 G6 v/ _( W
. f& o+ K% [7 H, l2 q( ]" Z( ]& _# m* U' E: k' p- r
$ ]1 x# L' Y% M, Y. Y6 e* i' g
函数名: chsize 0 u7 [8 w! @. x2 q( P8 C V" c
功 能: 改变文件大小
4 I z3 E! L2 ?3 @0 i$ `& w用 法: int chsize(int handle, long size); 3 F: ~0 \, b/ u+ |% o+ \
程序例: 6 I; |' r7 W4 p1 I4 C0 n- I" K
#include
Q: q4 b+ O- Z0 q5 H* u#include 5 Z7 z. A" C1 e
#include
: A9 N+ j, O0 [/ J, U9 Sint main(void)
2 R( B" K: n9 F/ r2 F4 F{
- x3 M6 e' v4 Gint handle;
+ _# \* E: \# O# f' O1 r# Tchar buf[11] = "0123456789"; e. y9 U; [# g1 o
/* create text file containing 10 bytes */
) w9 n& B8 N+ l/ ahandle = open("DUMMY.FIL", O_CREAT);
+ ]1 F% N7 y0 p7 {6 }- C$ }6 Nwrite(handle, buf, strlen(buf));
, Q3 r1 _, ~/ C+ y# Z/* truncate the file to 5 bytes in size */
# Q! D( g% g" b2 `3 u) Gchsize(handle, 5); ) ]' y( s$ R( C" n, {2 c; p0 N& i
/* close the file */ " }( M% s, E6 f9 m: w: ~6 L5 W
close(handle);
2 e4 s9 C) r7 p" Q6 ]' Q0 k- Nreturn 0; 1 W* ~# m% R! H, L
}
' A- Y: B2 R2 \* I) S
" q2 P" T1 t6 d0 ^5 F
& h8 G0 Q0 ~) t+ W( y' ]4 `函数名: circle
5 G3 O2 G6 D" T8 l, V% E6 R功 能: 在给定半径以(x, y)为圆心画圆 : P% x: L! P! k( H2 W9 j
用 法: void far circle(int x, int y, int radius); $ @8 C1 W+ G$ g' r
程序例: 4 W$ N$ }. g; j; \
#include
/ R, V/ p0 s6 o9 x8 }& ?% y9 x) y#include ! N6 [/ n0 f2 C# d0 y8 _
#include
v# G$ D/ o1 m2 l#include
, ~$ [% f9 \* J. pint main(void) " P/ i7 T5 a3 @- P8 Q: S
{ 4 T9 K |- q' r5 ?
/* request auto detection */ # ~4 N0 U0 c& p+ g& A
int gdriver = DETECT, gmode, errorcode; 8 q& p7 R" F" _1 Y. o: k$ @. ]
int midx, midy; 4 e7 q+ Y' D+ `" Q0 r! L5 s% U* l
int radius = 100;
! A, F4 ]) ~8 r9 C4 X/* initialize graphics and local variables */
" L1 U1 u) t. f1 Q9 x2 Ainitgraph(&gdriver, &gmode, "");
3 E$ {* D* V& @3 G! Q/* read result of initialization */ - N. y( n2 q: E/ e
errorcode = graphresult();
# e7 y X- M3 K6 g6 |& c6 Dif (errorcode != grOk) /* an error occurred */
6 I/ Q- Z. E% ~8 C/ D8 [{ ' k+ p; ]$ h; n# | [$ d% \0 e
printf("Graphics error: %s\n", grapherrormsg(errorcode)); + H& R( g9 e, y- _
printf("Press any key to halt:"); ) K# T7 r$ A/ o, o$ H0 S
getch();
& i7 ]+ i8 J) W! q. Z# Jexit(1); /* terminate with an error code */
) ^4 ^7 j/ m5 T7 u6 }# x# L2 V} 1 } [* G1 ~5 J* n
midx = getmaxx() / 2;
' C+ T9 j% A& B/ C9 X( b' tmidy = getmaxy() / 2;
) t6 p/ i- _1 F- H/ wsetcolor(getmaxcolor());
" F( A5 P9 N0 K/ T9 i4 j, ?/* draw the circle */ " b& Q b( {9 h4 j
circle(midx, midy, radius); : N5 W1 k' L: Q5 E" X Q6 T$ }
/* clean up */ 9 S" B! d) Z! F8 l# h$ a
getch(); 5 f' r o2 x- k. V% j6 L
closegraph();
' m* V0 e: H- Hreturn 0;
/ _% s( O( q" Y" M} ; ^8 {% U s+ g5 l" i
/ i; K3 ~ n6 d0 A2 [. E/ [) I9 @! R5 k+ P. v7 x/ i
- }$ t* g' [) |0 m, l% Q
函数名: cleardevice
' E' |( k# `! Z- s! F, \- h# ^功 能: 清除图形屏幕 5 ?5 r' S) O( N) [
用 法: void far cleardevice(void); - K8 `" Q" @6 H& P, h* N# V/ |. x
程序例: / c4 {3 N. Z$ |
#include 9 q* P. J7 b$ c0 J b8 }
#include
) O+ K! e% [. m) f- n! L! s#include
+ L$ W% G2 h7 x( t; o, o#include
& `7 J2 h4 f# G. |6 d7 Qint main(void) + @7 T$ |: L: n, }0 }/ l! x; v" z
{
' R b" W4 u' f1 x( y. t/* request auto detection */
\2 l" j6 L: q" Wint gdriver = DETECT, gmode, errorcode; 5 E& X* I% q2 j) E
int midx, midy;
) {: E) W% ]- s# Q. |; C/* initialize graphics and local variables */
$ S4 O) k8 s5 H$ v0 x* B+ uinitgraph(&gdriver, &gmode, ""); 7 K" [5 x) E% Q; v; U/ |
/* read result of initialization */
& g) w% q4 h. f2 s0 T1 merrorcode = graphresult();
7 ?4 @8 j& l8 e! @+ wif (errorcode != grOk) /* an error occurred */ ; d3 u7 b% L) Y/ F S
{ . h) m2 `! f. h( m5 C5 W& o
printf("Graphics error: %s\n", grapherrormsg(errorcode)); + \+ N: _/ U" L$ Q9 _. x" L
printf("Press any key to halt:");
: [1 Y; N* J, E( @' igetch();
7 W+ B8 r; t: {! Pexit(1); /* terminate with an error code */
9 D9 m2 \9 p0 `}
p3 k: [$ l% `2 }) i' g0 ^midx = getmaxx() / 2;
6 _3 U4 e" ^! I; f$ ~midy = getmaxy() / 2;
% w9 T% \; E# |" Ysetcolor(getmaxcolor());
6 B @7 [% t1 U/* for centering screen messages */
$ y! I2 j5 ?7 i- S; Wsettextjustify(CENTER_TEXT, CENTER_TEXT);
- L7 f' N" X1 X9 C/* output a message to the screen */ 7 g' M, i- J/ U
outtextxy(midx, midy, "press any key to clear the screen:"); / l3 w% C3 A2 p$ P) k
/* wait for a key */ ; [! D. w# o# |! O
getch(); % t1 N" m! z6 k; }3 s( u% ~
/* clear the screen */
3 c4 b3 H0 W9 s8 Y0 y) Vcleardevice();
4 l/ C# e3 f! r0 ~# b2 M/* output another message */
2 D' `, k: x6 U0 p% M0 c! houttextxy(midx, midy, "press any key to quit:"); N6 l2 ?; A7 I8 x( p; P( a8 T$ u- }
/* clean up */ 4 |0 g( Q R' t1 v% n+ J
getch(); $ s; u( V7 P1 }! B% f/ Q' r
closegraph();
% N K) p+ f4 n/ W, p* _$ {1 Rreturn 0; 9 \+ j* r& p' N( H* k6 Y
} , K$ T% j8 r$ ~7 b) D
1 A, Q! \) { T0 W
( s/ F; d: q- K. P7 O& ?% X% b
, x- H7 S' X/ n! F! `; Q函数名: clearerr
" G! K% n W# L7 G2 h功 能: 复位错误标志
L3 C- q* O& G7 _2 D# Q用 法:void clearerr(FILE *stream); - W# D2 ?+ P6 m' t+ }# ~* ~
程序例: : }5 @$ g& y d7 F
#include
: p* P: _ i; qint main(void)
) y. O D* ?1 a{
/ i `$ d. J bFILE *fp;
) Z7 K! K: L4 t8 k; o) pchar ch;
4 c# Z2 H3 |1 @" ~& Q6 A/* open a file for writing */
& ?" E! i* O9 ?fp = fopen("DUMMY.FIL", "w");
' a; U0 D5 d8 U/* force an error condition by attempting to read */
0 s" P6 }3 G( j2 x0 P3 Pch = fgetc(fp); % {" [1 v: O- o8 V u1 S
printf("%c\n",ch); 7 O! ^9 J- {2 f5 Q& }! m
if (ferror(fp))
. S, b# U& R! |+ ~* n$ a C{ 8 C. P1 d+ j7 p1 J1 x
/* display an error message */ 8 S) g' M0 ~; B& j# c
printf("Error reading from DUMMY.FIL\n");
7 t. D0 t+ m; R4 x/* reset the error and EOF indicators */ , u& I0 H( X- [' X8 W
clearerr(fp); 2 y. z0 F" i* ?4 b; g. @! I
} 5 n( H/ e! e, x6 J5 `' n6 J
fclose(fp); l& |$ I, y$ z( `: Y+ ~
return 0; 7 r7 c* j: Q k
} . G9 h+ G4 c$ _3 `; }. Y
6 a( p5 R' a9 N' |) t
+ Y4 |; T. t# g# {. k ' v. _; H% C z. l: |8 V+ y; G) v
函数名: clearviewport
, j5 ~5 J5 U& V( }' h功 能: 清除图形视区 ; ]/ {, ^$ z' v, A: x
用 法: void far clearviewport(void); / q! v/ Q; S& k" Q# k1 u
程序例:
5 Q" X& e& C5 s#include : B* f" g; I8 g1 ^
#include
% Q* G" C; A4 h Q+ X- y#include
: L- b. n1 Q8 t/ _: E% [# g#include , y2 A& y6 E D
#define CLIP_ON 1 /* activates clipping in viewport */ & T. \3 P) _7 O7 {
int main(void)
& L. j- l& I% V2 C5 H{ & s5 `+ u8 |3 N4 X) `8 n, q3 m: _
/* request auto detection */
9 f5 K+ _. y4 W3 b1 t" o+ hint gdriver = DETECT, gmode, errorcode; ; d% ~1 g* w8 P% G6 J
int ht; / C' n+ r, m. Q. G: y
/* initialize graphics and local variables */
- Z9 }/ s3 I& linitgraph(&gdriver, &gmode, "");
- L7 h: l* v% W \/* read result of initialization */ ' c1 W" j2 q' S& W
errorcode = graphresult();
1 c: o& V/ ]; L" B# Y& Lif (errorcode != grOk) /* an error occurred */
& U! m c0 q% ~1 v" a$ u+ X{
4 `$ ~+ f2 f P1 a+ \8 ~4 |- Yprintf("Graphics error: %s\n", grapherrormsg(errorcode));
, |- {. ^- @3 |5 _- Lprintf("Press any key to halt:");
+ S* b6 Z3 q: R! S) v& c" `2 Pgetch(); n4 ]# D, W6 }7 w2 q5 ^# s. B
exit(1); /* terminate with an error code */
# [7 p# O1 t/ ] T6 w! w}
9 m! F* g3 ?+ Y, y( qsetcolor(getmaxcolor()); 3 _: c4 o V- x* w4 `- u
ht = textheight("W"); ^! I$ _) O" U- S. G. Z
/* message in default full-screen viewport */ 9 a w! C/ {1 i' A! d/ ]
outtextxy(0, 0, "* <-- (0, 0) in default viewport");
, C7 O( f. [1 g% \8 L/* create a smaller viewport */
9 `" E k8 h5 f- S1 ^# H5 J" O1 bsetviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON); % [; Z0 S% p+ s! N# @
/* display some messages */ 5 Y/ d, |5 ?% F# X
outtextxy(0, 0, "* <-- (0, 0) in smaller viewport");
- W F/ Q6 [; W; d& p; v7 ^! Louttextxy(0, 2*ht, "Press any key to clear viewport:"); j+ ]: ~ |( p5 V7 e Z; U
/* wait for a key */ 7 g$ U6 m5 L+ e6 {5 @ p
getch();
9 x/ w( t/ e! a% R8 K/* clear the viewport */ ]* w" m- j* ]% O1 W
clearviewport(); & Q& k* s; \2 m
/* output another message */ & s K: b; Y* J
outtextxy(0, 0, "Press any key to quit:"); : {2 j. M2 w9 O& ]
/* clean up */
* k0 \: G7 _, N9 sgetch();
! g+ P7 I" [$ |- n6 }- Oclosegraph(); 8 k# G: G; J# {+ e w
return 0; 2 _, `0 B% z) r% r1 _3 N
} 5 ], d0 ?( O9 U7 ?
) f$ X. C4 h4 u( o+ ?5 [! K' c$ }3 k3 [3 N, T
9 l8 k3 n5 v0 y5 p' b2 J( r函数名: _close, close
^- g+ ?& n2 `: K功 能: 关闭文件句柄 & F0 g! Y0 v3 Y5 g; i2 B
用 法: int close(int handle);
* c( c: B9 t: z( p! i& t. s程序例:
& l$ G1 k4 `: |0 a#include 3 y/ t4 R) Z" ?' s
#include
" D/ n: J2 o+ C. r#include # d2 [! X3 {: N7 x/ A% J* X
#include 2 o/ M) L8 K- W T! V& h# l9 l
main()
, y7 h, C* K! P' b{ 2 k9 `4 t4 P, Q9 [ Y- r( e
int handle; $ Q, n6 @1 I0 L2 |
char buf[11] = "0123456789";
/ B7 [( C" t9 S; [2 D/* create a file containing 10 bytes */ / C l) x6 P0 _! j- J( Z
handle = open("NEW.FIL", O_CREAT); " z5 s5 g- r0 L/ H' ]
if (handle > -1)
I4 { v, {" J{ + h. k) v& S$ d/ I9 I' c/ T" ]
write(handle, buf, strlen(buf));
4 i. z- M9 e( B" _/* close the file */ " K6 O8 F" ?: M% F0 ?, R: c. I
close(handle); & ?. d7 }; p5 X
} $ t1 [7 N- _. z- o' s
else
1 X! ^+ u* P8 D3 ~{ * `. x9 @4 m# f& c
printf("Error opening file\n"); 2 g3 ~4 I* `8 K5 m% P
} # s- d9 y+ S0 ^1 _) ?- s6 o
return 0; 5 d* [4 ?- D! f! N; j$ A9 d
}
1 I+ b, D$ u) C) B! {# T0 A4 o6 f
* l; [8 n8 r, U1 k! W. x( y4 x& `3 K$ B9 u: ]3 e
8 w5 u! G. W0 Y, U% C% n函数名: clock i( I* a1 a1 l
功 能: 确定处理器时间
+ `: r4 [' |5 V( P0 v# [用 法: clock_t clock(void); ! K: V, l8 ~/ Y2 p* r
程序例: , t7 y G7 B- I) y
#include
( x' e+ [8 m' }9 M# y0 C#include
4 C6 E5 l" J Q- \/ M#include 3 ^7 t, J: y* I& z3 S. i9 w d) T
int main(void)
( P2 @$ T- D w5 J$ K6 G8 h8 e& T{
: l& j5 z0 r: ~" P2 bclock_t start, end;
1 E/ V/ n: e' d- l' Q$ L+ N( pstart = clock();
" g! i- O5 T, p0 V; Y3 d& mdelay(2000); 1 N+ n% N& X, q
end = clock(); " o- Q u" ~7 o1 p; u. v2 f
printf("The time was: %f\n", (end - start) / CLK_TCK);
0 D W6 {- s4 a: _( J( l% ?8 Sreturn 0; ' M' s5 R& y1 [/ T
}
' C7 S: T% G" g# x5 e& }; _1 I
5 a' p% I! X9 [( x3 u7 k M$ X0 i9 P& b& W! o+ V* b+ k+ K4 i! w- m1 m" N
- W( p! ?) \7 I0 W4 ~函数名: closegraph 5 J; W. ~% C+ q
功 能: 关闭图形系统 O- ]% i U! `3 v9 {1 {" N2 Y6 D
用 法: void far closegraph(void);
5 K) |& B' Q% E8 h, p. U0 S5 W. K程序例:
- k0 u9 p' a( j9 i#include + z3 {; V8 `+ ?- s
#include 1 ~1 S% q' f9 x8 m: \
#include
5 a ~9 C; f! W4 v- ]1 y: S% {* R6 v#include
3 n! `* R2 Y4 a+ O% Xint main(void)
# [% i& M9 c9 w3 F+ j{ 8 M) S$ J; l0 X. Q" f& ^
/* request auto detection */ , O2 b7 S8 I3 E; Y$ i
int gdriver = DETECT, gmode, errorcode;
3 _6 |+ M+ {2 V* I2 h$ T1 Jint x, y; ! h e7 \! v! q+ B& G
/* initialize graphics mode */
: D! H: q- i4 Q; minitgraph(&gdriver, &gmode, ""); 2 X2 O V: B# W& I
/* read result of initialization */
+ ?# q. n8 A+ j3 t/ Ierrorcode = graphresult(); 1 N; r' Z- b; |9 ]0 T! c" S: k N
if (errorcode != grOk) /* an error 1 T9 e8 G9 {3 V% @' k
occurred */
~" C; s- Y( Z% E, }2 x3 o: [1 b3 E{
0 t8 E# ^- g3 h/ T9 m. a* mprintf("Graphics error: %s\n", grapherrormsg(errorcode));
/ @9 P0 \ k7 C2 f3 i4 I- Iprintf("Press any key to halt:");
$ i! b8 B4 t& u% F5 rgetch();
0 D" n8 a- M2 nexit(1); /* terminate with an error code */
7 n' H3 R6 E9 X3 O) B} - H& z( M2 v$ [+ [5 z6 ?4 y8 w4 H* j
x = getmaxx() / 2;
# d$ Y, ~ p3 Zy = getmaxy() / 2; - V$ p* X6 f# l2 B
/* output a message */
* ^+ W! ?( d! l7 o3 ^6 ksettextjustify(CENTER_TEXT, CENTER_TEXT); 1 {" I; l9 Y9 S7 s/ B
outtextxy(x, y, "Press a key to close the graphics system:"); " W! y# N2 W, A7 u8 y
/* wait for a key */
, \7 W2 z; T# d1 }getch(); $ _5 Q0 S7 K, {4 {# V5 N e
/* closes down the graphics system */ * `7 Z3 G& g! b0 \" P- y
closegraph();
3 ]! e0 Z, o/ V& m2 w4 Zprintf("We're now back in text mode.\n"); 4 h, a6 L, g' ~. d8 A0 |- [) B
printf("Press any key to halt:"); " l" ]4 e* V# f/ p, m w* ^2 q
getch();
) ]5 B; b9 V2 a+ vreturn 0; , j5 w* G; m7 w' n. b, V6 z4 d
} ; r) ^) m7 e9 F, y) z; Z
" R* P- s% p; g8 z* a5 @
! a- p+ \ Q" s) J( P" W/ j
4 k: m% g/ |; y: K
函数名: clreol 9 a/ N" b' d+ `
功 能: 在文本窗口中清除字符到行末
' O, E7 }* R6 k, d用 法: void clreol(void); 4 U2 m0 H; K; }5 r
程序例: 3 D/ n# z4 u6 N. f4 V' V
#include
0 x3 z/ c u" Q$ N+ @8 `% jint main(void)
0 p [5 n: d# z! J5 c{ 0 f2 H0 ~3 Q( O' R9 _% M8 f
clrscr();
4 N! O" y/ |6 n3 Q2 d9 a* Jcprintf("The function CLREOL clears all characters from the\r\n"); * j$ d" Z% K0 t* o# c4 e
cprintf("cursor position to the end of the line within the\r\n"); , t: N! [% u0 d1 o4 D
cprintf("current text window, without moving the cursor.\r\n");
! l+ K S: s) W6 Gcprintf("Press any key to continue . . .");
P: ? V5 R2 kgotoxy(14, 4);
9 `& @/ Q3 ^; l7 d) c8 Y; N/ Rgetch();
1 n- w. O- r1 h/ g9 V5 t5 Rclreol();
4 l, W; k W' t, {1 Ygetch(); . U* O0 |7 j+ t& T
return 0;
3 w0 H: C: ]7 L} 8 ]5 H# C; j; J9 S. K
" r9 k& [$ i: d) Q9 _
; s z# p- w! u& s
1 }' Q" M$ q6 x; I函数名: clrscr * Z2 ^9 y" }- O6 s; {" f- ^- K
功 能: 清除文本模式窗口 d" q d! f5 w* Z' h
用 法: void clrscr(void);
) c0 D7 J6 x, L- U+ U: s3 U7 z8 R程序例:
. G. a' z' n, h( J |2 F#include
* p) }; k& O nint main(void)
% d- \3 R; v" G{ 3 V1 v; B0 K% l$ G+ \
int i;
' C- E: X9 C8 J# lclrscr();
* s# s1 Z0 E/ N% Efor (i = 0; i < 20; i++)
) U+ q5 N* i4 S& |cprintf("%d\r\n", i);
& U2 N" R9 d- L% d; \cprintf("\r\nPress any key to clear screen"); 9 S; D) }" p2 E
getch(); , J& n. `" c- t1 ]
clrscr(); $ ]: V, e; ?: N7 W. [6 k1 e
cprintf("The screen has been cleared!");
0 g* ~; C7 i4 u) ^! d& Ugetch();
# H; B# }1 o$ B" Y5 {. U {return 0;
a+ s+ v# v; L' V}
- f2 n6 r( k" c* s& Z5 R8 R4 u$ _+ u& c2 w. ^: U3 Q! r
. A' S5 h5 N7 s& S5 Y
! u" U3 m4 A) r" w. x$ m- c
函数名: coreleft
* z9 S( M/ h. p) d! F功 能: 返回未使用内存的大小
% U: ]9 O$ h, X; W8 N* i2 ?用 法: unsigned coreleft(void);
+ L! i6 ~1 k6 ]/ b8 P8 k程序例:
$ ? [$ z& T( U' [, Z8 M S#include
) X# f7 D6 }2 J( X! }7 { ?#include - Z$ } G3 Q; o/ K+ }- A0 k4 s/ `- v
int main(void)
0 N! O ~, [& D{ - N; o! q+ ^) n8 H
printf("The difference between the highest allocated block and\n"); 7 D4 } p& @' }* W/ z
printf("the top of the heap is: %lu bytes\n", (unsigned long) coreleft()); + q( y& T4 f8 f: P- W. J: q
return 0;
) N5 @) s! A0 n: d8 r+ L1 K) E} " h3 P" A+ X0 z; W$ ?! o. M' ], k
3 o @6 O1 t; b6 L% P9 }& L函数名: cos 2 ]/ t' X% V/ @
功 能: 余弦函数
1 C: R# F% J8 U; U/ ?0 B用 法: double cos(double x); * g; w& c2 z8 _2 D- \
程序例:
( q7 o9 s: o* z#include
$ }* V5 @5 `0 g0 `#include * t3 S* P3 A, ^# ^! y7 F
int main(void)
! [/ C$ Q$ I8 p' f9 N{
4 x8 [; D- D: M% R& [double result;
- ]/ B( n& ~2 b# r/ V7 ?4 ^8 P+ f# Gdouble x = 0.5; 8 m W2 D: B* t, O/ V, I
result = cos(x); ) v+ c. _. Q8 d5 s. ?: [& K
printf("The cosine of %lf is %lf\n", x, result);
' d: ]/ ^/ s8 _6 W9 oreturn 0;
4 A5 p# r/ k! h( I( \} , `; V0 e E4 V' y, C
6 ]8 E5 P, |0 B8 t
' n4 o' m9 _) `! q6 o8 {( i
# m8 s; N7 L( a# n$ T; s函数名: cosh
$ m6 J& X" C( r, z8 O+ c, F' o功 能: 双曲余弦函数
- I: P+ k5 ]% d0 j) e$ k8 M. U用 法: dluble cosh(double x); ' F* D. ^. m7 w5 _7 s1 m# y5 L
程序例: ' v! a! R) w1 k. p! V$ S! |
#include
% y5 R: v7 d* } }" z#include
4 O4 J5 S9 `6 wint main(void)
! x! [" j9 G0 C0 a{ 4 S8 H) G5 @6 x: g: G) T
double result;
1 J, n1 p" n) k5 {$ m) W% V/ fdouble x = 0.5;
* s" q6 ?4 F4 S1 l$ D% Nresult = cosh(x); ; O/ P# a3 G d% z
printf("The hyperboic cosine of %lf is %lf\n", x, result); 7 g. m" m& O$ f- }) A; i9 s: U" K
return 0; , L5 i& E/ |( Q* l# U8 h0 L, H
}
3 `. a+ N8 k/ i; I/ u) p4 l( U
6 g8 |1 n' A- ~3 N3 h: a" j3 O
- Q/ I3 ~3 h ^/ O ' Z# Q. _9 H1 W
函数名: country
3 m9 Z _# Q" {% t. N' ]& g: M% U1 \功 能: 返回与国家有关的信息 a, z6 O) L4 ~
用 法: struct COUNTRY *country(int countrycode, struct country *country); # D4 Z9 s1 h: F+ @4 T+ |$ s" g( ?
程序例:
w8 F8 Q% a; z2 L#include ' r! _ P. L- M# x
#include
7 p* ~& @# z9 ]' ^9 i& o#define USA 0 0 a. O( k" D: ^3 ?
int main(void)
' c( t, {% x' j- z{
+ q) F* e: [; \4 {: J/ Fstruct COUNTRY country_info; * P) _# L, Z1 Y6 H* F
country(USA, &country_info);
: w2 D% @8 J& J. V7 U V" sprintf("The currency symbol for the USA is: %s\n",
4 s/ a, A( `- Z W; S, Zcountry_info.co_curr); # Q- {/ } w! c5 Q: t4 I
return 0;
; y8 X: G K9 `}
! Z* C" v& V$ J6 m% L+ B; X' m o/ z$ l
/ I$ G3 L) x1 \+ v2 V$ X0 ~& l' c. U$ Z; o
0 o6 R3 a4 s9 |+ p函数名: cprintf ; }) Y# d4 ] U% H( a& N4 {$ d
功 能: 送格式化输出至屏幕
1 _+ u Y" P$ t6 u! _用 法: int cprintf(const char *format[, argument, ...]); - V. ?% b2 `1 ?
程序例:
# U A' T8 x W! B#include
- f. d3 e' ]- T3 C0 Dint main(void) / n+ [& R/ B5 h: _& f5 P% h6 X
{
; N: a5 e' s* N: M! H/* clear the screen */ % \$ T9 B! i' F: Z3 h" k
clrscr(); 1 }( u( S) E* L" O
/* create a text window */
) e0 e, {8 k& L) L& ~4 r- U/ ~- }window(10, 10, 80, 25); 5 |, r- a, U6 x- o
/* output some text in the window */
) P1 N4 T, q. _- q+ d* }cprintf("Hello world\r\n"); 7 _0 d+ Y8 ?; N, A
/* wait for a key */
* Z$ }4 B2 `, p+ Xgetch();
1 z j: G% o i: E [0 N' zreturn 0; / a: [9 S/ a' I9 R7 t$ k# |
} $ f# P, a% K) G1 T) Y
; U3 W+ y W% C! f
' @' y) z. E _9 H- F
% X4 Q; @, |5 W6 z. \0 L F函数名: cputs ' N4 H$ `: [( w) L1 O
功 能: 写字符到屏幕 6 N9 C2 p' N- W( L6 Q( C2 P
用 法: void cputs(const char *string);
4 Y X- k! p5 N+ U程序例: j: m, C0 M+ {" ~! G$ Q
#include
6 j8 D3 ~4 `& U u( F& y' i. l+ ~9 Gint main(void) " Z3 j: G/ M- Y6 l! |' P3 b
{ % R" b" j8 R2 P x0 b' V$ A
/* clear the screen */ 3 c' P( \- U9 t
clrscr(); - s* Q6 h# p" l3 ?) x% Z5 S
/* create a text window */
' V! Y2 s; H6 b: zwindow(10, 10, 80, 25); 3 f4 u( Y! o; f
/* output some text in the window */
3 y i4 j& z( x ~cputs("This is within the window\r\n"); / {2 P* k: r' g) A/ [% e
/* wait for a key */
& M; S* H! \2 j, o0 u- A5 rgetch();
" F* N3 j, O, o# M, @3 l4 _( ureturn 0; 9 P) U" g2 J5 l/ a; Z
} ) C: z4 P5 a. w, D
4 s0 `' t/ I3 @0 q% x
3 S2 h2 ^2 q l+ F1 n, n1 c/ T 2 X. N6 G2 l6 e$ B
函数名: _creat creat 6 S' [, | m+ V* T' @+ {
功 能: 创建一个新文件或重写一个已存在的文件 . A: q, X4 h8 l* t+ T7 d
用 法: int creat (const char *filename, int permiss);
( J% v& N# r, u6 F; {% Z$ v. P( _程序例: ' B* o! n5 A* H" C: J k6 X9 V
#include
" s& h- c# ? L: }#include . L1 u |( `: |! e1 u1 c, g
#include
1 O0 R+ j5 [/ |2 v#include
' l; A, J. |+ bint main(void) : c. B, r& z/ _3 y2 n4 h, @" O
{
# h; t. Z: w* @int handle;
/ C4 X- ?/ Z8 b0 V9 U/ ~( Ochar buf[11] = "0123456789";
. S) O" N: S5 F, V; Q; U6 i/* change the default file mode from text to binary */
; z( J. b' d+ U* `( L4 z- r0 ^_fmode = O_BINARY;
" {, U+ ~" O }4 ~+ l/* create a binary file for reading and writing */
: S/ _- l) ^0 {+ E$ }4 Mhandle = creat("DUMMY.FIL", S_IREAD | S_IWRITE);
. J0 ?1 l2 u9 l: l/* write 10 bytes to the file */
$ y: r. \& {$ @) i$ f: Jwrite(handle, buf, strlen(buf));
. O* z1 J2 b; |* y5 L. _7 I/* close the file */ \3 M1 `2 O! _( m
close(handle);
( O4 x$ s* N) \6 @, X3 J! vreturn 0;
/ ?8 S/ V \4 o$ Y9 t) ?6 p} & |. a. g: i4 X! e
: d) _( ^7 I$ k7 V+ K5 f4 r/ n
函数名: creatnew
9 Q( y' n, ?6 s H$ [9 }功 能: 创建一个新文件
7 R) b8 [' N4 k( L! w用 法: int creatnew(const char *filename, int attrib); - v% g. Q# u0 o- A
程序例: & t! @! _$ a2 n y
#include
( o$ V) w5 y! Y+ E3 k2 ^#include
; Q, g- e' U) f5 B) m p4 c% D1 A; L#include 5 q, ^1 u3 V& J( j1 v2 B' c p4 {
#include
+ Z8 l8 ]$ P! s! ^: \#include
g- H1 d- y3 ~* y5 _5 O+ Kint main(void)
# z/ f% Q1 [% L; \: J{ . L: T Y* D9 e8 {& ~
int handle; % x1 o" C8 Q( d
char buf[11] = "0123456789";
( p" t4 v: B- u" n/* attempt to create a file that doesn't already exist */
: J3 |# S$ c6 P+ N7 Lhandle = creatnew("DUMMY.FIL", 0); 6 c& n% y0 v S
if (handle == -1) & s+ P; Y0 c/ k- w4 E
printf("DUMMY.FIL already exists.\n"); 3 z+ Z6 P9 x; a% ]* M4 ?
else 7 P o& ~1 Y1 G
{ - ~5 y, Q0 {# J6 R
printf("DUMMY.FIL successfully created.\n");
" V0 }$ T, u3 t1 W# c! |9 \write(handle, buf, strlen(buf));
2 d9 {0 r) [4 M# T) Nclose(handle); - i3 _+ U0 X+ M. Z4 W! I n
} 9 V7 n5 B- e, c
return 0; , U( V* n: j$ E( F7 I
}
" K8 W2 j" f+ z- r. v: S: l' n2 t6 b3 t* M% y8 K) ~2 G, b
( Q7 S# V% }3 ~7 E
/ m' v8 l6 W1 B
函数名: creattemp
6 s, ]) M' \. H7 l功 能: 创建一个新文件或重写一个已存在的文件 : @4 s {1 D) z
用 法: int creattemp(const char *filename, int attrib);
; p* S7 k+ @* s: v6 \程序例: 8 U* r! p3 ^4 H% ]
#include
9 h7 S3 a7 q4 I& q#include : W a+ [6 h; ]8 F3 i$ o) ?2 z' C$ P
#include
5 {* g2 @& E- p* ^3 Sint main(void)
/ ~7 S, \* {$ k0 b& B6 {+ A{
$ r3 H& z* T& ?( gint handle; $ O B: X- F9 {6 |7 g) v( w c
char pathname[128]; 3 |( _/ W0 Q* w; ?
strcpy(pathname, "\\"); / T+ ?5 R, j d. l0 q) m1 d
/* create a unique file in the root directory */ 4 r& y* A1 A0 X6 r6 w$ t$ F2 O
handle = creattemp(pathname, 0);
4 H9 B/ |5 m, R9 s* L4 ?printf("%s was the unique file created.\n", pathname);
' B6 J0 R3 |! s# E+ k3 v# Zclose(handle); 4 I, X/ Y3 \* {$ Q
return 0;
- W/ u) L4 k8 F# S2 }8 j+ S}
- o5 g- W3 ]5 c7 a5 i* u( w3 a
9 v2 W" q4 E5 w2 Z. B
( Q( Y0 g K$ |! j! m
3 m8 h7 l7 m2 `, A函数名: cscanf
+ P8 r8 s8 s2 U3 W功 能: 从控制台执行格式化输入 2 N( m. P( }2 p, b# ]- L3 Z; t
用 法: int cscanf(char *format[,argument, ...]);
! j8 ?' v7 P4 U- K6 O. A# a程序例:
9 z; Q- V9 O0 ~' m#include
. u1 S$ X' E5 v% n& z2 Nint main(void)
% {2 B5 H4 x( `5 E+ l8 K{
, l6 T7 S+ `2 ichar string[80];
; ?5 F1 {1 J& B/* clear the screen */
% U" e; B9 C9 c6 w0 kclrscr(); : e2 w: ?$ E. x1 K/ r
/* Prompt the user for input */
4 ~1 J2 K3 E7 H7 N4 t$ g2 \cprintf("Enter a string with no spaces:"); 2 Z7 B) P, U, u2 H7 p" N" R5 r1 _5 j
/* read the input */ ; r. h( t0 X! E3 h
cscanf("%s", string); 1 F+ |4 ^' s4 ~& a }% u
/* display what was read */ 7 T& b' x& h" c- P- T8 t# L y
cprintf("\r\nThe string entered is: %s", string);
' ?) U7 \1 M- ?" E$ dreturn 0; * V' H$ z! b# a' e" b5 `! M
}
: G/ K: x- g9 v/ V* q0 ?7 p4 v0 O4 o/ D
" r% h) Q0 A$ Y. F* z/ b" \
& n7 R/ K9 F, b5 L4 W7 m函数名: ctime 1 ]& h! u+ _% i: D5 P( h2 C8 \
功 能: 把日期和时间转换为字符串 , ?. T/ n. e( j
用 法: char *ctime(const time_t *time);
2 f, ?$ S6 |5 T1 l0 [程序例:
& P" |3 m$ p. @: r#include 5 u8 m! W/ m& a
#include
- B# d2 A9 z6 m" Yint main(void) / N7 Y/ {$ d+ O' q2 C) E
{
! U$ P# a, N0 q8 F gtime_t t;
5 J* ]3 v$ F' p$ ^4 htime(&t); 0 K. k5 }. X" ? H2 M0 C; Z6 C' _: g
printf("Today's date and time: %s\n", ctime(&t)); 2 A* I# F4 ]+ O' R1 f4 u# _2 X
return 0; & p) A1 ~; m( [* T) \. U
} % M! z4 u! Z' ?2 v! X4 D0 `% e/ m8 ]( {
0 p# y. J7 M. t$ s1 g
: b, N& t6 Y; p& A8 @
+ V: _. r8 Z, q3 l- t函数名: ctrlbrk
3 D* I+ Y; F, I* Z% X/ T1 s功 能: 设置Ctrl-Break处理程序
9 D) s5 D W: N6 {用 法: void ctrlbrk(*fptr)(void);
8 y; U3 {8 M R X' \程序例:
/ L. n7 x* T& \& u: W#include 4 D/ h) u" Y2 ]5 X7 x
#include
& ^3 k1 g; j2 |#define ABORT 0
6 g% x5 R- f/ W; r/ R; Pint c_break(void)
' ?* } w1 F5 l{ 3 G3 Z& w4 T0 t4 B% r- Z" V
printf("Control-Break pressed. Program aborting ...\n");
: x) J" s- g/ V5 lreturn (ABORT);
& U5 _9 E4 K+ o9 f3 E V, Q: h+ F} 6 B' h, J+ _/ G- \
int main(void)
+ p6 C+ }$ a2 h# h D/ b( E{
& N* v% E1 j3 Ictrlbrk(c_break); - F, k' @' ], W0 j% R$ @' M& R
for(;;)
) U# G* s' P/ m( v6 i& l{ 1 `2 e/ ]' j5 v/ q
printf("Looping... Press to quit:\n");
1 t7 p" R' T. Y9 Y- T, }}
% Q' d. B! P- [' ereturn 0; ' t9 @# `6 S6 V0 s1 r
} |