|
函数大全(c开头)
% B4 s3 V- `: R8 n! `- c c
2 { @' x( [; b& {函数名: cabs / I; t" r( u" x! [( L
功 能: 计算复数的绝对值 / m$ o# u* k9 R# v4 p6 y4 O
用 法: double cabs(struct complex z);
4 }% l6 u' y5 p5 C程序例: 4 A2 E" S; l5 l: \8 p) X x
#include
4 f$ c3 A! g, w; o& L#include * I, b, s) c# {/ p2 ]* H
int main(void)
, P3 ]- @' K( b/ |! w4 K/ L{ + t1 \7 O+ i+ Y: E, O
struct complex z;
' \4 v2 c, |6 J6 v. E- U8 M/ tdouble val; 0 \ z2 n, b1 i! f I
z.x = 2.0;
# H5 ~8 _6 B( {! ]/ T; k7 a6 oz.y = 1.0; / y1 S+ d; c" q
val = cabs(z); 7 L: {/ `: F" _
printf("The absolute value of %.2lfi %.2lfj is %.2lf", z.x, z.y, val); " N0 {( j7 I9 B
return 0;
& A9 l3 O g: D. I: f6 U; k7 ?}
. o) ^- \6 w: Y) C* T% F; T" E0 I, a
% {7 ~; \! t( x0 O7 s2 f) ~6 v, m/ U7 j2 z# Z7 k" y" T+ {
8 l5 \0 z1 L& S* h! `( y函数名: calloc
9 A3 W( q& n4 Z1 N2 J* Y功 能: 分配主存储器
- A( u1 i f6 G用 法: void *calloc(size_t nelem, size_t elsize); # Z1 N% h( i% H& q- p. G
程序例:
) [% q7 A: C4 D8 P3 H% U#include
% ~$ t Z, X0 @; U U+ r7 r#include & B) Y3 c; w! G
int main(void)
# u; e2 r! ]( _+ t! _6 b" u; _{ # V) _ d$ N! g! {7 X+ B% |
char *str = NULL;
2 m& U7 ?+ l+ }5 g% M/* allocate memory for string */ 3 r) ]5 t7 c4 p) N5 U
str = calloc(10, sizeof(char));
% [! \ Q6 m2 p2 \3 g2 K/* copy "Hello" into string */
, ?. v) Y! f: q' r' r9 ostrcpy(str, "Hello"); 1 I* g0 ?( v$ T4 H. y3 r/ {
/* display string */
4 O$ n% D) _) }; G! Mprintf("String is %s\n", str);
$ W: B6 |) q, I& p. w/* free memory */
% u6 i; i+ C; B0 r. qfree(str);
5 W& i- Q+ U0 C3 Xreturn 0; + E& z. \6 w' `) E
} # s. J* d+ H* J3 p" T
! A" I: M# s% K
' \2 l) L n9 s2 M& |
[5 [9 H/ B" d1 w( ]函数名: ceil
9 F1 o8 t0 R. b% v! a, c功 能: 向上舍入 $ D6 P9 G7 `+ }' q
用 法: double ceil(double x);
9 w7 B) n8 ^, j9 E8 x程序例:
) Z' y3 o- x% o9 I% X#include
+ f. m$ Y# B# s6 s5 l#include
$ I* R9 p" Q4 h1 f# yint main(void) ! b( l* m0 T; \1 }7 s
{ * r' L% |+ R: a% j& U, `6 |
double number = 123.54; $ }# H! t. C- I, f( e) E0 l0 M M3 y
double down, up;
+ s* A/ H& L. @ z) ~; w' Adown = floor(number);
; H" A b W" |$ E) q$ F( S3 [up = ceil(number);
) y# _4 H* K7 Qprintf("original number %5.2lf\n", number); , B- T9 a- X2 `9 `( D2 ]
printf("number rounded down %5.2lf\n", down);
5 D; t0 G+ A; J% _! ?9 [- _$ Pprintf("number rounded up %5.2lf\n", up);
0 L' K3 r( L, Y7 p3 Greturn 0; $ s8 C j; D$ u8 q3 Z9 s7 e6 ^6 s z
}
0 _( K) {& d+ V! X8 N8 l& ^; S. J x4 T7 z9 `2 @1 A
, q& Q; s$ N2 h9 A4 k
8 p7 f: r( x% {+ E+ U/ x/ l) E+ x函数名: cgets ' D/ X. l$ J2 d( @
功 能: 从控制台读字符串
2 h8 D, G6 J2 Y用 法: char *cgets(char *str); 0 f1 D# M6 v# S. H' G# `+ | X9 D N
程序例:
3 k# N7 V% l' s+ g \8 l3 ~#include # Z; _- r/ Q4 y% R0 ^5 w5 x
#include ; l0 D t. \* h1 |- s
int main(void)
/ n, [0 h$ I8 E. |* E/ E" @$ P{ 7 a" b0 L0 h2 q* j+ n9 m* O/ m
char buffer[83];
; H6 {, g( i d& {6 p7 ^4 Ychar *p; ; r3 c( L9 ~+ i! S
/* There's space for 80 characters plus the NULL terminator */ 5 `$ g* r3 ?2 V4 p
buffer[0] = 81;
$ _4 m/ V6 C' w$ K) S' oprintf("Input some chars:"); ! C7 X( H; x, S% Z
p = cgets(buffer); 1 S( Q& @3 h* \7 h; y4 k
printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p); ) l1 T) o8 Q7 a3 m! r
printf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer); 3 ?9 ~0 A, e1 f$ i3 _& }% K
/* Leave room for 5 characters plus the NULL terminator */
+ A" \: w3 E1 J2 [; a3 }& j" e4 mbuffer[0] = 6; * G' a6 U9 D' u; o8 b
printf("Input some chars:");
4 p; v( G) @& \8 {p = cgets(buffer);
3 i( P- J- c0 V( T; Qprintf("\ncgets read %d characters: \"%s\"\n", buffer[1], p);
6 |5 I% \2 I, O" w) h0 `printf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer); 5 V8 l% N2 [) ]+ o- Z
return 0;
# q* s# l+ o' ^ r} " G L+ w3 D* k- D2 B9 [! n
; E. a# S5 [+ \! m7 S# n4 i3 O4 r1 k" [& q# c- x3 j
H: G$ |5 k. {! I. N) ]函数名: chdir . q0 u! {; \9 I
功 能: 改变工作目录 & Z5 {+ w7 L& j: [' ~0 e
用 法: int chdir(const char *path);
; J& @. O: S$ a. h% J! U2 p程序例:
$ E/ u! ]4 p& a/ H/ Z! q#include + ~ F9 S& x2 m4 o' D: C) l
#include * P2 Q9 ?! o: I9 m# r& l1 Q
#include 9 H+ D* Z- {; [# B( _4 s# k! g( }
char old_dir[MAXDIR]; 5 h' I1 {( i8 [8 X
char new_dir[MAXDIR]; ! |7 o$ ~4 s. E/ f; q1 V' b
int main(void) / F o& j/ u- c4 V% N
{
% l" ^3 X' [, w* S/ ~; y" [9 D/ rif (getcurdir(0, old_dir))
0 J, O' k7 J; h; S2 z{
* t" @+ K, D% @0 W; gperror("getcurdir()");
5 s5 b4 S' M9 Wexit(1); & Z+ k. f5 u# x/ y7 j: K( d. L
}
, [7 |9 r' t# e+ U( } Sprintf("Current directory is: \\%s\n", old_dir);
+ W1 t6 M2 d' \; n( K; ~if (chdir("\\"))
' y9 ?# v1 P3 p{
$ U( u0 m/ A, f3 f8 I& s; d% vperror("chdir()"); " R9 ^* |8 ?6 Y. S3 N; ]& z" `
exit(1); ' T9 Q5 P: E* l0 T
} ' J5 c) M- r! h" y x. g' f
if (getcurdir(0, new_dir))
7 t. y* O5 q4 o" W/ x{
& C; z% Q% V9 z6 s& q* Gperror("getcurdir()");
7 y( X( E% r4 O% r- x0 nexit(1); ( q; I$ Z/ u# K" U" H
}
F, W" a3 J: R% R% K! Nprintf("Current directory is now: \\%s\n", new_dir); 4 ?* I" j! R$ |! @
printf("\nChanging back to orignal directory: \\%s\n", old_dir);
5 X; [( _5 P! K8 lif (chdir(old_dir)) # e$ R# q3 I- Q' x; i
{
4 y6 [/ l" u: j% K# u8 Aperror("chdir()");
3 }6 ^" V1 U. o5 j6 F, T) nexit(1); , j: L- z" a; s* p( H1 d0 X/ U. _- D
} 4 E/ _% x7 n- F5 e: c& O% X
return 0; / u2 F Y6 S( m0 M3 p& L) _
} u5 y+ v+ C4 G4 f, M# `
3 v4 x0 Q( @2 f; h/ G ! Y& e) Y( ]! l' o5 S8 b
函数名: _chmod, chmod 5 i" T) |( P7 D$ {$ N) Z9 F
功 能: 改变文件的访问方式
1 y( R) d k. r; A用 法: int chmod(const char *filename, int permiss); / j% E6 e9 N z, R- r+ r4 U+ z3 h
程序例:
" E# _) _3 ~6 Z2 K3 c, K#include
$ u5 t% M: T% H+ y1 |" V" D5 M; |#include
3 E" K: A2 R- l: b& Z& t#include + U8 X$ I+ H1 ]
void make_read_only(char *filename); * j. S: a1 \( v
int main(void)
! B: z/ L. f7 j/ r$ k$ r7 |{
" u( G+ R( O& d1 zmake_read_only("NOTEXIST.FIL");
% G$ h1 D! f7 `/ H% B9 Wmake_read_only("MYFILE.FIL"); , Y* x, d+ f q: L/ o. n" l
return 0;
$ e+ p( w; B9 o) _) J7 x}
& C: a, r* }( e' ?; L/ F* s3 [void make_read_only(char *filename) $ V! j: L4 y7 D; B- z- e' F; B
{ + g0 f! V! [( [. O8 V
int stat; ! V- o5 }0 Y% @5 ^
stat = chmod(filename, S_IREAD);
) B# a% V. j1 \3 b# _2 c/ nif (stat)
) w1 }; p0 D0 o4 I/ F% f4 pprintf("Couldn't make %s read-only\n", filename);
6 F6 I6 v3 i6 Felse 2 D7 e) O$ l0 K6 @5 A' d
printf("Made %s read-only\n", filename);
. T7 P2 m5 t" t8 M7 F3 W. T( U}
. F% Z% b* Q. j* j# w V$ O" `
- r U6 f9 \( [1 R. o
0 T( C$ v: m x$ n0 U, B
- F; g/ ^( a% M7 r" g7 ~( x函数名: chsize
1 E8 X+ k; i8 _! s$ g3 O1 {功 能: 改变文件大小
3 t" i! z4 b9 w) _- q# m用 法: int chsize(int handle, long size);
3 P v" y. u* M) {$ i程序例:
% {* H" u8 k% F' _1 V; w. N8 S#include
) J7 r1 l1 i9 n#include
+ o3 V7 [9 A6 ^#include
& [: U7 w" f6 r5 ?0 qint main(void)
- ^! J: p8 l: r) ?) i8 s$ A{
4 d8 p, H* l7 @. ]2 y% {int handle;
" t0 f7 v& x; g! S+ Gchar buf[11] = "0123456789";
% b( m* @8 F$ y2 r/* create text file containing 10 bytes */
9 t8 K3 [" A; C8 `$ P4 V2 dhandle = open("DUMMY.FIL", O_CREAT);
7 ~! m; q9 s9 o! hwrite(handle, buf, strlen(buf));
; _4 O- s |9 M9 M+ W/* truncate the file to 5 bytes in size */
+ E* h' ~1 X$ b- X+ I* Q# ]chsize(handle, 5); + s: _- n, G( ?- ]7 u# u/ {2 Y
/* close the file */ % v1 r9 H/ u+ R& v, ~; }% L
close(handle); ' q7 O9 {# ^. e& `
return 0;
' r8 I9 Q, a# P. d}
& T, ^3 A/ j7 T( `" {/ h j0 b( e9 c* p6 h# L# D) a z0 C
( D% J& ?% E; T& |' W
函数名: circle : ~2 {- N8 H4 y% l3 S" A& J
功 能: 在给定半径以(x, y)为圆心画圆
/ Z, f" f; G% S( N/ m用 法: void far circle(int x, int y, int radius);
+ f) h. p2 q# b0 r4 e) K程序例:
) ?2 c# @8 y3 k. S" h6 [& \1 b#include + I d+ L& D; y9 r
#include
1 `9 D6 B6 @: M#include
7 o' L3 \2 Y4 S#include " L" ^9 n- ] l. F2 |: D, Z
int main(void)
6 E& V: f$ ~. q% B; F: a{
1 ^. r" I- d; \' u6 B/* request auto detection */
: J6 `; |( _3 iint gdriver = DETECT, gmode, errorcode;
8 ?: w7 o( n- I: ]% p) d' yint midx, midy;
1 c* f1 b4 d8 J( C+ w, Yint radius = 100;
/ `* \! `: t, y) T/* initialize graphics and local variables */ . I9 G' ^1 r c4 h1 q
initgraph(&gdriver, &gmode, ""); # f1 J, S; {+ W5 z3 A" J k
/* read result of initialization */ 9 L% b6 P2 J0 v0 S2 ~7 s8 F
errorcode = graphresult();
' k$ j) N, b! A, s. Rif (errorcode != grOk) /* an error occurred */
7 M4 e) u3 _/ U8 @5 H( ?{
- B m+ A/ w0 l5 |9 dprintf("Graphics error: %s\n", grapherrormsg(errorcode)); ( Q( B0 U' ]; Y4 L, R9 X
printf("Press any key to halt:");
' N0 T7 |4 H. w# @getch(); 6 c! M$ R2 y) @. q8 s
exit(1); /* terminate with an error code */
0 ]8 Y- M5 i, s5 v# l}
. c4 V9 m6 R* Rmidx = getmaxx() / 2;
9 H% x: S+ y6 Mmidy = getmaxy() / 2;
% } `3 s# ]1 ?6 ~' a2 x1 Z& \9 Zsetcolor(getmaxcolor()); ! ~- ^" y& x: j7 M/ `3 {' x
/* draw the circle */ . q+ l0 r! C1 k9 l0 i6 K$ G. k
circle(midx, midy, radius); 8 F: o) B! s1 _$ Q; a5 p
/* clean up */
% T7 D. m3 t% {* j# y* Ugetch(); % @' s8 q0 f( I8 _
closegraph();
2 i0 @/ U/ F* y6 R8 Y; u f" v5 ~$ `. nreturn 0; 9 C, z5 w* z2 r' e. a% B! c
} " z, G+ I4 r% T4 O9 x5 F( F
4 [* x) ?; V. {
( }7 |! k) @8 l- N8 M ) C2 D Z6 d" ^, Q3 ?+ I$ A
函数名: cleardevice
3 p: ]. Y- k4 p' M3 O1 l L功 能: 清除图形屏幕
" P) I4 O7 ]' V* M$ B @用 法: void far cleardevice(void);
, r( L) d9 D a1 B! T& m: [2 b+ s程序例: 1 W6 o; O6 V4 i5 L5 U$ P
#include ! R2 i4 d( s! B) m! d& z3 q/ p7 b P7 J* E% t
#include ) _/ ^- S$ h! j3 L9 S' T
#include , E% Y6 g% |. k4 p
#include
( o' H" W, N+ U5 F4 S7 b' qint main(void)
( {; l& A& w8 m6 N* a& y{
0 g. W/ b% `8 o/* request auto detection */ / L1 @4 r* k. U& W' Q& }$ W
int gdriver = DETECT, gmode, errorcode;
0 T7 f8 S t! U. S! Hint midx, midy;
1 a' t @- P3 K# Y- X2 p+ a/* initialize graphics and local variables */ * l! J# _5 y7 k% p! J. ?% b* o
initgraph(&gdriver, &gmode, ""); 8 y! M' m% t' k: w7 v! F
/* read result of initialization */
8 E' H- Z, Q6 z& Eerrorcode = graphresult();
$ I- {6 l' e* h7 lif (errorcode != grOk) /* an error occurred */
- a% A7 e' n8 }: ^+ B7 d, K3 [{
6 I4 J& [) J3 Xprintf("Graphics error: %s\n", grapherrormsg(errorcode));
/ t, i) S% `9 U* |& j! `$ Uprintf("Press any key to halt:");
( V S' V5 D# vgetch();
" t9 t! ]. s4 F, H1 `4 yexit(1); /* terminate with an error code */ - N/ `) |- T* h, c7 G: r, Q
}
1 H; H$ r1 Z0 k: xmidx = getmaxx() / 2; $ a3 o- N1 P% Y( S% @2 a
midy = getmaxy() / 2; & \2 N: v8 n ] d" C/ L
setcolor(getmaxcolor()); 6 n$ @/ T3 l6 \( }
/* for centering screen messages */
+ Y9 n* ~, z! R5 G* C& C+ b/ Bsettextjustify(CENTER_TEXT, CENTER_TEXT);
3 @. @5 k0 T) S7 p/* output a message to the screen */ ! O" |: u) Q8 x
outtextxy(midx, midy, "press any key to clear the screen:");
" A' f5 c" O1 w# @6 C: n/* wait for a key */ 0 x: ]6 k7 Z' c+ p5 d" [( j
getch(); 9 G) x* ^' T: t1 E+ u
/* clear the screen */
' U2 ^* Y5 C. }% P ecleardevice(); 3 t" |- I! _: O, p! R
/* output another message */
8 y2 d7 l2 ]: mouttextxy(midx, midy, "press any key to quit:"); & H7 z5 [& o2 o- w+ A
/* clean up */
7 ]% g) R+ Q: v; |getch();
& `, y4 f! D/ V+ V fclosegraph(); , m- ?) \6 B% I1 N5 s9 w: [
return 0; 0 a" S* K$ O7 ^; ?
} ' h2 ^* ~; `2 y0 ~# V
2 D1 v) \2 q1 ?" Q1 @& j
. W1 Y, r% `1 Y) v" r: Y: z
" L7 ~( p0 E% E( }8 [* |函数名: clearerr / }" ?7 k# s& U+ t# L8 s
功 能: 复位错误标志
5 v J$ x" Z. I6 o. l. v* q用 法:void clearerr(FILE *stream); 6 k2 N$ K- v' F O1 P9 p0 G0 C
程序例: ! Y L# w+ Z9 d; Z! v; R5 O/ @! P
#include 0 n7 \5 Y+ O) q
int main(void) ]( x+ `# A2 }2 K7 |, _+ Z
{ 3 C1 K# o5 T8 a2 K0 {% a) K
FILE *fp; , d+ u3 }5 e* V. G1 Y- a- S
char ch;
. S) F. Z' |* ]! l/* open a file for writing */
1 Y0 E5 r( h! P& | `* bfp = fopen("DUMMY.FIL", "w");
( l: R- w& c3 [/* force an error condition by attempting to read */
4 n8 @' h3 e1 ^2 hch = fgetc(fp); & K. Q; J+ K, L/ [% b
printf("%c\n",ch);
6 I* T: U3 D+ `if (ferror(fp)) 0 \3 N, `0 f* L! {2 k
{ ! D& m: H- q! ~& X0 A
/* display an error message */
: t9 n8 Y/ ~" o# A1 b' M' j6 Dprintf("Error reading from DUMMY.FIL\n");
0 I) b+ r6 h. {+ N( d, g5 L/* reset the error and EOF indicators */
1 h7 g( [! z9 x: ~! wclearerr(fp);
: B2 E" ]! m0 Z+ _( E/ n0 T} # k. ]" `- R3 Z$ m' j$ J
fclose(fp); ! [6 f8 @6 J0 T% X; p
return 0;
# a! ]) w9 A# l; ~ W) L) s}
& s* D& |5 Y5 R1 F7 a( y
( r8 [: z/ H# J; }# ^5 G5 ]% o& {4 ]! U4 h5 o& U' D
! O3 I( h* L4 I. f- [
函数名: clearviewport
- J ^. I) ?- b* n0 O! @功 能: 清除图形视区 ; D; t/ l- F+ {3 V4 Z* {& m( R
用 法: void far clearviewport(void);
5 y2 B9 P$ s; H5 d程序例: $ p& G$ R# x/ N, T9 ~' T* s9 q# C
#include 0 c8 ]' v, J; S- q
#include
, N$ } x" a" P4 a( ]#include
, F, u2 z o/ m- ]; A* s3 {#include
& V- @ Y6 R9 ?#define CLIP_ON 1 /* activates clipping in viewport */
( L' @% Q- \8 ~' f6 l9 aint main(void) 7 r8 S0 ?2 U: ~# A( z/ O$ R+ \
{ 7 M s5 {+ ~7 B# {' p. h/ Y
/* request auto detection */
. U! B2 o: E# tint gdriver = DETECT, gmode, errorcode;
% T/ ^4 U# r! r0 F) y' mint ht; 8 ~# p2 K* _- |/ v
/* initialize graphics and local variables */
\% j! [" c% [/ v9 t& c% ginitgraph(&gdriver, &gmode, ""); 5 p1 j* C- D8 B, q+ o- z
/* read result of initialization */ 0 d& E" z* R0 G
errorcode = graphresult();
, x$ _6 ~8 q8 ~( [: kif (errorcode != grOk) /* an error occurred */ " d. H! ]2 l1 B% p; ^! {; i
{
( N+ [' M! Q7 g/ P/ S! m @% dprintf("Graphics error: %s\n", grapherrormsg(errorcode));
* s' a! R* Y( }printf("Press any key to halt:"); $ ?' [* e* l* h& X/ k
getch(); ! Z0 p# a( S* T& c" E
exit(1); /* terminate with an error code */ 5 X+ ~; u6 v5 i
} 0 X' p% s% ?7 q1 U! }
setcolor(getmaxcolor());
1 A6 F. t/ M: ?+ j1 bht = textheight("W"); 6 e$ i; e3 t! F; N1 l7 |1 Q
/* message in default full-screen viewport */
$ n4 b# W ]" }* x5 D/ Y6 touttextxy(0, 0, "* <-- (0, 0) in default viewport"); ' h6 F7 y; N5 I2 U( V% t- ]
/* create a smaller viewport */
* G6 z0 [- h. d9 F) ~5 c2 R. usetviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON); ! T. o/ g) P4 L% E. o# g' k9 Y
/* display some messages */
' H$ ]2 {0 z. }% |. L- Vouttextxy(0, 0, "* <-- (0, 0) in smaller viewport");
1 Y7 o" V* N, q8 Eouttextxy(0, 2*ht, "Press any key to clear viewport:"); - |+ U3 k1 A0 m/ } Q) h9 m2 |+ ~
/* wait for a key */ 1 c/ O$ U! U8 R9 r
getch();
, u" K, p5 b# E" T3 F4 `0 @/* clear the viewport */ ' X3 t* p' q4 f! Z! q& G
clearviewport(); 5 \5 K3 k0 N" R( P' `
/* output another message */ 1 b6 \8 S$ K# J& t
outtextxy(0, 0, "Press any key to quit:");
: v5 F0 f3 B' A3 W/* clean up */ ) Z9 h1 l: S, f- S4 Z8 Y
getch();
# j* }3 H ~& Rclosegraph(); ! A1 U, j1 j4 ^& I$ C
return 0;
0 e* j& X4 Z, N- A4 S# {0 I: P3 I4 W} . j6 W/ _$ B8 e- H# }
4 v; N; Z/ T1 h7 s# K7 r* |$ j+ G
# _: M: M; |+ A$ s* t5 x$ P
4 A# ~; r( L6 D/ J+ V$ r函数名: _close, close . c9 I8 G+ l+ P) D' X$ Z* L* z# O
功 能: 关闭文件句柄
% @: h( D5 |. L O5 ?用 法: int close(int handle); ' o; O" U* s# s/ {: _3 O$ H5 S& k
程序例:
( o5 D6 p3 x/ w% }- p: E#include
* X+ y4 a0 z+ K; o+ W#include ! [1 w" o/ @5 ^6 k0 S
#include / w0 _9 t g2 P% K& o( e
#include , j5 J0 S( m8 z0 e1 U5 P
main() ; k- M: s4 q. M! |4 U
{
- P$ |& Y! M9 \ p7 U5 |int handle;
4 b# o5 Z4 @% jchar buf[11] = "0123456789"; 0 f$ p& ?8 f. Q- Q& r) n
/* create a file containing 10 bytes */
5 X' N0 ~& c0 ?handle = open("NEW.FIL", O_CREAT);
# y) T, T& Q- ^6 Vif (handle > -1)
- M; `- f2 k7 f W t: A) i( _& _{
# e+ [* I2 C0 m, e: K6 dwrite(handle, buf, strlen(buf));
3 N# K3 A6 R7 I- K, C* p/* close the file */
+ y! ]' h( j* u3 a3 O2 Lclose(handle); ( T4 ]' ~% M) p9 {# i9 x) O- Y
} 5 j3 h' R: b) ]2 U
else
- a: f& E' r3 ] a9 R: `{ " n% w- g' N0 C3 \; d5 D. N
printf("Error opening file\n");
/ @" k7 G9 Z- P) Y1 y}
2 x8 p3 R" B% g% C- rreturn 0;
/ D+ |+ d |# n2 R0 \# X}
/ C- E* {7 G) d6 O& K6 V j
# Y+ H. L' `9 U+ N$ |- m5 J$ `$ }; `$ L+ Y
: J* O o2 y0 Q+ C7 O9 G6 q. _函数名: clock * G1 K' a+ f1 U8 S* Y2 R. q
功 能: 确定处理器时间 $ \* T, I( {/ l7 N0 |# e
用 法: clock_t clock(void); 1 k* n4 s5 {7 u n w
程序例:
# P2 M3 |/ Y$ D( Z( l$ O- Q5 [0 p0 |# j#include
+ v% J7 S/ s* @3 [) Z" s" `#include
) R% k: S- y% K$ T! h# W/ ~#include l3 Y) v7 J* z7 @/ C/ V6 A1 s
int main(void)
, \2 n/ \7 u A) }{
" E8 ^1 v. r- z4 E4 F& R( Nclock_t start, end; ) L T! { U* Y) F2 N! R7 ]
start = clock(); 3 }2 ?( Z7 @8 d& N# d1 R4 [4 B/ K% B
delay(2000);
7 i1 f2 t7 ~: Yend = clock();
: f x0 c9 R, g+ f% _5 _! xprintf("The time was: %f\n", (end - start) / CLK_TCK); , i8 c- q3 e0 V( @. @
return 0; 8 {" u( _% x v% o2 u1 R# c3 ^& {
} $ N2 k3 d6 R$ z3 V: e$ l8 H
4 q7 f2 I' I% P% v. e# D7 [% }
! @7 q5 S' i! J x 6 ~9 C! t4 u3 e+ m c
函数名: closegraph * S3 J6 v7 k( P- E5 u# U! }
功 能: 关闭图形系统
) D# O/ t" a# N$ d% F+ @3 V+ n用 法: void far closegraph(void);
( G! o6 d7 r. h程序例: ; n* L1 N6 q% w) i
#include
{, t% k, l$ t* n2 Y#include
" P8 g5 X3 J3 k; A#include
. q$ r/ I8 Y1 c6 P6 _#include $ s& s: {# _ g: G, z. W
int main(void)
/ K2 x% A5 ^6 N8 r- `{
' I/ Q( q# j4 f1 `% ~/ E/* request auto detection */
' f4 S% P' Z: `; Cint gdriver = DETECT, gmode, errorcode; & E1 q( e/ Z" I* M0 ?0 g, c5 Y
int x, y;
( V0 m. \: m& ]3 o8 O3 ]/* initialize graphics mode */ . s; z9 A/ V! X* M+ M3 L3 o. P
initgraph(&gdriver, &gmode, "");
+ g% q4 k: Z" U, a* d. t7 _( R: B. W/* read result of initialization */
$ p0 ?9 `3 X8 O* T' \" verrorcode = graphresult(); 9 s3 _! U: \% ?5 N" g4 u
if (errorcode != grOk) /* an error
' Y- [& C" B2 e( Y X2 b# V: \8 Doccurred */
+ w5 b) x- o, i' v0 I& Q{
' [: P7 i' O8 ?) B+ |, P! c% dprintf("Graphics error: %s\n", grapherrormsg(errorcode));
0 z: ~4 i5 L" g& [printf("Press any key to halt:"); . j' a; R. Z3 |3 W! k
getch();
1 M; G4 c1 Z! W+ z3 Dexit(1); /* terminate with an error code */ $ @# b S6 T* c. z9 O7 n
}
! T; f* | P3 _; zx = getmaxx() / 2;
7 g/ c8 }0 \. h6 ey = getmaxy() / 2;
3 T* W, N( j O8 v& o$ j. T' d/* output a message */ 2 |4 t w- A8 [ q1 m. |) O6 W
settextjustify(CENTER_TEXT, CENTER_TEXT);
3 V) j5 @' [) S& couttextxy(x, y, "Press a key to close the graphics system:");
/ C" h# p; J; u8 S. r/* wait for a key */ ' |. K7 ?# L0 h" y Q2 o
getch(); 9 { Q/ t5 Z9 Z' r; z2 H+ _
/* closes down the graphics system */
4 X. c& r5 y+ G3 Kclosegraph(); m: c) g' @8 S) Y: z/ u5 S2 t
printf("We're now back in text mode.\n");
/ e$ e' F$ u4 J; [printf("Press any key to halt:"); 9 `0 O" a" {' j2 _1 X
getch();
6 ]( H- k9 C% T9 d% \- nreturn 0; % \9 |5 Z# ?" l! V2 L
} % J2 W0 ?6 ^0 L% J9 v8 _
' U" t! a# c& O9 m0 b6 G2 @) x3 t) E5 a/ \; n
& ?! y( m" o2 i. N$ }
函数名: clreol 8 w1 k; E& Z% R- P$ {
功 能: 在文本窗口中清除字符到行末 , b3 R W6 O& y
用 法: void clreol(void); 2 d6 }+ I7 H$ J
程序例:
* Q6 Q) P5 b- |* e3 y( O) B! i) T#include : \% a# s0 h' C
int main(void) . M0 I0 {! w( u* d! a) S
{ ; {3 e1 \3 H3 N. f/ f. S7 x' G: ]
clrscr(); 3 }- L& f/ U2 ]8 ^
cprintf("The function CLREOL clears all characters from the\r\n"); , O( p2 ?9 T- i$ }8 c! C
cprintf("cursor position to the end of the line within the\r\n");
9 ^7 u8 c- M6 m- O, Dcprintf("current text window, without moving the cursor.\r\n"); : t5 \2 S0 k1 L% u. X' P
cprintf("Press any key to continue . . .");
5 @; ^- V# F+ |" }* P- Dgotoxy(14, 4); ) Z( ?: O# `2 O
getch();
& W5 d6 [! w! a7 J. }" g+ ]clreol();
I- X% S! f7 o2 u: E% H5 Mgetch();
- \* H# L- {1 w8 E+ I( treturn 0;
" R4 R6 ]4 |: O7 p}
. r# f3 k. p0 ?% w5 g2 S, i
( J4 L7 L' n) B8 y8 B8 T# S F0 `. d3 Q- e3 t+ O
- p& H: \3 Y6 r) g! j6 |2 ]函数名: clrscr + _% l! h( u1 b* e7 Q9 X9 U9 z
功 能: 清除文本模式窗口 3 y* E. R0 T) U( [2 Y/ ^; j
用 法: void clrscr(void);
# k0 F5 k2 D+ c. r5 i程序例:
/ ]$ { ?5 h2 n( C$ }4 Y- c/ t#include
/ d3 M$ t# P& U3 F: D9 r* c5 n9 f1 hint main(void) $ u, k/ x* E& B; c8 z; _) @
{ - x/ p* {" ]7 g) S4 U
int i;
# J+ A8 \3 e9 C9 i- i$ c" Vclrscr(); 4 d$ r' L: H: V2 o
for (i = 0; i < 20; i++)
) |8 P3 \2 n3 l3 V; ucprintf("%d\r\n", i);
+ W- S$ b9 S o0 c1 Hcprintf("\r\nPress any key to clear screen"); , _) c1 A0 ^, s& I& n" Y# h/ Q
getch(); , k0 y! y+ c) z5 M U) D
clrscr(); 8 D+ ~+ d' @( v- R
cprintf("The screen has been cleared!"); * r# Z8 t3 [* f! R \; L) ~
getch(); ; [4 ^# C% z7 e* a. b" @
return 0; : V- \) \4 O7 [; G9 o7 i
} 2 Q8 q7 }' F( Y' l
) T; @6 X- r/ W4 u; D0 e; }
6 X# s- u& Y# @+ X/ J( A
# Z0 H5 T# k) N. }9 J7 d6 }! @函数名: coreleft
5 [0 G% }$ g) t, q功 能: 返回未使用内存的大小 3 u/ T& c" x- o! H/ `0 a
用 法: unsigned coreleft(void); 8 T% g# L& o' K
程序例: 3 v1 L# A6 d$ _
#include
% S$ N0 X) ?3 y% y& o: c#include
) h3 n$ @4 r9 \3 ^int main(void) 8 o3 A: r+ {" n
{
6 l$ E+ R& Q0 L1 n0 M$ T4 h2 b5 gprintf("The difference between the highest allocated block and\n");
$ \3 L$ M7 ]8 a; Tprintf("the top of the heap is: %lu bytes\n", (unsigned long) coreleft());
4 Z2 ?7 x5 ?+ s$ b. preturn 0;
' Z$ K! v& _/ G* v, }5 M* Y* b}
. P6 ?9 g; h, J$ {% Q) F# D
, J9 y& o& `8 O) R函数名: cos ! W. R* M M! N/ ?! h+ s
功 能: 余弦函数 ) I/ x% \5 o7 j+ s
用 法: double cos(double x); ) g7 V1 X/ I/ f, U' _) P* p6 C
程序例: 0 k" C: e% G7 m2 v4 l7 O$ w
#include
$ T1 X2 M0 S; C/ a+ `#include # ~. G0 \3 z$ Q* y; X' r2 w2 q5 c
int main(void)
1 _; ~$ ^2 G; ?7 T, h; d7 T8 F/ S{ ' v" ]" u: x4 G0 e+ ?
double result;
5 P) I2 G0 }8 W3 W* |' bdouble x = 0.5;
# Q* H5 n5 m. ^. a/ {) e. ?! B4 ]1 Cresult = cos(x); 1 O; @3 j& A9 j! h5 m
printf("The cosine of %lf is %lf\n", x, result);
) g6 Q6 \# j2 t. S$ q6 a1 h Hreturn 0;
& s, V- ? I0 f& z}
( l3 @: Z& b" Q0 X" n1 H3 l# @: _+ k% O( x
3 }8 b r7 X9 C3 T
# A _( M* B4 d$ x' s% w函数名: cosh
: l1 m, M R8 h; J: [功 能: 双曲余弦函数 ( W; `& y8 I$ C
用 法: dluble cosh(double x);
9 ]/ C5 o& [ \% O" l程序例:
& J$ ^1 L. d% B# N8 t; s#include 8 C* B0 {5 `' Z
#include
% C, A' K# E+ y3 ^1 d9 Vint main(void) % b: s& b4 R3 }4 N1 _, l2 N
{
: H% K4 s+ F: ]8 Mdouble result; 1 ~3 m' w# ?9 L! J5 U$ y
double x = 0.5; % P a8 E. o# r! ^) X( c& n( {5 }
result = cosh(x);
/ J9 s9 B1 ^) \printf("The hyperboic cosine of %lf is %lf\n", x, result);
9 c; H/ o! ]- g4 t! C& F5 x! Xreturn 0;
" t0 W' |4 y( s4 o} c7 K; \$ g% x2 x
5 l+ S& i6 b/ i" y$ [! K" L
5 ~# } [0 q4 i; M6 a q6 t7 H
- [1 D. V6 Q e" e1 d
函数名: country 0 D7 _/ P' P0 G( P. f
功 能: 返回与国家有关的信息 * h; |. }5 h* x9 H- o! j) T
用 法: struct COUNTRY *country(int countrycode, struct country *country); ' a4 C" s) [+ X4 D
程序例: 1 X2 B3 l* F7 d" Q
#include & v7 S7 M$ |. v
#include
$ j4 Y6 j/ g. _/ B8 s1 Q, W, h" r6 E#define USA 0 ( |. Q/ ]: | g( L$ m' S/ G
int main(void) 9 T: U" ~! ?5 U3 A9 C: w5 t
{
0 L9 V* w. A# U' I- p2 Rstruct COUNTRY country_info;
1 Y/ B& _( h+ W% t$ ~7 bcountry(USA, &country_info); " F9 A+ R+ Y; q4 `8 b
printf("The currency symbol for the USA is: %s\n", , ^4 E' p8 x- J5 ]; A# z
country_info.co_curr); " Y" h3 K* Y( V1 C8 @4 t' z
return 0;
$ n) Q; t6 N ]} ' d" E6 h# x% r) ?( [
: q6 Y& Z2 o+ n0 {* ^
5 t5 M- h; i- e2 P
* i3 q% H' Z" c l |3 z* w函数名: cprintf 4 D+ ?8 {8 [( Y) |3 x
功 能: 送格式化输出至屏幕 : F0 K! N: N1 c1 ^
用 法: int cprintf(const char *format[, argument, ...]);
! ?% j9 p( o& a程序例:
+ z$ U4 a0 W% h( V#include 6 h9 F9 M* b0 t8 A N. Q
int main(void)
9 o0 U' H: u0 B% `{
! U0 P- N) C0 w2 V7 {* H/* clear the screen */
- E2 K* E. }# u- oclrscr();
6 G! z- D: t; T% ^% A4 |9 Z$ [5 x/* create a text window */ 3 g* e) D6 C S5 u* ~! H8 t1 T
window(10, 10, 80, 25); , L. O; k ~( u0 `/ b g2 J
/* output some text in the window */
) `5 W& A/ q5 _) m, y3 y2 z$ |cprintf("Hello world\r\n");
, m# {3 r4 W; @/* wait for a key */ # _3 P0 S: B# y a- M, F
getch(); ) @; [& [1 Z- |
return 0;
1 S# f* U3 s% M4 r}
% S% a3 L" S: m7 Y+ }# a4 Z% f9 \ c: M$ k: r# Q) O+ O
! [4 Q- }) w4 \; u! l* x" b
: C3 [# J+ o+ M2 a- O7 D函数名: cputs
" y6 k/ v, Y& \9 k0 o4 b/ I7 h- K功 能: 写字符到屏幕 - \1 I9 K3 q* [6 i# U4 x9 R3 z
用 法: void cputs(const char *string);
2 V# t7 _/ z" x3 K3 ^; i程序例:
: H5 Q3 G [5 R6 A. S \" J#include 6 B5 O3 ]* c2 F' H; J+ g
int main(void) 7 @5 z% X& K! f3 T! p7 z1 o
{
& `5 E2 {5 V3 p% N* R2 ]7 l/* clear the screen */ % C. f" s9 c3 y$ T
clrscr(); 8 L7 a- b4 Q; n$ o* n
/* create a text window */
& V0 @$ z% O2 fwindow(10, 10, 80, 25);
7 n* ]) g" ]$ ?0 b7 s/* output some text in the window */ 0 @# a( m* C1 v& r: A0 o- h
cputs("This is within the window\r\n");
9 H' M) b& }$ E# R% U/* wait for a key */ ( u) y' ]4 a c* L
getch();
7 J+ x; s/ x4 M6 _return 0; ' F: F$ t7 x* e7 s
} " p9 w8 C+ i7 }8 ?$ s& q
( a$ _ K+ }$ ]2 f9 V: Y( L* B. h D4 c
% _) j; O1 Z j9 ^函数名: _creat creat $ \0 R6 D6 v+ Z# \% e
功 能: 创建一个新文件或重写一个已存在的文件 . E' h# a8 Z9 N3 \# ^( D
用 法: int creat (const char *filename, int permiss);
3 u7 D+ e! j* X9 \3 c' y程序例:
$ j: w) C+ M$ I7 c- p9 \#include $ n8 ^( u8 C# E' J0 l
#include - i9 Z- Y: k% y; Q' I' u
#include ) Y) {: _$ y) A; Q; D" ^1 _
#include
* l: z& X6 {! n. [1 x% Oint main(void)
. [% U9 G0 ~; W{
' Y5 [* Z' J& fint handle;
) ~% ]# v ~+ I8 E# i. jchar buf[11] = "0123456789";
2 }8 E- j8 W% k1 Z& g% e/* change the default file mode from text to binary */
% I% D$ x8 m, x6 R. ~_fmode = O_BINARY; ! e& k2 E8 m0 V" E
/* create a binary file for reading and writing */
: r. y' i" s( a# u0 U! ghandle = creat("DUMMY.FIL", S_IREAD | S_IWRITE); . ]) p/ q8 h* B( u
/* write 10 bytes to the file */
3 {1 I6 d( d5 r( p* Nwrite(handle, buf, strlen(buf));
^4 @! @9 N. z% U/* close the file */ 2 c( y2 @6 J: b! ~
close(handle);
4 m0 V( G+ b7 m1 ?return 0; : Y( p% E H/ _2 r" A7 m
}
) }6 x2 C1 O, f; U ' G4 b2 `8 v1 r2 B6 C
函数名: creatnew
" T5 I) e# Z! ~7 G- y* G功 能: 创建一个新文件 " e4 x I! l% K' W, b& }' b' l
用 法: int creatnew(const char *filename, int attrib); " D( \" X+ S V+ g9 ^! M2 Y+ f( z
程序例: 2 P6 J; ^, z5 v$ @9 L1 P0 i. x
#include
) |! g1 ~! i* ]- ^& R# W# S, h' s#include
: i5 a; m. o; O1 \#include 7 @& V8 [% L7 l4 b& _ u M/ }
#include
# n& {8 ]+ A" Y. {# y. Z S#include : Z9 u3 D/ c% Z" y/ W7 y9 h# N1 D
int main(void) * C+ }; k/ |$ |2 m) K6 _, z7 @ @
{ 4 w# T5 V: X+ d `# m
int handle; ! l& B% ~ A2 ?; o
char buf[11] = "0123456789";
" k: h! ]" }6 w5 l# V* H/* attempt to create a file that doesn't already exist */
+ d2 p- m! W5 Whandle = creatnew("DUMMY.FIL", 0);
+ R# P# t% d Q3 B9 x" aif (handle == -1)
! _$ E4 W" f9 W6 h% v- _: T/ tprintf("DUMMY.FIL already exists.\n"); - p/ t1 E: a7 t$ u$ z. G3 @
else / ]8 D" r$ l" R
{ 4 u2 G" ?1 ^5 s
printf("DUMMY.FIL successfully created.\n");
' J2 [" U: S& ?1 cwrite(handle, buf, strlen(buf)); 1 W( y, T) m- M2 Q& _
close(handle);
& o" l- l- b( @8 o0 n- e. k5 |; L2 n}
, \$ ~3 w& w/ l. `" j+ C# sreturn 0;
3 o: t' t8 i% b# p) J$ _}
% N: c( T4 O2 t: E- M5 u n# W: g3 X& U4 S6 k
% h$ O. w0 f2 X1 ]6 Z, x1 y4 x
: D4 S) a) M) J- h% t* g' Q函数名: creattemp
# j+ k7 f7 D3 \/ u* \# S3 j功 能: 创建一个新文件或重写一个已存在的文件 9 g. s& f% q- U) T5 f6 ~- S8 G
用 法: int creattemp(const char *filename, int attrib);
* X9 t" d3 V& V( Y" Z+ P程序例: # x z) `: f6 ~$ M, v9 C
#include
7 Q; y2 \* {4 p* S#include 4 @% L1 ^ p" S* B, k/ S9 E. {
#include
6 @) `$ W6 t4 I" R$ Qint main(void)
; U: y+ E$ i0 E5 N1 s! U X. [$ p{
2 M0 r" ^ h" E# i, Bint handle;
" z4 a, u S0 K! ~2 o# k! {char pathname[128]; 5 g0 b6 s. e. N4 O9 }4 s$ \
strcpy(pathname, "\\");
0 P4 |0 z% |% X" a! S; M4 g0 s- l1 }/* create a unique file in the root directory */
9 r1 Q4 w9 a' y/ J7 Nhandle = creattemp(pathname, 0); . o- S8 \/ \0 o
printf("%s was the unique file created.\n", pathname);
6 c E% {; L! x( a& i3 W0 C! o! ^close(handle); ) t5 H. C% [& s! t$ Y7 z
return 0; 4 n' [2 F& ]# r& G6 r
}
7 o {5 x2 s8 ]3 s% w5 x
, ]# u/ Z: o; Q& Z- J; u2 Z1 Q6 V W8 |$ I
4 X- b2 V$ f3 A' {+ p5 U
函数名: cscanf $ b$ h6 W! Z* H+ a- B1 ~$ ^; l
功 能: 从控制台执行格式化输入 3 e, M- L; f9 ^8 p7 `- j
用 法: int cscanf(char *format[,argument, ...]); 7 [0 Y9 C+ O5 g
程序例:
3 [+ p2 B* K0 M# b+ o3 C#include
! H9 T% @( Z' h3 Q1 N- j% }1 e( `& @int main(void) / T, u' O$ x5 T" \3 o" e
{ + H* y3 a2 O+ e4 V, g
char string[80]; & r6 E9 k; Z7 S& G* X+ x9 u% _2 o( N
/* clear the screen */
" c& j5 F( Z4 X' a8 ^- r8 P0 u- i mclrscr();
4 m4 N/ r, [4 o9 g/* Prompt the user for input */ 1 h, P# _# Y: J+ k0 M
cprintf("Enter a string with no spaces:"); ! b) t7 \3 Z8 Q0 {$ q
/* read the input */
. p" ? f4 `/ u/ b r/ X, dcscanf("%s", string);
" H, Y; D. d; x/* display what was read */
& j1 M8 S7 g) dcprintf("\r\nThe string entered is: %s", string);
1 b. P7 q, j% F) Freturn 0; % L, ?( H/ V% r4 Z2 H7 Y
} Z8 v' Q0 P' ]$ W6 m
5 x, P5 w4 K. l3 `. t' J( R
0 n# D4 e, e$ c! x
+ a2 W9 @$ e- ?: l# g函数名: ctime U( ?* p2 i, ?6 I" x
功 能: 把日期和时间转换为字符串
: J; a) Y' `$ \ z8 e( M+ R用 法: char *ctime(const time_t *time);
( e/ t0 g! b& P$ T, ]程序例: & C( s" Z) c3 {' @
#include
: O R# e& l: s6 S#include
4 U( C+ ?" B7 }, k3 V& D# xint main(void)
6 \2 A, Y4 w& v{ - P5 j" [( M1 T/ r
time_t t; 5 G! V! \2 k& U8 V& A/ H( O Z
time(&t); + A7 L2 ]6 F' E5 ^
printf("Today's date and time: %s\n", ctime(&t));
8 o' Y0 O, {) L* S T$ j8 k: mreturn 0; * u) w8 ^8 p9 C" b( U" M2 J! c8 L/ f) A
}
! A$ _7 Y! u9 |' d) s2 x5 s
9 r! O+ [- Q1 x2 k9 c8 h2 ` |
4 C+ U/ }3 X* Q( u1 n
8 n: h. z8 t4 K+ I; S% M函数名: ctrlbrk " P) w+ I( [5 W- t( q7 [
功 能: 设置Ctrl-Break处理程序 2 k g$ X! \" R6 n
用 法: void ctrlbrk(*fptr)(void); % R0 F9 Z8 z" |' O9 Z
程序例: & [* }. \# [/ c( n) x( [. I! D' p
#include ; N0 W4 q) }2 S/ C0 `/ M
#include # ]) i5 }5 C! y( D( @' l
#define ABORT 0 6 }7 r2 \0 K7 W' V3 B9 S v8 [
int c_break(void)
) p% W3 Z3 g/ m! f( ]( S+ }" k{
6 U6 f t' v$ U& ~( jprintf("Control-Break pressed. Program aborting ...\n");
; G! t5 A# { greturn (ABORT);
) _3 t8 x8 [- i8 C6 g$ A} : s" j% k+ ?% C
int main(void)
5 p' f6 n5 C) U{
% Y* T# S/ L9 c& F) ectrlbrk(c_break);
. O: c, m* f) x; Wfor(;;)
1 r5 G- L3 Y3 Q: H, {7 l; G{
3 m9 V3 u7 N+ W0 Y+ Q) Wprintf("Looping... Press to quit:\n"); # J* \% U9 |7 |( H: d6 _" Q
} - V9 t( G2 d- u, Q2 y* |+ \
return 0; + q: `; N# E/ \8 l
} |