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