函数大全(c开头)$ L5 X- k; @0 S* E! X7 S
% d5 A5 y) C6 K9 U9 q, n函数名: cabs
1 h0 C! ]+ f+ K5 ^; D6 k+ t功 能: 计算复数的绝对值 4 [/ r& _) O$ L; h) q9 P
用 法: double cabs(struct complex z); ( i# K5 a1 v; U( U K
程序例: 9 p0 m; k( n+ x9 J' {1 T
#include ' p& S9 o0 W0 U( N" n+ O
#include
' F5 R3 K( o* c' G; mint main(void)
) ^; k* h' f. Y1 H" t6 D8 M: J: q{
8 T5 G+ j. a# A7 L4 }2 ?9 }0 w' lstruct complex z;
! O E$ T: c" Z0 N- } P4 Bdouble val;
0 v& E* i% z0 l" C, ez.x = 2.0;
: i4 D3 a) n9 I O9 j/ M) p1 Iz.y = 1.0; " m# i ^3 b, @, Y1 D7 P+ h
val = cabs(z); # s5 F+ M: z$ b2 P1 ?. d$ o3 ^
printf("The absolute value of %.2lfi %.2lfj is %.2lf", z.x, z.y, val);
, p6 B1 H3 Z7 \return 0; 0 \3 I. h7 ~2 @+ G j
} " W) e% A& g W0 s
' _0 N# V$ `3 b% C1 y* d" h8 l x, c- _$ D
9 M( ]: W; h7 p* x% I- A1 L2 ]函数名: calloc
: h, v' v4 s6 `功 能: 分配主存储器 & b& u, M* |. x: ], w
用 法: void *calloc(size_t nelem, size_t elsize);
0 F/ y; L) q3 X程序例:
2 B6 s, U6 V) l#include
; H; K# T( I" t$ U0 O: k#include - u3 F/ {9 ~& _2 m6 }( U4 [2 }( r0 c
int main(void)
0 ^2 K( f: v* g! @. s$ M/ M{ ' u5 x6 e! l: S5 x' p
char *str = NULL; & Z: k; k9 a, E
/* allocate memory for string */ ) E$ q% }% [) |
str = calloc(10, sizeof(char));
9 c) C' D3 o: l( O/* copy "Hello" into string */
; G/ R5 ^" ~6 n9 {strcpy(str, "Hello"); ) `! P& L/ p: ?# B
/* display string */
8 A) K+ w) \+ E( kprintf("String is %s\n", str);
4 R( R( @' @; N) {1 y/* free memory */ ! b* N- ?/ y4 S7 e
free(str);
3 U% ?5 n9 }* P8 `return 0;
, i/ v: P+ v( d/ u1 p4 T}
! i6 S7 [) P0 ^2 `/ M% T
$ D/ y! Y, R6 B9 q1 [! Y% r R8 |$ D0 {; D3 _: i* q
! V( e5 F; f. F# h3 A7 ^( E/ n7 z
函数名: ceil
4 K( _$ \$ F, m& _. v7 F. M功 能: 向上舍入
+ r3 q: d$ P8 L; ^) t; A用 法: double ceil(double x); 4 f8 m5 h5 P- @/ B" M
程序例:
4 L5 F. b5 A, v/ V#include
$ E" i& C: B! j8 x, b6 O9 K- w4 p#include
/ @3 z/ e# n/ X$ G6 Y lint main(void) 0 i/ [% c) d- z9 N% ]0 `
{ - Q/ }7 d( h& E# S3 o: l& v
double number = 123.54; 2 z4 U! O0 a( w7 K/ U: r; ~
double down, up; 2 ~3 O/ g8 I5 u
down = floor(number);
8 U% I1 N' X0 B4 cup = ceil(number); {- j4 r6 k3 G1 F( F
printf("original number %5.2lf\n", number);
/ p B2 y) @% N3 Uprintf("number rounded down %5.2lf\n", down); . W G0 A: M# w: c1 k2 O# b- X
printf("number rounded up %5.2lf\n", up);
4 Q$ G! V) x% f r; ], nreturn 0; 2 e3 X2 A: h7 P4 t5 {" A
} 0 l8 r4 w) G4 Y) _+ N7 V8 ^
, S0 d/ d8 C. U" P% c5 r! s
4 U" S/ x1 Z* ]4 { ! N n7 N7 f7 Z! j* k
函数名: cgets ! F7 f; l# ~% A2 p& l8 w5 Q
功 能: 从控制台读字符串
9 T: H0 ~! O' n. ? a用 法: char *cgets(char *str); 1 V& Z9 O5 n* N8 w- w; C
程序例: + S; k% E' l) D
#include
# w* b. L6 }( Y#include % {2 j3 I! Z0 U" R* m
int main(void)
6 z/ ?& C* w7 i' w{
9 E3 T1 O3 Y$ m+ {& ichar buffer[83]; ; n' _( \3 _% N" W! c0 t
char *p; # @4 q/ L- _$ o. z, G# d
/* There's space for 80 characters plus the NULL terminator */ : m( n. M0 o8 y3 D9 \' O, L& v( _! ~# S
buffer[0] = 81; ; p4 g3 ]% m. Y( A; ^. K
printf("Input some chars:");
9 ]5 U* W/ ?/ @- j" ^p = cgets(buffer); # R; y2 m% K( e* ]- K
printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p);
3 L% V9 c- U, g% j: K5 Q, Aprintf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer); + s2 g) `/ k( j# O' `7 U& V8 p
/* Leave room for 5 characters plus the NULL terminator */ $ |5 K" V3 ^" l' N+ W P7 U d/ m
buffer[0] = 6; : r6 {( b( B! _1 S
printf("Input some chars:"); $ w/ i2 E% H! d! j5 L+ R
p = cgets(buffer); 2 s& H/ V# V5 m5 L
printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p); " y( V( r& w- V" e: r
printf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer); / S# [4 J: A' _% H( N0 V) ]
return 0;
( p4 R. B3 ^9 \- t4 T}
( }' ?0 ~- y2 t# B% H3 I3 \# w
: E/ m" X$ O* b+ T6 ?: H" y3 j" W9 [
6 ?5 l) X' S2 W5 Q0 i- t函数名: chdir ; D9 n/ Y0 C4 S3 z& O! h O
功 能: 改变工作目录 . a6 B+ F d) t
用 法: int chdir(const char *path); 8 k4 Q' F2 k9 | v; C
程序例: % o9 g) k; ]0 v' Q' N! Y% `' R% R
#include
W& k$ x W3 e#include
! y% [7 g4 R4 @# w3 z#include
% N4 \6 I% W' c, W1 Y/ P- d2 @6 Cchar old_dir[MAXDIR]; T( g% t6 F8 C, P' R# R( c2 {
char new_dir[MAXDIR];
5 B8 G- e- c" V" `" y& Zint main(void)
" i3 |5 M; G) V; i1 `: u{
6 i. ]( x( x' j* Hif (getcurdir(0, old_dir)) `8 Z* m& R( j) Z% Y- S5 o
{
. P$ `6 b: Y; R7 Pperror("getcurdir()"); * C+ v8 z2 X" K, w8 X
exit(1); - ~' j0 R7 Y5 X- X2 E7 t
}
4 D& B, s& O2 ]: O3 _printf("Current directory is: \\%s\n", old_dir);
' s' d O; J8 G* E2 iif (chdir("\\"))
% |/ K- f" _. ?{
! z- |/ f. h9 x1 `0 F4 iperror("chdir()"); |# t4 |0 w- F3 C' d- N
exit(1); 5 s% K9 c( t) D- S( B* [
} ( V. R3 A' M, X% y9 P: q
if (getcurdir(0, new_dir)) ' i: e+ ]7 k4 B, u( c: o. b% B1 m
{
: N7 `& w7 W9 U& _# i5 vperror("getcurdir()"); 8 J1 x7 g7 U$ Y( z7 F* n8 k4 P
exit(1); " |* d: q. e2 f. q9 w, @$ W: k8 V
} 0 S6 E3 C; I0 u
printf("Current directory is now: \\%s\n", new_dir);
; t# J3 s- E5 [) }% s* w, J' bprintf("\nChanging back to orignal directory: \\%s\n", old_dir); 0 r: ~7 u" w! R2 |
if (chdir(old_dir))
% s# t* x! F8 z4 k{
9 B* `5 q9 M) u( iperror("chdir()"); % A S9 c" x9 t$ y" p6 [9 h" G# x2 w
exit(1); 7 [1 D. G1 L6 x5 P+ O
} 4 j" t- |4 h+ g# ?! |
return 0;
5 b/ `9 m# ~- w4 W1 g}
/ P2 t$ O! w! |3 p
9 I+ }2 c) F- V/ r
6 G; B% K8 s9 b% Q+ ?& s5 | u函数名: _chmod, chmod 9 H' x3 `# {9 ]7 q$ l
功 能: 改变文件的访问方式
; `, u2 D9 u9 ^用 法: int chmod(const char *filename, int permiss);
8 X* h2 U# m) u1 X" K程序例: * @7 s3 F1 z$ Y! S
#include
. y S) h |8 ?) I7 I# ?8 d- v#include 2 X9 G. e$ U2 N) s
#include % v/ D! x- b8 n8 @( i, ~
void make_read_only(char *filename);
( o6 W/ m4 u0 K. ?, Hint main(void) 3 w) ]( ?! H/ x5 p2 \) C
{
8 p! o& S3 C1 f& g! ]& O4 P; Qmake_read_only("NOTEXIST.FIL"); ( h! K9 s/ k: d
make_read_only("MYFILE.FIL"); & s8 o/ L2 ?3 W8 j5 d, }8 c1 r
return 0; 4 h$ o( [ z- S5 C
}
0 \1 X6 l$ z1 Hvoid make_read_only(char *filename)
% \8 m4 z1 n3 d' Y: Y) | V+ [{ 4 T) M. w7 `, r# [8 R% w: L
int stat;
0 n: W; v) t! b5 c; astat = chmod(filename, S_IREAD);
1 q K/ A3 P& Yif (stat) % k% v c8 K* X3 r
printf("Couldn't make %s read-only\n", filename);
7 j( m, h6 d$ D, h8 ?" oelse
2 h. @1 J* k1 I! tprintf("Made %s read-only\n", filename);
( H: S1 g1 L( F: d; w3 A0 L- P" c2 o}
2 R: g, X0 z1 k
% D) I- a# I9 p' O( Q8 }2 W3 x4 N. s% `* L. e
. X7 l3 w, y7 x- ~函数名: chsize 2 R! C& [" U- r3 T3 y9 g4 M! U
功 能: 改变文件大小
1 Y V; F* Z) Y; n0 f用 法: int chsize(int handle, long size); $ U9 U5 Q/ p) s0 k0 ~& [/ G1 t
程序例:
4 b& R7 s% D4 i+ Z+ \' f#include - L0 q7 K, a6 b6 S2 N$ ]
#include
. c" p* X2 _# u. j1 n. E#include 9 C, Q8 v7 d: o+ ?7 H( D
int main(void) ; n; Q1 U a6 s( m9 X8 M' `
{ 4 e& ^# D$ U! [/ M: q* k, i" a
int handle;
7 ]1 R5 _9 F( J5 |: b1 Hchar buf[11] = "0123456789"; : b* s/ s3 X0 h6 H, f, \0 s+ w
/* create text file containing 10 bytes */
- S: Q7 ]) J7 `) F, i1 s: h# Vhandle = open("DUMMY.FIL", O_CREAT);
) l8 Y5 P% f* R" G7 @write(handle, buf, strlen(buf));
% q* J- {8 i& }5 D/* truncate the file to 5 bytes in size */ $ z: e0 U. z$ u+ n. o
chsize(handle, 5); / [: b; O) N m
/* close the file */
% f7 G, Y4 ^/ x0 N Zclose(handle);
- c7 h& M3 X0 d9 m4 x/ v7 v3 n1 xreturn 0; . W3 T9 R; o7 ]; d
} . _5 T& I: H# e6 p; L1 G5 F
# Q# Q" g: {' l/ E! n
7 _7 v0 t( v, n2 S# `
函数名: circle
% Z$ B9 a# \4 o- Y/ a7 I7 _7 E功 能: 在给定半径以(x, y)为圆心画圆 * p: }3 N% [8 S. [# R2 M& Q% e
用 法: void far circle(int x, int y, int radius); 4 \" j7 h& f) `& t8 O
程序例:
5 b) b6 A4 v, H3 y+ x: q1 r#include ! Q/ p S4 B: A5 [2 g
#include 1 y/ }, f' \# k; S
#include
$ b) Q; } B6 h# p#include ; X1 Z" O' X+ Q, W! Y6 [6 z( Q4 g
int main(void) " ]$ K9 Y s: B2 m
{
3 a& E3 P) A5 A: h/* request auto detection */
9 ?! K2 R+ `$ ]; }: p) k! |% M6 Fint gdriver = DETECT, gmode, errorcode;
8 g% S( p0 P# ]' Tint midx, midy;
' p+ l% ~- l: ?0 Oint radius = 100;
! j' A2 X( M# @. D" w# k/* initialize graphics and local variables */ # s+ L- e7 l7 ]/ `' _: \
initgraph(&gdriver, &gmode, ""); . P* T/ E. S- |8 g) I, \
/* read result of initialization */ " E' H' c8 x) A) @) {1 q; i
errorcode = graphresult(); - s8 v3 S" q2 X- @
if (errorcode != grOk) /* an error occurred */
M& y, I- D+ L* x7 _% K{ ; F* p) H7 Q/ V8 L
printf("Graphics error: %s\n", grapherrormsg(errorcode));
8 J# k* Q2 Q) h2 t7 u7 G( p2 Z2 jprintf("Press any key to halt:");
: g3 _* Q+ D ^$ n& c: U/ q- Bgetch();
* g4 r# d6 w }* T8 e5 fexit(1); /* terminate with an error code */ 7 G, X5 {2 D' S8 V. O3 t
}
4 h8 ]5 X, W# Z1 }midx = getmaxx() / 2;
+ b4 R) |, h+ K! ?midy = getmaxy() / 2;
5 }( ?% ^) B& H! ~setcolor(getmaxcolor()); ) Q; X+ G7 _7 Z8 Q1 }
/* draw the circle */
- N2 @4 H7 c5 p' Rcircle(midx, midy, radius);
. x/ B& h1 Y0 [1 b, e9 t8 W/* clean up */
$ A& {6 ^7 S! rgetch();
' U% r7 j! E; K' F, G! o5 C& Yclosegraph(); 1 s7 _! p/ Q W" R/ B; q
return 0; - Q) l/ o3 f. l& U
}
) a+ E1 U: A1 o) {/ c8 E5 k
* c O$ e+ U% }" J" V! M% r! a4 G( f% V9 s: L+ p; q+ R: N! ~
. a' P) E/ S( j1 e+ T2 E
函数名: cleardevice
$ \5 H8 N# W3 ^$ M1 ~9 m5 f6 ]7 A功 能: 清除图形屏幕
1 h' v. r" \% u- D用 法: void far cleardevice(void);
3 J. I1 |6 H |1 ~: _程序例: ; H8 n, F" D; S6 U
#include 0 m* q; ?8 g, a0 T7 u
#include ) g2 z0 Z! R$ f* l
#include
* m2 ~$ R3 E0 r" S( b+ M& K5 [7 o- G#include
3 }# e7 |- y" o# q3 U; r5 Zint main(void) 8 K# r$ U3 g4 q3 w$ j
{
1 a+ `, J: x+ k1 P! K' K* T) @/* request auto detection */
6 |, o8 l5 ~4 vint gdriver = DETECT, gmode, errorcode;
, G! r9 G* i: [/ X5 i9 {int midx, midy; s7 ^" s r& F% j: ^+ ^
/* initialize graphics and local variables */ ; c* c8 ^+ J8 R) @' L% G& s. k# t3 p
initgraph(&gdriver, &gmode, ""); 5 R9 Q. n3 l4 D0 V6 J( G% @
/* read result of initialization */
3 H# A& G' U! berrorcode = graphresult(); , n4 N' j$ A( N {6 k# d
if (errorcode != grOk) /* an error occurred */ ; w; t1 U6 }/ o- W. C+ B$ X! s, V
{ 1 T( X9 ?: X: t l `0 J% B
printf("Graphics error: %s\n", grapherrormsg(errorcode));
' o9 _9 i* o$ \; Q" z! Bprintf("Press any key to halt:");
3 J3 x0 W) r0 |$ lgetch(); : l% }* g. R/ x2 W
exit(1); /* terminate with an error code */ ' B8 Y9 _: r+ e6 s8 e# W! J
}
) I5 z0 P, _3 ? `- \0 |midx = getmaxx() / 2;
$ y0 R7 M7 H0 W8 O$ Zmidy = getmaxy() / 2; / \, ?) h; e. D4 A1 e$ p
setcolor(getmaxcolor()); . B1 `" x h6 `3 L+ v; G5 H
/* for centering screen messages */ : ~" c) U. K/ A, K
settextjustify(CENTER_TEXT, CENTER_TEXT); - C/ C/ @" A9 G& X1 G8 H
/* output a message to the screen */
) c: H. L( Z9 o' a: nouttextxy(midx, midy, "press any key to clear the screen:");
; M( Y9 z2 n r/ _. |# U* T/* wait for a key */ & b% d, @! ]$ w+ v& ~. e" v* d
getch();
, u* s7 T& z) p/* clear the screen */ # _7 m0 X# h3 x' t
cleardevice();
- k/ G. H" Q, }/* output another message */ 1 e/ f, H4 i1 r5 u3 v+ c
outtextxy(midx, midy, "press any key to quit:"); 0 y* u# }! n7 I+ a0 o
/* clean up */
5 h/ ]5 F1 r! G/ qgetch(); - o& r ?8 W* I& u4 M2 U
closegraph(); 9 |0 S$ ~$ u0 m: E. e4 }
return 0; 2 ?' \7 R5 T# _0 C# @. ~# ~
}
7 B8 v% b( W- A/ q1 g3 g
+ Z6 p9 U6 E" k! V9 p" `
$ r. D+ s' z8 b1 v 4 o# N+ C$ t) c6 N- ]0 _7 ~9 o
函数名: clearerr - {* V$ C2 `. @9 k0 j- ]7 ?
功 能: 复位错误标志 7 [5 E$ Z% z6 ~6 {- b
用 法:void clearerr(FILE *stream); 1 o% s' _* q+ u
程序例:
. {3 ~8 x) O+ J1 d#include / I& \6 U: v; Q! m
int main(void) 5 ^+ y8 N4 J$ s* s) ^: g
{
3 q8 [1 s% g, ^- rFILE *fp;
, ?0 N7 ~5 }5 f! Vchar ch;
! t8 G9 p8 V' H+ u5 j# Q0 H/* open a file for writing */
! X7 c9 c3 e3 J. l" o! }fp = fopen("DUMMY.FIL", "w"); * R8 k& e/ ]8 k' D/ n/ c
/* force an error condition by attempting to read */ ' ^6 k0 K. V% j! b9 A
ch = fgetc(fp);
0 _4 y" V! u, b* L% ~printf("%c\n",ch); 6 u4 f2 P v0 _4 e
if (ferror(fp)) 4 m$ `7 Q. O! j' Q3 t0 G+ F8 [& T! i
{ ; P7 ~7 q* U7 X4 a( [! g- ]
/* display an error message */ ) _" c2 {) R" d5 N- m
printf("Error reading from DUMMY.FIL\n"); " V. r1 G. m: a
/* reset the error and EOF indicators */ Z7 z3 R$ t; \: B
clearerr(fp); : v; n$ {& E5 t. J, A( E2 u. a$ c
}
+ u# _+ E4 j1 ?fclose(fp);
( A* q9 L4 Z. [* y) Zreturn 0; / ? A$ Y! u* U) E6 u/ I
} ) C e8 I1 n! v$ Y: M
$ a- g( V" T9 U2 w/ f' C7 k5 Y9 C. j7 w7 k1 b1 G
# r9 a3 q, L/ x/ g9 `函数名: clearviewport . j6 Z9 q" z! I( o
功 能: 清除图形视区 " t. ~5 E: n* [$ G4 f
用 法: void far clearviewport(void);
2 H; {' ^- @9 F2 k% y$ q6 H程序例:
+ j3 c% x. [ g3 ?#include / \& z) h1 @4 h! ?5 y3 z6 B h
#include + q, m! c* `+ z6 W0 j, L
#include
2 U( j1 P$ ^9 `5 r1 M#include 0 K& P0 a4 s7 j( g) k
#define CLIP_ON 1 /* activates clipping in viewport */ 4 Q: x6 u. T- W" _7 ]" @0 w5 \
int main(void) 4 ]7 q- v! k0 q2 ^2 T9 K! o `; \7 d
{
2 E( X, [$ K' w* L% c8 Z- H/ U! c2 A3 p/* request auto detection */ : U/ R7 r' [; b* y7 b8 u V" r
int gdriver = DETECT, gmode, errorcode;
+ I- K6 }* E$ P2 |0 D$ yint ht; 0 K d$ u2 u8 f" X; w
/* initialize graphics and local variables */
# e Z$ {' X% sinitgraph(&gdriver, &gmode, "");
& ~7 A. T0 Y4 e2 C% Q+ l: H/* read result of initialization */ 3 F3 z# u* d, A/ b# K" T' ]
errorcode = graphresult();
{( s, k2 l3 M' b1 lif (errorcode != grOk) /* an error occurred */ ; l! R( f* @0 ?
{ 3 c; Q: V. p% a; \8 ], k# w
printf("Graphics error: %s\n", grapherrormsg(errorcode));
, U) Z) F" M% ^. R6 Qprintf("Press any key to halt:"); " v. h* s. p3 ]# T0 z1 u4 b, M
getch(); 3 j0 ?0 {, g7 p! ]
exit(1); /* terminate with an error code */ / h# Z# `: f( b8 ?3 W
} * R# D$ `# O0 G+ f0 G+ I
setcolor(getmaxcolor());
$ R; y3 `" b7 D( } xht = textheight("W");
, A+ j& p* ?( ^( ^8 \6 d7 E7 g/* message in default full-screen viewport */
/ X# c* d# a2 s2 A# t/ Qouttextxy(0, 0, "* <-- (0, 0) in default viewport"); ' D9 }/ t) x) o0 S/ L
/* create a smaller viewport */ 1 r( }; n8 h2 N# C B5 c
setviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON);
/ A4 h5 R- I4 L2 ?( J1 _/ a/* display some messages */ & A3 Y0 V7 \+ d2 M% ^
outtextxy(0, 0, "* <-- (0, 0) in smaller viewport");
. ]0 V( X0 ]0 U% A8 h' k7 l" uouttextxy(0, 2*ht, "Press any key to clear viewport:"); ' V' a3 x! F0 |; j
/* wait for a key */
6 _( f( k, T% Jgetch();
" _) s6 e8 |. {7 ~: Y$ h/* clear the viewport */ N$ h" f8 g% y5 ]$ V
clearviewport(); ; {+ {8 X* y% ~6 X# i' c$ b
/* output another message */
+ W) h* J* T* C9 k0 houttextxy(0, 0, "Press any key to quit:"); 2 a7 H* j I, z. e0 t: y9 q
/* clean up */ ' ^/ w, k* F8 Y% P: J/ \
getch();
( S( K( A1 v, `* W2 `closegraph(); 5 f3 }; b) K" H" k* k
return 0; 0 v+ X+ S- L3 b/ Z2 i2 x( v7 M, M
}
0 T# Y) w5 J* E" k g5 ?# \2 V6 W( [) o# v5 l, ?. }5 H
" `+ O0 j! c8 f6 r/ B " H9 j& E# U$ r& b2 y$ ~
函数名: _close, close " Y5 s7 p! a$ b7 O6 a6 G
功 能: 关闭文件句柄
: F) k2 C) H$ X j B5 X/ [用 法: int close(int handle); ; Q. `5 b$ s7 X1 M
程序例:
$ ~% q/ z; I1 O8 D#include & k0 m* D9 _; \7 K X* n4 h, Q
#include ; C- L4 H2 V: r8 K; c0 B6 ]+ M
#include " J6 e2 P7 `3 e0 E4 a5 B0 _
#include 6 c6 }; J' a: R' u. n. }% l
main()
, C. B# V& h! l3 d3 K J& _+ j{
) l6 C) q8 X! N- f2 b' X: s$ G+ Yint handle;
5 p. f9 c: a3 A q* D8 w( t! ychar buf[11] = "0123456789"; 7 b& a0 p# {- p l, C9 `% v
/* create a file containing 10 bytes */ & c# t* E4 ^) J- w% g# T+ G
handle = open("NEW.FIL", O_CREAT);
% q' q" k8 T, aif (handle > -1) " c# B7 c. q' |, Q. P- o! y
{
0 q. ^9 g/ h$ u2 xwrite(handle, buf, strlen(buf));
0 s7 D0 y o2 ?- A L! b* `/* close the file */ : K, s8 H6 A; b: {" [2 L
close(handle);
8 K' b+ ]7 u& H} $ {1 |- v# D* \" `. Z
else
( `5 e) m: U( R+ J0 \' }{
. I+ F0 U! Z8 E, _: x# C- R+ E9 wprintf("Error opening file\n");
6 y* |& a9 Q8 Z! w& \2 I/ @/ w3 a}
/ P8 B% m" g' G4 treturn 0; ) m* {7 ^" _8 g
} 9 e4 t( v5 o4 J4 H% i
0 C' m# v$ O3 w/ {: Q- W0 v& r4 s0 {5 |9 C! a
) G, Y; ^1 d' @, W0 Z$ g8 t3 g* _函数名: clock
* ]( \# |8 p0 Z功 能: 确定处理器时间 / o( |, u# e% k/ S; _3 r* `
用 法: clock_t clock(void); # [2 T0 D, s* D# ], @
程序例:
9 U' @$ q- P8 J$ G9 I1 m#include
/ l$ I3 Y3 y4 |$ a1 p( R#include
5 j: P) S" R# I0 E2 C) z#include 9 b# K$ }1 A T4 D
int main(void) / d3 g, v# F$ O
{ : u; p/ z4 U1 C) _( i. w' ?8 c
clock_t start, end; ; ~" S5 o) R; V9 ^
start = clock();
. C% A8 L" p1 a6 S0 q" Rdelay(2000);
~1 f# e6 J9 @) l+ Y! _end = clock(); ; _' g- s8 t5 a1 z( j+ M$ x/ v: ]' m
printf("The time was: %f\n", (end - start) / CLK_TCK); ; R! d/ q: r7 U5 g- c4 a) n
return 0; 0 E7 B; D9 j8 O6 ~& w$ Q) f0 n
} - x5 F" u4 s8 n& B, m
9 U! ~7 g7 Q5 Q+ z" w' d. g+ _. H- u- `
1 @ L; D3 o* K z
9 B1 b0 k5 b' J" A/ p" Y
函数名: closegraph
+ ~; v6 V3 p6 c; ]! w* Y! Y功 能: 关闭图形系统
& l) N3 ]8 [5 l$ c用 法: void far closegraph(void); " r9 {) U. S# ]$ F. ?6 K
程序例: . R# f& D& a; W% M: y( P8 n
#include
+ w% N( P& p; M* d#include
& Z2 Q8 s" M% ]) V6 i; e#include / [5 I+ ^% @. `' D3 W; X# C
#include
" @. ]$ S W+ Pint main(void) * s# c. r" [% d% e4 S
{ / R4 U: ~7 i& d
/* request auto detection */ % j1 S# W: V k0 L& `
int gdriver = DETECT, gmode, errorcode; $ |( Y: |3 t, h0 ^
int x, y;
- P I( H: }7 [2 C# s/ `4 n/* initialize graphics mode */ " N; X4 f$ g* A$ f4 y4 y: e) ~
initgraph(&gdriver, &gmode, ""); 4 \' M9 E" J* |' `9 T
/* read result of initialization */ * K0 x7 W9 G+ X# p3 a7 b
errorcode = graphresult();
) d. C8 O: l4 S; S/ jif (errorcode != grOk) /* an error 3 B* }$ Q; v J: a
occurred */ * h8 a' m) J* o; k4 [
{ : r' V/ T! q+ r, [9 I; `
printf("Graphics error: %s\n", grapherrormsg(errorcode));
' {0 L# }6 Y2 ]2 a) z( nprintf("Press any key to halt:");
) i5 u. T. y6 g! `- p# Ggetch();
- Y' C! N: ?6 `exit(1); /* terminate with an error code */ 2 X0 K8 k$ R7 Q
}
) X! N( P" i R- y- p& i" D6 ax = getmaxx() / 2;
9 V3 L: I' P* O7 x: Ty = getmaxy() / 2; - Q3 }7 f0 m5 R2 G. M# R: {9 e, R
/* output a message */ " _8 r1 r" d1 F7 v4 E, @, V9 B
settextjustify(CENTER_TEXT, CENTER_TEXT); 6 V; T. Z9 v0 O) b' s: Q
outtextxy(x, y, "Press a key to close the graphics system:");
; |& |7 X/ F$ h1 V2 n/* wait for a key */
4 w* T1 L/ f0 a' f2 i( \% w3 ~- B1 Sgetch(); 4 D2 z$ t5 Y( D4 B$ z# Q) j
/* closes down the graphics system */
& W2 l! j8 Y. G/ }9 S( B0 mclosegraph(); . g @, ~# i c5 P Z
printf("We're now back in text mode.\n");
4 ^4 G) U. F3 Y+ Q0 Nprintf("Press any key to halt:");
1 S& f" G/ b* U* s; jgetch();
2 N9 J7 h4 T9 v& @- O* N- |return 0; * n% }& ?- ]! H1 r: q
} / a3 [! S2 _# Z7 @* i% @: q9 I
; h2 @/ ]1 S4 H, ? e5 W
% I% k( q8 M8 t; K( W4 N( R* w
E2 Z; u d# o; J$ }, Q函数名: clreol # d; Z% U2 @7 s. x ?
功 能: 在文本窗口中清除字符到行末
* g( o8 b) [1 D! I' k用 法: void clreol(void); * k, h+ r3 L+ z$ D& L
程序例: + K" y b0 w: {* B$ u# @
#include
4 ]2 F0 s: B; U# Hint main(void)
: J ~, D+ q6 h V2 Y D{
$ Q" ]' i% {# t& h, Q+ H" Oclrscr(); $ U% Z; a; Z/ S! K5 L/ @' [
cprintf("The function CLREOL clears all characters from the\r\n"); 8 H+ Q/ Y6 w6 J* T
cprintf("cursor position to the end of the line within the\r\n"); 8 ~, {6 }* @$ C" g) d4 o1 O
cprintf("current text window, without moving the cursor.\r\n");
5 a2 V- ~& p# I3 E/ B$ ^ Y% Acprintf("Press any key to continue . . .");
/ N1 _1 H7 e7 g1 D: Kgotoxy(14, 4); , C8 @: W3 O' O1 z6 T, ]$ U
getch();
* ~4 e/ h& `+ _7 ?6 j' g3 b* ?, F2 Z9 ?clreol();
. J; h; H. P+ s7 P7 mgetch();
( B5 G9 v+ R1 p' @return 0;
( x3 r$ k1 ]# Q# M} ; j! v/ y$ C! `0 S7 c3 V5 g
! Y u; H& `1 m" V( K: \2 a' ~5 a4 y4 Z% `; P
# Z4 F5 ~- s: m( j) D. E函数名: clrscr
; L: U& g% D6 ~+ I( ?0 I% H功 能: 清除文本模式窗口 : K* J2 E, c5 x5 i& W4 [3 P' Y
用 法: void clrscr(void);
, a I) ]# q/ |7 L4 M程序例:
" V# q: n' {( v5 K#include + k. ]- `! V! L
int main(void)
" e* O$ @3 ? ?{
& T) ~- ]; I- Y6 Dint i; , L! e5 v7 v2 h7 L& |
clrscr();
3 P0 _5 t$ l# {3 }/ E. ffor (i = 0; i < 20; i++)
! H9 _" @% o3 f! P& r+ kcprintf("%d\r\n", i); , B! m8 m- C- J: }
cprintf("\r\nPress any key to clear screen");
x- n, w6 U; tgetch(); ( w0 q* `' M3 v
clrscr();
4 z4 C5 A$ c9 m* Zcprintf("The screen has been cleared!");
4 Q$ [5 M" o9 Mgetch(); # n; w& a$ r* b! K" k# q8 K B: o
return 0; ) r" W3 X# B$ X2 ]) ~
}
7 s. q* q. O7 c3 J. J% \% k# a% [# x
- w5 w1 V. }" F2 r
: v8 B, S" \% j0 k5 ]
6 I* g6 L' N2 @6 O3 I函数名: coreleft
! k+ Z3 F3 k: t" n/ d, w2 e" U功 能: 返回未使用内存的大小 8 j0 `% J! u: z, z- A+ M9 g
用 法: unsigned coreleft(void); # F7 p! g& p% j6 U- u/ v
程序例: : D& e5 Z1 Z8 j, H+ B
#include
! A% h1 y" \6 j' ^#include
- d3 z: h, g: v0 zint main(void) / r2 d. b4 u/ r/ W& t _( ~$ N- i. V
{ " }& q0 ], r$ H
printf("The difference between the highest allocated block and\n"); : ]3 {7 ?- l) X* H# Z3 O* U
printf("the top of the heap is: %lu bytes\n", (unsigned long) coreleft());
9 P. N3 u! n# B6 i. o3 greturn 0;
) `4 @$ F D7 X, b S1 ]% C& p}
: D- n# l8 [5 N% d6 l : k5 r: z$ L/ E |# y
函数名: cos 7 K; Q6 i3 R; } n* T0 k/ }5 P* r
功 能: 余弦函数 3 M7 F- n# T8 A# D
用 法: double cos(double x); / k# O" C+ ] x6 |: L7 d& K
程序例:
, ]4 ?, z5 c% x7 z#include 5 R. T$ c O/ p- M3 D' F
#include
; ?! t* ^, ?2 b( b& Oint main(void) 9 r4 ~2 J; {! V) B& A3 p9 o
{
5 A$ X+ p0 \ l# g u; C+ [6 cdouble result; . O. C- [7 r3 B: u% z
double x = 0.5;
$ `; l( f u5 |. F: vresult = cos(x);
1 L/ B! ^6 w: ^# W% \, P* `printf("The cosine of %lf is %lf\n", x, result);
# [+ T8 X3 u1 k, u" k$ C1 Mreturn 0;
2 h8 C9 o g6 L" r t k) g} * ]/ O& { X% \9 D# q
2 l8 O8 K) ?; E" S0 _- K
* S; `: E& ^2 G" b1 B' l/ ~ - I$ \* w- @" M& p- c8 U# c
函数名: cosh
) r* Y) f' \8 g$ g7 Q; e3 d# r# G功 能: 双曲余弦函数 6 x: ?0 p3 X! E- k( u. W
用 法: dluble cosh(double x); ' |# m8 C/ f! K
程序例: 3 l5 e. _" f- g4 i' P) C9 `
#include
& U, c9 `$ o5 i+ l1 j& h, X#include - @% }$ [5 B. ?" O+ P4 _( }
int main(void) ; S' S A- C* n; E0 M
{
" n- z$ c0 O0 q. e0 N' Q0 L* p" }double result;
6 E) G# N5 k* Q3 r; ]( M& ddouble x = 0.5;
: a* `& L7 v! O: i' A' X0 Q4 Oresult = cosh(x); - M! e7 o; P& z. \) X" |
printf("The hyperboic cosine of %lf is %lf\n", x, result); 6 x+ H. o! v. G/ v
return 0;
4 R% w5 R6 r! z}
' P4 Q( o& b! ~! S J: ~
* P" E+ t" A+ X( ?, R4 `% S: _, d: c! p9 h
4 S& n! j+ T$ T# I函数名: country & c' S; q, A* S) C: A" q6 B
功 能: 返回与国家有关的信息 9 E( P# r# h% T
用 法: struct COUNTRY *country(int countrycode, struct country *country);
3 M' u7 j9 m; x4 ^7 g7 J程序例:
' C9 f8 w- k; O' ^, x' c& ~6 c#include ! V- x% m% i" m# B
#include + T! X/ I9 m }7 }& R
#define USA 0
" m1 }1 c- n0 u. _, A+ G' qint main(void)
! |7 Q/ I# i _3 q! _. y7 D{ 7 w6 ~/ b5 {- z C( s8 _! i
struct COUNTRY country_info; 6 n+ U9 Z; A6 ~7 E( q; G0 h
country(USA, &country_info);
$ S) f( Z! v$ i6 D+ aprintf("The currency symbol for the USA is: %s\n", 5 f1 z3 W# L4 z) v( i4 W1 ^' B
country_info.co_curr);
6 V7 H6 F2 f$ l2 [; A( ^( mreturn 0; 3 ?# P6 O5 g, P1 P' k9 p. }
}
0 W4 X: `! y6 ~/ c# @
+ m6 W$ ?! f t) p( ^/ f" M0 |% k2 m$ y3 {7 M
5 N9 j! N1 Z4 }1 R
函数名: cprintf
/ |, U, h) C$ G2 l$ c0 t2 \功 能: 送格式化输出至屏幕
: ?+ l/ z+ f# d# z6 _! R; r用 法: int cprintf(const char *format[, argument, ...]); % Z: H9 q7 g9 x. R I9 ~# \9 c
程序例:
& l, ^) Q* L6 C6 b8 _#include
0 ~! K2 }4 Y# s+ v& q! N/ {3 Aint main(void) / W) `/ A, h+ r* } _2 N1 w
{ - M! t2 h7 X& i' {* R! F
/* clear the screen */
; }6 b2 e0 y' F% R8 j( O' dclrscr();
$ K6 Z. |' w% u/* create a text window */ ; u, O5 W& S% f+ d
window(10, 10, 80, 25);
# U- P) V; M8 W9 q+ ]/* output some text in the window */ ; J* f( S2 m- x' n1 K4 R
cprintf("Hello world\r\n");
7 w$ ~7 Q6 o7 _$ v/* wait for a key */
. d, H4 a! v- m% K$ j, [7 o: lgetch();
' z7 u6 O9 M. z% y5 a) jreturn 0;
; N1 S( ~0 z# D1 U, y4 a6 h}
! S. ~* y# o& @; A
+ v( d% M3 H: r# t: v- D! S
! M6 m' P- K6 V( X6 s ; l* s; y3 X: i0 ^ W/ I
函数名: cputs 9 l; `: \( k+ ?
功 能: 写字符到屏幕
3 }4 l {/ |' X: T3 p, D4 H' ?" w, ]用 法: void cputs(const char *string); $ P, w1 O* G! j
程序例:
. {( f' }" G' |6 j- d# Y. ?) l#include
s: n; K4 t% B/ e. s0 j) S8 [4 Pint main(void) $ k5 z, e, e: l
{ ! K9 v G5 \. p* s
/* clear the screen */ # d C9 C$ ]& X1 L
clrscr(); 1 ]! I' [/ T& i3 ^) [; c
/* create a text window */ " e$ E+ e* Q) a K
window(10, 10, 80, 25); ; O1 [0 q. f/ d/ a/ Y0 B
/* output some text in the window */
, F. k! a+ |. w/ s" Zcputs("This is within the window\r\n");
% M# L5 R' V& Q6 B$ }1 S1 l/* wait for a key */
, C$ R3 @/ ]4 a0 B+ N; k lgetch();
, A7 a7 [+ E* [) P3 r/ l& b$ g/ e) oreturn 0; 9 c' V) e* r8 X) l
}
4 a, @7 T- M* A$ A. ]5 I: _5 c' Z. m' \. X! y! R
1 t7 d7 |) f2 D* b" F2 x' d # j. c. J c j& U2 e
函数名: _creat creat 9 J3 P5 l9 G# z) b! h6 r+ O1 m
功 能: 创建一个新文件或重写一个已存在的文件
. S" z) _+ v3 z6 R用 法: int creat (const char *filename, int permiss);
; K0 s; a- f$ S" q4 X程序例: / q4 Z: V1 m. @2 D, g9 f- @
#include
! {8 \1 m! ^+ ]: f#include
, C: Y* E- v% Z( x. X#include
3 j! y, |! O8 f; P. o#include
3 e/ s3 W* j) ?/ I5 w: V- _2 `) h5 wint main(void)
" \# l* Y* A( k1 V$ z{
/ z$ {3 P Q: Y s) }+ y0 Tint handle;
" S' K/ L0 y; @. Rchar buf[11] = "0123456789"; $ y7 t8 W# m! U( L# G$ ^' Y
/* change the default file mode from text to binary */
7 B, _( v5 _- ]+ }$ f5 h$ i_fmode = O_BINARY; 2 r: w: w% N+ j; S* N7 H" W M
/* create a binary file for reading and writing */ 8 P4 p, v1 I2 a# p' a- R
handle = creat("DUMMY.FIL", S_IREAD | S_IWRITE);
2 V. C+ Q) Z7 B1 j; {. J: O! x/* write 10 bytes to the file */ $ d" x; p$ ?: u: {8 U* ]: w
write(handle, buf, strlen(buf));
( o! ]2 N! R) L/ c- q2 r( n/* close the file */ & f0 f' A y8 N9 h0 ~/ ^
close(handle);
n" z w8 h6 e" r0 }! sreturn 0;
. w& r# E& |0 }. ^* l8 X}
- o7 J1 L2 y i& k% K. T/ X ' ^0 p1 e. U# e- Z. O; l9 n
函数名: creatnew
) V* ]; `" A7 F3 ~# q功 能: 创建一个新文件 7 B* b% ~% l; M& ]
用 法: int creatnew(const char *filename, int attrib); 6 Q3 x: W8 r+ a3 y* g+ s
程序例: 3 P" v( E$ X w4 u T8 j
#include
0 [9 f% W1 h% p# G; j! J0 ]* J#include
& L1 `' }+ Z3 ~7 k#include ; R/ e8 b6 e, ?: @3 `) H
#include ( @# t% b' W7 ~9 {! V
#include - V8 p3 c+ X2 a) e* K) H
int main(void)
3 f% D. Z5 Y5 R8 E1 S) A7 B8 x0 g{ 2 K: R! A9 ?7 e$ @; a
int handle;
3 d6 y& Y( C- Y( l8 \1 lchar buf[11] = "0123456789";
% y* V) ^/ L/ }1 f, ?/* attempt to create a file that doesn't already exist */
0 S. X: B4 x; w8 Z9 hhandle = creatnew("DUMMY.FIL", 0); ' l2 d; G V. _/ h. Z
if (handle == -1)
- ?+ ^6 G0 h, y* @printf("DUMMY.FIL already exists.\n"); 8 j5 e# A1 o! `3 T3 }; P4 Z4 x6 G) O8 |
else
7 |1 D7 d2 w5 E{ * [+ m$ r6 M7 q6 P9 @7 n! v
printf("DUMMY.FIL successfully created.\n"); , F9 N: ~; d0 C; t$ f9 j" Y; Q
write(handle, buf, strlen(buf));
0 L' d" U$ m; |: L1 bclose(handle);
3 x/ `& F3 x; o. E9 k}
' V: ]( Y- {$ p; D3 o+ C7 |return 0; # h& l# p$ I# q0 ^8 c# G2 T
}
, H5 B& s* |. u; D4 E) g% ?+ U- \2 n' E
, D8 I0 o* O. B9 @
, Y0 F& j5 o+ Q X5 M+ E& m函数名: creattemp - p8 w7 Z* H9 E
功 能: 创建一个新文件或重写一个已存在的文件
3 W1 {* P: t ]5 B: D用 法: int creattemp(const char *filename, int attrib); # S2 Z% c, J9 Z) |! ~
程序例: , t- F) Y! Q6 M- B9 O
#include
( k0 d/ J6 u0 h$ `9 ?$ O8 ]#include & M. F( h$ Y/ T. y7 B/ K
#include
) |4 g, \1 A6 m3 cint main(void)
- l. J7 a0 H/ L* Y) L) f' V{ - e4 g+ A% @( j+ [% p7 C- x% h; m
int handle; " `6 H( ~8 a+ {' E" a# x
char pathname[128]; ?" @' q/ r q# C
strcpy(pathname, "\\"); + [( R3 `! g1 a) a& T1 _, i: q
/* create a unique file in the root directory */
1 G( J ]# ~* Nhandle = creattemp(pathname, 0);
' a! }- U; B4 X4 _0 j* ]printf("%s was the unique file created.\n", pathname);
: a' {6 t2 p% ]close(handle);
1 r5 p1 E- X( ~* B- M- A% Q- I: o8 B4 areturn 0;
* Y+ b5 U# F$ ^( |} # e H; U- f; ~) o/ h; t: |
0 n ~/ m2 Q% b, u z% b
) N( o1 k- C) l6 K! T, _2 I - d1 l$ x/ r0 t
函数名: cscanf
1 S; g- }: R) E* j0 A6 l( j功 能: 从控制台执行格式化输入 ) y- p$ Z- |* W, Z5 z: x
用 法: int cscanf(char *format[,argument, ...]); 5 |" W0 O6 @8 \. R% x
程序例:
- \+ j7 n0 {; X% f' C#include
) T% K% Y- v" K' gint main(void) P( W3 |8 V; y |' Y3 Q4 C
{ ! W7 ]. x/ V1 e+ {( ]0 U3 ~
char string[80];
1 ^7 m# m2 m' V. ~/* clear the screen */ $ G' C# F% t! i! W" A: M4 U
clrscr();
0 v) q1 G5 [% t0 n y2 @/ N/* Prompt the user for input */ + ]$ O2 o$ T* z3 [
cprintf("Enter a string with no spaces:");
% ^* m8 \5 o! E/* read the input */
2 z) e4 q: X4 S8 [* Z- Acscanf("%s", string);
& D6 }2 A& e+ _, q* q/* display what was read */ $ n3 I1 ]. F/ {- k' p: N
cprintf("\r\nThe string entered is: %s", string);
, K( [. c4 X% i( l* W6 creturn 0;
) L" f! m: F( I4 y% w2 f5 Z}
; J3 p5 x! k2 B* N' m* c8 d6 w6 j: K, G2 r1 F4 @- G& B- g
( J) w/ T; p2 ?: f& H
s5 X2 P/ G6 e$ I9 N% k3 k1 ]函数名: ctime + _/ ~/ N) w& z
功 能: 把日期和时间转换为字符串
# H& ]5 A/ A4 {. _. }用 法: char *ctime(const time_t *time);
& D5 j5 E4 m4 I) W8 p1 |7 J程序例:
) Z2 V! x+ a% N4 Z) k#include
5 J6 {" v3 R$ K; B#include
5 N2 g, U) w1 c; P9 `7 H& k, [, ~7 lint main(void) 2 L8 W' o/ Q, L x( k
{
, B( v( \6 c" G! O a# ftime_t t; 6 l( V! D4 w! v6 x2 w0 t/ H1 m
time(&t);
9 j! d9 H9 u6 y% |printf("Today's date and time: %s\n", ctime(&t)); 0 d* l, g( \4 P4 k, X
return 0;
4 M) k; K$ _: g* v. f6 o}
0 J; N) s3 ]( u: v, O* L7 b4 `6 t( ?5 T7 A& p1 D, `* t4 @2 C }
- m3 y+ C \, K& t
2 P5 m/ S7 E. }: X6 j' y
函数名: ctrlbrk 1 m$ M' n! N$ _+ ^; }
功 能: 设置Ctrl-Break处理程序
7 s$ \- V7 g- M4 i. f用 法: void ctrlbrk(*fptr)(void); ' w3 ^2 h) H( u2 V q
程序例: 2 K" S/ D! }$ b2 r
#include 4 L' Y, R; |9 m7 V6 R
#include 1 ]9 Z u& P \' {5 p1 k' ~& [+ b! z" L
#define ABORT 0
7 s5 `$ U/ Z! f2 e/ m% pint c_break(void)
8 i4 j9 E d. N4 ~{ ( }2 j) e- T, W$ C6 }+ s. q
printf("Control-Break pressed. Program aborting ...\n"); * e' H) \4 p6 _. H3 `# X
return (ABORT);
% u7 |: F. V/ W# ?- {}
+ I! X- V. q* G& w1 l4 D# Iint main(void)
" [7 Y$ M: ?4 S! ^- l{ ( p. k3 G% o( p3 ?. t5 @
ctrlbrk(c_break);
/ f' \. q( \% [6 a, i; j* Y2 wfor(;;)
$ q i' c8 G2 k- G( T3 a' w5 P3 g{ ; _: c0 {! Y8 a- `1 w
printf("Looping... Press to quit:\n"); # g1 w* R6 b- [9 O4 ?5 h
}
' _, f/ x( U' C: n- ^return 0; 2 n" \# L8 z: o+ \' F2 v O8 H
} |