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