|
函数大全(c开头)9 ?) F5 e7 ?" }% e C3 a
* O* T4 M N [9 b
函数名: cabs
& `1 {4 Q4 d' X, i功 能: 计算复数的绝对值
" y- O6 Z- P# x5 m. {! @用 法: double cabs(struct complex z); $ [: ?% H. G; v# E
程序例:
+ {! e1 h& S& y$ S#include
% g+ g8 r: a+ d#include - p+ j& ], e, t; A0 e) _, C' c" y2 v
int main(void) . {$ J1 C) X; Y' `7 A6 `! Q& o) @
{ 8 p' w6 K8 y% q& Q+ U; m
struct complex z; 9 q2 Y5 C9 G( X4 q6 k! M4 z) n6 v
double val;
( j% [) k4 h5 o4 }9 X& Vz.x = 2.0;
+ k4 B! ~ [ A, d2 v8 h% Iz.y = 1.0;
! n5 |5 g& x% K2 p! C0 f# }" Oval = cabs(z); $ }! L, _5 l' l$ L# W( R
printf("The absolute value of %.2lfi %.2lfj is %.2lf", z.x, z.y, val); 8 M8 ]8 }, X ^8 a: A' f( c
return 0;
9 t; w5 [$ y* \1 i- z* F} ( I# X" z x& ]
' ]) `3 z+ \; w0 j( \: L
) O1 h8 i, E* j: y+ ` % ]; h4 w4 F6 Y& t
函数名: calloc 3 Z! e0 s. e8 i2 w% a; D
功 能: 分配主存储器
. `5 v+ P5 d8 z# }* c5 f) T用 法: void *calloc(size_t nelem, size_t elsize);
% N \4 F! q) M$ J( U, D9 u程序例:
& l; P6 L9 R) s' ]#include 1 r3 p$ q7 b9 l \* F
#include
. _$ N" Z& C' y) ?' k. w& z% Oint main(void)
# h4 D: N( W& Y( R9 e" a{ 4 z& K( T" x9 H* @' r% W" [
char *str = NULL; " [& x) p, B. j: h* `) p1 `
/* allocate memory for string */
$ T- B: f7 o6 Bstr = calloc(10, sizeof(char));
8 h4 g, ?3 Z" L$ [0 x e( _" `/* copy "Hello" into string */ 6 Z c" t: H' Z
strcpy(str, "Hello");
. a. `) e Q6 v. h" U7 _- s1 ~$ z/* display string */ - b' G. a0 z5 a5 P" k" M" ~
printf("String is %s\n", str);
# \8 P9 D4 g, U1 \4 T/ u, U/* free memory */
9 t) j- l: p/ f) P3 _6 q7 Y, Ffree(str); n$ X+ Y3 {4 P
return 0; 2 |& C. `. ?, z9 O& O7 U
} 6 k: c e$ Q( ], b* V
2 }2 i3 r, h) k* e8 K
8 A q, j! `- c8 b 8 D& F3 v: K2 ^# x0 e
函数名: ceil : s* r. z. i' c* M5 N# Q
功 能: 向上舍入 + f7 C P0 l5 d" ^
用 法: double ceil(double x); 5 j- f, V" M6 f9 ?* K) E
程序例:
& n0 x- i1 W6 r- M6 q. C#include
4 O& L4 ~9 t R4 M/ O+ t#include
1 [0 q$ i' ~! fint main(void)
# a0 [1 l; Y7 z1 y- \$ j# n7 m, P2 l/ D{
% y* n. p0 Y2 | Y! C3 Ydouble number = 123.54; ( k% ^" U4 |- d) F% d, n' O
double down, up;
7 {! B H" v+ V$ Q; \6 \' |down = floor(number); 3 v# ^% B$ J+ a* b
up = ceil(number);
4 `6 E$ _* ?" O0 c. e* rprintf("original number %5.2lf\n", number); # D% T5 S* U! o( q: H8 ~0 g9 V
printf("number rounded down %5.2lf\n", down);
& x0 e' A& L% ^ ?' }printf("number rounded up %5.2lf\n", up);
) t1 M6 L$ E8 Y8 `0 c+ |return 0;
$ r5 |. d) q+ f& d- Y}
# S4 ?% d1 z# O2 E
, K( S5 X% m% Z
+ l3 k* i5 h; b$ ^ f3 M' Z ; y1 k* a7 f, Y
函数名: cgets ' |8 }' V- ~7 z' o: i( Q
功 能: 从控制台读字符串 7 Q4 G4 @ `. X, n( a0 Q
用 法: char *cgets(char *str);
5 d) X: V' i0 D" ?程序例:
$ w; m9 N3 V4 G1 P0 R# Y#include
9 ` _7 O7 c2 A* ?#include
$ ], k$ h& g& s$ Lint main(void) 1 K& M+ Y1 j; L- g. x
{
/ E* W& O* R$ {! A- Vchar buffer[83]; * H% X) i, y% e- Z( E
char *p; 4 n, s' z% n/ X7 b' q, F2 f
/* There's space for 80 characters plus the NULL terminator */ - G5 s2 K7 ^: H; s
buffer[0] = 81;
( D; s9 J* Q8 g" V& ~: c+ kprintf("Input some chars:"); 8 d+ R% B9 {8 q, k) `5 `. o3 Z
p = cgets(buffer); " c: j( N" J3 {+ l5 z
printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p); , f8 i( A/ i) R: K) a. |3 l
printf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer);
9 A3 W: ~5 \+ Q- C# u/* Leave room for 5 characters plus the NULL terminator */ ' {5 A9 o5 v9 E- D# T4 l
buffer[0] = 6; 0 t, R+ A$ G3 A4 ]
printf("Input some chars:");
( M: ~1 |+ K; ?% \' np = cgets(buffer); & @" S) Q+ {; {5 _
printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p);
& n; M2 w$ D- J0 ~. vprintf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer); 8 J0 n6 \0 |5 H
return 0;
( ?: y/ `1 X# _- Z4 K} 7 d& v. o/ g) B3 t& t; J3 h
7 h- S' x( ~! m
) E+ I& l( U& S$ Y' z ( ~0 n8 U4 P) F
函数名: chdir
4 N d7 i& F9 b5 K- q' m9 Y功 能: 改变工作目录
' y+ l) x7 q7 ?. u$ x用 法: int chdir(const char *path);
+ T6 z$ H+ ~& X2 U% P) P5 H# z程序例: : _8 |6 @" D D. j7 X
#include
1 J8 T* U" m: z, B) K#include + R+ |3 y2 f3 }7 b
#include * o- z6 K1 j x: K+ N7 K9 [
char old_dir[MAXDIR]; " y) S0 ?) H, ^
char new_dir[MAXDIR]; & }/ i: [4 Q" c, N" @3 I
int main(void)
+ h* S7 e. e1 k& \* e{
, |0 N& `- @, D! Mif (getcurdir(0, old_dir)) . C8 v, |6 _/ H$ U, E
{
9 H$ A" a2 j# K/ x5 }* Wperror("getcurdir()"); # ~& K9 W- L! f: l1 L W8 {5 Q
exit(1); " h8 V% m& L4 g, ]8 Y" ]' |; [* k
} : }* H+ p$ z2 C8 c) ~8 d
printf("Current directory is: \\%s\n", old_dir); 5 r+ y3 O F, k' V0 a* u9 C
if (chdir("\\"))
$ W5 e2 j. X/ N# j( t$ X, C{ ; p# L$ C, H6 s7 {
perror("chdir()"); , i& s4 ]; ~% k c: `
exit(1);
, E5 f' z4 W- l# {}
+ G0 G) f4 {9 \7 j J8 L5 gif (getcurdir(0, new_dir))
: u$ h8 [5 S" _2 H# X, u{ ! ~$ k1 _" K. s/ T$ ?7 s! `, I
perror("getcurdir()"); 7 \4 }* W* t# N0 P0 w1 E& N
exit(1);
6 J/ D1 N T5 N" U. v: `" _} * m/ ]. X! g* m: `* i7 i3 q
printf("Current directory is now: \\%s\n", new_dir);
) p( `% p1 c) f# {; l( wprintf("\nChanging back to orignal directory: \\%s\n", old_dir); 9 c2 C$ S- M' D: f9 o( ?) {2 Y
if (chdir(old_dir))
( O1 _$ f3 `- G7 N: P3 x* k{ 2 q$ b- ]% Q1 _
perror("chdir()"); 7 Q; b$ y1 _% K( E1 b2 B+ h
exit(1);
, z0 l7 q: g, |# x# h, `/ m5 r} ' ~' ], P/ V" ?8 U0 X& L1 m
return 0;
7 N* b: z2 W: C' Y. P}
# f* E/ u4 i1 J0 I- P2 |! Z
* @' b! o$ d1 }) }3 f) w2 Z. e 4 X8 I3 W, M/ \- T6 Z
函数名: _chmod, chmod
5 K& s, E! ^% v4 N功 能: 改变文件的访问方式 ; T9 }7 ?: k+ H1 U4 n3 T" R
用 法: int chmod(const char *filename, int permiss);
! |/ M+ g# R7 T! q程序例: 2 M$ F/ B# F" b* v9 @
#include ; Y( v2 `, |& y
#include
! y; c5 S* i3 C4 ?1 m#include
" c5 ?7 T& Q, d( W* Y( K, Lvoid make_read_only(char *filename); ! {5 w5 F& ~ ~) V0 ?6 t& S: \
int main(void)
0 b* C4 z2 X) r( Z4 g# k{ - \$ f5 t2 j' W f$ I+ Y- d6 c8 a
make_read_only("NOTEXIST.FIL");
3 r8 r- W- e$ G8 P% n! Imake_read_only("MYFILE.FIL"); + Z, Y8 r r6 v: p+ G
return 0; & \5 A7 \; T3 c
} - {; w5 b; N* X" a7 H* L
void make_read_only(char *filename) % O; a+ d' m4 I. a. |# g8 _. Q
{ 0 Z% @# I4 q* F+ b$ X& k6 F
int stat; / Y# W& F. d/ x+ v+ p( n7 Q
stat = chmod(filename, S_IREAD); * p# Z6 E$ c- \8 I4 H# i1 V
if (stat) 1 a8 s9 }& s6 X6 h+ H$ V4 }! G
printf("Couldn't make %s read-only\n", filename); 6 ?9 ~, Q/ D t
else
$ L7 E! p5 I, Tprintf("Made %s read-only\n", filename);
9 W0 h& i7 a; k. e: M( S# x; @} / t2 q$ s' O# ]& R( x
2 ^/ I" b' h3 V3 g% K& ?
% y% C4 z$ M( h: w
$ g2 L4 V! d8 n4 y函数名: chsize : a0 E* `- l: ~ v1 I+ b- c
功 能: 改变文件大小
, W" G1 j9 }: B. T7 E用 法: int chsize(int handle, long size); 5 m* K* O: o! U ^0 [1 L
程序例: 6 [0 q* Z! W/ Q/ N1 X" J$ T
#include O1 w: h( B2 ?: Y9 M) h7 v+ @
#include
; V0 b# V3 T; K& M+ f#include 4 i H. } }2 w" ~
int main(void)
; m" D8 y# f4 G' V) Y% O; l{ " Z- q; q7 \; X( f' J9 L! y
int handle;
" j# m1 ]" x' Y! ~char buf[11] = "0123456789";
9 z! C+ C+ d5 @: V: l& I I8 r1 G1 a& ?/* create text file containing 10 bytes */
6 x; z9 p% u7 l& `handle = open("DUMMY.FIL", O_CREAT); 3 r; B* l2 ]) ^5 U# k8 n" ]8 r
write(handle, buf, strlen(buf)); 7 z7 g4 k2 j; m0 H# W. E, ~
/* truncate the file to 5 bytes in size */
$ r* l# |" ~% M& `. U2 ^chsize(handle, 5); ( F( y+ g, H( R) }7 X" S5 G
/* close the file */
4 V$ ^' U5 b* K* K. ?4 vclose(handle);
) P: S" S/ `$ j# T' v7 zreturn 0; & |& S3 }4 [/ Q5 ^1 ]: D+ y
} A$ Y0 h. m8 n
; q8 U: ~' Q$ O7 h
# ^8 Q V. v) r函数名: circle 5 p# m, N/ g @# f- W! r( p0 ?
功 能: 在给定半径以(x, y)为圆心画圆
0 `9 c4 D) }) Y2 X用 法: void far circle(int x, int y, int radius);
V: f% [/ Z! I. q$ `% D8 k, k程序例: / z2 |/ z, {- {8 |
#include & ?: B* \5 i1 W9 m0 b! U: X
#include 2 x( o. R; @) h6 \8 ~1 Q( p
#include 4 `' J8 ^4 m' y7 ^ ]$ [
#include # q- H% B- { p% n
int main(void)
P8 |) f7 q5 g7 s{
7 N& I1 [ g) u% R- I% L7 [9 T& a/* request auto detection */ Z# J& g; q4 w1 R
int gdriver = DETECT, gmode, errorcode;
. Z5 N4 b8 a5 V9 g6 Kint midx, midy; + S4 p9 i+ d, C: Q) T; g
int radius = 100; Q% P/ p; K4 g, e% C+ {
/* initialize graphics and local variables */ ' f4 E7 `; _/ z- x- w3 Y/ H5 Z$ n
initgraph(&gdriver, &gmode, ""); C5 S- O% M, s. O3 v
/* read result of initialization */
$ f/ l! M# l8 K+ ~ |5 Q6 ?, I0 Cerrorcode = graphresult();
) M! [( a6 ^# z8 u% fif (errorcode != grOk) /* an error occurred */
/ K- n6 v1 F' h t+ c{ ' x1 j- P1 H7 |! @) j; W$ _
printf("Graphics error: %s\n", grapherrormsg(errorcode));
- e( ^9 S) t) c- m$ ]printf("Press any key to halt:"); ; F# v. h8 a- w$ Y
getch();
1 E4 v! g' i4 [6 ^5 ~! w9 D- p+ Dexit(1); /* terminate with an error code */
7 A, [ i u% f6 Z" n} " y" c; l K- L& C+ f+ D1 W9 W
midx = getmaxx() / 2; ) N5 \6 ?7 m; {4 M( K7 }
midy = getmaxy() / 2;
1 P& q$ f2 R. m$ I' h o4 usetcolor(getmaxcolor()); 3 D a3 ?) Q! M% w' I4 K8 \
/* draw the circle */ ) X5 S9 x2 ], u2 B
circle(midx, midy, radius); & i6 P' P8 i/ d; [4 J( U
/* clean up */
; f! `5 a2 t8 m5 }! |2 ^; l6 H: `" N3 vgetch(); 1 D$ `' p, l d" m
closegraph(); * b: A1 L* N0 e
return 0;
# v- l5 k3 P8 r+ N) u1 u$ i) d}
+ B) g9 m8 C1 {- O! ~4 Z- [) i# u' @' I( N
& z S8 u5 S# f' v* [; A% R& X
" ]3 S6 X! n; h0 v函数名: cleardevice
% c4 o) Y' E* b功 能: 清除图形屏幕
4 L; c! y# l/ }! u3 r) f6 l用 法: void far cleardevice(void);
* b5 e5 \7 `0 ?程序例: 1 H, ~2 Z. T( \/ Q1 D/ a; u$ o7 e
#include 8 T1 W) b) [$ G8 ]: U, e' [
#include
/ D4 ]" S5 G+ Y1 k& {0 l7 l! J* C#include - a% |! D8 m8 q: j
#include 2 a. ?' I; v Q. @
int main(void) ( h* l) r P* q0 k. U+ X8 ^ _
{
, N/ g' k# H2 M& l; F6 O/* request auto detection */ 0 }9 k' D5 s2 X5 l* `
int gdriver = DETECT, gmode, errorcode; . u# R2 B8 r6 I
int midx, midy;
$ N/ e& w! S1 b9 G/* initialize graphics and local variables */
, Z( q& M6 D: Iinitgraph(&gdriver, &gmode, ""); , S' u/ G0 v+ T) l& j4 W
/* read result of initialization */
% o7 O# I, j! Y$ Lerrorcode = graphresult();
" W; F9 L4 g# q* P: v% G2 h4 wif (errorcode != grOk) /* an error occurred */ & C6 f$ ^' n' s, f8 A, H
{ - u* C" [# k: P: V3 j' s) S) b0 v
printf("Graphics error: %s\n", grapherrormsg(errorcode)); 6 o r: ~0 {$ U* ]5 D9 ]% F$ M- v
printf("Press any key to halt:");
# ]$ d$ ], ^0 }7 f$ H# k: Jgetch(); ! F0 `6 e" c" v) w
exit(1); /* terminate with an error code */ ' F, f5 s# B" C/ [( I
}
$ X4 }: p. |$ j- x2 U. @" u7 rmidx = getmaxx() / 2;
6 j' N: C8 H; \" v G& g1 e, W! n% H/ Qmidy = getmaxy() / 2; % Y1 G& T% h6 S% K8 S
setcolor(getmaxcolor()); * V8 @- Z3 I9 ~- [
/* for centering screen messages */
6 f* g1 O* X0 [4 N B8 usettextjustify(CENTER_TEXT, CENTER_TEXT);
' F$ d; u# b w" B1 N- b. b/* output a message to the screen */ - r$ p! ~1 r7 s, j6 d+ g5 B! \
outtextxy(midx, midy, "press any key to clear the screen:");
0 U" M0 z+ t( l- G5 \# Q! L( L/* wait for a key */
x- z& V/ `$ F Igetch(); ; A& j; f0 I+ Y C: g; i& I. I
/* clear the screen */
2 t4 G0 s, A& w I" |8 C! vcleardevice(); ( T& }6 h4 Z, Q/ T
/* output another message */
7 }2 x" W) W' |8 b2 }; U2 F( T nouttextxy(midx, midy, "press any key to quit:"); / z# ~: b; |5 b& `0 M
/* clean up */ ) N; D( \5 c2 C L6 O- Y$ T
getch();
" Q, P! I' A% Yclosegraph();
' [8 H1 x" l% L4 |. X- }return 0;
" ], ?5 L: m/ B; `- g9 e! X* Z' F1 T3 t} ( @& B, ?. d# M% H8 H
1 x3 K+ ^# R: o ^
" x0 e7 T/ d( o- Z! }
5 K8 s7 g- T+ O4 h
函数名: clearerr
! `# g1 J" o+ r7 ^* w; C功 能: 复位错误标志
7 b+ u/ n/ f+ o% h/ u* @用 法:void clearerr(FILE *stream); # M' @ u5 k& z- Y2 U
程序例: / q z! c q8 Z$ @! [ e4 s
#include $ i7 u. D/ Y+ t
int main(void) 6 S5 O9 d' R3 W% f
{
% t* I& M8 r5 a2 _7 ]- z, rFILE *fp;
) [* q5 Z! z8 I) |char ch; % P3 F4 H3 M w! d8 D
/* open a file for writing */ ' Z% Y0 {" m. a7 K# l, N
fp = fopen("DUMMY.FIL", "w"); " O Q" i, G1 N1 E
/* force an error condition by attempting to read */ 1 t+ h" r- C* k# G# a1 X/ Q8 O
ch = fgetc(fp); / y1 i% A! ?! ?8 `( X) |6 z5 D
printf("%c\n",ch); & o6 ^( a' T9 q& T; i" O
if (ferror(fp))
; G# I& G* Z+ J' w$ Q{ % [! C9 V1 X+ _6 H
/* display an error message */
9 z# `% N1 n' f, F+ u8 ] \printf("Error reading from DUMMY.FIL\n");
. V# Q1 l1 {5 L) M3 V3 o, j' T/* reset the error and EOF indicators */ 7 f) Z: F7 [; |2 u
clearerr(fp); ; s [8 A8 Q: b% A; K
}
( L5 Q/ S" _, M+ c% \" V5 t8 V& vfclose(fp);
f$ X0 Q; z1 ]$ P9 F4 A3 greturn 0; % Y8 P6 a9 h- V! p. @! I h
}
, D, W" T3 o, m9 Q4 u5 d! F0 D4 Z' q$ p' a. F, e/ X
: [: z9 D6 x9 N
6 C& i- O* ]5 F1 V# z9 D
函数名: clearviewport
0 ]- A' r1 M b$ [! h2 N功 能: 清除图形视区 S0 P* o7 e ~0 M7 T; P. J
用 法: void far clearviewport(void); 4 f' l- [. }/ U1 p
程序例:
) _& @( p3 r! x) s4 |2 {& w#include
* P- z! s6 e4 n' H2 T: U#include
8 I; E; U9 J2 A' y/ H) W#include 7 k+ {; e8 } O- w* m% X3 y* w
#include 5 [: t8 j, O8 a, R9 A6 a9 D1 n
#define CLIP_ON 1 /* activates clipping in viewport */ $ n4 j/ c: X# [3 l+ M2 I2 l& }/ C
int main(void) 0 ]1 ^0 ]& e! d$ a1 ^. a9 X
{
- I; [# `- l7 k5 G _/* request auto detection */
; @; c' ?0 H9 l) Eint gdriver = DETECT, gmode, errorcode;
8 X5 Q% a4 x$ x' G+ Q$ @int ht;
. t# r6 s# w% b" {/* initialize graphics and local variables */ + A( k' d- y ~ ?" X; M5 p
initgraph(&gdriver, &gmode, "");
) |( E. Q; l) A/ X' A5 h9 b; y2 E9 K& z/* read result of initialization */
& K. ~5 _1 `& u- h0 q/ u9 Qerrorcode = graphresult();
5 L" P9 I* q: W! m% mif (errorcode != grOk) /* an error occurred */
* ]7 d) D! T0 {2 b: i( h{ ( x* \3 J6 t; L+ T7 `. b! C
printf("Graphics error: %s\n", grapherrormsg(errorcode));
/ k- ?3 l& H- m! Jprintf("Press any key to halt:"); , ]& X* o2 r6 j! b3 B
getch(); 1 @2 @9 Y) |# z
exit(1); /* terminate with an error code */ ; R2 t5 o( w8 p% u) V, L
} , _, C- o0 V" a0 [
setcolor(getmaxcolor());
* \0 G# {" Z5 _; Dht = textheight("W"); ) ?, u$ c6 g$ i$ m! l* C' W6 k
/* message in default full-screen viewport */ / I2 g% l- Q3 v; T
outtextxy(0, 0, "* <-- (0, 0) in default viewport");
: K# F, O: l( o- ?! v/* create a smaller viewport */ 6 u* u: ]+ G* Q2 d
setviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON);
1 R0 U. `, K0 ~& C& v2 s% x/* display some messages */
1 T( H/ T+ Y b6 uouttextxy(0, 0, "* <-- (0, 0) in smaller viewport"); - Z7 X6 G' s# \
outtextxy(0, 2*ht, "Press any key to clear viewport:"); 3 O2 d+ N, M; T+ A" l! y
/* wait for a key */
* A' ]7 q6 ~& p! i* r+ r6 Q# P6 Vgetch();
" K2 L& [( `: M1 e/* clear the viewport */ 0 J. G4 v8 Z" Y. Y4 @) E! X
clearviewport();
+ c$ N, x9 b. e4 ^# X/* output another message */ . M, Y' z' E. w! e% ?. R
outtextxy(0, 0, "Press any key to quit:");
5 O5 K& ^( { G+ o* m/* clean up */ & S2 P3 P, C" X
getch();
- ^: Z5 I2 s, u1 qclosegraph();
( ~: m! Y6 r2 ^: j% G0 u" L. lreturn 0; / E+ q% ^6 J: m4 i' @
} 2 v; n+ m& o6 J8 Y F
8 y* S. z D/ d7 y
) \% d- m0 c0 e9 p, u
- W5 |' r! c5 ^函数名: _close, close + U u% c7 {) S
功 能: 关闭文件句柄 - F) }6 U) I. u
用 法: int close(int handle);
1 @, b- a! h# H9 ?8 c: |# t程序例: 9 d5 Q) S4 R( U' y. G, s
#include
8 D( S$ l$ t: J1 q( Y# x5 W/ l#include 2 O& z! \6 u0 J1 M* S4 e) z
#include ( N! m: T7 n! P2 _+ C# L5 g
#include
1 O& @' |' l4 E( w2 H7 |main()
$ v9 ^/ t/ o1 \9 V' n{
2 K. ^3 t2 ?, w0 s! ]+ P- Cint handle; 4 o) n3 t( \ }
char buf[11] = "0123456789";
+ }/ t' ?. s# Q, f( A( g/* create a file containing 10 bytes */
; d W- w- J! h- @) o% r" Thandle = open("NEW.FIL", O_CREAT); ) g" |: c% S# Y8 H
if (handle > -1) + ]8 q) Z2 O9 A( [5 r$ a0 v* ?
{
" v( R" A1 H0 Ewrite(handle, buf, strlen(buf)); ! P6 }) D+ P0 D ?; P
/* close the file */
' H3 W. Y1 r$ ?close(handle); 1 `$ t( A) R- B! O; U
} 0 L H. ^! k( J# \) c! j/ {1 ?
else 2 v; i: k) S7 y
{
! R+ [, N1 F& K3 i- x+ Uprintf("Error opening file\n");
% a* |4 J: n% F}
$ r2 E9 J! {7 F3 o* freturn 0; 5 R1 D/ w3 a/ A* Z
} J. n& d5 ]' F3 |% J
4 W4 T3 T; V! Z; F9 W, W
" x4 T8 _, o0 G5 S7 `$ y ' O- V1 ?# l1 A6 I0 z) j- |
函数名: clock : ]6 V" I, l6 x$ x# m, i+ I
功 能: 确定处理器时间 ! p$ X! h& ]1 c, d
用 法: clock_t clock(void);
& m( e8 G5 T3 y* N程序例:
& \% k* w( l+ k! }#include , J5 K& r8 K' u2 n- P
#include 0 V9 D6 m# k m+ ~1 h
#include . \* [( k- h2 S2 w; e( q4 S
int main(void)
, [; B3 h6 R, Z! X7 e, i' y{ 5 y) o2 U7 y) G+ B2 I; V- p" k
clock_t start, end; + ^* `8 t0 f: Q% Y1 h0 R
start = clock();
* m; P+ o- a, _$ ~# hdelay(2000); ! U& _$ t' @& G/ A
end = clock();
! ?. o% c0 w4 h7 |printf("The time was: %f\n", (end - start) / CLK_TCK); ! W! z5 M; e/ d z5 o) g
return 0;
|1 J5 l3 l" X4 _+ T5 u}
& R+ s) p* v0 G/ b0 J+ X
. Q# |5 C# z4 j5 M1 u+ ]4 u- F, x! |, s; {6 P
$ k3 h+ ?& h; J函数名: closegraph
% z5 a5 T3 f' R# @+ B* d/ A功 能: 关闭图形系统
; d6 f' ?5 [7 f$ P. ~. P# f用 法: void far closegraph(void); % Q9 b, X* I' O( N3 F
程序例:
# m' A' s* }, T- W, q#include 3 y! a. l: X5 k2 _% v" P$ N
#include
# z8 Q6 V: N' w#include
; }- J3 @- W4 U& @& S( u ~$ G#include . R+ h% {" ]- T0 R6 h
int main(void)
4 H; J3 X: C) v+ I* h# u9 l2 S{
6 D7 [! L, _$ U5 b" E+ W+ g ^/* request auto detection */
2 n. `5 S( g4 ?2 \6 K$ g% G0 [int gdriver = DETECT, gmode, errorcode; 4 |8 c) s# \' _$ t {
int x, y; 9 [" p! a/ Q$ q0 H% C
/* initialize graphics mode */
% S% [. [- c/ i. g+ ^% H1 r) iinitgraph(&gdriver, &gmode, ""); # [4 }9 ?7 G6 n# w7 z% u
/* read result of initialization */ - u- p4 Y' U. o# u
errorcode = graphresult(); & u! e" E; C+ F
if (errorcode != grOk) /* an error 0 [. K- j( \) n* G7 B K7 X
occurred */ + _2 o0 f: V3 I4 }) d2 L6 w$ U1 P
{
! h6 [% Z* y6 `0 v, t$ sprintf("Graphics error: %s\n", grapherrormsg(errorcode)); - d' f4 p `0 B9 H$ R
printf("Press any key to halt:");
8 I: ?6 t6 Q) @/ x, Wgetch();
5 b X4 k- P& _1 A& u4 H7 T* Pexit(1); /* terminate with an error code */
0 B' K: S, j+ U: E( g} * q: ~" S3 U4 l
x = getmaxx() / 2; % O! O7 V& \: x( n! h
y = getmaxy() / 2; - \. r2 M8 O! u* @* s3 x
/* output a message */
; o% q/ K7 Q; r" g( ?9 e$ msettextjustify(CENTER_TEXT, CENTER_TEXT); # O+ }$ D8 l+ B( `
outtextxy(x, y, "Press a key to close the graphics system:");
* V- g* M9 w4 o- {/* wait for a key */
2 `9 R* T% ?$ |( Ggetch(); % f! x$ s3 { n& r# M
/* closes down the graphics system */ # C. s! Z! U- B9 y
closegraph();
* A) F1 Y. Y) q( `printf("We're now back in text mode.\n"); ! v& p `% C, c- L1 H4 w) G2 a
printf("Press any key to halt:"); ; s+ y' F$ t- K* Z& Z
getch(); ( p4 X/ L: y$ k2 _4 V
return 0; % o0 o% Y6 k' t" y8 b
} 1 h0 ~3 v8 }. A' W# }
1 {9 ]7 W& M( `% H) S2 U& b! o( u: ?& _- E) R
1 G' a5 w6 O! B- s c函数名: clreol
6 F8 e: i$ V( h功 能: 在文本窗口中清除字符到行末 , X& t% ?! U3 j3 c
用 法: void clreol(void);
5 g/ |9 n' i: C0 T T程序例:
. F/ B5 G6 U# G6 d#include & c, m; {, |8 {4 [, L
int main(void) $ t' i; i8 J9 L. Y; S6 |& y/ g {
{ ! D5 Y. K! y' z# b+ O4 p( G
clrscr();
8 {' Y' O: L$ W; p. v/ g2 ]cprintf("The function CLREOL clears all characters from the\r\n");
+ T1 v: s' I, T- Bcprintf("cursor position to the end of the line within the\r\n");
8 \0 m9 W6 [: x4 v- x" U# Icprintf("current text window, without moving the cursor.\r\n"); ( h: ]9 O2 _! q; l6 q
cprintf("Press any key to continue . . ."); & y0 k- m ^) e r- }
gotoxy(14, 4);
! a3 J4 G! F; U/ Y4 Z/ Rgetch();
' R( u8 N9 M4 V& ^" L% x3 jclreol();
% x- K* A8 k% H3 X3 M- g) Kgetch(); + e+ J& }. U7 V X) ]9 U
return 0;
, q) b8 a' f7 S: e; _} - N6 E$ n; }5 Q6 D8 Y c0 z
5 t1 }6 s/ m% p; [; l0 c9 Z7 ?: f6 e8 g; s' z7 W
+ ]( D/ u0 {% {1 g% k v N7 M4 p
函数名: clrscr ( T5 N, H/ b# ]/ t
功 能: 清除文本模式窗口 8 Z- X- u- U$ v, H
用 法: void clrscr(void); # l6 W7 S5 |( n6 s6 O) n5 r; H
程序例: % D1 i3 m6 _: L7 X. e+ U3 n
#include
7 Y% a( {. J# J; Nint main(void) - K+ I9 E. `/ G
{ $ D1 x( x9 c* g1 v: q% o% j
int i; 4 Y) @5 B3 e; Z$ @; m
clrscr();
7 J# u) s& S0 {for (i = 0; i < 20; i++)
3 B8 X- j% B X) ^cprintf("%d\r\n", i); 5 [2 v0 K4 I! Q( D( }% A" S6 @! `
cprintf("\r\nPress any key to clear screen");
! Z0 y' c- @, e2 ^3 k: ugetch();
/ M& g+ w% x% m0 d& Gclrscr(); ) u' @+ j- v: ?( b" h9 x7 t# Q
cprintf("The screen has been cleared!"); & F3 w2 }8 o# a* {( t- k
getch();
z$ j) O+ |8 p7 m: x( N: h; Freturn 0; 2 U* \$ h8 \; Z) c& H( v7 U
}
" P. q, ~ e( V& C% s( s1 |1 p' x" Y
2 |* s& P) e: e+ g
5 s' p6 _0 C5 p# B/ Z( ~6 U函数名: coreleft 2 O1 |% o* c3 K- k( p: K, b
功 能: 返回未使用内存的大小
; M( H( l9 s' o" J: r D用 法: unsigned coreleft(void);
1 c# ]! M! y9 k! {5 n5 w程序例:
( c6 J3 c% u; \' x; t. ^#include 9 H+ Y" j: R- q$ D- G# R
#include & H& q+ b& R1 f6 U
int main(void) 9 z- x. E1 K u8 v. k5 c. |, G
{ 6 K9 h, B ]# z4 O- O, f# _
printf("The difference between the highest allocated block and\n");
" c/ X* g" [% }$ J% Zprintf("the top of the heap is: %lu bytes\n", (unsigned long) coreleft()); 9 f& }0 D- o0 F0 \, P% S
return 0;
5 I3 H, b' V8 h! h/ q6 i} $ `6 Y& o1 Y) R0 n5 x3 _
' x- s7 F- N! |5 O4 I函数名: cos
! Q- G8 O2 H% P# L, Z/ a; K+ }) i功 能: 余弦函数 * M: U) n& v2 O
用 法: double cos(double x); 9 I- J) S5 _, J3 ]3 F
程序例: & ^1 J4 n9 ~7 u+ K$ O
#include
6 x& d* w1 S# ^4 q7 y' r5 ?#include
, {0 D1 O' O% K7 S* X/ H" J, L$ m" Uint main(void)
6 H e2 e7 ^1 X w9 T- y- }{ * G* Q( {5 x9 O( W- c, y7 X
double result; ' b9 }( B0 U& O9 R H" p2 W6 @) d6 a) P
double x = 0.5;
# `+ T: g$ L! _5 h; W$ Y0 l6 R5 }( Dresult = cos(x);
% |1 k; d5 W( c7 c9 Qprintf("The cosine of %lf is %lf\n", x, result); ' [% V$ J3 {, T' [
return 0;
5 m9 J6 G$ b/ z- q" \}
7 T4 |% q$ n) {% v
) |9 P% r# k, j
4 B! J& Z) {8 i5 J- l. o
) ]. A% s" I( j3 [; [函数名: cosh : K) d7 r$ `% B, \
功 能: 双曲余弦函数
) `" S9 M* I0 Z4 `' C用 法: dluble cosh(double x);
3 s% X3 [4 b3 o" q0 j1 O程序例: . R6 l! W+ W- w; u. ]" o
#include * Y2 o5 N! o5 _; B6 P9 W
#include
2 @ [& e% X5 ^2 T/ O' A2 vint main(void)
' v7 ^! ~) z# P+ l8 _! b9 }{ 8 p+ ]* i5 y) H6 ]$ I
double result; " U) B- ?7 E5 y) L; \ t8 ~; m& l
double x = 0.5; . L7 A4 ?2 \0 v2 J9 [( K, n4 J
result = cosh(x);
( V! T* h+ B ^, @1 jprintf("The hyperboic cosine of %lf is %lf\n", x, result); 9 D$ B0 K$ x. \. l* m+ ?9 E7 z5 a! x& G" P
return 0; 2 V8 y9 |- w: v* F! j' n
}
5 _$ b- n; z& _* `7 o1 B6 _" C) k
7 X- J; B5 `) _( h
& s! R j- m, h1 e t
/ B$ h7 z6 h4 p. z0 @8 ?函数名: country - h( i m2 Q/ D: O
功 能: 返回与国家有关的信息 6 o2 L" C. j: h
用 法: struct COUNTRY *country(int countrycode, struct country *country); ; B' E# q; Z2 t7 v! D- w4 H
程序例: * G i4 Z) C6 |& A) o: l' d/ ~* [
#include * E& A( F" a. P& v) a
#include + a# O% c: R. k# |9 P
#define USA 0 5 {0 z$ h) L7 q0 O# O
int main(void) - ]3 Z$ f- H: n3 A" t9 }& u
{ 6 H- b5 y+ _5 N
struct COUNTRY country_info;
& x% r3 q" J: @" Z* U9 Icountry(USA, &country_info); 9 @2 p. l" ]; a3 y6 J; o
printf("The currency symbol for the USA is: %s\n",
. T* E& ]2 W' F2 k4 ~% bcountry_info.co_curr); + K1 d# H3 `( ]2 b5 x# f. e0 z b
return 0; ' b: ?9 Y, X: c8 \; g! g
}
: K: }/ d: ?) ~' n8 B Z; k8 k
6 o3 k& x6 |& M& c" D( g
9 I' q) y5 c' M O" X6 q
" F6 ]1 m% v5 t- u" b6 {* p, M函数名: cprintf : u1 s! z3 e, m$ q* D5 t2 k
功 能: 送格式化输出至屏幕
- n& |$ [% {) m" _. v. O; i- [用 法: int cprintf(const char *format[, argument, ...]); - t# x$ [+ \+ F* P: m
程序例: ! ?9 R) i9 i+ `* \. G7 x5 q
#include
) R, i3 O- Z# ?0 m7 u; Lint main(void) 2 P7 K7 s6 n* x3 q, I* t
{ ! }; J j. g* @' m3 N" [
/* clear the screen */
- u: ~; m8 b* R4 I7 c0 Bclrscr(); 0 a! h- z2 y0 M/ v/ W
/* create a text window */
B2 }$ O- A( Wwindow(10, 10, 80, 25); % {4 D7 E5 W9 D' y Z
/* output some text in the window */ / z( a, ?- z6 C
cprintf("Hello world\r\n"); 6 M% p2 g- f! v* Y6 p
/* wait for a key */ ( f/ k2 @& y% p: ?# m7 G1 O4 n
getch(); " m) w3 s; m0 z) I c( H
return 0; % ?) {% U3 k% K6 ]. y5 U4 s. k
} 9 J5 B9 f, V+ Z: ?& k9 F: w. K
: A( Q/ K' C/ D& M/ l1 [2 [- s7 F. ^
6 r3 ^( }7 N: d- u% F$ l3 F函数名: cputs
8 b* d: J+ Y) d: C5 }) Q功 能: 写字符到屏幕 0 {; @& {2 Y) C' l
用 法: void cputs(const char *string);
% h1 q# H( z! X8 W+ z$ v- c8 ]程序例: , u) x4 z- M- B% m6 A. x
#include
8 \0 Y2 M, r6 ]int main(void)
7 M- |8 }8 f; K- @{ 7 X0 `0 l7 ~! v, d/ \) P
/* clear the screen */
, ]$ z7 ~1 z' N/ Jclrscr(); % K- t% p5 O3 g( y
/* create a text window */
( U' x2 z' P2 U' `window(10, 10, 80, 25); & E4 J4 e6 c4 t2 ~
/* output some text in the window */
0 w; N3 z. h" C" acputs("This is within the window\r\n");
- j8 K S+ w- W1 w3 _/* wait for a key */
5 O5 g5 E: B3 J0 H, C8 B1 zgetch();
/ c: X k- `3 Ireturn 0; ; I$ {: z1 ^2 i( M
}
) W4 a$ [3 g/ P( s& k3 v( Q) m# O5 W
6 ?! r7 S; m$ C' _6 u* ^1 e ; C# o( X0 S8 M2 j5 p% c
函数名: _creat creat
1 o; f+ J& y, T. R' B" [功 能: 创建一个新文件或重写一个已存在的文件 . T: M# K2 [3 y/ w7 _$ h1 Z. M: ]9 e
用 法: int creat (const char *filename, int permiss); / N% l/ i* @0 h$ o' j4 Q9 w; P
程序例: * R) \1 P! ]& ^ J% |# P9 H
#include + k; n3 b' O, |) A% v
#include 1 v, a8 g% `5 V5 I& @. z
#include
! M- U. Y0 |$ ^% _, \" D#include
, d- k- s. {5 x; g }int main(void) + L/ j# x# H% p" { I
{ ) k5 f, i0 ]# d0 Z
int handle;
" R( W7 A* x& T& Q; T5 ^9 X5 ?char buf[11] = "0123456789"; : Q+ f0 i' m8 E/ x
/* change the default file mode from text to binary */ 6 P( }+ P7 C) q' k! U+ r k
_fmode = O_BINARY;
4 M- k( [/ M: e/* create a binary file for reading and writing */ 1 E2 r0 Y" M) c- r- M
handle = creat("DUMMY.FIL", S_IREAD | S_IWRITE); 9 n, t( I/ \" Z" r% Q1 e) N: g
/* write 10 bytes to the file */
: x0 m1 ], y, _) Hwrite(handle, buf, strlen(buf)); ) I" u% X# C" r
/* close the file */
& n: m. [# b. R# u- |5 hclose(handle); . s; a, L1 R. X0 l7 }
return 0; 3 C$ m4 W( V4 E- d3 f% \
} ) M" s4 _8 _& B! S: y" x Q
4 e5 l2 ?; |/ ?& X函数名: creatnew + }: {& Q' c% r0 F* d
功 能: 创建一个新文件 & b: i' V M1 p/ A; W
用 法: int creatnew(const char *filename, int attrib);
8 f! H7 f% @# y9 Y1 |0 L程序例: ! }# t9 `0 \8 N+ C/ E L, ` r
#include
0 `0 u9 u- E2 `. y7 [- `6 ~- Y6 D#include
5 q6 O: {8 w( W- r#include 6 Q7 b% \( j) v- W9 A2 C
#include
+ g; d9 ?4 p3 m0 v8 p' Y#include
# z3 s+ c5 o: V7 V& ?6 uint main(void)
. D" Q/ F. S7 @( M8 B{ U9 p$ r1 a. ]- q
int handle; " m# @# u% r* `' p/ S$ X: m
char buf[11] = "0123456789"; - L8 M$ {$ w+ U8 p1 M& l; ^4 d
/* attempt to create a file that doesn't already exist */ 7 r, o. f* h$ F& S) J
handle = creatnew("DUMMY.FIL", 0);
; o& d' |! a0 l* y' [if (handle == -1) 8 J" L: ]1 ~ G; I
printf("DUMMY.FIL already exists.\n");
( B/ \, N$ J9 J* @# X$ g$ d+ xelse
& ?9 Z! I" Y* t4 F2 U7 f* g- q& H- H{ 1 b4 F! M! K/ R3 d9 }
printf("DUMMY.FIL successfully created.\n");
$ k6 I7 h! g; ?* wwrite(handle, buf, strlen(buf));
# n! \# ]) H) w! o0 J. |! mclose(handle);
. G0 P7 ?, J! m" ~+ m& T: p0 r}
. }2 O ?$ C" D$ z2 w6 w- `return 0; . }8 b* l$ j/ M
} * }8 G1 [( C* V% F7 E
( Z' v& U: A- R I
& Z/ I* @- E6 K
' I& P: Q! S( E7 j8 |
函数名: creattemp ) B2 x, ^+ a, _% x! T9 D
功 能: 创建一个新文件或重写一个已存在的文件
" n8 T: Z& e5 V用 法: int creattemp(const char *filename, int attrib);
9 p& J# W8 f& c8 r程序例:
- u: N* r, ] a5 Q3 D6 L#include 4 J/ y5 V2 K! ?" n8 Q
#include 5 x5 o D3 z4 M* t2 k: I
#include 5 l& E; ` _% }5 a" ?& i1 o& r8 {# C
int main(void)
5 }0 ]9 @& d3 {: i- l9 B{
3 z& ]$ R. C( @$ E2 t' Dint handle;
' b) U) A- m3 G9 f- E( Uchar pathname[128]; 7 r2 J' \& H. x; J
strcpy(pathname, "\\"); 2 L8 k( M1 V6 ]0 Z# k: \
/* create a unique file in the root directory */
3 b& W, P3 B% Chandle = creattemp(pathname, 0);
2 E7 Q2 \, d& y+ S+ G/ y4 qprintf("%s was the unique file created.\n", pathname); 3 I' w' U) v# Q1 F, k3 s( Q, K
close(handle);
) N6 u/ F* t% L m2 @return 0; 8 c2 C0 o7 M) G# s& g% @/ s
} 2 h/ g8 Z o! B9 P" D4 e" I1 J
2 E i9 w9 a, Z: c) L& Z
o$ ]6 [6 E: o1 x4 Y2 k6 K! t
1 j' e, F8 i+ I$ S函数名: cscanf
, }4 L% a2 r7 r% X) [) x0 c功 能: 从控制台执行格式化输入 4 ^2 k& j1 N0 e! {; v
用 法: int cscanf(char *format[,argument, ...]);
' U& u/ c6 R: f程序例: ) Z; G/ Q5 u( O% U3 a8 O$ r8 J
#include
~1 m) u( J/ `int main(void) 7 d7 T# a9 Y) |' U9 m4 {
{
6 {# y" |5 z$ N% wchar string[80];
( B) {: ?2 J4 R! l. m, e/* clear the screen */
" g! K) q: M) S$ A& [7 J U" dclrscr();
% O/ u$ u, z+ m/ B3 z/* Prompt the user for input */ + c' k& t' Y: I
cprintf("Enter a string with no spaces:"); % K" x5 {9 j* e) _* s# Q1 t! h
/* read the input */
4 a) X# Q/ S. c3 [7 acscanf("%s", string); " Y: D# Z$ U/ A6 g
/* display what was read */
$ L* \, z% x3 f7 T4 G; ecprintf("\r\nThe string entered is: %s", string);
) L* k* E: C* W8 P5 J" @9 Kreturn 0; & f1 d/ R( P, L3 S1 y7 a( ~
}
7 |0 h" n/ V) C' s* T1 o' g# F* @% q, G
5 Y7 _5 H" U0 d F; Y & k$ C8 _* B. z" s. n$ @( z
函数名: ctime ' {$ ?- v9 J8 a0 a: V& n
功 能: 把日期和时间转换为字符串 & U: w3 j/ w* d
用 法: char *ctime(const time_t *time);
' v2 I+ U8 L# w% k) O% o E程序例: ; d& U3 Y$ h& N7 ~" V% x
#include
7 |: T, Z+ q4 n' a8 |' ^, A3 d#include 8 `7 Z: A$ E5 L% R B1 u
int main(void)
- l1 _( y0 I9 _* \2 M, r{
: e! j G3 t5 g! otime_t t; & z; I4 @1 b9 |) y$ M
time(&t); % R6 y$ r$ K' V( k
printf("Today's date and time: %s\n", ctime(&t)); / _# U5 b4 Q* J
return 0; % j9 u5 U$ c0 p- ?. r
}
0 `2 O9 a) A. b! O* [3 R. m
) _6 }2 B9 W* L' J+ W% A
& x4 K' K1 Z1 t3 G ( x& J, x# Z- `& ^& _7 G. H& V
函数名: ctrlbrk
0 c3 M3 Z; G. [! A3 t7 b功 能: 设置Ctrl-Break处理程序
' ?9 J! d% w6 [6 @' h用 法: void ctrlbrk(*fptr)(void); ) _ O; `) p1 x6 m9 S r, D
程序例: ; ^3 K* r! g H+ l5 L/ F! w+ h* J
#include
' Y$ g; B1 q5 X#include
2 r: ]2 a, ?' C& c5 b' H, h#define ABORT 0
: A( d; j/ @1 y Kint c_break(void) * j. T, }4 N2 ^- H# H: {0 E, @
{
% U+ m; b' a5 Q0 [& b. J4 Yprintf("Control-Break pressed. Program aborting ...\n");
( ] h- I+ J0 ^* \return (ABORT);
$ q' I- g/ [# Q4 @} U% d8 J. L+ ~6 U J; g) _2 z& w
int main(void) ( y- Y# F$ P- e- T0 d2 m
{
/ ~* p0 }- `8 S6 a9 ^7 Actrlbrk(c_break); N9 p" ?0 E$ D, _3 {
for(;;) 6 Z# |/ y' X9 Z/ L. B& P }0 v# y# i
{
. G2 H0 H5 N- B, B* V: oprintf("Looping... Press to quit:\n");
c: h a7 K% c. _* K}
! K6 Y7 k8 ?. V! }2 a3 Qreturn 0; ) k3 @8 Z& ~/ ~
} |