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