函数大全(c开头)) j q( w: p2 e+ X
% f# i9 K1 r1 U) U `
函数名: cabs , v& h; H3 W6 f6 ]; ]5 _7 B
功 能: 计算复数的绝对值 & N. s! }! T5 Y) b0 E
用 法: double cabs(struct complex z); # |" O D$ l2 w! `0 O% m! ]5 C
程序例: % Z6 |9 K& _& _1 _0 P* g( \/ O
#include 1 f+ c/ Y, \3 f# R B( d1 V
#include
1 f4 |( r3 E, @0 s! Nint main(void) " k! I1 h2 X9 L0 |
{ ) ~; f/ O j0 M2 N: m* t9 b
struct complex z;
0 C. T: e1 M1 d' rdouble val; . ]3 D+ g* B: m$ V& s* x
z.x = 2.0;
# ]7 v, x1 x: K' s! N2 K U( Hz.y = 1.0; & p( R' P6 z+ d2 C
val = cabs(z); 2 i2 Z0 f+ L! b8 p& R3 X
printf("The absolute value of %.2lfi %.2lfj is %.2lf", z.x, z.y, val);
/ a4 q4 e) {+ q2 y7 [ x( m6 Xreturn 0; 5 J- w* _, P* X# A7 R4 i0 k
} $ z' m: c- H6 |
" h$ ~& G0 T* `1 Y! K
3 g ~. O- f9 |3 n* s5 c, x ! n, c3 R' p3 @" g- n
函数名: calloc ! c1 A1 n6 i( G; m, V5 k
功 能: 分配主存储器
, k& Y3 G; w* a: ]+ G; X8 q9 X5 d用 法: void *calloc(size_t nelem, size_t elsize);
9 p" Q, j8 z/ v: r程序例:
. y5 N4 X' H& k8 b#include : _) E# Q0 y+ t* o* V4 ^
#include
( J2 p4 Y) j7 [int main(void) 4 l D0 l+ u( \( D& l) k
{
: M- h1 E# X1 h* W' Bchar *str = NULL; 8 _% V+ o1 Z( S* R( g* p
/* allocate memory for string */
/ X; m4 l3 w+ e: tstr = calloc(10, sizeof(char));
( C5 f$ T' H( T$ @: g# s$ I/* copy "Hello" into string */
/ q' A" {6 I- b& j9 t" Ostrcpy(str, "Hello"); 6 S9 L" x; c( N' ?9 h) o
/* display string */
' w. ?$ e; k' j; Oprintf("String is %s\n", str);
; m5 J3 ] ?0 ]# x" H6 M: W& M/* free memory */ 4 C! h( P# D6 H/ m, w
free(str); , y, c, o# a% n1 z
return 0; ! u0 G6 Z8 t; h6 x3 l% w
}
/ i4 i. g2 \7 p
# |+ ]% u$ w$ K2 r
7 |& _4 x" I$ [) V* ` {3 p
7 @; e. x! U" O9 n; o函数名: ceil % c! f; P; Y7 X; C6 K" z
功 能: 向上舍入
. n- u$ ~+ x# |! @3 X用 法: double ceil(double x); / u1 a5 B9 [) M( c# o$ P2 v; I
程序例:
" n# Z) ~9 W C#include
' x" V4 p: C4 f( `9 [) R#include % @( D, A/ ]- Y/ N# E
int main(void) # v( Q( l7 I8 B% C( V
{
, N, D6 @8 P. }# x3 ]) |* \double number = 123.54;
# G3 B' @; G. x+ f Fdouble down, up;
- w7 F4 H! V i5 D+ Ydown = floor(number); . m# W. R/ |! S/ Y5 n8 \4 @: P- Q
up = ceil(number); $ r: z3 R q6 w* {, J8 W
printf("original number %5.2lf\n", number); ; w- C3 ^9 M5 \; e$ H
printf("number rounded down %5.2lf\n", down);
$ {# b# s; w' e( d% b, u" Xprintf("number rounded up %5.2lf\n", up);
5 u3 f R8 s- H. Y. D5 N; creturn 0; " u7 j }0 Z9 O& i/ f7 q1 k! Z- y
} 7 W! z& C& m7 t: z+ u2 `
: k6 [. _4 g* H/ P8 D4 B- U1 \. t2 ]3 r2 l8 Y
7 o6 ^- k# L, _- D' U函数名: cgets 8 p) K# ~) H9 s
功 能: 从控制台读字符串 7 V3 z' g8 i+ y8 I
用 法: char *cgets(char *str); 9 f0 L) W- d! x. [
程序例:
& h8 K+ p/ z5 B3 H5 g: A8 c#include - \' ?% s6 G+ S! Z, c6 c: P
#include 2 s; F' N$ _$ r2 ]) `- o! m9 s5 a
int main(void)
" E8 h0 ?9 J% p' s{
3 N* F& } p* ]* W% h" L1 wchar buffer[83]; $ L: A# `& u! U6 }" T) L# c: F
char *p; ' r, L4 k, ^' L" k7 V
/* There's space for 80 characters plus the NULL terminator */
8 i1 n8 p. w- L5 ?* H! p5 d, _! [buffer[0] = 81; E' E3 o/ W: I0 i
printf("Input some chars:"); 0 [. D6 U9 m1 j
p = cgets(buffer);
2 w8 t: @: b5 z0 D5 K) y8 ^printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p); & ^: N3 L3 Q/ l: H
printf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer); - _' ?' a8 L _9 S2 f. N+ x
/* Leave room for 5 characters plus the NULL terminator */ ; F, @; G- u2 ]8 K( J
buffer[0] = 6; ' G2 q) O) c8 S6 E1 C' b$ w: d
printf("Input some chars:");
: _3 o6 @: j, W$ A5 e: E3 x, `p = cgets(buffer); 6 x& ^- e$ \. G" l
printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p); 0 d" n/ K: v2 }' }) ]
printf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer); 0 i, G2 d2 L" P$ g
return 0; . F' J8 c; C+ x6 U
} : M/ u% \* v. l* Z
$ |% b7 U$ z& \# _$ F
* q/ m3 G9 Y4 C/ X0 \6 V% F 5 j% w5 j4 |. r% { |! `" O( Q6 m5 q
函数名: chdir 3 V9 ~1 Q' g( D' Y. t
功 能: 改变工作目录
! g d1 z8 S& Q( F3 m) m' a用 法: int chdir(const char *path);
: U. A/ _8 m7 M2 |2 i$ D程序例: 0 I$ Z |) G9 I! }6 q
#include ' r! r, H3 u3 q
#include
+ h& _1 C" A+ u" i2 W# [4 s#include 8 p, t+ u# k ~/ }6 S5 _+ a$ u
char old_dir[MAXDIR];
& P: w) N( ]7 V% ^8 r' uchar new_dir[MAXDIR]; ' l3 P5 P5 x9 V0 S/ z% J
int main(void)
; m, z5 N# k1 d+ E: D{
" j; K' Y1 z5 ]! y, n- ] [% a+ `if (getcurdir(0, old_dir))
* K) M& A2 O$ H! @# p& R) T{ ' R6 `3 @' X; O) n7 a
perror("getcurdir()"); 1 b) |0 T9 g! [9 b/ o! J" j6 L
exit(1); 0 ~, E7 y1 m3 Y1 Z1 I Z) m
}
* z( F$ Q2 S) |8 [ ^2 nprintf("Current directory is: \\%s\n", old_dir);
1 }( @( e" Q* l0 _+ S5 I/ Fif (chdir("\\"))
- q; S. d7 Q2 L/ F8 }7 h{
# N! X2 ?& S( G" n8 D) Aperror("chdir()"); $ s% @& t: t: W
exit(1);
7 E1 t- M* f5 J' @' D( O}
( P7 J) |% ~3 |% gif (getcurdir(0, new_dir)) * S8 z. Q6 L- Z; ^
{ 9 j6 Y" G4 L+ q' t% J
perror("getcurdir()"); % O$ W9 f: `. @) u' J
exit(1); 8 Y( c2 S! ~5 P1 I: Y9 z
}
- d% [! W6 W. D$ A' h0 G$ t. yprintf("Current directory is now: \\%s\n", new_dir); 5 {* z2 S7 k' [% c" L6 `: L
printf("\nChanging back to orignal directory: \\%s\n", old_dir);
( I5 K. |7 T. j+ S& eif (chdir(old_dir)) * o, n, h4 W: P9 I3 [
{ " W/ z$ ` D* |* X+ i
perror("chdir()");
9 d) ~1 t% c, m- t/ ^4 c0 A& I& Eexit(1);
$ Y4 K7 C5 L, ^' o( x1 ~} 3 f: V3 j$ j* |9 v8 M
return 0;
' u6 r( P! g4 k( S/ _}
& M. T1 M5 v. j1 A) J: V+ S$ V- h7 |/ K
4 g3 q& ?$ S3 f; m
函数名: _chmod, chmod
u2 C, @% i* [; }7 ~0 c9 e9 v! u功 能: 改变文件的访问方式 6 U1 B' c/ a, I9 I& F
用 法: int chmod(const char *filename, int permiss);
* p/ ^( {. ]+ U' m: D程序例: # J6 O8 m* x3 v+ }# q9 y3 q
#include
, @7 d% n3 x! e5 G) b#include 1 B6 a! B/ _- Z8 g. h, ~& q
#include - _- s( d" U0 p
void make_read_only(char *filename);
" l/ Y; b& p& { G9 o, Q- Nint main(void)
+ h5 f9 C5 ~% W( C) z" C( y{
: y7 L( z' n9 ?4 ]# Rmake_read_only("NOTEXIST.FIL"); 6 _! R$ O9 |) f
make_read_only("MYFILE.FIL");
# j I6 D, M' _) B4 p3 R: Creturn 0; 8 U' ]! A3 Q9 C: f
}
2 a0 V# ~8 o4 G3 _: t6 _void make_read_only(char *filename) 7 C7 j7 P/ }! s3 }8 B
{
$ |3 l3 j L8 yint stat; & ^ E5 w6 n U+ A' u% p
stat = chmod(filename, S_IREAD);
4 i( _7 k* S7 P6 g O" kif (stat)
# f* Y3 A- D7 A% a2 i% \% [' Sprintf("Couldn't make %s read-only\n", filename);
$ u5 Y7 @- z# ~' ~7 @# Qelse
! f# W M. C$ S$ g& zprintf("Made %s read-only\n", filename);
4 _0 l9 }( A' c. g9 w}
0 P. j% {0 _# I1 P1 ?! S& E0 T
9 V o' W v, i# h( y
7 o; S* ], Y3 O7 u1 p$ a! b) p
3 j3 u. \/ t) I2 W) F% v/ I函数名: chsize ( o+ [0 h1 ^' S
功 能: 改变文件大小
/ g( o$ v4 q3 O- F$ W用 法: int chsize(int handle, long size); 0 Z6 E- t% N7 J7 O8 k
程序例:
3 {) N* t/ \2 q% j# Q: X0 D$ k#include / B. \% _7 p3 ]
#include 1 u9 z* i* k+ m! [; S
#include
' U+ X, Q2 ]0 bint main(void)
4 |8 V0 W8 Q) n* i7 D{
" v" |3 e- v7 qint handle;
' W j/ T' V( m' ichar buf[11] = "0123456789"; & _4 c$ ^6 w/ b9 \
/* create text file containing 10 bytes */
) `' i0 A9 I0 ?& h A5 o& g+ Z' c' mhandle = open("DUMMY.FIL", O_CREAT);
1 z* C. ^! ]) \0 K3 w' @7 [write(handle, buf, strlen(buf)); |' ^3 C" X2 ^. P1 g3 s# t5 U/ v
/* truncate the file to 5 bytes in size */ 6 J0 w! ]% m$ J1 i+ q
chsize(handle, 5);
" o. ?2 B- j: I3 C7 X0 D/* close the file */
& I' u& P+ j9 s6 p2 jclose(handle); . O6 n0 W3 z* H* B: G9 W
return 0;
8 L2 }- u/ ?- u5 ~# Z8 p0 ?, g P} ' c3 ~2 S4 ]7 {& O. T G
% Q G" i& ]2 B
0 |% K; Z4 O: t, s9 d$ `函数名: circle
" {* h& P& F9 E. A# ?功 能: 在给定半径以(x, y)为圆心画圆
% c+ {+ l- C2 J用 法: void far circle(int x, int y, int radius);
8 L4 C. N' j1 g程序例:
3 O! B: {6 M& b1 t+ a0 D#include 6 L9 S7 ]; a, a: }0 U1 Y. q
#include
7 K" ]6 o+ ~' N- h& a# f) y#include " L2 }; n( x1 L; `, {4 Z
#include
{: `' j% t7 d4 B3 A) _) D' cint main(void) 6 E+ |/ ?1 C* U4 G1 t: _
{ 8 X" j4 B: l* e: r
/* request auto detection */
+ b* ?: E$ u( v1 H! O2 Pint gdriver = DETECT, gmode, errorcode; 0 b- ]3 @! k: Y; e6 x. G
int midx, midy; # [/ C) C9 t. c) P
int radius = 100;
Z7 O9 @/ c( j! r: T1 X/* initialize graphics and local variables */ 0 ?* K# J1 S! W: e @
initgraph(&gdriver, &gmode, ""); - N# Y! f" \$ B" ~# b/ t2 Z
/* read result of initialization */
) D8 j7 B$ m' \1 ferrorcode = graphresult(); . p8 @5 r6 r% |+ ~
if (errorcode != grOk) /* an error occurred */ $ k1 }8 D+ Y/ V5 x
{
+ e0 K, X/ P% j) cprintf("Graphics error: %s\n", grapherrormsg(errorcode));
0 ~7 p' M' @8 u' P( n2 ]! Jprintf("Press any key to halt:"); 7 g! ?$ g: {5 W2 i% f
getch(); # m y$ I' D( k8 i
exit(1); /* terminate with an error code */ 4 ^ s/ ?0 T7 V
} , j( h4 S$ L. G0 e! T
midx = getmaxx() / 2;
, C% m; o: e2 smidy = getmaxy() / 2;
3 d# z2 y( x- c+ ysetcolor(getmaxcolor()); 6 o3 p2 L4 Y* p W
/* draw the circle */ 3 @) l7 S- B' G' X
circle(midx, midy, radius); J( V7 i5 E' ^) m% q; s; |( }
/* clean up */
3 ~8 P# g- O+ xgetch(); 5 c4 m4 A: }- |+ I% O" o: y
closegraph(); 0 l0 t5 Q( y/ U1 J: b4 M
return 0; / G1 y; q$ F" a0 _ Y: K& m
}
/ N5 {' Z6 [6 S3 F# Z8 z- B
. z5 K% M: i) Z, L
4 i/ C7 t0 |4 w1 E1 I
2 V7 u5 S9 ` K) |4 K函数名: cleardevice
8 J, B( e9 r5 E. j1 H' T. O功 能: 清除图形屏幕
6 D2 G* i; m6 C/ X/ g! Y. _2 e用 法: void far cleardevice(void);
g i) I# c; F/ @, x8 \程序例: 6 M; K, P; j1 b8 `0 L+ z1 Q: D2 X
#include
% {3 n- R! |/ P; @5 h#include 3 W) ^6 C9 C# d
#include ' A2 t; }. I3 _
#include 2 Q6 f! o$ g# c0 q
int main(void)
& g- c. H- j, A3 U{
" `8 [( y0 f6 T! d7 B# U( W/* request auto detection */ 5 k* \1 E. a( {# ?
int gdriver = DETECT, gmode, errorcode;
# i& ^! O) g# W' Y; Z/ _int midx, midy; ) n: V e0 P0 W1 A1 `6 i& ]
/* initialize graphics and local variables */
2 n1 v7 ?9 w/ R2 ]. Oinitgraph(&gdriver, &gmode, "");
& m e4 O. d8 R- e3 G% O/* read result of initialization */
7 M; F3 b; K+ ^; ^. V% perrorcode = graphresult();
9 }6 Y5 `0 }. @ A; @if (errorcode != grOk) /* an error occurred */ B5 T7 r7 x$ e* A: J
{ : X( _) B5 `6 |2 k4 e) a
printf("Graphics error: %s\n", grapherrormsg(errorcode));
0 k' ]3 |/ }5 j# Q1 Mprintf("Press any key to halt:"); : Z, U+ q& Q% c0 D0 |9 }. e
getch();
- n: k4 k, `6 S( Qexit(1); /* terminate with an error code */
2 O# h& f; A6 P# W2 e}
/ U9 a, f7 |+ w/ Q2 Omidx = getmaxx() / 2;
- y7 x3 J# h& o: e( Vmidy = getmaxy() / 2;
2 t: X2 L3 E# C1 J9 j4 G$ wsetcolor(getmaxcolor()); 7 d! f2 z& m0 Y
/* for centering screen messages */
% h3 ~1 E2 _ \' [" x. psettextjustify(CENTER_TEXT, CENTER_TEXT);
. O# @7 d/ r% P" ~) R/* output a message to the screen */
! P% E% ?, F: V) Y# n2 Routtextxy(midx, midy, "press any key to clear the screen:"); 5 A7 Q( u4 Z6 R8 B
/* wait for a key */
5 }0 U( y; G2 ngetch();
% ? {! h" ~+ T" |/* clear the screen */
3 ~1 K/ c3 ^' O; i; e) J+ Icleardevice(); N5 Z' S$ R/ u: \2 X
/* output another message */ ! ~2 p6 z5 Y0 c9 o2 f9 X* x" {
outtextxy(midx, midy, "press any key to quit:"); # Y6 c( D3 P9 C7 |0 m
/* clean up */ 1 a& d7 N/ C0 u$ c2 s) y* R
getch(); 9 \/ _) M- A. \, h" V Y
closegraph(); ; G! T, |6 Z3 q
return 0;
3 ^1 P0 p1 M" U! B}
, X8 Z* P( U7 w5 s( H9 H: |, o( |. f; e: I
) G- o4 e0 Y3 u2 z) x
; i3 J. H8 A$ b( [; {9 }$ w* G+ Q函数名: clearerr
% B) d' \& J: d, Y功 能: 复位错误标志
6 z6 X! ~! e2 f8 h0 I; {8 H用 法:void clearerr(FILE *stream); ' y/ Y0 \( f; {3 i
程序例:
2 d& {8 n- Q, c7 R0 U#include ( Z; z* o; S' }* R
int main(void) 5 e1 L n. M* R: a* d
{ , p2 K! P! D& x& U. K# y/ i
FILE *fp;
+ r4 p3 y6 Q( r% a* ?# P Nchar ch;
$ A3 T: E1 L' J5 |2 k; p* z/ {/* open a file for writing */ ) n( D9 R7 F' o7 g! S/ k6 P
fp = fopen("DUMMY.FIL", "w"); 0 [0 c" M! B! i$ F5 W0 q# r) ?
/* force an error condition by attempting to read */ ) z6 r( k+ q9 R! x( K
ch = fgetc(fp);
/ q5 Q: J, B% Lprintf("%c\n",ch);
! k! h+ M/ {, A* Kif (ferror(fp)) # U- V' x' `3 k+ v0 g* ^
{ ' |) u/ B0 ~& R# X) a3 r+ a& K* c
/* display an error message */
! P9 S: T3 T; h! r Uprintf("Error reading from DUMMY.FIL\n");
9 j% R4 A' u p9 a! W5 \, R/* reset the error and EOF indicators */ 4 B% T a1 w, v$ B
clearerr(fp); 9 P+ J7 q1 u; e" U4 W
}
, n" W1 h V! r1 ^. zfclose(fp);
, p) g- F5 z2 D/ O9 O+ areturn 0; 1 L @: [! U/ y, K- g' G
} & }& U- X* [6 p1 Y# @% j9 n
2 w' U$ e* v$ w5 @2 V/ L
- _# S1 _6 V% ?& D) ] ( v; u2 d C& W8 h9 g2 \+ G: Z9 ?
函数名: clearviewport
% a, |) c, P& x7 J( O功 能: 清除图形视区
$ n7 g1 s" z* Y, b F I, r用 法: void far clearviewport(void); : x6 N/ V5 Q/ p# R9 {
程序例:
' q; S6 \: n6 Y1 K6 d& ]#include
) h v4 ]2 C. f5 i+ }8 ?#include
5 m. e% T$ B# x1 b#include
% Q5 k, k! G& T; ^. P5 ?: F#include
' q. k" S! e d4 F: a3 W- ~7 P6 A#define CLIP_ON 1 /* activates clipping in viewport */
( @" A/ H# R0 F1 _" T7 qint main(void)
2 B$ p% w. N/ {{
; Q+ s$ J5 x# m5 _2 t3 g3 U8 T/* request auto detection */
* a. x" u" C; n$ Vint gdriver = DETECT, gmode, errorcode;
% I3 x( l6 G. Q( ]4 Y9 {int ht; 4 P+ z1 k" U; V" [* I7 M
/* initialize graphics and local variables */
: e! N: k) T7 n& m. {- xinitgraph(&gdriver, &gmode, ""); 4 t; X9 a. o* A. W# W9 D! m
/* read result of initialization */
a$ n' o# l% l1 C5 e( Qerrorcode = graphresult(); - C* ~& q& ~1 _4 Q# e1 o
if (errorcode != grOk) /* an error occurred */ 2 S6 P, ^, M/ s1 O: J5 Z
{
6 f) l8 a' g# y' u tprintf("Graphics error: %s\n", grapherrormsg(errorcode));
. E& R" k! M [: w3 r( Gprintf("Press any key to halt:");
( j8 K' c$ D( v2 o7 ?1 M# ygetch();
, h( b: i6 U3 ? R$ W4 @exit(1); /* terminate with an error code */ % x6 P5 ] m# m
}
" t' z$ k5 r7 ]; \setcolor(getmaxcolor()); ( F0 F* t7 U. M( u( y0 o. \0 b
ht = textheight("W");
$ n0 h( `6 M0 w8 M) n9 D/* message in default full-screen viewport */
3 h! B; S# _8 E' {5 C- H r* Zouttextxy(0, 0, "* <-- (0, 0) in default viewport"); 7 F/ i- C! \- n$ g! ?0 m h. E6 q
/* create a smaller viewport */ / I3 {8 S- m# f4 ?0 }- [/ g8 @
setviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON); 9 `# Y) v0 \$ p* p% y% n
/* display some messages */
! G# D$ A; k8 N' J' N/ l: jouttextxy(0, 0, "* <-- (0, 0) in smaller viewport");
. x& P/ U, k! Touttextxy(0, 2*ht, "Press any key to clear viewport:"); 9 B: W4 O$ Q. v7 }/ {; ]5 `
/* wait for a key */ - ^. o4 I! w2 r1 C* M) b
getch(); ! w; }, E: N# B# s7 e5 p* U' u
/* clear the viewport */
( U5 o% a3 j f! p6 zclearviewport(); / }$ H3 N* O7 ` q% r
/* output another message */
4 J/ E# c5 ]# C9 W- couttextxy(0, 0, "Press any key to quit:"); . J% k: G- `' l. f# W
/* clean up */
4 N) n" b0 N! j% Ggetch();
& I( p1 w) ~: z7 g. r3 pclosegraph();
1 L7 ]+ h' s: N3 C0 ~9 {return 0;
& a3 b2 d. J7 A* y} . ]) I7 {3 A# ~" U7 O y F
$ {9 d! w* }, G1 [1 v, k! G
* n+ k: X0 I4 _& X/ Q7 U ( Z8 @8 r: T0 w* X
函数名: _close, close 1 M8 t- P; [7 y% Y1 J
功 能: 关闭文件句柄 * C2 u! j1 K) ?7 ^
用 法: int close(int handle);
" Z( `% V3 C4 \; a程序例:
8 k; _1 q' b9 I) ]' K- X! F#include
8 Y1 |3 I5 r( j! M#include 8 Q1 Y- f3 F Z4 [
#include * ?! j9 W+ l! b3 C" t }
#include
4 J, Z% e- {9 A7 b: P/ ?main()
7 j2 z. p6 j: H{ ; D) n, |+ D, T; N- ^: {' |# ]
int handle; j T8 z9 x+ n% ]8 M! x
char buf[11] = "0123456789"; 7 y! k6 i' G r7 h! w3 ]
/* create a file containing 10 bytes */
/ Y8 X! k( y/ g# X4 M! thandle = open("NEW.FIL", O_CREAT);
' C( Q* I q$ T# Bif (handle > -1) 9 S) s/ k, B$ Z% s: R
{ ' `, p5 j! a6 k; ?# d
write(handle, buf, strlen(buf));
" Z8 ~' [4 X, ]: ]: K/ y/* close the file */ 0 S: e7 z- n& B) z( X- i- b9 ^4 m
close(handle); , K& K* X% {) x/ ^5 d! M% ]/ _
} 4 b7 j' ?6 ?6 J' d. P- Z+ a3 S
else
1 x. X8 j: U7 A ` e$ H{
. l3 k5 I( l" o& l" d+ D4 `+ mprintf("Error opening file\n"); 2 [* \+ @& z# w# ^, F- T
} * e$ a5 E3 E* E- Q8 ^
return 0;
4 _3 E8 ~8 k, V' c( X& W: |$ R7 `1 G}
4 m$ H1 s" I3 g" k" t6 c$ T' X- [! A6 G8 Y7 E4 A! Z- ^9 o$ V
: S1 O" N- `: t+ u& E5 L- @+ C* @ % _! s8 G) m- ^ o8 E% W
函数名: clock
) o7 W1 g9 @7 {) K* A0 T+ Y功 能: 确定处理器时间 ) L- [+ x) R+ g* W7 m$ |
用 法: clock_t clock(void);
, ?. a) z, F: B# R程序例: 8 i( h9 N' F8 h; z
#include % T7 @4 t3 F4 K' K, m0 n1 m
#include
" j! h8 L+ h3 S6 I/ @) e0 u# k#include
! C k8 e5 r/ t2 Yint main(void)
. ~; d* u" a5 Y9 n' n* C# i{
9 G [1 E0 T4 oclock_t start, end;
q4 b$ X" b5 q2 y( ~# d8 Ostart = clock();
@( N3 N* Y2 I) [; Kdelay(2000);
) m# w( O- `. n: Vend = clock(); 5 d+ D; e0 u4 S4 {. t( I' R- j8 U
printf("The time was: %f\n", (end - start) / CLK_TCK); 1 }7 J T% g y' A' Z
return 0;
1 f) L, z3 q9 Y% m2 j) k7 ?} / d, f: V) G5 v' u# R% g
# l/ F; L- ?8 Y
1 A9 [2 T- n0 s9 v# w* O
. K% [4 B0 K3 z. w0 p1 u N函数名: closegraph
9 N0 a/ x6 R+ E+ D( V$ w功 能: 关闭图形系统
; k% ~2 D3 H- c9 I1 l ^用 法: void far closegraph(void);
* m+ Q8 [4 ^+ r5 J3 ~/ i1 S% s+ M程序例: D, N8 h! ?2 U$ W
#include
8 i" [8 W' t0 M9 T1 K#include . ?; E& c }' ?, z
#include t9 R2 B9 U3 w; ~
#include
, p5 _, o. ~' D9 p) m' h+ Bint main(void)
5 \" f! P5 y' K& F/ m! w{
' T" Z! ^8 p6 E/* request auto detection */
: ` ^3 I8 U# }% o$ fint gdriver = DETECT, gmode, errorcode;
$ z( z9 x1 U. X' F: [' z" dint x, y; # ^. h S9 i+ z% f0 f" ~
/* initialize graphics mode */ : N" ~7 S/ b# x' W0 e3 |
initgraph(&gdriver, &gmode, "");
; _& ?! y$ F4 j% v6 s: g8 G/* read result of initialization */
) H- w) J2 @, e: `errorcode = graphresult();
* I6 Y# h) R" O/ {: aif (errorcode != grOk) /* an error
& X8 |+ B( {1 i8 ~! M/ poccurred */
: R) O; g5 A1 n: H$ b: j9 U{ $ F' I( h" f u* o
printf("Graphics error: %s\n", grapherrormsg(errorcode));
& m8 z2 x. O; J9 V& gprintf("Press any key to halt:"); . a x/ n8 `) Z3 g5 W7 Q% {
getch(); # q" D7 ?$ L; E& G
exit(1); /* terminate with an error code */ 9 I3 A% ^7 Z/ L( j
}
8 s# S0 C( h. A1 x Hx = getmaxx() / 2;
, }' M+ ]/ m |6 d xy = getmaxy() / 2; 3 c/ ]6 z/ H1 z2 j" ~7 x3 X% a
/* output a message */
% J& S/ O# }2 }# k* L$ E% Lsettextjustify(CENTER_TEXT, CENTER_TEXT);
+ u( F0 b. g( Zouttextxy(x, y, "Press a key to close the graphics system:");
- g8 ~8 {; }& w9 I: @) e$ ^/* wait for a key */ 7 [$ g- e& X0 k9 q
getch();
: [+ O( a- a! a7 `, k/* closes down the graphics system */ # {+ h. n8 \) }" x4 h& r) ^
closegraph(); $ n9 e7 J$ Q+ r" o2 h" a
printf("We're now back in text mode.\n");
, J2 B! W& ~' q" X2 \2 R2 oprintf("Press any key to halt:"); ! j$ p1 \! n7 b, @0 W
getch();
" y- l; U x7 A X5 a0 p: ^7 kreturn 0;
" v% ]. w1 C* ~; N& C) C3 e* }, T} ) l% k0 B- h) `
, g A2 E+ e) \% l6 `: s, S2 i* Y" `3 q; H* m t: I, A, U( Z
+ N' r( \' e+ b" i
函数名: clreol # a% f2 |' u8 @. N" `2 S7 _
功 能: 在文本窗口中清除字符到行末
/ u. H$ |' s1 k: f6 v. u2 q用 法: void clreol(void); . M" _) t6 U+ V
程序例: 3 S1 L3 o+ E$ {
#include
9 Q: O' ?, d0 h y7 x& Vint main(void)
7 J! C# _0 k: a0 F& p. M* U{
. y% \5 m/ ^4 |% D+ Jclrscr(); ( @% e2 I9 i) A' `
cprintf("The function CLREOL clears all characters from the\r\n"); $ {# A7 F8 F4 t- Y6 m: S
cprintf("cursor position to the end of the line within the\r\n"); # K' n. u5 q/ s4 k; \' t* `
cprintf("current text window, without moving the cursor.\r\n"); " O# D$ |+ x0 D
cprintf("Press any key to continue . . .");
& S! @6 T% h7 dgotoxy(14, 4); 7 L; u# ^3 F4 N, t: M
getch();
: ~) s5 G! S3 M( d6 ]6 _& F* F0 B, A6 rclreol();
4 \$ N6 D6 U0 U$ ]( Y# zgetch(); 2 C. y: Z5 y% p
return 0; 5 ?+ P; @ H5 _" ^! i9 Q
} % h" ]! v9 n+ I6 M
$ I$ K8 D4 Z3 c' F3 {
* Y; v1 p( U$ T % h$ w- g2 L1 \. S$ O
函数名: clrscr
3 A- E7 T7 R7 u; `功 能: 清除文本模式窗口
# z) u; s8 Y: Z% d& d7 @用 法: void clrscr(void);
1 q0 A0 Y, h, y5 S* c程序例:
8 |" Y# |5 d- `" D& Y/ C. \$ Y#include
6 ~- {9 q X* q* q; uint main(void) 3 S; K( W8 Y5 ? s- O9 c/ B& A
{
, G4 x8 n& m3 p2 tint i; % s' L( L0 Q% U6 o, p
clrscr();
. q( _$ B2 {4 p5 _for (i = 0; i < 20; i++)
) Q' ~8 [7 p% \" ?5 N4 }2 Bcprintf("%d\r\n", i);
4 H5 x& l) M: y- Y' Ucprintf("\r\nPress any key to clear screen");
: x# H1 Z; X$ P" fgetch();
! L( C3 ], G* Z0 |clrscr();
, t3 ~5 @& i+ U) X- h( A) y; bcprintf("The screen has been cleared!");
* O& [2 o0 v, y! Y( |3 u+ I7 Egetch();
4 X7 @5 X: J" o) p8 Sreturn 0; . w' w3 B- [ k5 X8 N* N
}
9 e( I6 E5 g+ \" B
2 O9 U; i/ Z2 }! v# b
6 r5 k/ A0 R0 G& f y1 F2 r / N' c% N; R# R/ Z: \
函数名: coreleft " D1 I8 F& @! B; [+ Q" H7 @+ I
功 能: 返回未使用内存的大小 $ b1 i% ^1 f. U3 v4 X. ]
用 法: unsigned coreleft(void); & }2 h8 z2 F: B1 p# a# U7 A' g
程序例:
; w! ]! B8 J0 F, f" M#include 1 C6 |" W) \! n8 ~7 \9 o M" W8 j, x
#include
1 E" k& n/ Y' Kint main(void)
0 @* H `) s6 ?- V' A{
/ a! S6 _. U0 _printf("The difference between the highest allocated block and\n"); $ S! P: }8 O8 z( ~# Q- u
printf("the top of the heap is: %lu bytes\n", (unsigned long) coreleft()); $ k. y% W: @3 `
return 0;
+ l& z, y7 R% m" p5 j" j1 ~}
' L7 K& D3 |& ]* G, n7 M / E4 Z8 F2 D/ `0 C# ]1 g
函数名: cos ' k1 X6 B: a3 n. p' |
功 能: 余弦函数 9 G- o, a* s# D$ O' [1 O
用 法: double cos(double x);
# J1 |# }# v/ Z F' g0 h' A0 T' S程序例: 5 a6 |, p6 @8 |" Z! o- Z
#include
9 r/ ?/ H5 Z, }: j8 H#include & U9 o& i4 u2 [
int main(void)
3 B, z5 C0 t6 x& }" }7 W. e, o{
4 z; f. Z+ J& J1 D% cdouble result; 6 B+ z9 U: c8 O- a' x
double x = 0.5; # r* J) B9 K8 D. u' g
result = cos(x);
, P5 J" x" T6 ]/ _% S# u0 Zprintf("The cosine of %lf is %lf\n", x, result); t, G( N4 {8 ?, S7 L
return 0;
2 n( ^# k8 i8 J5 ?4 ]! N! P}
$ P! W* y1 V* H& o# ^3 }# Q
8 `. B6 b. T# \3 W5 V8 q) l$ z
# v2 h$ G4 B, m) X函数名: cosh ; K2 e1 Y9 a$ h, x, r' s
功 能: 双曲余弦函数 ( s/ s$ ] Y* u0 K
用 法: dluble cosh(double x); " W D$ T" v% w! @; D* f: |
程序例: / W& Y( I- ~) x& Q& ]
#include 3 Y1 C1 ~0 \( x4 B" @3 J+ ^
#include
# r2 g5 {2 z7 \+ ]% [4 S \int main(void)
0 ^* @$ b' G! o6 j/ X) c{ ( p5 f @5 v/ M( t9 I3 _& v3 ^) P' @
double result; # E" g7 c, d* L+ {0 b8 ^
double x = 0.5;
2 B e k# J0 x3 k) D% i, aresult = cosh(x); / t1 ?) t# u, O. q
printf("The hyperboic cosine of %lf is %lf\n", x, result); 9 @+ Y7 s& n0 u4 T
return 0; / s2 q% ?5 Q( `. y
}
2 I9 M' m: x9 q/ _1 h$ ]5 x: t+ x/ {& A* W# j
, H. a0 j" G$ L* `/ g4 x
7 k- ^( U, _( v3 p) a6 x1 V/ U函数名: country
3 m+ X' ?) D+ Y功 能: 返回与国家有关的信息 0 L: t+ g8 ~' L& {- U: _
用 法: struct COUNTRY *country(int countrycode, struct country *country); 4 P& L* Y/ M7 t& V- d7 t6 L1 \$ u
程序例:
& ^& y0 Q4 G% z9 m* o% {: q#include
4 h. A7 s, v3 R0 F2 w: k#include
1 E- p7 v1 ]+ ]5 O#define USA 0
$ V# ~# h' M. I, b+ ~int main(void) / K& }" R5 o* T
{ 9 Y. U Q. T3 ^9 A0 `' B' e
struct COUNTRY country_info; . b- h; M- Y) d& s. K" y* f
country(USA, &country_info);
1 U8 ] k2 a% Z# r9 O; k4 mprintf("The currency symbol for the USA is: %s\n",
# ]8 N. Z6 z' I Scountry_info.co_curr);
; M* N# S6 a, y0 t3 |8 Preturn 0; ( J; o- ]( x$ r6 @
}
# g6 [8 V" U; H _$ R% y9 @! i0 M6 R7 w* P0 I5 R" b+ j
: ~ k- a$ z D3 P. J
" z w& Q3 N" O1 r5 N' e' M0 \
函数名: cprintf
$ Z% V: f; J3 _+ }功 能: 送格式化输出至屏幕
7 p; [2 L( ~7 O5 D. m c9 q用 法: int cprintf(const char *format[, argument, ...]);
3 E B( ~+ h: c6 `$ S5 r! B0 b3 `程序例: 3 H5 V5 w9 C9 Y5 y
#include 0 |% O L+ a0 u* {
int main(void) % {, \8 A: V! R
{ ) e" E) K0 y' E6 q% U$ R3 [3 F. N
/* clear the screen */ 8 v. p8 h& R% `1 R" i; \
clrscr(); + A, X- X" _+ e6 d
/* create a text window */ - D3 T9 R8 Z. x& G) G7 ?
window(10, 10, 80, 25); , G: l- d+ o5 g& C2 X5 S: }
/* output some text in the window */
- @% X6 M2 W+ ecprintf("Hello world\r\n");
0 ]3 m* P* p) |0 Z& r) h/* wait for a key */ 3 D' V3 e1 P0 [6 z8 l; p
getch();
3 i, x7 m" q/ R" B# p* Oreturn 0;
1 w: y2 ^6 t- @' A5 S} 7 J( l, u& g& V* ~
* [( X% a* Y5 Y3 { P7 e
, N$ |; J( v6 K
3 \+ N& ^& x+ |4 h8 _2 O函数名: cputs * g( D* I; T2 ^
功 能: 写字符到屏幕
" j) |) B9 \7 g* x& \9 X4 B9 B用 法: void cputs(const char *string); 5 k" y. B2 G! o1 ?
程序例: % H2 G, X) \" t
#include
. N; ^! C4 P' J+ v, [4 R7 Vint main(void)
; S L0 F* v6 F( n{ * f- G" W) P* t& |/ h; _
/* clear the screen */ * P! W" S" b' c/ Q, w# s4 i
clrscr(); 8 b% d2 X2 Z& q
/* create a text window */
1 \' F/ d* s4 W( g8 b4 gwindow(10, 10, 80, 25); / T: y3 G: R1 K* }4 a+ }) R" x
/* output some text in the window */ # s- U8 W) y/ N, g/ n( e& v& [
cputs("This is within the window\r\n");
1 L% z- T3 i, s, }! G/* wait for a key */
# B& `- J7 }# |4 J6 l4 N) k. ygetch(); 7 ?; |3 E, \! W" B$ S. @
return 0; 0 ^) F. e) f1 R1 B3 A( m/ L& }. s
} . I9 O1 Y l# A7 q) `. z
7 \# B! V) P6 ~; e0 N7 H8 F
7 r$ ^- ^3 i& }0 ]6 p
3 T; G; Y5 Y) x& C3 r, a3 C% V
函数名: _creat creat . E7 s( C8 y7 W" v
功 能: 创建一个新文件或重写一个已存在的文件
% {; i& o. s! e, t: ?用 法: int creat (const char *filename, int permiss);
- R$ S2 ~4 j( Z$ S+ I% k程序例:
7 s) v* z% U D, m& C% S' _& \: @#include $ n/ t3 ]# V$ K4 f% l* s) N
#include
0 e( l# ]0 k) j1 @; W#include
! H* a- A z6 C) \* q7 u3 i! `#include ^: \: B7 _& Y% g9 ?) F
int main(void) + H- {+ O( m5 d: l+ \" T8 d
{
/ I( _# q" H' aint handle; 0 H3 M4 N. W) C- r
char buf[11] = "0123456789";
& `# r! U$ D; T- f) k, c/* change the default file mode from text to binary */ 8 ^( t+ s2 a& k. M$ g( X8 P
_fmode = O_BINARY; 8 Q0 ^' x( r3 w. V, Q
/* create a binary file for reading and writing */
$ a2 n1 Q, d1 u+ Nhandle = creat("DUMMY.FIL", S_IREAD | S_IWRITE);
4 Y, p' a6 w! G/* write 10 bytes to the file */
- m+ G2 d \- w: Q [" O% x0 Jwrite(handle, buf, strlen(buf)); " X4 n# S% O8 U$ r' K* I Z- _+ U
/* close the file */
# F" R+ s# L) ~' @; X2 [close(handle);
" Z7 @$ l2 s/ ^0 b( ereturn 0;
% r" e5 V+ s3 x6 ?} ! Q* h' q2 C: I
S3 Q9 S. J. x/ R+ F) S' T函数名: creatnew 6 }3 `) S/ Q' }1 ~$ |/ r& M
功 能: 创建一个新文件 7 o z1 A4 Q v4 E' f& W# M: B
用 法: int creatnew(const char *filename, int attrib); ! J+ p' ~, ^" n/ m% O/ v$ g; P% N [& C
程序例:
; n( @$ T* ~$ o4 q/ s# S#include
9 [7 |3 n: J5 z; ^6 ~#include # a. q. {6 b% _; }! e
#include
+ U; O# J& _% j#include & t9 k: U7 d) [* l9 d
#include
9 L* _; y# k. \0 b- }int main(void) + d* \% l: J+ }4 S
{ / Q: h. U* l- O
int handle; # D L$ F% I U7 P! }
char buf[11] = "0123456789";
- a/ }3 {' {5 v2 R) s/* attempt to create a file that doesn't already exist */
. Z' x2 i3 V" p& t. V" M. khandle = creatnew("DUMMY.FIL", 0); ! r5 o" t, H" V
if (handle == -1) ) z- _* \3 l$ r- R
printf("DUMMY.FIL already exists.\n");
' S4 g- l! E& o) d) felse
2 j( A7 s0 }4 I( y& O$ z, W{ ) f; v7 w0 l! {# I% r
printf("DUMMY.FIL successfully created.\n"); 9 t. B6 `+ e. n! ?. I! Z
write(handle, buf, strlen(buf));
! a" w0 {) m5 x! O3 A! Y% D& V0 sclose(handle);
% c1 X4 C4 K% W5 J} ' s A+ q3 T! P! T. E" ]) ~
return 0; ' X* l8 j8 V$ D( k( R7 F
}
! n3 \5 f6 p, m3 S
. Z& g \! w9 d. u* R1 f7 n% g9 g+ Q
3 Z! v) L0 p* ]# I函数名: creattemp
: X! a2 _) x" P5 i功 能: 创建一个新文件或重写一个已存在的文件
% {2 \5 @ @' ?8 @8 j# C, B用 法: int creattemp(const char *filename, int attrib);
7 q* r) X. x! ~" [程序例: - d& M$ O- o) t) e6 J, V* D0 x
#include 9 k* U; z3 V$ C5 N! ~" S
#include : l( {- b; C7 |3 Y# U' V, K
#include , b2 ~2 k) K3 V( _
int main(void)
7 k1 p' q0 N8 s. P0 [# C" {% I$ _; Y0 B{ . G3 v1 h/ o) k* a0 N1 u( J4 q
int handle;
" _. h% i8 C6 K& y5 o; o: X, Kchar pathname[128];
/ _+ R1 w8 Z: Q' J: H* Wstrcpy(pathname, "\\"); $ z: K# N3 M# u$ Z; y
/* create a unique file in the root directory */ 7 _( Z9 i8 N$ D4 [/ a0 T
handle = creattemp(pathname, 0); 9 R8 ~/ J7 ^1 I: Q+ D, |: V; A
printf("%s was the unique file created.\n", pathname); 7 }* _; M% a. H' j7 y
close(handle);
7 F/ h" \) {" i Freturn 0; ) u& u3 I% M* Y; H; V& `1 ]: _5 i
}
5 i4 M- W9 n! v2 q& r# k4 N# O" H. T" X
, L5 q/ _0 _; C: E1 o0 c+ p, O! C
' j1 [" v# F- J, T& e& B4 }
函数名: cscanf
0 C6 `+ ^2 r/ U功 能: 从控制台执行格式化输入
& C' a+ ]1 ]7 M! K: D& a用 法: int cscanf(char *format[,argument, ...]); T6 p. @+ g8 \! C0 U" y$ e
程序例: - `+ K5 r! L E9 v; O' q: G
#include ! y' _. K: L- I. Y: D _! j
int main(void)
0 N' C1 z( Q7 A: B# M{
, S+ C! S% o* q! c5 Q/ schar string[80];
, S+ H$ @) t) w/* clear the screen */
2 ^9 F. j" A6 s& A; R8 r" W, Iclrscr();
$ `9 P$ A5 {& m; u) T! i( A/ }8 ]/* Prompt the user for input */ ; P, A5 I% y _- A5 Y1 p
cprintf("Enter a string with no spaces:"); 6 S, k. s& G" }% v9 k: K
/* read the input */
- C& V' ^, }* R% ~& ~cscanf("%s", string); ; I, e( m4 P% L- b; p
/* display what was read */
: ?# f9 d. _+ e; ecprintf("\r\nThe string entered is: %s", string); " C5 c9 U) ?4 |5 F
return 0; * [* ]: y" o8 q; Z6 f# i
} . I* e, o8 @3 ~1 I8 A. I3 }4 @
" d# Q6 }) i5 _' d8 T! @. u O: y& _- K' F* K& V
! x4 F& C" G& j8 |0 I% M ^函数名: ctime
: _. h/ ` W: c. M0 o; e* K5 }功 能: 把日期和时间转换为字符串
: e" p0 b2 G$ z) @8 Z# } M用 法: char *ctime(const time_t *time);
; l. J- Q8 `5 T+ e- o* b程序例:
6 r7 e! E# l; p! w/ O" |#include & D, c9 D/ C: o
#include 2 u+ L3 z. n' ~5 k. L A
int main(void) 3 S$ G4 H2 a _
{ , `+ R$ l9 c* q
time_t t; 1 a v# u/ i, p. O+ W. L
time(&t);
7 G/ N/ j ~* t+ _printf("Today's date and time: %s\n", ctime(&t)); . j$ P" ]& q& S$ w2 o/ I$ J% f
return 0; 6 D$ M6 ?0 P ^3 K& j
}
6 k p, S+ D$ f7 G- Z/ T N
u- ]+ x9 b4 `. X I8 M! g' E0 r, T" y
/ y4 Z9 `. t V( m5 a7 a$ Q函数名: ctrlbrk # o' F; d8 }" y( }& ?
功 能: 设置Ctrl-Break处理程序
% j# T' m# e7 Z I用 法: void ctrlbrk(*fptr)(void); ) ?. |5 o; L$ k
程序例:
* f+ J. h ~0 X8 u2 X, u#include 8 ], n% x3 O6 N) | q4 u! P$ o
#include
- o! ?1 h# h/ P! E#define ABORT 0
5 a# X! ?- P( b3 nint c_break(void)
1 J: W8 d' E& l{
( R. c0 O0 r* v5 J+ I. k2 q5 G' bprintf("Control-Break pressed. Program aborting ...\n");
. d) C& s0 v0 ]2 kreturn (ABORT); ( j& f. G3 l. H
} - |; T- N ]% ]6 V" D" }. {
int main(void)
& E9 Z% h9 W" y+ ?/ Q{
: q% U8 U; H, O4 ], O X4 bctrlbrk(c_break); 3 O6 e. s; c2 i, y
for(;;) + R i1 m8 ]0 ^: _; s# l
{ # B3 D5 T1 ^- [
printf("Looping... Press to quit:\n");
H' Y( K5 \/ H} * ~' n% |2 @- x, A
return 0; 9 B5 e+ ~ N: S# Y) H
} |