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