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