|
函数大全(c开头)8 {* }4 ~2 q6 S# ^
$ M' ]' w4 \6 H3 Y( k& n
函数名: cabs
! v/ X% P# t" F2 F; l功 能: 计算复数的绝对值 1 Z9 Y% s3 i7 z6 R9 S
用 法: double cabs(struct complex z);
# R* Z. o; b8 N$ Y程序例:
7 r; J, ]: ]2 [#include 7 l' p$ i6 I7 f- o+ F
#include
/ z" u: b0 c) i P' \int main(void)
4 {- `$ I: H I1 }" w{ S" b1 ? Z h9 p
struct complex z; # d+ Y& H! ~2 N$ k f8 I
double val; ' Y& g) X5 g- U8 H1 E5 p0 [3 E
z.x = 2.0;
u( F8 h8 [( z* e/ b. C' }( Lz.y = 1.0; 1 P/ d. w. [2 N+ r C3 c' T& D# w- j
val = cabs(z); ( p1 ~; m+ t, B b! ]1 ?. T
printf("The absolute value of %.2lfi %.2lfj is %.2lf", z.x, z.y, val); * b9 ~% I1 J# |* n7 _
return 0; * D$ Y6 [. d/ _& B7 N$ L, v
} 6 R1 ^! l+ c i; \; p1 s. d
4 @( J2 P! c, }, p5 a7 a' M, S1 p$ c F+ e. ^
- U1 A+ x: b6 j& C% ?: z
函数名: calloc , _: s: c( W) D/ `9 N
功 能: 分配主存储器
( x: \* G: J) p0 l" b6 K用 法: void *calloc(size_t nelem, size_t elsize); % t, A: m8 C& w% L7 `5 k0 U) o- k
程序例: : m- B( f- w, c
#include % |# A- T2 y. Z U: g; F0 Y; P
#include
5 I& N7 B+ A, D) Yint main(void)
% j. E T+ Q% \8 ~, r; C6 g3 f1 [{
* w4 D6 }! o9 ^4 A6 T) Bchar *str = NULL; ' x/ [5 v5 C% o* }0 g3 s, I1 C
/* allocate memory for string */ : c" x v2 W& k# v3 t9 F4 t! b
str = calloc(10, sizeof(char));
5 N. j( ~7 I/ M1 F$ ?! o/* copy "Hello" into string */
# V* i3 C( `, J: b, @- _! z: ~- Q3 bstrcpy(str, "Hello");
3 h5 B) N/ f! h4 o/* display string */
" \4 z+ B, O. f+ y( d: U/ ^1 X- y; Hprintf("String is %s\n", str); 9 u1 |( r, Q, L: K! I t0 r
/* free memory */
9 j5 ]1 p( g0 n7 i' \+ U( v# ffree(str); " M. j7 l$ ], k) h4 V W, i# j0 l
return 0;
8 x. X. g( m N} , Z; t$ s1 e, c4 \5 e/ m2 R V- \
* f( F4 `/ l7 ~# k! S% Z! U8 B* N Q# n
6 Z4 T5 s9 |7 n* Z3 i2 u
! A7 [5 p" X4 W& M! q7 P函数名: ceil * S! |$ ]- {2 R* x
功 能: 向上舍入
5 ^ H* O9 i6 b' K& Z1 N) ?用 法: double ceil(double x); ) l8 ?5 d* p% g$ X# G$ p, W
程序例: 1 d: ?: v1 h! j& l. F3 n
#include
7 f9 G; T# W% \/ Z' C0 K#include
( y% P( \1 y$ j3 t, y+ H' j3 E7 Cint main(void)
7 {9 V9 ]4 { W& E. F) p{ 0 a% s4 ]& E4 C) { U8 e3 j
double number = 123.54;
/ {& S* `: \' a3 P3 f. n) ^, ddouble down, up;
0 K2 H/ m: f. h4 sdown = floor(number);
0 u, @# |1 e4 Gup = ceil(number);
! e: x1 Z( P+ [5 j" rprintf("original number %5.2lf\n", number); / S3 d% A7 K8 R) a/ c' V3 x- {* |0 l
printf("number rounded down %5.2lf\n", down);
6 `$ ~ I+ X3 r+ Wprintf("number rounded up %5.2lf\n", up);
- ?5 ?% C& d: [" e* {! j9 ^. ~) }6 Jreturn 0; & q' D; ?# C& d3 O7 }! i
} 6 D3 a! _$ w! b! e [
2 [7 `9 k1 Q" G+ ?. ]0 Y7 E2 V
. B: E- n. U; R- |) }3 Q# e t + Y* m# h5 ~/ F; U" v# i
函数名: cgets
' _5 }% _! Y4 p% i3 N0 C) M功 能: 从控制台读字符串
r& |8 p9 u9 _用 法: char *cgets(char *str);
0 ^. y' V1 a8 \- t程序例:
' ~1 ?9 k$ O' ~#include % T9 j: e% b7 m, Z7 O, L, X
#include 0 E6 \& e" Y) C/ f$ M
int main(void) 7 m, n4 z: C, |* L& A2 T: U
{
2 Q5 ^, e2 z9 d+ m" wchar buffer[83];
+ _+ p+ [8 z2 c% e. ~char *p; 6 g; w5 c/ _) P/ ]" k" f" K! m
/* There's space for 80 characters plus the NULL terminator */ 0 K- w4 C5 m: Q
buffer[0] = 81;
+ z# [0 N2 [- k" [" m' kprintf("Input some chars:");
9 p, {: a- n6 {p = cgets(buffer); % S$ E9 K$ U" {- P$ W& c9 [9 X
printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p);
! P& m! h7 X9 p+ {4 W. o4 V) w4 Uprintf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer); 5 r5 \% Z+ ~; o$ k6 B# P6 K
/* Leave room for 5 characters plus the NULL terminator */
9 i/ {+ R: }7 U, vbuffer[0] = 6;
$ Y& V5 X& l9 o1 ~4 uprintf("Input some chars:"); $ q0 d% W6 F) M* N. A( ?
p = cgets(buffer);
' E0 f+ [& f/ E& rprintf("\ncgets read %d characters: \"%s\"\n", buffer[1], p);
3 h0 m g8 M& ~) }5 O/ ?printf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer);
. }8 }" i" V2 _; N2 `, xreturn 0; , Y- p* R" O6 t& ^. ]; _0 {$ ]
} 3 l8 O. f& B6 }, M$ Y( Y% a0 A1 k F
' d0 \- ~2 m" s
8 }' H3 Z9 |6 c
1 ?: g& e" m% h9 _5 `5 c8 `9 A函数名: chdir
( g, [2 k4 Z" p; m% L. }! n v/ ]功 能: 改变工作目录
" I# |8 U' N2 h# B6 r8 c用 法: int chdir(const char *path);
0 U& S! h3 T6 V* w! @/ H程序例:
; o$ f0 p4 V3 J! o: T#include 4 n/ S6 Q& O: } i9 T
#include + f6 c1 q& r1 m0 s6 A
#include
5 z% \8 Y3 s4 z6 hchar old_dir[MAXDIR];
/ y. F" z5 l1 x& a; `1 mchar new_dir[MAXDIR]; " v% e6 ]/ n) E: P
int main(void) ) t! i& C3 z5 w
{ ; S8 [" N4 }8 D& }( _: J; c
if (getcurdir(0, old_dir)) 3 w% C2 m& }( E( _
{
8 V& X8 z9 G7 _ L3 ~perror("getcurdir()");
8 G' t* @0 {; W% fexit(1); 1 Y% t% [ q; [
}
/ h9 e& M- T1 k/ |0 |. D% s7 I: G) Jprintf("Current directory is: \\%s\n", old_dir);
2 \, m0 E) z% _# i) u# R" i. Qif (chdir("\\")) ' R5 d0 T( _ K6 k7 K
{
# c- E, [6 {( ^perror("chdir()");
6 O% P2 W6 K3 y9 W D. y9 [exit(1);
% a: v% ]; |1 ~}
2 d4 u% A @1 X# Z) _if (getcurdir(0, new_dir)) - k/ n" a: T) _
{ - K; R& a# `: |# {
perror("getcurdir()"); " g% Q2 g V; m' e
exit(1);
2 M, S8 {, p2 Z9 R! u} , t! x# W$ g0 e
printf("Current directory is now: \\%s\n", new_dir); ! G) X8 |$ u! i& G
printf("\nChanging back to orignal directory: \\%s\n", old_dir); ! t3 V( W. J; S2 }
if (chdir(old_dir)) 2 e0 g# g( Y7 I3 t. s
{ * a" A& C6 U, ?' N2 A9 }( S
perror("chdir()"); & `7 u8 T+ @3 ?4 B8 l G) {
exit(1); 3 b$ m. E* @6 L6 P5 b/ l; |
} / L- P7 R& W$ n* t3 N" Q1 i j
return 0; 1 c) R, \/ ~4 }# T
}
8 S3 ?; {) S7 y: { d% P
7 O$ ^: I. q1 i ! J7 A, X+ p C- @6 A
函数名: _chmod, chmod
; `; A/ z3 D) r( g功 能: 改变文件的访问方式
+ e4 p5 x- w/ t& k4 p( A6 [0 Z/ z用 法: int chmod(const char *filename, int permiss);
- a8 _! r, L6 i" M3 t9 R程序例:
# z5 h. t6 Q U7 K#include H" b4 Z, G' F6 b* |
#include
3 U) w6 n8 X' D B/ ?0 y; A#include
$ y' ^1 S8 O0 D9 nvoid make_read_only(char *filename);
/ q: m& n3 C, `7 } c0 d0 Qint main(void)
9 E+ D' R. n7 s0 M$ d* v{ 0 ^. k+ D. m, V% r
make_read_only("NOTEXIST.FIL");
& w2 t- a; d2 d6 F6 Gmake_read_only("MYFILE.FIL");
?% v6 {% X/ _! t- K. {: {3 Hreturn 0; p \5 A0 L4 u" p H
}
7 |" h: S& }1 Y( C% Vvoid make_read_only(char *filename)
& t3 W! l8 s8 I( U{
4 u5 y. e9 q! x0 R9 n# ]int stat;
# L3 y: D- Z- H# r% }& |. _% kstat = chmod(filename, S_IREAD); 8 H$ x$ v* x: c0 x- P6 z! m
if (stat) - y" m7 h# R& k
printf("Couldn't make %s read-only\n", filename);
- s+ }9 b, c& Z0 {1 H( z/ w! o' Nelse % @+ g6 M5 M6 G% A
printf("Made %s read-only\n", filename); * }0 d3 a+ `- ~
} ( F( j+ g6 q5 h3 P* |
! r& i4 l2 C' a9 k
, V4 s- h8 ]5 P
- E& b5 S8 |( ~0 x7 b! @; l函数名: chsize / h. g, P$ U1 t6 n3 Q, Z
功 能: 改变文件大小 5 ^% y3 D ?5 U1 X5 r' m
用 法: int chsize(int handle, long size);
; p$ ^% M. S0 W1 i程序例:
3 J1 w9 W1 Q( a8 A" e#include
' ?( f& P+ t, R* y* Z( |' K#include
' v$ n& Q/ g3 U8 s q. S9 z7 g#include 3 F6 q$ W% z3 N/ a4 j
int main(void)
7 I) _ X9 M: l3 ?$ |; c7 t% A{
# @* s: R& d+ Z/ @+ n5 a* {1 \int handle;
8 R$ L, Q2 U- L" p3 v- Achar buf[11] = "0123456789";
6 X+ e7 I+ l) u3 Y# s/* create text file containing 10 bytes */
1 G' y- r6 Q( r! I0 B9 whandle = open("DUMMY.FIL", O_CREAT);
$ x, K/ }: T% V0 C! q2 lwrite(handle, buf, strlen(buf));
2 d1 M# \7 c! c$ K" N$ Q5 k/* truncate the file to 5 bytes in size */
4 f0 f1 I" ^4 F' Gchsize(handle, 5); 8 m# F" l6 a4 \
/* close the file */
& {* x1 e, v( q$ rclose(handle);
/ K% v( j# y& G0 Nreturn 0; 4 N$ q" R6 x, V/ S
}
) q- s, V H( U+ Q' M. R% K" w
5 Q) Z2 e' h( ~ ?! b1 ^/ o
; J0 X7 D& k9 |" }函数名: circle , Q7 f5 `4 g; t3 h, I( V! U! F' D) J
功 能: 在给定半径以(x, y)为圆心画圆 / l) J' W& U2 K2 W( J8 A
用 法: void far circle(int x, int y, int radius); & M% I6 w) e, \
程序例: % w2 M& D! A: d+ h T4 M$ R8 j
#include ! b+ e& {) I9 O2 |7 F
#include . A; W& {' x5 _8 q
#include ( W" l! F7 I, x- Y6 i8 e, B
#include
3 g, H5 i6 I/ ^; w" hint main(void) - ]5 R+ A& s( W; i
{ ! \9 H( G |8 Y2 n
/* request auto detection */ # ~ E" u* M. `4 r
int gdriver = DETECT, gmode, errorcode;
/ K( ~) P: Y' D! E, aint midx, midy; , N1 m: ?3 R$ N: `$ X9 `% s
int radius = 100;
+ x2 o; ?# e/ I2 T' l7 D/* initialize graphics and local variables */ & [( g& H2 p, q; U3 c
initgraph(&gdriver, &gmode, ""); ; a' r5 ^4 P: V
/* read result of initialization */ / p- Y+ }1 ^% `# a/ d% g
errorcode = graphresult();
+ T( u) `7 P4 |3 S8 q1 Mif (errorcode != grOk) /* an error occurred */ $ [, D) M, N: z( C6 c
{ & \4 s& }* `0 v2 J
printf("Graphics error: %s\n", grapherrormsg(errorcode));
% w6 ]# ^0 `( L0 D/ D$ j( Aprintf("Press any key to halt:"); & ~' D4 r' y% n; k- M
getch(); 3 g ?) m/ V2 L. m, ]) d% ]
exit(1); /* terminate with an error code */
8 l9 L. t2 O, I( p" i! B# r4 C}
5 j/ N% Q0 O! @; cmidx = getmaxx() / 2; " Y5 l* K8 m) ~# ]4 G
midy = getmaxy() / 2; / [3 A6 U( n5 \% r* E! j) L7 C
setcolor(getmaxcolor()); ( z8 X2 b' R+ \# p+ `$ C
/* draw the circle */
2 G7 S* O" Z$ ~; c" @! icircle(midx, midy, radius); $ H. M3 X _4 [, K: j1 y- J
/* clean up */
, K1 ~' j; X2 m, b) Z+ v1 ~$ rgetch(); * j. h/ G- E& [% P" R8 S
closegraph(); 0 ^& p W o1 Y
return 0;
5 d4 {. G$ \# G* V$ p1 S5 D} d0 ^: |# L3 b
* q" X4 t/ b4 ^ m2 N' B" y" T
% g! W/ X h+ l2 j
- G6 Y- v" m6 t' }) N7 T% \; E函数名: cleardevice
2 f% m% b1 v0 b( [3 @& M0 e. d功 能: 清除图形屏幕
7 T" R3 c6 H( S: m; ]用 法: void far cleardevice(void); , l2 u" B8 g- \! I( [ y7 X
程序例: 1 I8 y/ l+ s) W
#include / H! G8 J! ]7 `- Q( A( r, I
#include * Y% ?' x; Q7 j& C
#include - b: [8 k x' {3 Q
#include
% N9 w2 s! @4 I# Z2 Xint main(void) K- E: e' M+ i8 V
{ . @8 y. v) a! P1 v1 v; M$ g4 t0 f
/* request auto detection */
, B' u a, ]% O. K/ Z) R8 M8 c3 Y9 ^int gdriver = DETECT, gmode, errorcode; . e4 C( x8 z }) \# W: |
int midx, midy; $ i5 }+ g0 G7 |
/* initialize graphics and local variables */ 7 D+ U3 b6 k, Y$ k) k
initgraph(&gdriver, &gmode, ""); ( [8 M/ R1 ^! x/ o* X
/* read result of initialization */
' D; F1 }, z Y: Q1 @2 jerrorcode = graphresult(); 9 Q% I7 Q" i: F
if (errorcode != grOk) /* an error occurred */
6 m% Q: ~3 L" K{
9 \& k( g- x, z2 b; Z) dprintf("Graphics error: %s\n", grapherrormsg(errorcode));
& C: r. d z2 f2 G1 t+ l6 ?printf("Press any key to halt:");
7 @* X7 T% i$ L: {: ugetch(); 0 [3 D6 H5 y5 m* ~
exit(1); /* terminate with an error code */ ' V( D! }' p* s# p0 z. W
}
$ p7 ^1 p' W9 p" y/ J% B- |9 o' |midx = getmaxx() / 2;
- C7 \. f6 e# l) lmidy = getmaxy() / 2; & J! z/ d' f7 W, _2 n# b t
setcolor(getmaxcolor());
2 q3 [% a0 D# c& ^9 u( q& L/* for centering screen messages */
: s6 r* r8 S3 `4 e# O( Hsettextjustify(CENTER_TEXT, CENTER_TEXT); $ N" e# w' ^; X# b/ P; N& p
/* output a message to the screen */ 5 E# o: q: F& r. y
outtextxy(midx, midy, "press any key to clear the screen:");
7 c% p% Y, T1 H2 _* i. b/* wait for a key */
4 z% g' s8 K/ \3 @# u2 m9 u* O4 zgetch();
" H9 j6 I' H& [' h% ]; A/* clear the screen */
" [7 c+ i. X7 d, x6 Bcleardevice();
" Z; U& o- M' m' J3 `9 K- ^/* output another message */ ; ^3 g1 o g3 `- T
outtextxy(midx, midy, "press any key to quit:");
( d" V. F- E @6 B2 i+ u$ N/* clean up */
; Y) [% x/ ~( h+ ^2 }* s8 [4 ggetch(); + V6 g" t& s$ L
closegraph();
$ M9 O% v: J9 {return 0; ! G9 A1 i9 h5 ^/ E! M. i
}
6 k6 E! M$ G6 j+ V9 P3 Z
6 P( D7 e7 M* Z& ]% V6 w' L( B6 D- q0 n# u5 g+ a
$ ~7 p4 j5 U3 M* C6 d8 g' I$ o函数名: clearerr ' t4 b( v* J/ L) _$ _; _
功 能: 复位错误标志
6 e+ ?! m- ~& z* o用 法:void clearerr(FILE *stream);
" O q t1 J/ c) d程序例: / r9 v0 [4 ?/ _# Z; D8 G# [. `
#include
, k" d5 t% p1 `2 h2 ~) nint main(void)
( U% s+ [' C/ ~{ - i0 }/ L) e5 W; o7 L5 I$ _
FILE *fp;
+ |+ c8 D' U F x/ u8 zchar ch; 8 Y! N* t5 m- p. Z z2 X+ y
/* open a file for writing */ 5 @& {1 A4 _( P6 T! H( \$ I/ J2 [
fp = fopen("DUMMY.FIL", "w"); i- Y- _3 k9 l! I: c# _5 D
/* force an error condition by attempting to read */ 1 N0 f" ?" G+ @9 E& H# f2 x
ch = fgetc(fp);
3 b$ ]. ]8 P, H6 _printf("%c\n",ch);
* }# d) V+ n) }) \1 R& T4 H$ m' @if (ferror(fp))
6 ?8 p* U# ?' L: K% |- D{
; q7 Q' i6 X! H! ^# I+ e6 \/* display an error message */ : ~/ q9 M- j5 ]4 ]6 K: `
printf("Error reading from DUMMY.FIL\n");
8 [5 R1 Y/ o, n$ l4 S4 L/* reset the error and EOF indicators */ / i# \( n3 [ s7 `; r
clearerr(fp);
- F9 h6 U/ }% R}
4 E: I/ Q3 d! e4 n8 l* dfclose(fp); $ b8 A3 U6 ~ f& u$ f
return 0;
- L- G, v1 s- ~; }6 `$ ^- R}
- f7 a4 ~* ~- ^. p# @% A* g
$ |$ e$ f* P8 X4 D# i5 Y; N
. @4 K( p9 F4 S" ~- C0 I. n
* P7 Q) a* A1 ~: o函数名: clearviewport 2 O% V7 g1 y( F" B" I% Q+ S
功 能: 清除图形视区
. g: s1 D/ k3 W. Y7 Y1 ?用 法: void far clearviewport(void); 7 K# y, [+ w' d, L; x
程序例:
8 P. x0 I# K' L( y: h' r#include
! J0 v: ^2 C2 \4 x% K+ N/ N0 T q#include
0 O* b: F7 [( @, o. E, x8 G- i# x#include ; U; p( u7 C1 }) F8 q C2 N7 b5 f
#include
3 I# n9 U9 t$ W+ _7 \, [+ c#define CLIP_ON 1 /* activates clipping in viewport */ 7 K( _ Q& C" Q) E0 H, l
int main(void) 1 _: I2 \- X) r4 z5 I
{ % S+ ]# {/ L! @4 j" G
/* request auto detection */
- q/ G% \; P+ a. K0 q, uint gdriver = DETECT, gmode, errorcode; & D2 R4 v3 d- g+ G! Q
int ht; 9 ^& {) h4 J9 ^* G
/* initialize graphics and local variables */
; W/ F( B) [& E! s3 yinitgraph(&gdriver, &gmode, ""); 5 E) u( G, {9 I9 s& D' S: k p: ]2 i
/* read result of initialization */
; A5 d2 B3 }. f, Xerrorcode = graphresult(); 8 t$ q+ c3 v6 t8 t) {0 w
if (errorcode != grOk) /* an error occurred */ 6 D* o' U5 T1 @! G2 Z
{
% f! N- u; U; y3 A+ S( {( ]printf("Graphics error: %s\n", grapherrormsg(errorcode));
+ P$ O( h: u# B9 B) Jprintf("Press any key to halt:"); 0 @ [$ V( p6 Z2 N+ c0 n. V
getch(); h! ~/ i( a* [
exit(1); /* terminate with an error code */
! y# m0 P% j0 u" k& I- H}
9 H6 Q7 I" F& ?8 B2 W+ i( c1 xsetcolor(getmaxcolor()); 8 U) b* T( o2 [1 w) F5 w% ?. m
ht = textheight("W");
. Q& A1 `( [0 i s" a/* message in default full-screen viewport */
J) O* H v4 I4 |0 eouttextxy(0, 0, "* <-- (0, 0) in default viewport"); 0 B2 G& g4 j- {# y- ~4 n
/* create a smaller viewport */
8 o4 G8 Y0 S: vsetviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON);
; ?6 b4 u* g. n" [: j/* display some messages */ # S' P# g; M8 Q$ M$ F
outtextxy(0, 0, "* <-- (0, 0) in smaller viewport"); 1 a: F4 }5 e) y7 _* X( Y9 O
outtextxy(0, 2*ht, "Press any key to clear viewport:"); . @! m/ P7 W& ~8 q
/* wait for a key */ 7 h$ `2 r' f P. } T* ]
getch();
/ q5 a7 q& k( t# H% J- ]0 G/* clear the viewport */
- m, o* N% p. u, s/ U( Tclearviewport(); ; `. ^+ ]4 a. _2 J( |
/* output another message */
& p$ V2 b1 K% V. v/ jouttextxy(0, 0, "Press any key to quit:"); ) T9 s7 { u W; s
/* clean up */
0 L0 e, Z6 a8 f+ Ugetch(); ' B* f4 T& K$ o% Q! Y" K8 q
closegraph();
% D+ h: h7 s8 yreturn 0; + j1 Z/ y) L. H0 g
} 4 ` t0 h Q' j( C* m" b+ ^3 A) e
S) t% q2 t' d4 m6 ]4 J
4 x1 U& |6 p' r8 j
3 U2 d9 _2 u7 L! O! P5 u函数名: _close, close
, D. L; m2 D3 o功 能: 关闭文件句柄 " L: s% j( B% ]3 g) x$ J% O$ Z
用 法: int close(int handle);
" p) t ]# r! ^) g程序例:
, X- i5 L7 O# Z1 S& N+ W" ^#include
, q& k" u5 ]( s0 K; N' T& c#include
# S% Z& Y! u- d, ]: _#include ' `; `8 M; D9 i
#include + M2 E0 t/ N" ]! I# b1 _. k
main()
$ d+ l5 W0 ]# ?9 A* H! o{
0 Y. |4 K* q% F2 F% Rint handle; " n: g. P' O9 {' K7 L
char buf[11] = "0123456789";
+ T0 _( v9 D# z [# `$ p/* create a file containing 10 bytes */
/ r& v( r7 l3 ?4 ^' C( Thandle = open("NEW.FIL", O_CREAT);
3 a6 H; N0 n# h' hif (handle > -1)
# [& \* A, Q: w2 I2 R3 y, j* c4 n{
# h! B3 Y" {6 R1 H# p9 b" Jwrite(handle, buf, strlen(buf));
y# y+ V* l$ M$ m# t: D/* close the file */
$ K/ e3 k/ l- j }/ `close(handle); * _4 w$ |1 P: t6 i% @- `
} $ M! m$ L I) C J* {
else
: S5 e9 l7 |5 P2 z{
) A( q0 c: r Gprintf("Error opening file\n");
! T/ t. x4 p) C; O5 [% I& V6 s} 3 U$ w/ L, k! _3 c
return 0;
; |5 K$ l8 X1 `; j( Q& O}
4 n2 f9 o) B, R" J* \8 p
9 R2 K8 G: {' G( N2 f0 \; z& X" K% w/ y1 h' R( @! g
8 Q3 n; w! e' k7 u
函数名: clock 9 D9 ^' |( R4 {- n
功 能: 确定处理器时间 5 {' O7 G; @( ?; P8 ]$ D
用 法: clock_t clock(void);
8 T, R. W4 A; w: L- j7 i6 i程序例: + ]2 `& \+ c' z: W7 \( i9 x
#include
' |# v7 |; `! ~( P6 N1 u/ w* g% s#include * F! V( z# O# A+ v
#include
! t6 X6 o% R9 p' N- q- ~. Gint main(void) 7 E5 ^7 S: _* T! j$ a
{
e% }: K+ W; Kclock_t start, end;
9 n$ e3 f% D6 wstart = clock();
+ y! ~& ]( B+ m9 w/ I- {6 p* F. Ldelay(2000); : @; p+ J! Z1 [9 {% |
end = clock();
. P6 I/ g; c' Fprintf("The time was: %f\n", (end - start) / CLK_TCK); - Q5 \1 o- Q6 \( Q4 v* w- L2 p
return 0; $ B, w& Z$ R, l: [3 t
}
9 y$ s# u) G9 ^" c0 C9 I0 H
, N7 @. E" A2 {! S: ]) A _- h" D0 u
( o T6 j2 R8 m/ i2 o6 z2 O8 p# }
函数名: closegraph $ ]4 [- i- }+ Z# I; R1 ]
功 能: 关闭图形系统
- P4 T4 B) U0 B6 D/ w4 w用 法: void far closegraph(void);
6 p5 Z( C: J8 ]4 L程序例:
f/ ~0 m% H3 ]. j9 q#include
& B5 e. p, Y( m W) P: ^. z% O4 k#include + [! g. `/ T8 m1 C' q; P
#include
; u' Y0 w" G9 Z6 g, F. x) ~3 N#include
# d. c4 h5 q y( v+ eint main(void) % [ X0 Z0 o! F7 [# v& K
{
" z, l0 d5 z* h# z* J/* request auto detection */ ! e9 u2 R3 y* v6 J; X
int gdriver = DETECT, gmode, errorcode; 0 H9 A' T0 n% M% d! [, O
int x, y; + G# i4 u2 a$ m- q( F l7 f
/* initialize graphics mode */ ; O6 ?4 t( f6 N' r9 |" c$ p
initgraph(&gdriver, &gmode, "");
' I$ b D$ m% e" g" W6 s/* read result of initialization */ 3 N1 [5 {( p& H1 y) r; ~
errorcode = graphresult(); , w$ e F* ]" ?6 y7 ~8 p
if (errorcode != grOk) /* an error
l) k M+ s, u$ B( soccurred */ 1 P+ F# }0 z' R' R- t$ v: ^
{
6 c. y' c+ `. Tprintf("Graphics error: %s\n", grapherrormsg(errorcode)); , ]) r, J# D* L
printf("Press any key to halt:");
; L: S6 u9 f6 egetch();
7 j* v' l- G2 h) r& j, ]! Jexit(1); /* terminate with an error code */
$ P) d4 B3 e! k. b. ^5 r} # z. c$ y# E4 ~
x = getmaxx() / 2; . }6 T O( s/ P J, m- w
y = getmaxy() / 2;
* g6 d$ Q% u# Z9 m5 k8 u1 M/* output a message */ 0 d3 c9 e% e+ h. ~, C# A1 C" w
settextjustify(CENTER_TEXT, CENTER_TEXT);
1 z3 z+ C$ |; @outtextxy(x, y, "Press a key to close the graphics system:");
: {" d) i r- F" V/* wait for a key */ & Z, K5 }" d6 u- S4 [6 Z2 m6 c! Z
getch(); 5 w" A0 l9 e: [) S1 Q+ t
/* closes down the graphics system */ " _6 `6 V- A2 k/ k( ^8 p+ d. k3 {
closegraph(); / X; x7 B5 Z7 C$ E& s( q; Q" h" k; E
printf("We're now back in text mode.\n"); . {: I/ l& u% n! T
printf("Press any key to halt:");
/ X4 p4 ^" O" h/ Q7 s Bgetch();
7 Z0 p/ Y- F0 P. \4 Treturn 0; / F) `& ^3 M. n6 N9 E- {0 t
} 2 D7 Y0 G! S9 u* w* J0 R b' y
( ]: u+ R1 e4 P/ z
% i$ s# n4 m' I; v8 v
, ^, @, X6 C9 X2 H函数名: clreol # A. l$ m. B3 q# l0 A" o+ m M
功 能: 在文本窗口中清除字符到行末 1 }8 B1 R& l! j4 g$ t4 D2 j8 d
用 法: void clreol(void); & R+ u% X1 b: Y0 Z! S* l/ B
程序例: 9 j& |: D8 y! R1 P N/ t( S
#include , z) s: f5 h0 ~3 K
int main(void)
! H1 f4 [0 Q5 v0 K+ d l{
$ n2 U5 e$ [6 d9 v3 `: Dclrscr(); % v/ P. @ {4 ~' u5 }6 |- ^: c
cprintf("The function CLREOL clears all characters from the\r\n"); & X( \. @! G# p% o9 F; X
cprintf("cursor position to the end of the line within the\r\n"); ' u7 ?+ X; y5 W
cprintf("current text window, without moving the cursor.\r\n"); . E0 P j9 M6 S, P ^! V
cprintf("Press any key to continue . . ."); * a* l! @* t8 O# @+ z+ R
gotoxy(14, 4); ' e2 f$ L" X" V# i
getch(); 6 w; ^; t x& I+ `4 O
clreol();
7 N$ i# @4 e" {- @* H( lgetch(); 8 T0 \* O3 O! Z* s" y8 M
return 0; 5 g: ^; ]$ l+ y5 O0 E# _8 [" H) z
}
2 Q( D8 C. x8 H5 ~) f/ D+ N' R: n0 d: }7 V; Q
8 G" h. e& I* V * U9 c" e3 n J. Q* p( v
函数名: clrscr
, n) {( \1 r) p; j) ]功 能: 清除文本模式窗口 + w) R* D" I3 X% ?
用 法: void clrscr(void);
t+ D+ t% D; f6 q6 a( {5 w程序例: 4 G T% n* b- F, }2 a% N
#include , W1 O6 }7 e" T) @/ d6 L
int main(void) - ^: [' L4 |2 m* I6 K z
{ 6 a5 J) y/ j$ F/ q
int i; + c: F3 ^$ J+ a0 P! ]
clrscr(); % F/ N+ V. H8 }7 F& R' C, x
for (i = 0; i < 20; i++)
# b% B% `3 y+ i) C9 ]$ F# ]" N- ncprintf("%d\r\n", i);
; W4 Y9 [# {9 Z) @8 |" }cprintf("\r\nPress any key to clear screen"); 8 w! q) R8 m' K2 [( ] x
getch();
' u$ G0 G& s* [ _; fclrscr();
" @2 u$ A3 V& K2 w' Hcprintf("The screen has been cleared!");
) O0 Q) m' r1 Y2 `4 M n! Dgetch();
6 ]1 n# J, x& w- l0 {( B- b @return 0;
* O" o0 A7 l' A1 ?4 u}
H' d* o4 H6 z6 b) Q7 M; ]8 \
3 k( @- P# X6 A3 e6 j, f+ ~9 ]$ u! n" r5 P1 p* m( H
, M7 @0 n& c' H$ V8 E/ m
函数名: coreleft
/ W+ M9 \, y4 R- @功 能: 返回未使用内存的大小 $ M' g1 R, O% s: P7 M
用 法: unsigned coreleft(void);
) R+ w, ^8 Z% n9 v- \. C0 Q程序例:
I6 L4 O8 K, f: M2 {/ m: n#include
8 w9 [. g6 [* L1 j8 H$ @#include 5 q: b9 c! n1 n. Z( R# E1 [
int main(void)
0 i7 S8 A/ n9 |{
4 w: ]) m& |7 k3 C0 t" Lprintf("The difference between the highest allocated block and\n"); 5 N, P: ~. ~: R" Y. B8 b7 |
printf("the top of the heap is: %lu bytes\n", (unsigned long) coreleft());
1 W. `0 C6 e6 ?3 R$ Preturn 0;
; b+ B3 U# y/ ?} 2 g: d6 g$ o( X0 R3 M0 L# W
' S: _/ s/ c3 J8 f, ]函数名: cos
^8 h+ p# _2 A0 t功 能: 余弦函数 / P1 ] _$ D% g; h( I
用 法: double cos(double x);
5 c, W" F# E; f: s0 o程序例:
# e0 G: I6 U; ^- ]: _; x) {0 D#include
& D; _9 x- V' v4 w. a+ s) k#include
5 V% U* w3 M z) p1 B; A) pint main(void) $ C' @+ `9 n" e: Q# K
{ / _" _# T6 D% L, k2 G7 }. q$ s
double result;
* H+ S; R) q3 `6 f, m+ I8 t& v) @double x = 0.5;
$ C4 Z& l6 \+ Cresult = cos(x); 6 }( m8 k8 e3 H
printf("The cosine of %lf is %lf\n", x, result);
- J3 P: W0 a- \4 Q oreturn 0; - F- r$ l- ~9 Y& ?; c0 f3 @
} : k3 v+ ?# @' C* M
( d; s# |( ^' T- _
f' T0 P# D4 `
- w3 M E. {# R" _, |) w9 i, T函数名: cosh ) c1 I: A3 P0 a0 i
功 能: 双曲余弦函数
1 ]1 \0 L( f3 B, ^) N7 _用 法: dluble cosh(double x);
$ Y7 g6 y" M [/ x, C程序例: 1 L. b% e5 w# i' G- L
#include
4 B3 [; a; ^( }7 p- @: \/ n. x#include
7 z! G% o, B- pint main(void) + d5 N! d% j, H
{ 3 \, x2 p5 K( e1 k
double result; & g3 ]$ G$ c# o7 X4 f8 K
double x = 0.5; # Z \% H8 S0 Q) E6 _; _: \
result = cosh(x); 3 d) z( w5 J" I
printf("The hyperboic cosine of %lf is %lf\n", x, result); 9 Y% l. f$ T# R
return 0; , x6 _* v3 a) X: r- e
} $ V) w; y- j# f& r
/ E/ [& V5 {# P# Y( `5 t2 h
0 Y. a5 C0 M: ]( }- r% j3 x 1 f V: U) \. G9 Y: J' g
函数名: country
1 k7 _$ ?3 R- [( A9 Z) C( J功 能: 返回与国家有关的信息
* m; J/ ]; K% P2 b0 _8 m用 法: struct COUNTRY *country(int countrycode, struct country *country);
" @+ }: n0 e0 G5 A" |程序例:
+ L+ v! p; ?: L9 U2 S#include
8 G6 d/ y% Z5 h) P5 p' Q#include
# x# F0 s8 o# l) }#define USA 0
+ q! B" E8 {4 x) j Rint main(void) + `" k3 w. ~2 S) X
{
, @3 H8 M5 l; Z6 w! g7 Bstruct COUNTRY country_info; 9 m; ^! G2 p4 O9 N. X, {, n/ r3 ?! c
country(USA, &country_info);
6 @$ p7 x/ J: h9 t c* i( D& s, I/ fprintf("The currency symbol for the USA is: %s\n", 6 ~& c9 n3 \9 e: o$ _( V
country_info.co_curr);
/ e; D1 S, O1 @3 P. J+ breturn 0; . d9 @5 a2 |! k
}
; w+ \$ E3 U! U @; m! c' t# X
' K6 D" x9 `+ b$ C, s; D& x( X/ m) ~
6 J+ x5 s; t0 f: T/ X1 a
! S6 z7 c6 L# x) b函数名: cprintf
3 W) ^( L7 E$ v功 能: 送格式化输出至屏幕
+ K4 X4 V; t9 a/ R7 V. p+ z用 法: int cprintf(const char *format[, argument, ...]);
2 W! ]1 Y3 u: R$ S) D' T$ f6 s( N- j! p程序例:
1 O" T4 e: S9 X4 ]$ V5 D# T* x$ N#include
5 I7 z/ t. `" yint main(void)
2 Q F, }& a9 g5 ^{ ( n& b! w6 L5 ^0 @- J) r
/* clear the screen */ 7 S6 r, C0 t' G# N' z' j
clrscr(); . L1 I$ l7 `9 S
/* create a text window */ & m' A) }/ F" y$ D w$ @; F/ p; B7 o
window(10, 10, 80, 25); 4 ?9 j+ C' ]9 \- @9 |* p' _1 E
/* output some text in the window */
& k! m* v+ _5 l4 J) @- {# Mcprintf("Hello world\r\n"); / c7 _1 O, w& s
/* wait for a key */ 2 ^5 F6 _8 `+ X9 Q% q8 _
getch();
, X% x0 s: I* C% Hreturn 0; , v( e- Q& J- o' J& b/ Q- ~
}
% I* U9 l4 Y4 d. k0 D7 e& w8 @/ a* y \7 M. s
! R% c5 }* a% D* M) ]
, E: |/ q/ z. P, D2 C函数名: cputs / ?: Z8 a" F/ e- V9 B
功 能: 写字符到屏幕
2 J: e r1 N: {0 _! c9 B9 k k用 法: void cputs(const char *string);
: w5 a% t7 L6 h: R9 q程序例: 1 ]& ]# F. o6 C
#include
& Y: X0 \5 ~& F- j& }: F& Lint main(void)
, j5 X' k6 C1 R, o{ - r- L5 a0 ]. O
/* clear the screen */
0 ]4 z; Q; q' h9 R& B' N5 Z$ Hclrscr();
! X4 Q+ z( F* q8 q, T7 G% v$ g/* create a text window */
7 m, c. D- K% Qwindow(10, 10, 80, 25);
; }# h) F1 y& k( q7 B) E, H/* output some text in the window */ # F2 Y2 e+ f+ e# m, k; E
cputs("This is within the window\r\n"); ! P* X# G) n2 V3 X
/* wait for a key */
8 t L7 S* P% g, M' i& Ugetch(); a* U$ ?- p2 q& ^
return 0; - y1 s/ A) A0 C B# _& p
}
2 M2 w( S% T E3 _
8 _3 s, C6 ]* Z/ R! A2 H
2 u# ?) Q! g! v/ S+ L6 c2 a9 q( K
* H* |8 M9 t. b+ I函数名: _creat creat
/ F; d! B5 G3 s/ h8 t( R功 能: 创建一个新文件或重写一个已存在的文件
" z* }% m8 K1 x1 `+ ^6 ]1 ?7 r用 法: int creat (const char *filename, int permiss); * u4 d' Q: L" j+ l/ A
程序例:
+ b7 E; j. _( x2 \! R#include 4 p' }8 o) }' z$ g( u5 L
#include
" t2 J0 ~: z2 |#include 1 L1 q. i: H; ~! R; d l' c
#include ) M5 Z* x! ]/ U# ]1 @* u
int main(void) , ~* j5 O! \# s- ~( D
{ - W: u1 Q/ b7 g/ ^- R/ q
int handle; 1 p. \) Z q! P# o' u2 O4 u
char buf[11] = "0123456789";
( `0 t+ p' D8 `# F! d- o1 E: J' Q: m/* change the default file mode from text to binary */
5 R8 U1 u7 b- a% E/ w- I9 k_fmode = O_BINARY; 3 ]" g! `; b: [
/* create a binary file for reading and writing */
1 g' P! k) h" V. z% dhandle = creat("DUMMY.FIL", S_IREAD | S_IWRITE); 4 c' l5 @: q5 f( z
/* write 10 bytes to the file */ 5 l# |5 f2 {9 ~9 M
write(handle, buf, strlen(buf)); 9 X# @* j5 Z9 m3 C
/* close the file */
( }1 A- l& `$ e% Y9 \& `close(handle); 0 H% N' s7 V7 I7 N2 ]& H
return 0;
. s( G9 C: B) X2 ?} 4 Q5 q8 t3 s g0 p; h n( { g+ f
) T5 O7 Y& J- U' U3 l% Y/ c
函数名: creatnew 9 O: N1 h H* {, h# n
功 能: 创建一个新文件 8 C- R( _1 y7 p* T/ D+ U
用 法: int creatnew(const char *filename, int attrib); ' \6 z( f1 j' n
程序例:
2 I& D2 {! ]+ Z* R/ J1 ~- h- f#include
) ?+ b1 S; v% [: B3 S#include
4 H" x$ T( w+ h O% x: E% h3 j#include
, B7 o9 {) `/ f$ G#include : C% p8 {* h9 S$ F/ S; g
#include
. o4 P' o) w' m7 qint main(void)
) e( j' ~. F$ m7 u; \{ 8 n, w% w1 R) h
int handle; 0 L/ {1 j( P( e4 W
char buf[11] = "0123456789"; 7 Q) v7 \! w2 m5 y
/* attempt to create a file that doesn't already exist */
& i& d2 k2 y* y4 O/ ~+ U4 m3 r1 khandle = creatnew("DUMMY.FIL", 0);
7 F6 M6 T" q: f# w$ T6 x% vif (handle == -1)
7 g* l0 A3 R4 w) `! J3 ]printf("DUMMY.FIL already exists.\n"); 7 o8 _ k. V( d7 i6 y
else 1 B6 o( U+ I$ O6 r- J0 W
{ 3 v8 {: e8 S" R$ x" [- a! t7 K X9 E
printf("DUMMY.FIL successfully created.\n");
/ V; U. q9 Q' \7 `write(handle, buf, strlen(buf));
: X5 @4 f0 w6 l% ^! Y7 tclose(handle); . g+ N+ K% v4 j) I' I
} 2 ~& {$ R7 q5 }/ M0 v6 U
return 0;
- _8 ^( x) Q) d0 F: Z( H2 z( D} % ~) i) f7 H! {: h |; T# W
+ F8 r w7 r( K% p$ t& F% B
; s- t' A1 h: Q% j) g% g8 S 5 h" }: j5 h% s: f+ o& {" l
函数名: creattemp : b0 s8 o8 p3 o6 y
功 能: 创建一个新文件或重写一个已存在的文件
- w* ?9 m+ B+ h用 法: int creattemp(const char *filename, int attrib); # Q# x% k3 w% E! W; ` ?6 q
程序例:
0 h4 q& d( J3 E/ a$ ?#include
4 g0 \; z+ ^0 k! ?. G6 a/ p4 i#include
) c! i& W: W8 P& s/ ]) {; t#include
/ \, \3 U3 B0 l) R6 Iint main(void)
7 d4 F, i+ d! w# R) W% p6 `' p2 e% M{ $ E5 ~: X; B' \' [
int handle; 4 G# }6 E ~; A
char pathname[128]; ) x; {( E* v; M1 w
strcpy(pathname, "\\"); * ~7 J/ K" f$ y: N! k4 T
/* create a unique file in the root directory */ 9 z# _- D5 Z f" {2 W8 P8 e" O
handle = creattemp(pathname, 0);
8 _& |1 q: _0 V4 `2 D% jprintf("%s was the unique file created.\n", pathname);
) y' f' {5 S2 N& Y& pclose(handle); 5 d5 }' p/ \% b; c' n E- }8 `' p5 k
return 0;
1 e/ J0 `' y3 Y7 {; \& |}
1 C0 [: O; z4 l* S$ t! A @" z
2 v. h2 K( @, i4 X% d4 f. v( F- Y0 s, S. o K0 S8 {
' X2 X* g1 ]+ @8 {0 Y- y: N函数名: cscanf , h }: H/ u8 `" s; L$ a& p
功 能: 从控制台执行格式化输入
; d3 m2 a( G5 o O用 法: int cscanf(char *format[,argument, ...]); : y2 E& O6 i) Y, U' E
程序例:
' B; o. y4 w7 K; q1 a#include 3 Y& \" L1 p) {$ a! m8 c+ E+ K
int main(void) $ z) e) N- s& v, X
{ % @% ^7 o7 n: l
char string[80];
1 @) W# k+ s6 l( _: `, K3 t/* clear the screen */
0 E( ]( K8 C4 r' F/ ?" t# [clrscr(); 9 n3 @7 C* I4 Y) Q- U& t! D
/* Prompt the user for input */
0 t- T+ t9 V0 f: b) G2 Wcprintf("Enter a string with no spaces:"); - r6 S! R' ^- a4 {* }0 A2 F" P
/* read the input */ $ h# `# w) H% _: \! H: x* N) C
cscanf("%s", string);
T8 [' N* u, P( a/* display what was read */
# i4 O( T) `& Q% N' X5 U3 \, ycprintf("\r\nThe string entered is: %s", string); 5 c+ I7 R1 Z7 a' {6 t9 _! q) M3 f
return 0;
4 I7 Q* p5 C' t3 F; z& w} / p e% l, _4 `4 ?
/ W8 q! U s f7 f# u
S5 E" D' P1 D% \1 B4 M
6 l' z. g" T; |7 M; E函数名: ctime
( l; ^- w/ {8 u+ j/ |功 能: 把日期和时间转换为字符串
/ t6 n `* V4 j/ |用 法: char *ctime(const time_t *time); 2 K1 ~! p. I" V( G8 ^) u! E
程序例: ' \1 H7 B$ R' R4 L
#include
4 k) y9 I; n3 h" d6 u3 g9 v* v#include ) }5 p8 X: p" T. H1 }0 j
int main(void)
# d5 N# Q4 X: I3 k$ m, p" [{ # B' v- e, Y; a" J4 B7 t+ Z
time_t t; ( b* ?2 B; y8 D j
time(&t);
9 d' n# Q% ]1 r9 g1 hprintf("Today's date and time: %s\n", ctime(&t));
2 `0 g' l! \7 E @return 0; 3 n9 W/ n; L/ F/ U4 C/ N H1 _
}
& k3 T; X6 X: h3 P+ g
. B3 Y) r- I" k* z: D; D! |9 P4 G6 _' n7 m; R8 O) v: ]- j
+ u/ m9 u( V, s. D) \函数名: ctrlbrk 6 E9 Q+ I! J% Q) r
功 能: 设置Ctrl-Break处理程序 " V. g% I1 O0 ~
用 法: void ctrlbrk(*fptr)(void);
8 g$ s" J. g! p8 d/ L; d( R$ ]6 W程序例: % P+ w6 x. p7 L1 y9 a
#include
9 M4 s7 I3 B- O+ S3 g7 F$ ]#include ; d+ \+ W! c" o, `
#define ABORT 0
' V4 F( t, U9 z5 hint c_break(void) ! g$ Q) R# e+ `
{
- Z. V4 `* ?( Y4 E+ Tprintf("Control-Break pressed. Program aborting ...\n"); 4 @' D: y# P r6 M9 P' E
return (ABORT);
4 T- A3 X* \" U- r} / I4 r5 s4 m' b/ v1 p0 K' @4 u
int main(void)
' M# Y+ f9 {. p& y% E{
! l+ Q. }: J( T& B- hctrlbrk(c_break);
+ Q! `- w [1 [& k+ `8 U% Dfor(;;)
$ W5 ~! K% c8 Y7 m6 E: h{
' o4 ^) \9 i3 h# O6 D7 n9 xprintf("Looping... Press to quit:\n"); * v% \# e3 z- j0 m; v
} 0 i' V. | u2 [% q
return 0;
1 I0 J6 u, Z1 A} |