|
函数大全(c开头); `! q# A- P& M; I" n1 z
3 H, ~7 h9 m" K9 [0 q, D- L8 ^函数名: cabs 0 A9 v8 w2 ?# w* _6 m) n
功 能: 计算复数的绝对值
0 z2 _2 @: D8 u$ H, k/ X) f L用 法: double cabs(struct complex z);
0 O% ^' b0 j$ F7 [程序例: , l1 q9 i: |) A( S9 F# \ v' Q
#include - f( f& v6 N8 h. ?
#include
1 H% S1 }; }7 }- f" Wint main(void) 9 r, \, z; H& a! M
{ " p% q6 Q: Q, o" d( L, Q9 u
struct complex z; ' r/ P6 a5 b7 p) z" @
double val; . [/ q' f) Q* m% J
z.x = 2.0; 4 e3 N8 g8 p2 N/ |
z.y = 1.0;
4 V7 h) h0 D% c5 L: Y- Pval = cabs(z);
, F3 r' E9 K$ S0 H: _/ Xprintf("The absolute value of %.2lfi %.2lfj is %.2lf", z.x, z.y, val); 4 i' u w# v$ A8 |
return 0; " I$ q8 l2 Q K. y: R2 R
}
3 c; T q& i9 X, v* |, `- P! N; x/ e1 |( v
" ^- I7 }0 g1 s6 K+ m1 y
9 b/ t9 c A7 n( T- W函数名: calloc ! \" P& |0 C8 u0 g
功 能: 分配主存储器 ( Z( E% D* Q4 z4 p. [+ ~; J8 j
用 法: void *calloc(size_t nelem, size_t elsize); # b: e6 \) T" g1 d) X1 h) t
程序例:
' o3 W- A, E0 e5 J" ]" z#include
( M% ]2 @0 r2 v. y2 p' z I3 z#include
' h; R; q# x- Vint main(void)
1 O9 v0 o& D6 k8 ~+ i% }9 {$ V{
+ w% S; G) r9 O6 Achar *str = NULL; " i' k6 V; O( o. ?
/* allocate memory for string */
7 l) F5 B! M4 e6 nstr = calloc(10, sizeof(char)); 1 t3 O, T8 B9 d4 p! ~
/* copy "Hello" into string */
9 x6 r: p* j' `) Zstrcpy(str, "Hello");
) \- Q0 L. n6 e/* display string */ 9 C. n' Z) o( t4 I7 J
printf("String is %s\n", str); 5 o- B0 Q# A! c5 O0 V: r
/* free memory */
5 J0 g" ?+ j$ _9 A- P5 yfree(str); $ t2 v0 P) W. |3 |7 T: y
return 0;
/ F$ X0 @0 m I: F} ( h l' ?4 C! ?. [: }) L) G
* g, d( R# D( @1 @4 l0 j
6 n/ j0 i- z1 g' g # @7 w2 Q! a" E* P; n0 W
函数名: ceil * m! e7 o! q0 o9 B
功 能: 向上舍入
6 y. I% |# ]) u1 U! u用 法: double ceil(double x); ! N: C$ F4 c0 `$ N; }
程序例: / G' H" _9 [' ^1 f( \+ Q
#include
% Q& V2 C2 A% C#include
* d% Q8 t Y3 [! U* n4 wint main(void)
! T5 w: s; V2 N0 Z( v{ 9 q2 `4 Y. x/ J! o& C- E9 c
double number = 123.54;
- L2 d+ F. P6 {. t! V X3 `6 i- ^double down, up; 6 R- }1 b* {$ Y8 H. X X
down = floor(number); / z- q( k$ R8 w! E# |; I" h2 |& h: R
up = ceil(number);
7 Q2 y; W" D. z7 O" e& ~1 b, o; Oprintf("original number %5.2lf\n", number); 3 \0 M" f& q0 a4 [/ H0 r
printf("number rounded down %5.2lf\n", down); . Y8 G5 G0 S( g, N- f8 s
printf("number rounded up %5.2lf\n", up);
9 a7 t1 B$ x" m3 H6 Nreturn 0;
; s+ ]9 D3 h; t6 P. R}
0 T O: G/ z. E: E! V; O: V1 ~6 y3 u& {
; T+ |* v) \1 H( ?! E8 A5 \
( D" q" X+ J2 a0 \1 J7 `函数名: cgets
: p3 l' D% _" P5 @7 d7 z1 \4 ^功 能: 从控制台读字符串 8 f- |: I# a8 g& D o
用 法: char *cgets(char *str);
& Q# v- F; X: W2 P! w程序例:
0 ~* O/ l; Q: C* B) C* z [, X K#include 2 w: r, C/ U8 i% S. u8 R" X
#include
1 k+ Z4 Z0 F" c4 _ }int main(void)
4 q! O/ k T1 G- B" D{ / U6 H7 U9 l2 V# T Z1 A: z3 ~
char buffer[83];
7 m G" ^7 X" W. P; kchar *p; ) r9 T5 \( F' t* d7 S
/* There's space for 80 characters plus the NULL terminator */
4 E1 q8 x; I& p9 m2 q, \buffer[0] = 81; O2 f0 [* ^$ G! r( ]# F: }: ^
printf("Input some chars:");
( `! L+ a7 w. Gp = cgets(buffer); ( r+ _* ^# m* B& b' ^, ^$ O8 Z
printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p);
5 |' P% H0 _9 u$ N4 qprintf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer);
5 w! [, i5 P6 k# U/* Leave room for 5 characters plus the NULL terminator */
# z0 z+ `# y0 ?buffer[0] = 6; 0 H1 c: v( ]& J" E5 n# q
printf("Input some chars:"); : L% B8 a5 b+ U) X: {& U6 |5 O
p = cgets(buffer); 6 h4 `5 P: W+ F9 d
printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p);
' ~4 G% P- G8 m0 F2 ~' Q6 Bprintf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer); Q) L" x# Z! o2 h, {
return 0; 1 T8 U5 Z. }7 a2 A
} ' o, O; Z( `. |0 B; U! i' U
, ?- o6 L3 D( e1 C4 R3 O
* [, A+ P' P) F" a8 I2 d* u( x4 k & X; m; C1 Z) q% [
函数名: chdir ; m, ^% v3 h* }+ g8 ?
功 能: 改变工作目录
/ o0 w! a% r) v, R$ ^% n! c1 Q用 法: int chdir(const char *path); 5 i- l t4 j9 W5 q8 L: Q6 A- `
程序例: 1 k1 `, Q- L2 L$ q
#include - I4 ]/ g3 Q3 k8 i9 H' M7 H5 _
#include
" @+ v$ V3 |8 J#include & g9 y g7 R; i
char old_dir[MAXDIR];
9 h" C, }% _) t9 Xchar new_dir[MAXDIR];
6 k$ S/ ?5 x9 E- K+ ^/ T& @; \ ^# Dint main(void)
3 W I Z H) f# Y& P; x{ ! j% h1 C+ ~( O8 L6 N& a4 f6 d
if (getcurdir(0, old_dir)) 8 `5 A" }# |" S- A) P
{
4 N5 ~0 U6 V4 J% m$ P* U7 H" H Cperror("getcurdir()");
: M# r: C* `) j4 G nexit(1); * Q4 g1 L/ X/ Z$ A6 p/ Z
}
0 |$ e8 c4 i8 A. N( Bprintf("Current directory is: \\%s\n", old_dir); 7 Q. ^1 H; P1 E |' h) o3 p! N6 H
if (chdir("\\")) 3 }0 O6 c _* Z+ L0 f1 I& m
{ $ a3 i. {$ h7 j; `
perror("chdir()"); ! Z o9 W" z6 Q5 I, F% Y* B! R! m
exit(1);
: J5 \& n# _5 U5 |/ o3 c+ ^/ P}
$ S- S# k. U6 T& P% ^if (getcurdir(0, new_dir)) . t9 t1 T1 z) I- A+ {4 ~ ^
{ 7 C- D# |- g3 j
perror("getcurdir()"); , _- }* L! |. E5 a
exit(1);
5 P. d) x1 ^. b" `( w& X} 1 I6 }1 Q- u: e W
printf("Current directory is now: \\%s\n", new_dir); : d1 P* O" U; ^1 ?
printf("\nChanging back to orignal directory: \\%s\n", old_dir); # e2 ^6 M7 a. D0 q2 p0 D
if (chdir(old_dir)) $ v! W K6 q- U) |9 P' C6 Y- P3 p: m3 h7 {
{
! J' }0 \$ t2 M3 A* Sperror("chdir()"); / h( ^( k+ w$ T& A. x' ?$ g# t% K
exit(1);
. t% E9 s; d- n; F} 2 c* F+ a. ]0 ?7 s4 o C; u
return 0;
8 p. K4 g1 a1 h. _' s}
/ |8 Q$ Y) ~& [% I6 ]
& p. W& l1 z4 z. Q, b8 C- e 1 U( e0 N+ e: H0 i! C* k1 s
函数名: _chmod, chmod
s0 C1 y; g, m$ s功 能: 改变文件的访问方式
; d, l5 m/ Y7 h2 \用 法: int chmod(const char *filename, int permiss);
' B' Y- w* e! i8 U* a( ^: m程序例:
7 f" O! y3 H8 p#include
( f( ^3 ?% o- j6 I2 S#include 6 P3 V! |4 s: l, U2 y2 b
#include
. _- E: L4 g* d9 Yvoid make_read_only(char *filename);
- T I+ R0 `2 T/ y. p/ pint main(void)
: H- [/ H& `8 |+ V8 h{ 6 ~: f# U) n! Y8 h: t
make_read_only("NOTEXIST.FIL"); ' _- S4 c ^ i, ^/ f$ E+ A; t
make_read_only("MYFILE.FIL"); % q3 ?) d# q% L% Z5 R
return 0; ' t2 l9 L1 O; H" I
} + w! F0 H+ c% b( y
void make_read_only(char *filename) / }% @7 p0 B7 \& X
{ 8 t: J$ N& i4 H
int stat; , I, e3 W1 A- C. E) g; C+ t
stat = chmod(filename, S_IREAD);
1 n, q8 y2 m1 Oif (stat)
# C" F) @% k! b% n& Z4 F& C' pprintf("Couldn't make %s read-only\n", filename); . v# _- `+ V- k8 ^. d
else
$ r; g: K0 F9 | \' Zprintf("Made %s read-only\n", filename); ' }6 o1 \. S2 [
} & b `: l% B$ {* T6 [
, t" b' T2 U, E H& N4 g
8 t* l7 H6 U* ~- W( y$ l. ~
, t2 i6 n3 _% Z* R, s) T
函数名: chsize
7 ^- v4 N0 g* e! E {功 能: 改变文件大小
6 Y- Q) t4 R" p用 法: int chsize(int handle, long size); N w/ ^* D+ ~- B+ h, [1 u/ f$ R
程序例: # v6 E6 N4 v1 S4 Y0 @
#include
, M. s' }( D9 S, X#include * ~- L. E. h/ d- E! F# Z
#include
1 v' _! L- o2 u. e: k/ g- H Bint main(void)
. L9 G, }/ g+ }! X4 q" T' I{ % c$ v, A7 j, F/ F. ]! Q
int handle;
9 @" U. l9 J. q8 zchar buf[11] = "0123456789";
# F3 r) ^- P" `. [# n/* create text file containing 10 bytes */
( y" V( M" v3 I2 }" a9 L zhandle = open("DUMMY.FIL", O_CREAT);
, ^- C+ i1 B1 f, a) R7 D% k) Gwrite(handle, buf, strlen(buf)); 8 T. }* u7 u O5 T! R, t
/* truncate the file to 5 bytes in size */ " P/ _# O/ p4 i
chsize(handle, 5); 7 L- l1 ^* m0 D/ J4 A
/* close the file */
2 e/ N" W9 o" @; |( L$ N+ kclose(handle); 9 u/ |7 I. _+ o, D" P( a, p$ G5 @
return 0;
' D1 b2 c7 b& S" q4 \5 a} 2 f5 L8 q) F! l& x3 t# Z3 \9 `
' x, [0 D" e) A! _8 p E! w, ?0 [
4 T7 {/ Z8 f8 B0 k# \函数名: circle , h, P8 N/ X% F0 n z# d: f
功 能: 在给定半径以(x, y)为圆心画圆
" e2 }% H% w4 K& ]& x用 法: void far circle(int x, int y, int radius);
8 ^' p9 L1 e' ?程序例:
1 ^/ a0 g' Y$ Q: D#include
- E9 C5 M1 t/ x, U) a#include
& l3 D5 V# n6 H#include & h2 g8 H! a* R" k/ ]5 E
#include
2 A* d0 P* z* g% r$ Mint main(void) 6 N( z: A4 F: d, J$ g/ d5 Q
{
4 c1 {7 }. G8 W' `/* request auto detection */
$ X9 D% g& l6 u4 B/ C8 W# qint gdriver = DETECT, gmode, errorcode;
$ E. G. r3 h) [% v$ ~5 u8 X: `int midx, midy; 1 p% A/ u8 D k2 V0 @' S0 E2 K
int radius = 100;
7 `0 \8 x- V1 j9 O/* initialize graphics and local variables */
1 T4 Z" {+ [' e" y3 _) Iinitgraph(&gdriver, &gmode, ""); $ d# `6 h& B# K2 M- B
/* read result of initialization */ ( q$ p2 T: Y5 v. g- e0 ~- T
errorcode = graphresult();
7 h8 x \2 ~; m) \if (errorcode != grOk) /* an error occurred */
- u6 c0 E+ L. T- k{
+ }- _$ u+ k6 Y$ O/ D* ^; iprintf("Graphics error: %s\n", grapherrormsg(errorcode)); # p0 j' U% u& r. ~! X- |4 L
printf("Press any key to halt:"); ( c7 z2 |0 k$ b2 R& J
getch();
) ]& ?+ p$ G2 U: j4 U. K6 lexit(1); /* terminate with an error code */
1 k' `: B* ^! O$ G} " d8 I; q6 {9 k2 H9 N) @0 O% |
midx = getmaxx() / 2; / t7 x& I2 z0 y. _! E
midy = getmaxy() / 2;
2 I1 h$ I) ~1 _# D: Tsetcolor(getmaxcolor());
: a' l' I J/ g7 q5 l/* draw the circle */ ; t m; A& R1 m5 F% V8 b4 K! Z( E
circle(midx, midy, radius); 3 [% o$ X: Q4 x& e
/* clean up */ ( J+ Z3 B; j2 q4 j- X) z w& a! f. M
getch();
+ _6 v6 m2 [3 bclosegraph(); / W. e* o( `# {+ H+ a- c. ~
return 0; & s. c: Y$ R+ z6 `1 Z2 @( _" r# l) B
} : Q- r4 G" S, V! X: U
7 [1 I4 M' H" m2 T# k) w- b- |" e. @$ C( J
5 L% b" @( e3 n4 s9 v% o* g: E
函数名: cleardevice % {$ I8 L+ G/ d# ^
功 能: 清除图形屏幕 2 a) @7 f) i' k1 n4 t( P( @ T
用 法: void far cleardevice(void); * D( e. u! _6 F3 `
程序例:
. B! D- \ V5 ^- [( v' i9 g#include
: s6 `( ~9 i/ u5 k#include " u/ P7 P& V: i& U1 j/ f. `% R# W
#include / c9 S% r" M6 s
#include
. r2 ~0 ]$ {0 {+ O z8 r' E7 Wint main(void)
. F. s' _6 U Z4 B$ d* t9 [{
+ v. X* s5 M/ z& Q/* request auto detection */
1 E. @" a# F& E9 A1 ~int gdriver = DETECT, gmode, errorcode;
* }) v) H$ p' q9 e; H, E+ T/ A F3 vint midx, midy; # h" k( y& E( Y7 l7 G
/* initialize graphics and local variables */
9 r! y1 ~4 a0 Hinitgraph(&gdriver, &gmode, ""); 7 E0 s b! d+ N, J( n
/* read result of initialization */ / P. A6 c7 E+ r
errorcode = graphresult(); $ f8 b) i: m" q# v
if (errorcode != grOk) /* an error occurred */ 2 H8 w! ?6 | J% |3 D8 n
{ H) o, ~# w# r4 n1 O" C
printf("Graphics error: %s\n", grapherrormsg(errorcode));
- b- ?+ `7 e. c* {; n! r2 Uprintf("Press any key to halt:"); 8 \: m1 L: I) J% i: S, {* g
getch(); ! J; ]! n* K1 M" B
exit(1); /* terminate with an error code */
" s4 C) c g" B) S4 k2 L n0 G}
+ g2 X& Q$ ^7 e) a- q: mmidx = getmaxx() / 2; ) V9 h Y5 Z0 O9 \6 [1 M' g2 F; Z# V6 \
midy = getmaxy() / 2;
# g7 I6 |" P B" a8 |2 {setcolor(getmaxcolor());
8 h [: \" S9 b4 g- @* J! l9 X/* for centering screen messages */
: E) v* z' e! x# z5 S. L5 r4 asettextjustify(CENTER_TEXT, CENTER_TEXT); " A% y! Z& r# Q7 L. \' i1 \
/* output a message to the screen */
b5 y& ?0 E' j7 X1 z: p+ t7 eouttextxy(midx, midy, "press any key to clear the screen:"); ( N& w# l2 H5 e; r
/* wait for a key */ 9 O( b7 F; L& b; @6 o
getch(); + }9 k- i4 w6 H5 ]) N6 j/ q
/* clear the screen */ 2 v, ~ i z7 s8 Z0 o
cleardevice();
/ P* J8 v4 e. m/* output another message */ $ N2 c+ w( M1 |( R* D4 ^
outtextxy(midx, midy, "press any key to quit:"); , }* I) G( V% o! b, w: g
/* clean up */ / O5 i7 M% S( b% ^* X9 r' t
getch(); 4 L$ L8 s% M' S+ P9 n# n$ W- B
closegraph();
I( Z9 c) v/ O2 s$ O8 X5 Ireturn 0; ; w' Z1 b1 N/ ~* T* X# b# i
} + Z$ Y {7 O2 e+ q; n# N6 |% d3 Y
: C9 _' @ S6 S( E# F
4 W( D! X- }: B7 u& g
6 [- f( w7 Z5 N: d. e函数名: clearerr & Y: K' \* ~5 Y6 h+ B/ x
功 能: 复位错误标志
; L# Q( p# Z6 ]用 法:void clearerr(FILE *stream); $ M3 W8 r2 b: p5 e
程序例:
0 Y7 d; Q9 H I% j! A* K. L6 j#include 1 d$ S3 ~- S0 Y% I* a% N
int main(void)
8 b; X* ]8 D8 r! {{
7 Q3 U; v0 H3 f) R/ R( ZFILE *fp;
+ m- Y6 ?5 r# p0 l5 gchar ch;
3 O( M, q. g! }# ?9 v3 Z/* open a file for writing */ * Q4 [ T- R+ S9 j. l# g
fp = fopen("DUMMY.FIL", "w");
0 J- t/ Q3 v& p/* force an error condition by attempting to read */
" A1 |* ]) F0 ^# N( N2 |/ d4 ych = fgetc(fp);
" j1 `& f( q" m- Z3 Y2 mprintf("%c\n",ch); % H( |0 T& q1 h" _# g( @( r$ p; m
if (ferror(fp)) ) r* J2 A- }5 B/ n A
{ ; Q, e, V* h) {
/* display an error message */ 9 V2 b2 ^0 C) |% \3 }
printf("Error reading from DUMMY.FIL\n"); / x8 u4 `7 W5 r- s
/* reset the error and EOF indicators */ ' ?: I5 g1 @) ]4 M
clearerr(fp); ( g! G7 M+ ?# r5 f
} . \+ J, p! \) ~2 t+ \" g
fclose(fp);
5 S2 } m9 v/ |; `8 greturn 0;
f, q6 h1 [- h! d6 ^; W, w} ' |$ z. Z' F: H' R# _
$ U; Z# ~+ J4 \5 Y6 q5 P2 i. e. U% ^ }# B2 b2 V! ]' {
' N; M) U: e' G; K! F2 r* ^函数名: clearviewport
! P* v6 H' A; b0 ?6 a! d功 能: 清除图形视区
3 \1 c# M3 z. V用 法: void far clearviewport(void);
; B- j1 C' @% |3 N3 r6 \% V, b程序例:
! C' `6 d# n1 h#include
/ \7 D: ~- |' I" ^; S#include
0 s4 `, |: a* f. s0 w#include
( I9 y# s5 L7 y5 J; c& T#include + N+ U3 n3 z( z' k/ Q, f$ B0 c; L# [# b
#define CLIP_ON 1 /* activates clipping in viewport */ ' p; ^' o, ~% b
int main(void)
7 I7 @9 w0 V" q7 h, m( @{ + T2 o! O1 w' F9 q7 P
/* request auto detection */ # U0 S* [" r7 B# T5 H
int gdriver = DETECT, gmode, errorcode;
}' a, D. n# d4 T) Y0 Dint ht;
! A, S& g9 o2 n" E- O( D/ } Q/* initialize graphics and local variables */
* M1 u4 _, F) l( g' s: _initgraph(&gdriver, &gmode, ""); 2 j5 h- C6 Y1 A/ M V1 {5 l
/* read result of initialization */ % ]0 ~' k5 R' |6 f, E
errorcode = graphresult();
; y( Q5 V! A0 N0 h* i* @4 Vif (errorcode != grOk) /* an error occurred */ / u- Y! _4 [* b4 x" [$ n. c
{
+ s9 \- n+ j+ m- w, lprintf("Graphics error: %s\n", grapherrormsg(errorcode));
& V; x8 P4 V# p# @; o- R$ Vprintf("Press any key to halt:");
- X! r2 Q# A* n) y ^- Qgetch(); ! u2 W$ a! q! ^
exit(1); /* terminate with an error code */
+ ~' `/ L/ f- |+ ?( p3 L$ I} 8 v( H5 B) ?# O+ |6 h1 G+ y
setcolor(getmaxcolor()); 1 H: i6 F, \4 p8 ]$ X: r( A
ht = textheight("W");
8 W2 Z& m1 U! l/* message in default full-screen viewport */
) P9 W" f! T* e" L I0 uouttextxy(0, 0, "* <-- (0, 0) in default viewport"); 9 |6 L) {/ K9 i2 q1 H" s. x7 }
/* create a smaller viewport */ e6 p. _1 W4 F: Z
setviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON); . ]2 i" i" V( t. M7 _# Y, ^
/* display some messages */ 1 ?" z: S) o4 g$ p1 w
outtextxy(0, 0, "* <-- (0, 0) in smaller viewport");
" D5 @* }9 o' s, Iouttextxy(0, 2*ht, "Press any key to clear viewport:"); ! a. P4 L A; P4 F- P' ?2 I4 [
/* wait for a key */ ( W) n( r. e7 V- N9 ~% L
getch();
% m a- _ ^1 V4 R; J/* clear the viewport */ # D2 Y; e7 l$ e* N" [* ^
clearviewport(); $ U" B0 `8 f3 n9 C" p
/* output another message */
2 X$ m4 D+ n$ f) Louttextxy(0, 0, "Press any key to quit:"); [4 \1 a9 J" {( P _
/* clean up */
; y5 b7 @9 H' s5 p/ c8 qgetch();
+ C+ j8 z/ D+ D# Uclosegraph(); * ]1 R0 c& S/ g: k$ z$ y& w3 r$ N
return 0; 4 ?7 F5 i. g% H ^. N/ Q
}
7 G C h( V/ o F& z+ J1 w2 `% X* T; c
4 x) h) J- l1 B S# g/ c
4 T' g% j! [, c: b0 l函数名: _close, close % b* I7 j4 z- f" j1 W# m
功 能: 关闭文件句柄
/ w+ B9 G& L2 w$ \/ ?( M. Y用 法: int close(int handle);
" B6 Y! N) h- r程序例:
8 f- @2 o( o/ |3 l/ x% x#include
3 p" u3 R o O V. N* ~$ }1 p#include
0 K, b2 l1 ]. d+ M" ?- Q' {. M#include
# Y# e. ?' m: S P. c) g$ A#include
+ C1 u j7 s; \, O% Tmain() . f# y. v8 F$ f1 j
{
: z& y, Z" [7 l- iint handle;
5 h( s n* O7 Tchar buf[11] = "0123456789";
. V+ U I7 O, Q- p/* create a file containing 10 bytes */
: @' b4 R4 ]: A' Z. I# Fhandle = open("NEW.FIL", O_CREAT); . z5 {. N1 Z$ J9 x
if (handle > -1)
! ^) k& `6 O( z; U{
) W& Y9 B& J: `, R$ D3 wwrite(handle, buf, strlen(buf));
/ y+ Q# b' m' N! U- T% W/* close the file */ 3 R" h6 w8 `: p4 H* ]' m+ R$ Q) o) r
close(handle); & B5 c) d5 G' j
}
4 s$ p5 y+ I$ E. k" Gelse 4 M+ W. K5 B, f' J3 }' d4 f
{
: N r8 T, ]. \. e7 Nprintf("Error opening file\n"); 7 f, f& j* @) x' P8 L3 [
}
0 M" k8 |( O/ [0 p" k' _* Hreturn 0; ) D3 @1 D$ {% Q0 ^
}
8 T6 d# V7 T. I' U% q) r$ \
4 w- |! i3 h$ G) S; ~, `3 D5 a5 d7 V8 o8 m) L
9 m0 \ F- U F
函数名: clock / S* k# a' U8 g) ?
功 能: 确定处理器时间 5 R, |! ^, f. `9 d% E
用 法: clock_t clock(void); 4 M) E" i$ ]% T- p5 @, P
程序例:
/ }' I, F- `8 T- s/ {. T#include 0 e. y' p5 q i, V" k
#include
# t; Q# x$ [" E- y7 @& _1 R#include 0 O$ X( U1 ` x! M- v$ _5 s0 a7 n: [
int main(void)
. U2 l$ S! k0 l! ?; D Q" Z9 _# M{
5 I6 Z8 Q% N( s; k5 lclock_t start, end;
# S! n6 K4 g& t6 O0 a1 cstart = clock(); , I# a) t% d" H, r" O) X
delay(2000); - _" t/ R1 w# C% r" V
end = clock(); ' }$ J! c- W2 J. q/ l: j7 z
printf("The time was: %f\n", (end - start) / CLK_TCK); ^% V/ e& ~6 }
return 0;
+ I, O2 |: e6 m} ! A& d0 x) \6 N* }, P
* W2 U0 f, N, ~2 E7 ?( ]1 q
0 v7 ]3 C' z8 a K8 q _& ` & j7 E% w4 U3 R( N1 K# g4 I
函数名: closegraph
( z5 r" | R! a% }功 能: 关闭图形系统 ; D; A8 }8 T2 J% G
用 法: void far closegraph(void);
$ s$ Z4 i ~8 X6 D& ~' a程序例:
% A" v) u6 u& Z! R#include 6 b9 `8 G+ P. x4 l
#include
$ t1 L9 p1 |- [' i& t#include - s% O# n' A" z9 F3 G9 b
#include $ v. F' j1 ^8 I! Z
int main(void) / s. t" p) U: r8 X" J! n$ _- _- B
{ : M3 t& `) l4 R7 u3 J( C
/* request auto detection */ * R, }+ v) s0 n' }
int gdriver = DETECT, gmode, errorcode;
* L% u) T6 t6 ^+ ^& v5 jint x, y;
; }6 s0 y( I; ^/ U! }/* initialize graphics mode */ 9 Y6 @+ Y/ Z4 C
initgraph(&gdriver, &gmode, ""); ! Q7 }0 l* h1 @- ~7 P' a
/* read result of initialization */
1 Q' w& B) n1 @errorcode = graphresult();
: t4 n. w/ i% i5 O* `if (errorcode != grOk) /* an error
+ g+ P/ Y" i! \1 poccurred */ ! ^+ a; i" }3 ?5 t) L- o
{
d$ d7 E% T( L( D* D4 |$ oprintf("Graphics error: %s\n", grapherrormsg(errorcode)); - M4 x# [% M# g2 C5 N1 x
printf("Press any key to halt:"); N; c6 e: t5 l l4 \/ ~
getch(); , p; H2 k1 ^, e* _
exit(1); /* terminate with an error code */
; `% I: m3 \' }; z8 m! B* P}
1 b+ L# S5 V- F9 ?1 y5 ]! S# kx = getmaxx() / 2;
. z3 ~! { E7 M% |9 |; Q4 Uy = getmaxy() / 2; & P. r$ g* F- r. B
/* output a message */
& [7 j% f! |( [- s Dsettextjustify(CENTER_TEXT, CENTER_TEXT);
/ Y. g6 `5 I jouttextxy(x, y, "Press a key to close the graphics system:"); 2 {- ?7 C# r- j% ^/ S- g
/* wait for a key */ 6 J8 }0 S$ I3 _" i6 `$ {
getch(); + M' G- P! ?* X0 d6 ~( n( V
/* closes down the graphics system */ 0 E% r7 ^; J9 N
closegraph();
# ^* F' r/ Z1 |2 N9 V/ cprintf("We're now back in text mode.\n"); ; p; D, Z* J$ t) d- U! z
printf("Press any key to halt:"); 0 M$ S, F1 O5 r9 C7 M- j- j
getch(); 7 t& g4 Z# D1 r4 a' q* A7 Z
return 0; : o: N9 a$ H2 L
} ' u ~ b) R5 L( X4 ~, i" W. n5 u
9 S. j4 x4 Q8 b. U- C# t# m) O0 F
; l; C# y& w+ [9 t, W' J
' x8 z% f* o8 e4 K0 H0 q% v
函数名: clreol
: P2 A! f& ?/ Y功 能: 在文本窗口中清除字符到行末
- f2 s$ A' G; s" y! }用 法: void clreol(void);
( I# Q" X5 f2 K4 v) o: t# G8 `程序例:
9 c. T/ @6 i. e' W& B4 j& Q. i#include . R2 q, g5 v4 c! e
int main(void) % e; ~. i3 A+ k( p
{ " O2 A. t3 b9 W, U9 ~7 K) V: d1 C
clrscr();
0 P6 r+ W6 n4 I# N( O K& F" icprintf("The function CLREOL clears all characters from the\r\n"); 1 V8 d% c9 p1 m0 @
cprintf("cursor position to the end of the line within the\r\n"); ; b# p# A- E# H/ \
cprintf("current text window, without moving the cursor.\r\n"); ( g! B! h+ b; q6 `' [5 M/ s
cprintf("Press any key to continue . . .");
2 b) R, i/ x# ?5 {; ~# \gotoxy(14, 4); ( v+ f& p8 Q) M# R/ _
getch(); 1 L) f P' i6 ]# C
clreol();
3 ?4 t6 r$ V) vgetch();
# g) T$ }* Y) `return 0;
. T# i: C. M. e}
1 w! X+ M6 R5 t* N8 u5 E8 V, j3 J7 Z
( {0 @2 g) @( r% c! J
) u- r: ?6 \* Q6 m6 u1 f
函数名: clrscr
- t5 W- v* b$ O( n1 H功 能: 清除文本模式窗口
" v, c/ {. g, N S0 J0 I用 法: void clrscr(void); , Q# ]' f* m/ w" `8 I
程序例:
& @% ]( I1 Y$ ?#include
! R* p: e, N2 C; qint main(void) ) D; E2 M A! `* r, Q$ }& {& [
{
t* w5 T7 q/ Gint i;
& l1 q$ U$ I$ t. o/ m0 K u- Xclrscr(); - ~. o. b! t0 o/ q2 L1 s9 i# _
for (i = 0; i < 20; i++) : |; ?, ~0 u' K3 C
cprintf("%d\r\n", i); 9 P; x1 d: p- e0 W4 g1 j8 p
cprintf("\r\nPress any key to clear screen");
( m% L9 g* ~8 Q5 M4 `( m# V9 ~* Ngetch(); : e+ y$ M- |6 g4 ?
clrscr();
c& p. B2 V2 t3 Y4 B' t4 Bcprintf("The screen has been cleared!");
4 c6 D! `4 \ R- V1 ggetch();
3 \: f( d- {2 F9 s& greturn 0; ) q0 J1 J. Y& v$ q! o
} ' k) L+ v+ q: E) Q
; J$ ~' F7 I! M
$ ?: _$ C: r8 w, L
( E) n. U& C6 e# W/ I. K( c函数名: coreleft
$ G) h' e4 ^' [0 j8 R功 能: 返回未使用内存的大小
2 u" V% F [% o& O" q7 }用 法: unsigned coreleft(void); ' Q5 ~4 g( a- s( M9 T
程序例: 4 w4 p% A. j) x0 a# v/ F
#include
) [/ L$ `7 A$ J- n" } @$ }#include
. S5 f! A' E/ ?% r" t5 ^5 dint main(void) * v# V/ G8 @. Q& s
{ , O$ o5 i e6 e- w9 X" j
printf("The difference between the highest allocated block and\n");
* o3 {8 E% I! i0 w& s3 ^printf("the top of the heap is: %lu bytes\n", (unsigned long) coreleft()); : ~! o: n" ~9 }) ^* l, ~, V6 O | C
return 0;
) J2 k7 r V7 P) d8 C}
# z! l( g1 k" S: _) M
: R6 a% C, {7 J7 h$ |函数名: cos 3 [( r) f$ m8 h/ L8 @
功 能: 余弦函数
: R2 U( L+ { r# W6 M% l8 U4 Z用 法: double cos(double x);
+ F$ }+ L7 U6 Z% |3 O; H$ q- j程序例:
: m _! E5 V3 O# ?: z0 I4 k#include
1 Q& P! C) S2 t) k W#include $ p5 X5 Z4 N4 N
int main(void) + K/ ]" {4 B# J) v
{ 5 E1 R6 Q: |+ @' \
double result;
. E8 J0 q" T7 [0 O9 z0 ^! S' N2 ^double x = 0.5;
7 g' @. R. h5 `# N4 ]2 T$ iresult = cos(x); 8 K3 h- v4 B7 q9 x. G$ p0 {. h
printf("The cosine of %lf is %lf\n", x, result);
& g! P( @9 N f% h2 ?7 F- [return 0;
3 l, ~# u! a+ Y5 _3 J8 m: m} 3 ^( |. u9 L. W" E0 h* m- D8 A9 V
% Q4 Y& a, O$ j. K5 ?
) H/ F7 V2 G7 t/ F 5 e" E8 i/ ?9 x: F5 t; _3 O9 U
函数名: cosh # C5 `$ [) q6 \( ]
功 能: 双曲余弦函数 & r7 p& J+ I$ ^7 p' L$ e* h
用 法: dluble cosh(double x);
! F. j8 X7 g: z7 g! Y程序例: # Z( x9 J$ J0 j& ?2 U
#include $ i0 ~0 S5 J v% ^& y& z4 C9 x
#include % R( g% q+ l9 ~6 u+ t
int main(void) 6 g$ k6 F$ q0 \4 K; H6 Y0 s
{ 1 e7 a. P$ c' ^) F5 z5 P- T7 g% c
double result; 3 {9 z, x" H8 u; C0 K) u5 F
double x = 0.5; ! O1 H4 X) V& s0 d8 ~* M) Q3 x
result = cosh(x);
X2 o; b% k9 |printf("The hyperboic cosine of %lf is %lf\n", x, result);
0 z6 U2 y! |8 b; [! g3 l5 sreturn 0; K3 F4 ~' p# h1 f
}
; S; y# {9 i' E
& l) ]9 u0 Y! I8 M( @& A6 W" \' F* B
$ D2 _1 j% |: M. G4 N2 x
函数名: country
: n8 i; S+ R; H6 J) Z: v% a! Q6 j功 能: 返回与国家有关的信息 ( w6 o3 y5 c5 m8 C/ v
用 法: struct COUNTRY *country(int countrycode, struct country *country); % |2 W" y# V& G+ ^9 X3 e: E# R: i4 x) G: E
程序例: ! @& }0 b7 v% V! b# D0 `
#include
x- V7 G/ H+ [# \9 D. K#include
4 X5 H( E+ x& K5 |#define USA 0 # J+ t& F" E% L6 j3 z' |+ _$ I
int main(void) ( ]+ I8 A1 _2 X+ A
{
{% z' v) u& H) jstruct COUNTRY country_info; ( v/ C) W5 H8 [; z/ n% p8 J
country(USA, &country_info); # m; Y% }9 U( W
printf("The currency symbol for the USA is: %s\n",
: U" N$ M8 d y; x1 ccountry_info.co_curr); 8 a- D5 e0 F4 l3 X- v: K/ n* {2 }
return 0;
7 X# c9 N* l4 W4 r} * _" O9 E2 j& i
, t: n2 a" O4 Q& B; V0 A1 R0 J7 {
5 K8 ]2 O8 H# k9 } J) a6 v C: c* v4 H2 X. h7 c' P
函数名: cprintf 1 ^9 i% q" ~3 H; s. B
功 能: 送格式化输出至屏幕
) C# s' G+ H! r; F* q用 法: int cprintf(const char *format[, argument, ...]);
1 d1 r5 a8 {+ F, h& J3 R, E程序例: ' ^2 k, r0 y0 Y
#include ) n; z+ Q3 ^* K
int main(void) : p! v# C7 ] W8 m" k9 P t
{ $ ~9 _, `6 N# b
/* clear the screen */
7 V% U+ P) D [/ `7 u1 bclrscr(); 8 [- C) c9 ^( V+ D
/* create a text window */ * b; _# u& B$ s$ ]/ w% c8 g
window(10, 10, 80, 25);
( W% {7 p$ m2 ^) K+ ~7 _. K/* output some text in the window */ % C7 l) \4 A; k$ k& m
cprintf("Hello world\r\n"); ! v7 z9 M* m7 z) Q0 B
/* wait for a key */
- I0 e, [ f9 D5 r+ n4 N# D2 g. \5 l" y/ ?getch(); 2 g: G; V9 r5 P! X
return 0;
& O7 `9 I e+ V* |} : W. T$ |8 t S) _! ~7 ]) }4 m
# P1 Q" S9 ?. y; o7 U G8 B) t& Y
( X0 v5 b2 y, \ * L0 Z2 h5 h* z! o9 Y1 B* t
函数名: cputs
x7 Q" M3 i1 f功 能: 写字符到屏幕 1 t0 T1 Q4 U0 l- j0 U
用 法: void cputs(const char *string); 6 ~1 j9 W' `) d3 l, f- K
程序例: 1 b. G9 u5 O3 X8 P* g2 G
#include ( K8 h Z+ Q+ @8 m
int main(void) ]% a5 }. Q# T
{
' e8 B) j. ?9 |. _- w/* clear the screen */ $ d$ I0 s2 R5 |6 E. N* M' s, b
clrscr();
6 p; H v# p1 b2 g" T1 \/* create a text window */
* [3 a8 f3 \$ ?9 W! t, T( T; y& wwindow(10, 10, 80, 25); 7 t( e7 s7 ^# y$ ^ r1 v! Z
/* output some text in the window */ 0 _% {9 T/ I' z1 l" X; z% K1 B4 s
cputs("This is within the window\r\n"); 7 M- a& f I: V$ F1 y
/* wait for a key */
, ~6 o, k& p/ l4 u7 z; r! hgetch();
( B6 k) E: u5 b, Zreturn 0;
* C7 c5 I/ g+ x} - N) r1 p p h6 u; c
8 ~ j$ o3 d2 V
8 F4 d& q' _ O
( `9 u1 l( ]2 k, h函数名: _creat creat
9 H7 v: z: a# Z# q0 ]功 能: 创建一个新文件或重写一个已存在的文件
" u: V# S8 [) \! W8 [% ^6 e用 法: int creat (const char *filename, int permiss);
& o6 l& y/ |/ F+ {- n程序例:
A$ C2 w/ H0 a; I1 Y* ?7 ^/ ]' n#include ; F5 r7 k, @$ R
#include
; R/ J! `- H* Y" H" Q% V7 O#include
' t5 I! N$ X& p% m#include k: ~ l- ?4 e+ C( O1 s0 t1 U3 k
int main(void) 3 v. a4 ~, ]/ p8 y, J
{
j5 Y9 c' {( h! iint handle; ' i4 ]9 g: \( t7 q
char buf[11] = "0123456789";
. z1 v( O" X; H& r/* change the default file mode from text to binary */ " p8 @# I. G# @) B0 ^
_fmode = O_BINARY; % M' q, @% e6 t' |* C( ^, i
/* create a binary file for reading and writing */ 7 G5 V$ S# }( C0 ?3 [, }0 A: |
handle = creat("DUMMY.FIL", S_IREAD | S_IWRITE);
: x9 J7 L: `3 }1 r7 z/* write 10 bytes to the file */ 5 x6 b" C& `. [: s/ [- N
write(handle, buf, strlen(buf));
7 W( o8 Y7 z# |. D/* close the file */ 5 g/ u( J5 b9 k- s
close(handle);
# c% i5 F5 n! G7 Breturn 0; w6 U( u- I& P# f6 e1 Z+ w# w6 e
}
) b: w; T4 k0 n% V0 D
; V& l+ ?2 b1 |( H+ j函数名: creatnew
$ b8 l% k3 t7 b4 F G+ `4 n) u功 能: 创建一个新文件
1 q7 N3 C- E& Q5 D b0 v用 法: int creatnew(const char *filename, int attrib);
5 |" }$ @. |; Z$ Q' L程序例: : P5 k F. H: G2 A+ u! E C% j
#include 2 M9 @9 F& e# J1 W" w% N
#include
6 P( c6 }, `( i: }/ z#include
, f4 ?/ k7 d* y# B0 u#include
- f% |* W- `8 a0 D#include
1 r- I& c, `- Y9 d0 Y0 [6 Hint main(void) * f8 a$ R( R- R( Y) r# ^. _( I% Z
{ 8 ]- i1 g* }" u' P/ r8 o* T
int handle; ! L: m O6 d* O
char buf[11] = "0123456789";
4 Q7 e* w8 D; O3 q2 _% f/* attempt to create a file that doesn't already exist */ 8 O0 l% P. W: Q- k" \! r
handle = creatnew("DUMMY.FIL", 0);
& ~. g# @2 Z' o( M$ {8 T4 uif (handle == -1)
; x+ }, c. d/ \5 n$ O6 mprintf("DUMMY.FIL already exists.\n");
( c2 }8 l& i6 ^5 i- A: M, ~" belse
$ M7 y7 |0 X% @$ @0 u* n{
& @" _! I; p6 o* ^! C: Kprintf("DUMMY.FIL successfully created.\n");
2 h5 {$ e% s' i/ O E1 K# Bwrite(handle, buf, strlen(buf)); 6 `# p2 y* O. S! u
close(handle);
1 Y5 x( l, o3 K# i# y# J8 r} + I6 W- K9 p8 Y$ [
return 0;
& i7 c0 u' Z9 ]& P$ |8 y} + g+ h& H) k( |5 }7 ~( T2 J
|% G2 Q. ~1 m7 L1 ?- \' h
3 U! M5 R+ h, ~) ~: U
7 u: W: a/ i6 {7 C' J" p: ~1 w6 B函数名: creattemp ; ] M0 H( u5 B+ ]4 a
功 能: 创建一个新文件或重写一个已存在的文件 % J7 [; c* E' C: K7 E/ M W
用 法: int creattemp(const char *filename, int attrib);
* O7 ^" {, o! \8 u h* k+ F& v0 H. F程序例:
9 X% B/ D7 i. ~5 t1 H#include
$ P6 {2 |) V; u+ ?2 T#include
9 S) n! Z' v. i, f2 z- X6 B o" I0 H3 H#include $ q6 y2 i2 K6 O
int main(void) L5 k! ~/ A: N! _; C
{ , F. p& C1 z, n
int handle;
, I: d9 y" D) nchar pathname[128]; ' O/ y8 }) \: Z. E2 ]
strcpy(pathname, "\\"); 4 p- [( C# i. T4 c9 |( L( S$ I
/* create a unique file in the root directory */ ( O. f, f" E5 S& C; v7 R- q* J
handle = creattemp(pathname, 0); - M9 P8 S8 @% D( T' M. s, n
printf("%s was the unique file created.\n", pathname); ' T9 A$ U: O: W4 ^3 I; r. [
close(handle);
& z1 ?5 ^; Z) R2 n! C Wreturn 0;
6 U* y8 b3 Y5 E( o1 ~1 ?% a}
# ?# S$ M D6 G) K2 i, C" ?& N* V
2 ]% J0 Z0 v7 j; ~# h5 ]3 Z
5 r- G+ u2 K- W: i0 V, M ) m; ]; _1 d- L z! v8 B
函数名: cscanf
1 {) W3 T4 W9 a! H功 能: 从控制台执行格式化输入
/ d2 j, g; c+ t$ V) E用 法: int cscanf(char *format[,argument, ...]);
- n, @5 d; T. [8 |1 ^# E程序例: % ^* h: V: u$ F6 z7 m
#include
* K/ M7 U: X$ D9 yint main(void)
9 |. v+ ~& P' r) N7 D, _6 Z{
# u ^2 U0 _9 {3 |2 N* Ichar string[80]; # d' Y. a% I. Q
/* clear the screen */
8 h% Z& w4 y; T! w/ B. A, ]. @clrscr();
! r: r6 O- h9 {, U) @" l2 S4 S" `/* Prompt the user for input */ " e8 Q5 o" q+ X/ p! C \" m
cprintf("Enter a string with no spaces:");
6 W8 |1 k7 y: ]* m! v: |/* read the input */
}2 F1 l, Z/ l3 ^# B# Xcscanf("%s", string);
- c) P, N; `' X- ^. Z) h" q/* display what was read */
5 ^" e% q8 [3 Q% o6 f2 H' Ucprintf("\r\nThe string entered is: %s", string); ! v' I: F9 v M N* e5 d
return 0; ) G" q7 o$ |3 c3 ~; I/ F
}
6 F# s" M+ f/ B6 D+ E$ N: d' l& @: j1 C s! V7 q
% m% I( Y9 P8 l , K! t8 \. V- e. ~. F% u
函数名: ctime : W% r/ a' i4 C" s% |4 X$ e( ^, e
功 能: 把日期和时间转换为字符串 4 E& a, f4 F1 {& k0 G
用 法: char *ctime(const time_t *time); / D8 Q4 @" W; x. h6 c2 t
程序例: & C% A: `$ c% i' i' ~
#include / {1 Y) Z6 e Z. i# d- x) m
#include 0 t# v9 R' n, v* O# s5 F6 r
int main(void) 4 |- T4 _ r3 F, c
{ ' d" w6 T! M" C+ Y
time_t t;
: w/ e M$ p S( `/ d1 C4 mtime(&t);
) L; P' Z. P% B1 A6 k( W& J1 hprintf("Today's date and time: %s\n", ctime(&t));
* e) h) x& b# Dreturn 0; 5 f q4 S' X+ F+ H4 f
} $ h9 n1 Y, S+ ]0 n, F% F8 l( k
$ P6 D C7 |6 B! |& u# v6 W& \
+ s1 t, ~" S; g6 h% o
9 @; M' ^- M% _$ f( ~函数名: ctrlbrk * f H4 J* P8 K5 O0 [
功 能: 设置Ctrl-Break处理程序 7 ~) K5 |$ i- \4 C
用 法: void ctrlbrk(*fptr)(void);
, k" x; j3 I8 C- b程序例: $ K5 E6 l9 r1 D! S
#include 2 L, _0 c4 \8 B9 W0 a9 j# Z/ D* O
#include
0 O& w7 G r1 V+ O$ i7 z% H7 C6 M#define ABORT 0 ( l5 p% f: _, Q# s; Y
int c_break(void) - E0 N. H ~: p0 ?
{
- o+ c* v, T6 Z; |3 {9 N# o2 Wprintf("Control-Break pressed. Program aborting ...\n"); & z% q9 l8 U! ]1 e4 A# H
return (ABORT);
% v' ]# u" C1 d# r% l& l} W7 ?5 B8 @; ], ?
int main(void)
. p" J" h: W+ ?{
3 i8 B" l# ?2 f1 H0 M4 Actrlbrk(c_break); * K V5 {9 L2 |. G' R( P0 S. J( ]; s
for(;;) $ x" Z% q* q; j z
{
; j( L$ J+ [& @printf("Looping... Press to quit:\n");
2 _' J# @+ s) n3 E+ D}
/ x' v5 w, i# e- x, qreturn 0; 5 M8 b. k% G9 r" X. f" c& Y7 }
} |