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