|
函数大全(c开头)2 W! Z5 E2 U- \2 D: k) @
' f. S( o) g, j4 a# _& l
函数名: cabs
- a# Y3 ]8 l8 e- A功 能: 计算复数的绝对值 & a# ]/ F- W* `
用 法: double cabs(struct complex z); ( x# t7 p2 s8 q! z2 n! h& b
程序例: . s0 X% P+ V7 C9 S: R
#include
$ z. F4 x( y7 [# M- E$ v, S#include * V2 N5 m- x* w" Y: J( Z
int main(void)
# ~" A1 E) \* s4 W$ t: Y+ C{ * V; g, v/ a/ c' Q! U+ m# O
struct complex z;
7 [! J* j: _1 h' A \double val; + ?- U7 M, a: O' H
z.x = 2.0;
) s7 v* E- c1 r" w/ G0 S$ u/ H4 h7 V- cz.y = 1.0;
3 B2 r0 t' g" b o/ x/ A2 Aval = cabs(z);
2 Z/ T, m" [, }; Z& P: ]4 P+ oprintf("The absolute value of %.2lfi %.2lfj is %.2lf", z.x, z.y, val); / m/ T; k" v _& P* j, A
return 0;
" A3 H5 ]' ?2 M9 ^, D3 [}
2 n4 ~8 Q+ j3 C" A) `8 X) z
% F: e/ g- j$ s0 f1 g' a+ o
1 r9 N; |1 I2 l+ ~* Q9 k0 Z 5 P8 S/ K- A. F/ f+ k
函数名: calloc + [# [9 ]# x4 b8 j" V+ n
功 能: 分配主存储器
# S" M& {' Z }% b _" @: a用 法: void *calloc(size_t nelem, size_t elsize);
8 m4 p& d& g7 i# ]5 V% z9 \程序例:
( {$ z: t& Q9 ~& x#include 3 _9 G1 y7 J) N7 g, H
#include 7 M4 q9 W7 G" k8 u
int main(void)
# Z. i7 W. M5 h- e/ [$ J{
/ G8 L) u" T; \6 S( l8 i9 E: ~1 zchar *str = NULL;
2 b8 }2 Q* `/ A1 q( W/* allocate memory for string */ + R) o5 s- A& `5 H/ Z- T1 s
str = calloc(10, sizeof(char));
/ M- L% S$ R7 B: F" O/* copy "Hello" into string */
- y" D8 B& c, p; b _# y7 j, d* W# sstrcpy(str, "Hello"); / L9 E5 @ E8 U. ~3 d3 u
/* display string */ $ M. A' N- M" |0 K5 {
printf("String is %s\n", str);
/ T, l* W3 T* \! u& P- K# D3 v% ]/* free memory */ M$ ], U U' Z3 }; y
free(str); ( q O! Q# C/ {# @1 |5 W
return 0;
, `( o2 G" f8 K( v& y}
: b0 p8 t# D+ b' ^, u; h* C# U; t7 J' R0 Q0 L
B, S- P5 U+ S( W: N6 d * I. S& D( w5 `0 Y: L
函数名: ceil ! z' p6 d/ O2 F; o
功 能: 向上舍入 , Z8 I0 q f- M# \: W" H% f4 r
用 法: double ceil(double x);
, z, T C3 _1 y. M程序例: ' h8 _% M; ~8 ?
#include 8 o% H" b/ g/ D/ K) d7 n
#include
0 _% I$ o6 \( X2 u/ sint main(void) * ?1 I! o& |/ _) w# a% u
{
3 T A) x9 I) u8 E; v; w+ u2 Kdouble number = 123.54; 4 N6 @' d9 R. b5 R# ~5 d
double down, up;
; W& x( g% N6 j4 u1 _9 Y% Vdown = floor(number);
" d9 V8 C S5 }up = ceil(number); 5 H; }; K+ g4 R, G4 Z
printf("original number %5.2lf\n", number);
" t' D& ~4 s: m: k* \: ?printf("number rounded down %5.2lf\n", down);
1 j# U4 P a( u: u: vprintf("number rounded up %5.2lf\n", up); 9 ~1 [9 b0 H+ |' R2 A* q' o, f. I
return 0;
+ T4 S% _. F- w% f0 ~/ c* C; l}
7 |& B( ~% a- ^: D; [4 ]* y) A6 a! X6 e0 ^! I( H5 d. h
+ A1 `; `1 a, m+ X4 a# T
5 v1 c- y9 Z, `3 P }1 o函数名: cgets 7 |# B8 o/ I; J4 Z9 Z; j
功 能: 从控制台读字符串 . r' D) z2 a% |3 K/ P$ t; p
用 法: char *cgets(char *str);
$ N5 o0 A/ Q0 u程序例:
' z) t* S0 c# m5 y" T#include 4 j; l1 j5 M7 _: S: L
#include ) g. q" X' R5 t e
int main(void) $ G' h/ f/ v9 s) [! k5 M
{
) X) W* H c' d7 tchar buffer[83];
& s. P- H0 z4 ]2 A/ {% L3 uchar *p;
7 {. N. p7 j/ U) H/* There's space for 80 characters plus the NULL terminator */
9 U% e) p1 @8 y; k4 o& Rbuffer[0] = 81;
+ p* G: \# q- j+ eprintf("Input some chars:"); ! e# B3 e7 ^; {3 w
p = cgets(buffer);
# M0 l0 L) }$ E4 jprintf("\ncgets read %d characters: \"%s\"\n", buffer[1], p); ' U* Y% {2 I9 {
printf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer);
, N1 [9 ^: t: H ` p3 g/* Leave room for 5 characters plus the NULL terminator */
' R4 w+ Z6 V# [. f0 z' Mbuffer[0] = 6; 5 _2 ^% z! h( e/ K" }% g8 m: m( `1 ?
printf("Input some chars:"); ) y1 ~; L* [' d8 t0 n/ @+ P
p = cgets(buffer); ; ?8 A5 Y% f( n; [- m
printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p);
8 d9 t3 t- f% Zprintf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer);
; c) D5 l, g1 a4 s2 H: \return 0; , D9 i" y& t9 `0 U, I3 M4 n. [( c
}
( u; T. D* ]8 l7 Z- ?' s6 P* m { S! {2 ` `
2 h4 E$ u& g7 }- }" W
" u- f- o- n2 m) k; k函数名: chdir 0 {6 S w+ |$ J
功 能: 改变工作目录 5 |' V. D' d, I- }& i$ D
用 法: int chdir(const char *path); " e( [) V0 u4 l8 a2 U3 @
程序例: # {9 D* ?& L! a! S. E7 D
#include
. Z" e: h, [5 n2 N#include
" |$ |% E( Y0 @. D9 y- H, X& `#include
2 e: r( P9 |2 h, D+ S% Ichar old_dir[MAXDIR]; * _; c8 t M2 j' c
char new_dir[MAXDIR]; . A% M% P+ [& k
int main(void) K$ @( G: v+ M1 u8 q- j
{ ! Y( G/ q: n5 L0 C! C; w$ ]
if (getcurdir(0, old_dir)) 7 Q5 \5 v' J- W5 C0 g
{ , r3 U$ w) B3 p$ [5 s
perror("getcurdir()"); / v3 T z, h q5 w q$ m
exit(1);
* G* ~/ m0 B. l; k9 t$ s8 Z}
% P: q Y3 O& \- ^( @5 D6 k- ], wprintf("Current directory is: \\%s\n", old_dir);
+ l/ q1 T1 ?6 K. K# rif (chdir("\\")) * W$ e! v0 I: i3 r' S; ?7 N
{
, J; ?9 O# X1 X" w t) t6 X3 Pperror("chdir()");
: I, z8 P" h4 n% \: g' G) Cexit(1);
, [0 A7 E$ ]' @, ?}
7 E' N5 w, `( zif (getcurdir(0, new_dir))
* k3 v, }* G1 f0 w& @{
- o3 y- @: Z/ g4 Hperror("getcurdir()");
: d, K2 c4 \: {' d2 lexit(1);
9 l0 W( t/ F) J. E}
% M& g, K, A( g' fprintf("Current directory is now: \\%s\n", new_dir); / d1 @; E7 }' q: d- G+ F' n& T
printf("\nChanging back to orignal directory: \\%s\n", old_dir); / p7 K- V$ W9 D! R0 k/ \# v3 d# X
if (chdir(old_dir)) 4 E. P0 j. b P. [* Y
{
& C8 I5 B( S' h4 ^0 P; `9 ?perror("chdir()"); . p- `: }: J' {% G! Y2 V; p
exit(1);
6 J7 d& m: U; W0 g- i$ E}
- o( {/ S( D- {return 0;
& ^5 H% Z. i! s. L}
3 g, I7 I4 l7 M9 z2 v
- Y& ]! ^4 p/ ~% R( ] / z P& g1 o0 e* ]( T8 e
函数名: _chmod, chmod
5 }, h5 n7 c5 l) I& v+ Z% [功 能: 改变文件的访问方式 7 E# l `- t0 B* A
用 法: int chmod(const char *filename, int permiss);
# c' Z8 N z5 c) N2 O5 @1 m程序例: 3 z4 R2 f6 t3 f/ b+ E
#include
# b/ X3 q3 v$ e1 |. a#include " [5 U/ e, m1 }3 t* c4 a$ H
#include
4 N: z! a+ k3 X( c, uvoid make_read_only(char *filename); - M- x5 p( i* K
int main(void) 8 |+ B5 U* w I. V$ ~" K
{ ( z4 ^$ Q* g2 x) v5 ~
make_read_only("NOTEXIST.FIL");
& d9 a, B5 I3 L* s0 g. D" b3 o* ?; xmake_read_only("MYFILE.FIL"); 2 z7 E! j8 h5 b% Y9 e
return 0;
+ S% n" g/ i! t$ I}
, T1 T7 I0 A' `( c7 a" Qvoid make_read_only(char *filename)
1 M9 X I( I* n! \{
+ G3 X; w, I9 X% }( V; Dint stat;
" b6 v% y7 i& Q% K8 L# h2 K% X( Tstat = chmod(filename, S_IREAD); M }3 a# Z7 b" y( e
if (stat) 0 K6 g# i& [' U2 t7 n
printf("Couldn't make %s read-only\n", filename);
" m# B u3 g6 {3 e$ Kelse & E; k0 s: K' ^9 K( c5 [5 p0 b
printf("Made %s read-only\n", filename); $ Q+ h- x- y; h: Q3 i, h
} ; {+ n" U: W* q
/ u& R, a- X0 a6 q0 H
! A% j1 I" y$ r5 a
9 [- S5 e' R+ K' y/ r. [& p
函数名: chsize # v$ R ^4 `5 m: W* m. C
功 能: 改变文件大小
( Z) }4 X6 c) k用 法: int chsize(int handle, long size);
7 E0 u; j( S. ?$ Q+ h" r+ Y7 V程序例: n) O7 u! R- t6 P! m# y' ?2 X
#include . x1 u- i# p# s
#include
5 w, R4 w, N+ g2 Q! ]0 G#include
$ v. O! g4 Q, k2 m" F F9 wint main(void)
0 T6 C5 i( e! r{
5 }) `$ }, _# c: x" wint handle;
8 l9 y9 w* i% L8 n( d- @% \char buf[11] = "0123456789";
1 ?: \' W1 {" N/* create text file containing 10 bytes */
2 ^5 s, ]& Y0 M% `+ Hhandle = open("DUMMY.FIL", O_CREAT); 0 B8 l. D" K+ L+ B( K
write(handle, buf, strlen(buf));
5 }/ d; w8 H- y/* truncate the file to 5 bytes in size */ / `7 I$ o3 ?8 ?) b. R
chsize(handle, 5);
s% [/ r( t" K+ h: I1 S( l& d( d. Y+ s: |/* close the file */
z: X* n+ H: f& \close(handle);
, i. S4 e1 A1 J- G; dreturn 0;
5 r) Z T5 M" R) |! j% o3 b2 Q}
5 x$ a/ _3 c! z: J7 y. }* P! P% H7 k" t$ R
, _' z. h" m3 ^! I
函数名: circle - l$ T: t3 p' i6 j
功 能: 在给定半径以(x, y)为圆心画圆
% A* Y+ D4 p- U5 n- {7 Y0 h: i% Z! z用 法: void far circle(int x, int y, int radius); ' @* G. X& Q7 j
程序例: H6 C9 U3 C* t$ ^- }
#include
9 x" y& i' H! D1 \#include 2 X+ H d6 c, A, U x2 w. V
#include
1 X" J6 ~; D; u9 P1 U#include * y) }! O q& l R* y
int main(void)
% k. q( f4 q* A* ~- A, o5 H7 f{ - q- F! J4 [8 N4 F: {" ]; D$ n
/* request auto detection */ / R/ e+ W, ]- F! ~7 } ?; Y" t
int gdriver = DETECT, gmode, errorcode; & m6 @3 ]( o9 {) o) v% a0 `0 ^
int midx, midy;
( C8 A! @$ u& u9 mint radius = 100; 1 m, Q9 L2 O4 F$ f
/* initialize graphics and local variables */ % c8 y% b9 R! w( L( X
initgraph(&gdriver, &gmode, "");
# Z2 S% P; ]1 y. ^* A# z5 s/* read result of initialization */
& z; a# J% d8 |) _ eerrorcode = graphresult(); $ B# c z# U( c5 t, y4 t
if (errorcode != grOk) /* an error occurred */
, Y5 R! R% d8 d- e+ @{ / ]$ x" N: x) r" |, k7 \/ r
printf("Graphics error: %s\n", grapherrormsg(errorcode)); 4 z. O8 o2 j- b$ D) `# \' ]$ N/ q
printf("Press any key to halt:");
s5 k( r1 z3 i* m7 n9 ^getch();
+ \8 @( N1 R+ [3 _2 aexit(1); /* terminate with an error code */
& Z! A, Q7 b; B3 {- ]" j} & f$ I. T8 y2 g4 f- ^
midx = getmaxx() / 2; - ]; T |- I9 {1 f
midy = getmaxy() / 2; 4 k. s5 }: T, l/ p' m% \' G
setcolor(getmaxcolor());
1 w: p. P& R. L! r/* draw the circle */ ( s" v6 p4 w U3 q
circle(midx, midy, radius);
9 s7 E! K3 W. C4 [3 I+ A/* clean up */
* x6 ~2 H+ Q8 S( t0 b0 q& G: ggetch(); % X: `6 q. Z: a9 n
closegraph(); 9 q# m3 n+ ?! w: H- T
return 0; G; z, Z$ m4 [3 K9 t4 M) F {6 `
} ) _2 A7 m+ x% o) u" U
5 B* f/ k0 f5 b" z: D0 w0 V
! O6 e) F z0 a! |- u0 u: t; Y+ B
8 O* N) Y( t; [6 c0 ]! f函数名: cleardevice $ _7 T: ]( d; r) a4 K
功 能: 清除图形屏幕 / N! L& k8 g. w& a3 Y( f
用 法: void far cleardevice(void); ( C( ~2 L2 u6 }5 F2 X3 M) D
程序例: % E0 i, f4 i& x0 X. }
#include 3 t6 Y+ N' E9 Q- o1 `9 ?
#include
$ X( E! O& d" q/ ^: ~) X#include * B/ A" U A2 C) D
#include 0 k1 i- \& k) g/ Q% G6 F8 i
int main(void) ; y4 a6 _: @) y8 A7 c6 p
{
+ _2 D9 d" w1 ?/ p t' r N% y/* request auto detection */
* ^9 b! c% E8 Y% H# ?3 Pint gdriver = DETECT, gmode, errorcode;
3 n4 m0 w, T% ?; W- u% o% A7 mint midx, midy;
5 [" T/ c l* m& T/* initialize graphics and local variables */ M4 o' T1 y" b# Y4 v7 R t
initgraph(&gdriver, &gmode, ""); 9 l; f K; ?& c! w0 @ O& L3 m
/* read result of initialization */ ' l$ T+ Z1 q, M3 _
errorcode = graphresult(); # }4 ]; b' ]9 l1 l# r- J5 h
if (errorcode != grOk) /* an error occurred */
9 D4 l4 L" A( F# q3 V% N{ 8 O1 }! a+ J; d' ^
printf("Graphics error: %s\n", grapherrormsg(errorcode)); c& e1 F3 V* k q
printf("Press any key to halt:"); . X& N1 S4 ?4 \$ E3 p* z, D
getch(); & e# _( C% X5 f+ ~
exit(1); /* terminate with an error code */ 4 A' ]! v& T0 w' U) {0 t& C. V
} 6 b3 i% ]0 _- R5 V* _5 }4 e2 Q
midx = getmaxx() / 2; . h0 W$ R$ h' Y
midy = getmaxy() / 2;
) |* q7 @& O4 @0 m6 @setcolor(getmaxcolor());
% F D" Z* E7 t$ n5 g: A0 D+ q6 s" ?/* for centering screen messages */
/ |) s& v9 L8 s7 k8 D9 o* \ Tsettextjustify(CENTER_TEXT, CENTER_TEXT);
4 s2 o1 Z; t8 O3 L/* output a message to the screen */ : F9 t4 y! P6 h. p
outtextxy(midx, midy, "press any key to clear the screen:"); 5 d4 v/ k% H2 q
/* wait for a key */
! V) w+ N1 k* s. H& Dgetch();
! y' C' D o4 Q+ o* }3 G6 p/ h/* clear the screen */ ) g% I, _. f' i! M. w" X! d
cleardevice(); 5 p1 [0 p% Q4 U+ T* M$ c9 ~! r. ]
/* output another message */
2 q# l$ ^" ~- y3 Q* ]+ oouttextxy(midx, midy, "press any key to quit:"); ( W) L$ z0 D: D9 J
/* clean up */
$ c* B5 M2 t. K* v. s9 q4 xgetch(); & R* D# D/ Y# p" S
closegraph(); n0 s4 E8 f5 S! x7 o5 U! j
return 0; 1 k6 [# z% k5 s: o% s! t
}
. V3 |; S' V' A" \
9 N8 G: Z; v; B& f
8 @3 l( X3 f/ Y
. m$ Z: G/ @& [3 E函数名: clearerr ( E/ m6 R, H4 O% C7 ?
功 能: 复位错误标志
- `' B6 F0 ~( B+ C. X用 法:void clearerr(FILE *stream); . a2 J9 j- T4 C6 k8 _4 N) m
程序例:
# _4 `3 q+ L L8 X8 F% m! f#include
* g# I; J; j# H. `) N2 iint main(void) 4 w y8 Q. M$ l& u1 t) r; H3 a
{
6 D+ k) A4 S& vFILE *fp;
0 ~6 \& Z" h* q$ |1 Y0 Lchar ch; & x- v6 ~$ g+ u1 r* V" e2 {
/* open a file for writing */
0 }. b8 Q. Z4 g( I5 `fp = fopen("DUMMY.FIL", "w"); 7 Z$ t3 N: A' N7 k
/* force an error condition by attempting to read */
: s' g h5 z: ?$ [7 ^ch = fgetc(fp); P. o' F" r, y8 F1 G2 B. ~4 b
printf("%c\n",ch);
, K' T, O& n: K* _( A) o" `- }2 Bif (ferror(fp))
, B0 j* J; i1 W- O. p{ ; T* ]9 r5 [# [. R( N% e
/* display an error message */ 7 D% P0 L- Y# ^
printf("Error reading from DUMMY.FIL\n");
, }7 e+ i) D/ r- @+ X! _& ]/* reset the error and EOF indicators */
E ^. N6 i* Z$ I- Cclearerr(fp); + Y" V! o# S& n. A6 t
} # k/ n: j+ s' b+ V
fclose(fp); - ^& x7 G# w6 y# z8 o
return 0;
; h. _! M) q$ V}
! N3 v1 v: n( B
4 `, U# U; {8 e" O; d" O- L1 l
. T8 d6 E: k& a4 L0 g. z + x, X/ b( D y
函数名: clearviewport
% q& N% b1 S# B) B4 h功 能: 清除图形视区
3 h1 r; W# u6 V1 U1 ]9 ^) E' G8 I( R* L用 法: void far clearviewport(void); ! T( O* ~; t7 b
程序例:
) ?3 S9 a2 N4 a0 M" k% Y9 j) T#include
% o" Y6 L2 H( h! W: ~& z#include ' E8 _1 B5 m7 Y* A% u1 I
#include . w' ]# j; R' h
#include # ^ |( _$ |& f7 C g
#define CLIP_ON 1 /* activates clipping in viewport */ & }; A4 h7 h; M5 U& P$ ]
int main(void)
" Q4 U0 a8 a7 P1 H{
. ?- h: n5 R: e, o/* request auto detection */
9 a4 G r {8 uint gdriver = DETECT, gmode, errorcode;
3 L7 \8 Z1 j: d1 ]int ht; 3 U) Z' K5 w8 P- _. d) D1 \7 _3 U9 w
/* initialize graphics and local variables */
2 N9 |3 Z; X$ z& T3 b6 \4 S& Z5 iinitgraph(&gdriver, &gmode, "");
2 d( @0 d% s; n: Z: @: K/* read result of initialization */
( e/ ~" m! L0 n( Y+ uerrorcode = graphresult(); y1 G" x6 c% Q7 u2 z: r C
if (errorcode != grOk) /* an error occurred */ 3 U6 W) z7 j c8 k
{
0 d9 ~- Y- n! p$ P5 Vprintf("Graphics error: %s\n", grapherrormsg(errorcode)); ! [4 ^* c" D- m2 u6 C) v% _
printf("Press any key to halt:"); : z- O' ]0 h7 }( ]. v
getch(); 4 W" {8 B1 h! s
exit(1); /* terminate with an error code */ 8 A8 i$ f( c* C1 T
}
8 k5 H+ W1 z2 q& y: ~. q1 Tsetcolor(getmaxcolor());
" Q& c" S7 L* r7 d2 Sht = textheight("W");
$ m) `8 R% N9 D9 l; t. X4 T! l/* message in default full-screen viewport */ 7 H# Q! u7 A k* f- ?! x
outtextxy(0, 0, "* <-- (0, 0) in default viewport");
2 L; b; x5 `4 d) Q( w4 t5 o' A/* create a smaller viewport */
% U9 t1 k. k1 f( Bsetviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON); % l+ `+ Z- ~: @" X- ^( P
/* display some messages */
9 [4 Q3 S. O$ c6 u& f$ ^outtextxy(0, 0, "* <-- (0, 0) in smaller viewport");
7 n% H6 J, Z F5 P. g3 E: G/ S- Couttextxy(0, 2*ht, "Press any key to clear viewport:"); 4 _7 N# ~ C$ {3 q/ q
/* wait for a key */ 9 Y9 T" |. ]& J6 H( D( p% @" w
getch(); # Y M) i7 Z& K3 d# `. r8 y' X3 }
/* clear the viewport */
" x; J& \6 r! y9 J# rclearviewport();
, R( A0 D6 D* U: Q$ S/* output another message */
8 z; H: W) q1 j( ]3 T4 t( k; `8 Nouttextxy(0, 0, "Press any key to quit:"); 5 n) P8 k+ `4 q" L0 k
/* clean up */
$ `5 _0 A3 H4 F0 \getch();
0 y2 _0 E/ M$ p% S5 gclosegraph(); / Z# S+ C, I2 i# R! n
return 0; ) O" C& z, R2 C. e2 d. Y0 q& \
}
+ X' Y+ o w% f4 P7 P6 s' |. {5 ~) y' W" s8 V
& U. q$ D- ]0 W! V% Q/ O: f: f / V; a5 Y) B& n9 x6 W# V+ S( {
函数名: _close, close
+ C7 i" W/ f6 n功 能: 关闭文件句柄 : h8 [2 L: H# _' b, N1 x
用 法: int close(int handle);
8 a5 f) p9 Y, u/ r8 L' x2 Y% b A3 T+ J程序例: Y9 q( S& ]7 k! F
#include # t9 C# [7 N s5 |* w* ?
#include
/ {( n- x- ^( f' j#include
* \ f8 ?7 R! k" Q1 p$ [#include * |9 }, s+ H$ F! ~1 e
main()
/ v5 X \' t* p0 ]1 y' W# \6 K{ 4 ?6 ]2 ^4 Z2 [# m* p- F6 Y
int handle; 5 c- h" v) |9 W/ y/ l# z; m' |
char buf[11] = "0123456789";
# c2 W: O% T% {) e+ n/* create a file containing 10 bytes */
8 _: `& \; F2 b5 ]+ Thandle = open("NEW.FIL", O_CREAT); 3 x+ |9 T- X! L2 `9 `# a8 R
if (handle > -1) * |( u$ p; Q/ G" `
{ % [# w; v* }! o8 Y
write(handle, buf, strlen(buf));
0 I$ b& C+ m1 w9 b8 s: g3 K/* close the file */ ; W1 W( Q. Z. H" C
close(handle);
* F" @- ` z* O' r0 `. S} , b) m4 B% O1 [0 N6 s9 c* L
else
/ ]1 r# K/ m4 T3 W7 R! Y/ \! v4 _' c{
" X$ t% q, w# `6 g( z& uprintf("Error opening file\n"); % w& g+ c, r! p+ K+ G
} 0 p; r, C9 d1 @; \7 h4 O
return 0; & D# R; {8 B* ~% _( x8 K
} 5 @* x' @% Y' w( Z! x* u* D
8 c& F% B' K' F7 q$ @6 n$ h
. [0 h N! o! [1 x! e
- x4 K9 V% k7 Z: |) B! S; n! P) s
函数名: clock
. `* Z/ a$ y. G. L# Z; K功 能: 确定处理器时间 ; `; Y% h3 L) [* P; A; L
用 法: clock_t clock(void);
, Y) S* O: O2 ?程序例:
; I% {8 o8 b3 R( K#include
8 D+ Z, Y8 w$ G2 }# @8 w- y6 X8 T6 v1 g#include
! d$ Q' `8 |; m v( }#include
9 g5 `: a5 g( X/ f/ Vint main(void) ( B& B/ q; V* m7 r9 B
{
7 p. J( ?: i- G) }% j4 kclock_t start, end; ) M* D3 m4 k* W5 r3 `/ F6 Z7 n/ \
start = clock();
/ \% k+ f' e( ^9 bdelay(2000); 1 b- j$ P2 e, @
end = clock(); " d+ w! J- G/ Q4 \
printf("The time was: %f\n", (end - start) / CLK_TCK); : t$ g5 i0 I; P4 A6 |
return 0;
" e2 ], S8 C# y0 ~4 Y3 R} # {/ t Q1 n( j
7 n) B7 K, \+ l
5 P. ^3 n9 n+ j! h6 v. I ( O' B9 Y* U4 ~% |. @0 \2 Z' @
函数名: closegraph - b5 l, a8 ]5 c# G/ y% F" w
功 能: 关闭图形系统
3 c* I, Z3 u9 R5 w, z, M用 法: void far closegraph(void);
, p6 i" h" e: y, T程序例: 8 o w Z4 \+ J* L7 G
#include
L7 v- i9 d; e5 J#include 6 O* d! q8 m4 v1 q, h
#include # z/ {1 t' u8 _
#include 0 f# D5 Y$ C5 T: u8 j* D
int main(void) " T' L- w& Y7 T
{ % l3 H! s% _. a# N; Z( D6 B
/* request auto detection */ * ?$ Q* \- W2 e6 d
int gdriver = DETECT, gmode, errorcode;
* _; g. z. C% s' wint x, y;
. l/ T1 F9 `" g/ n, K/* initialize graphics mode */ / y. m- `/ a6 ]# P, f$ f$ t
initgraph(&gdriver, &gmode, "");
4 u! M2 N0 x% f9 N/* read result of initialization */
( D/ l0 W/ P* B1 [- `, O( kerrorcode = graphresult();
# w9 h0 v; v! t. e" u( fif (errorcode != grOk) /* an error . D+ p& [# N, i
occurred */
$ \6 A u2 z2 ~$ D4 s{
4 h n' Z1 T4 E) Dprintf("Graphics error: %s\n", grapherrormsg(errorcode)); % P: i5 [- ?3 }& E1 t4 r
printf("Press any key to halt:");
3 ]6 v8 p$ |) j9 Z1 J. F* Lgetch();
, i" D% _* \) X0 B# ~6 @exit(1); /* terminate with an error code */ 6 d R7 T$ \* F \
} 9 B }# f$ n; t N. s3 y
x = getmaxx() / 2;
/ @$ j- {0 U# W% hy = getmaxy() / 2;
' c, ` Z# \3 \" j/ m5 M/* output a message */
E" [0 }; u/ X+ j# s2 o8 D( Msettextjustify(CENTER_TEXT, CENTER_TEXT);
o4 P3 [1 `, p5 Xouttextxy(x, y, "Press a key to close the graphics system:");
; W5 w- T6 H2 J& Q4 _/ C4 D/* wait for a key */
- N; O* p+ x' p0 agetch(); 5 Q! n$ m; N2 ]! d9 H4 p J1 Q3 z
/* closes down the graphics system */ 8 M, F$ i% s, y$ [+ T8 ^+ F
closegraph(); ! K, L- X! a7 [# x
printf("We're now back in text mode.\n");
7 ]; e. D- W, w. _2 }" Y# v( H1 fprintf("Press any key to halt:");
* y- P! w4 s% L5 L6 ~% ngetch();
& `0 M6 x6 g4 q) h1 P& lreturn 0; . M4 f& |5 O/ |
} ) e% V. [( [, y, c& i; I# R
/ _* j+ M, p# ~# ~* S' {2 _
) j$ o1 ]* w$ @9 c k1 b! U
# ^' z: K. L9 |( I+ S2 h x
函数名: clreol 4 Y# E3 U' H: Q3 w+ m
功 能: 在文本窗口中清除字符到行末
/ Z8 i3 E; h. R4 ^& @6 g- S2 x4 V用 法: void clreol(void);
# T( Q6 K0 H0 m" U程序例:
) z: u, T1 A7 t, {: f2 T' g; W#include
* Z2 i4 r: q" nint main(void)
' d/ \4 @# l. M* E$ e2 h{
4 L. p7 i. b, Fclrscr(); : r. P5 f7 z# A' u( u7 E
cprintf("The function CLREOL clears all characters from the\r\n");
+ s! q/ `$ [8 `! p4 z9 f4 Zcprintf("cursor position to the end of the line within the\r\n"); 4 a( P3 T0 w. S- Q
cprintf("current text window, without moving the cursor.\r\n"); 6 Z# {/ M' q' Q; ~ h" h# S
cprintf("Press any key to continue . . .");
5 m: U" L! M8 q# Q7 A$ K6 ugotoxy(14, 4);
! U7 H W& [3 v/ _getch();
5 ~; h9 M1 m9 Cclreol(); ! z w8 d2 a: `& |
getch(); 3 F7 K d9 u; h$ n4 N
return 0; 6 f! u1 z. S1 F
} ' Z/ I' s$ a1 Z4 q* F8 Y, t1 z
4 g5 q2 V2 t- h g/ Y9 @
4 U+ K5 e) M% M1 z$ O) `
4 B# G" v# i' `/ \: L函数名: clrscr / I! b6 K2 z; ?
功 能: 清除文本模式窗口
9 a+ ^) y b! k$ `3 h$ }- o& n! x! ]% ]用 法: void clrscr(void); 0 C0 c6 H U7 R1 ]- Z. i J
程序例:
' w$ D( L% r9 Z# i3 [4 _9 ~#include
% z$ G- v) ~" e' u) H" ]int main(void)
) L2 `+ G, n+ N# Y5 K+ L4 t{ 2 [7 c. R# t: R m* ?# a0 g# g- T' W& b, R
int i;
: h' n( I' n7 `. y1 e8 O7 @# oclrscr(); % M" L0 ~0 q* f; x" U" x
for (i = 0; i < 20; i++)
" ~( h' |- d! a% p Ecprintf("%d\r\n", i);
4 s3 B E9 y! M1 L% D0 jcprintf("\r\nPress any key to clear screen"); ( P8 T3 u8 f* d4 \4 V/ H! Z
getch();
8 A" \/ W. A! ?$ y5 B. k; @$ dclrscr(); 0 V& W8 I% w( e
cprintf("The screen has been cleared!");
+ S' Z+ c/ Z v6 Y+ xgetch(); G/ I' J" c2 C8 ^
return 0; 3 }& D( @) R% O+ G- L
}
" h& w% o, x1 ]4 Y' r9 H, p
* R" Q, L, g9 _0 q
; m% S$ G- ~$ W5 F) T# S F( a& L! r ! n' w- |1 ^2 E" n
函数名: coreleft # j# b: k: C0 O6 N
功 能: 返回未使用内存的大小
- K9 R" S& D; B7 O8 {& ]用 法: unsigned coreleft(void);
& \) p8 I! b4 p( B; N程序例:
" f3 t: V! B: X; E( L) g#include
0 x: a, B' w- X: T' T$ _9 J#include
, v6 P- [3 r0 Q# q6 Z* E& }( ?int main(void) ( Z! R* J, b" M( A* m' y h5 _' w
{ ) X7 K6 j- m0 e7 n; x6 Q) b
printf("The difference between the highest allocated block and\n");
3 ^( f* h0 C& E; [6 Tprintf("the top of the heap is: %lu bytes\n", (unsigned long) coreleft());
7 ~# M! N! g6 }, w# rreturn 0;
. H. p0 m9 R- E' L; d}
# O. `# U) ?9 T7 K: R. e
0 X' N$ |. m2 D函数名: cos
8 Z$ t7 W8 l3 q F+ H功 能: 余弦函数 ; s0 `( S5 ?: ?# W& a' D$ K
用 法: double cos(double x); - G- d* |$ N5 j+ d) C& H
程序例:
4 m; ]# k1 F' H5 `! j#include % R3 [: o* Q, p V8 z
#include
( q: @1 L6 T7 D& kint main(void)
" b2 ^5 O& @% f. c- Q0 l8 G+ H{ ; b. d8 w1 K& B) M+ q
double result;
; B" P7 I/ C5 S* K' vdouble x = 0.5;
# F5 E1 B2 I" T; u2 _$ D0 ?% O6 cresult = cos(x);
; ~! e: Y/ c, }& {printf("The cosine of %lf is %lf\n", x, result); + L4 l1 S8 e" S) }9 Z
return 0; $ W: Q" P5 w: P: f+ a g; I
}
1 U% G3 T! z4 K r, u$ f4 F, C6 W$ Q7 T* [1 ^
2 ], E1 u7 Q) u; f( F+ O
, E( N. H) J5 u函数名: cosh
5 J a, j' z) ^功 能: 双曲余弦函数 5 f3 c ]2 x/ M' k7 V# m: P
用 法: dluble cosh(double x);
: O4 U& d) w/ B1 Z2 `% b& ~0 G$ O程序例: $ s) l. E" N% r& P
#include 7 h: N+ G8 S2 q4 `) D
#include " z a0 j( L! G( X$ V
int main(void) * Q( @5 N6 v4 J$ l* I
{ ; @- E' \$ Z9 c# K* `) h
double result; 6 D2 L% B' N7 D7 B+ ]
double x = 0.5; . A* V7 j. [9 d# a* A4 j, q
result = cosh(x); 8 G$ J% B6 v$ j A& x
printf("The hyperboic cosine of %lf is %lf\n", x, result); # i) r6 l. V3 V( Q- V+ x
return 0; 9 ~/ r Q% y9 `# R
}
+ S: Y8 r. S2 p, E" e y" y9 T4 Z% m2 D0 `; o9 {* m
4 F. l2 w9 P: Z. ]. r# b : h p% w$ t" f( g
函数名: country
5 q; o; A( D9 n _ D. \* q I4 L& D功 能: 返回与国家有关的信息
) R e1 f( b p* O2 e) V用 法: struct COUNTRY *country(int countrycode, struct country *country);
0 I3 L! y9 ?8 B% |8 }2 a6 j程序例: - B' C/ K# S/ x: b* k5 Z
#include
2 `1 f6 x5 _. V' l2 q#include . h, `+ F- W% D' T/ `% ?/ k. x
#define USA 0
; r2 o4 T& u) }+ Y( j9 z$ pint main(void)
: Y% w. J9 t" I; X, T$ C* N, Q/ ]{
( O; n% L$ }# g( ~; Vstruct COUNTRY country_info;
% u9 W2 ? u' ?$ A7 t# _" jcountry(USA, &country_info); + k- h5 ~5 r2 P
printf("The currency symbol for the USA is: %s\n", M2 U$ D. ]; \, n$ _3 O
country_info.co_curr); " N0 A! E' j% c3 u j" C) W
return 0; |& Y7 t- e& w
}
" w7 Y9 t' R2 f6 E% E
0 |/ o. l: C% |( v: R7 C7 P, x0 f- W+ F! K' R4 K
# o/ @4 X1 n0 _6 l. L2 t9 a$ y
函数名: cprintf
9 D! |. R p7 E功 能: 送格式化输出至屏幕 * `. Y( h5 j0 m# Q7 G) S
用 法: int cprintf(const char *format[, argument, ...]); / Y# R) N/ U8 M1 L. d
程序例: ! _4 i" E: U% I3 D$ S5 o
#include 6 T. F5 ]+ u3 p$ v0 _: j; k( L
int main(void) & M2 w6 w/ Z/ n/ T. X8 N5 p. o
{
1 J' E! F$ g5 ~/ o6 ~+ _/* clear the screen */ : e4 p* P/ C' [' d
clrscr(); 4 F, O5 R, Y9 V. v8 E' ?$ _
/* create a text window */ 6 u+ Q9 t+ N* q$ n" h0 y( X! [% m
window(10, 10, 80, 25); ( k; @. ]! a7 c5 P+ M6 \# r; h
/* output some text in the window */
6 [: a' y6 m2 O* Bcprintf("Hello world\r\n"); + O% g* }/ S5 E" y& o
/* wait for a key */
" c' L, }/ j; `, egetch();
8 j- p# V" m" b* V. a8 sreturn 0; : E0 _/ v$ q2 ?7 }. D% @2 G6 D% ?
}
( y- C0 C/ ]7 ?1 J& f# {) b" | Y) E/ f. N9 R' A, b l- L
8 i; v% p) Y6 J, |# H) H) ~
( q1 l6 l0 D0 L6 @0 ]; Y1 V
函数名: cputs k* H7 d0 |* r/ T. u, N
功 能: 写字符到屏幕
; s3 j2 X9 u, }- w0 z用 法: void cputs(const char *string); / _9 ~9 N* }( F$ e, _7 O* K
程序例: - N. r3 `8 d. p) O2 ]( B8 ^
#include
2 ]! {/ r: S8 f4 }! O0 b2 ?0 M2 zint main(void)
! e( M. H1 g8 X) u# h{ ! n/ X5 _4 c+ g9 u" h- M
/* clear the screen */ $ s- h* m( ]4 f/ a* A
clrscr();
4 }# W( } b3 k0 x) L/* create a text window */ ( p/ ?7 p1 z- Z
window(10, 10, 80, 25); - Q2 O- x0 u/ o6 H+ o1 t) \2 |; S
/* output some text in the window */ ; `* Q1 B! a( ]
cputs("This is within the window\r\n");
+ w& r" \( n. E' J' }9 y5 s/* wait for a key */
% m- d# x9 |$ } `5 G' [6 xgetch();
4 `8 R6 f$ y/ L0 l$ Jreturn 0;
4 L: p3 G3 ` h6 W/ k, t} & z, I- G/ \2 D: [* e" S
; i3 @7 B$ g* {6 @& P( t+ ]" ]% g$ w/ n. r g
' z; x: e# d L: p2 \- @函数名: _creat creat
$ h, |# ^/ j( F# Q8 K# G/ L8 Q8 B功 能: 创建一个新文件或重写一个已存在的文件 - p& A: Q1 H" q o( F
用 法: int creat (const char *filename, int permiss);
: f2 n* G$ T# D& n程序例: " [) I/ M' `6 V7 V
#include
7 P q; J! B& e8 ~. [#include * ?; O$ j. ?. V0 {
#include % u( W: {3 P' O3 L9 Y9 P# e
#include 1 V' y, }! Q- J4 x
int main(void) ' w3 i8 U+ y* c8 V' S9 e z
{
$ P+ ]7 F& G) @ Q" [9 W7 E; c$ xint handle; . {6 ]: U5 `1 ?8 N! i1 p
char buf[11] = "0123456789"; # o$ K$ T E! i( x: Y$ P
/* change the default file mode from text to binary */
" I8 f1 C+ p$ |( b: J6 g% H_fmode = O_BINARY; : y) Z$ H; `3 k' x2 m7 e- p! v, l" A
/* create a binary file for reading and writing */
/ R/ t! E) f4 J; w* O& Hhandle = creat("DUMMY.FIL", S_IREAD | S_IWRITE);
- p& L+ H8 K' {. u0 x$ ?/* write 10 bytes to the file */ 7 |1 @* }$ B0 M( H& g" k' m
write(handle, buf, strlen(buf));
5 E5 ~6 }, q7 M3 k8 q/* close the file */ 3 `2 ^# f. x: ^& B g; |# w
close(handle); 2 y% X+ ?) [1 i3 r5 g6 l
return 0; / g& L9 q: O7 _( V2 c
} , d4 o( Q/ }. R8 c* g( z \# I
/ S7 n6 K/ f! r
函数名: creatnew
9 h- l! r" l( |* g' \功 能: 创建一个新文件 7 i2 X1 I! M$ a1 {; n
用 法: int creatnew(const char *filename, int attrib);
. c& I6 c0 b9 L$ D |4 ^( F# M程序例:
& H1 N6 {3 g. G4 k, n- g$ s#include 8 ?* \& P: i Q: v- R7 ?# y9 i
#include 3 s ^) Q. a9 M! x2 I4 y, g
#include
1 {; i. q4 k/ N$ T8 q _#include 6 H& t" x: m d8 i
#include 4 H2 I+ U* @/ ^+ a
int main(void) & j4 Q ]4 B. p+ w6 w3 P
{
, s6 x$ b/ _& u) r: Nint handle;
, n, \) e& r( P" t: C* K. h0 Rchar buf[11] = "0123456789";
# F; v J7 [" U5 k/ ^1 H/* attempt to create a file that doesn't already exist */
. J( Y- j- P- }$ @! `# ]9 V6 v1 \handle = creatnew("DUMMY.FIL", 0); 3 o9 K. m* b+ z% A# t
if (handle == -1)
2 E! @* s' O/ W" A- S0 r/ z8 kprintf("DUMMY.FIL already exists.\n");
) s( W0 `5 P7 lelse
5 a+ O2 N" U& Y{
. u7 F% y. F: n. R* Y( H, _2 k; w* Mprintf("DUMMY.FIL successfully created.\n"); * a$ i+ N9 _, H K" q( d
write(handle, buf, strlen(buf));
: I. D5 z% ^* }' K( Fclose(handle); & y9 \% a' Y6 D4 H! m: p r
} 2 _0 Y2 e+ l, \* x8 t0 p4 o
return 0; , r, Q6 U% }1 x+ ^" m* R% y9 ~
}
" v/ u' s8 u1 c' n; n$ s; g1 s$ f' e
7 n, E2 ]: s9 I1 y& k4 I+ i0 c- P$ }4 s
/ T/ V; C7 g) g! k函数名: creattemp
$ c4 h& |) W2 e0 p: j& m+ E功 能: 创建一个新文件或重写一个已存在的文件 2 l# `* u7 _( H0 d8 h+ d
用 法: int creattemp(const char *filename, int attrib); - I: i' S* H1 X+ Q3 U" z4 c7 ~: s
程序例: ]! O3 t5 n& A" H
#include ! \% |9 F9 R4 d- X
#include . u5 M" X5 l* C3 r0 f, O( c7 {- J
#include
0 b Q' m a) \% k! W. Y" x1 Jint main(void) 0 e) R* L% Y2 N! i2 v
{ 2 X* d$ I4 u. [! e0 Q
int handle; 7 W4 J% r5 a; J% J
char pathname[128];
0 v8 [( V: g: J; k9 k0 f( o, estrcpy(pathname, "\\"); 5 e& r# I7 \7 `* d( h& W
/* create a unique file in the root directory */
2 }+ v: [ I; ]! a Khandle = creattemp(pathname, 0); 7 E5 Y! n% B/ i; q4 {1 E0 |/ b
printf("%s was the unique file created.\n", pathname); ! y6 ^8 ?6 k0 S; c2 g4 P" v7 _
close(handle);
7 L z3 n" i3 c( _( s& preturn 0; % |3 s! B9 e* X4 [0 s
}
& C6 _2 a4 b3 A0 v
' C% M( ^, d" r' D0 l# V; [! A$ _( J! Y6 W
7 Q" c) m- \/ [* {( V3 t
函数名: cscanf , T) I F% Y; V, T6 ?% C0 L
功 能: 从控制台执行格式化输入 X0 G/ E' U1 e. x
用 法: int cscanf(char *format[,argument, ...]);
$ d9 ]0 c% q" ~* ^程序例:
. g8 y6 v7 K5 ~* A" u6 J#include 3 H9 ?/ ^. ~: F2 N
int main(void)
& e3 g0 L! l$ ]% C4 R/ Z{
/ s4 q3 s& o0 V+ H' U5 uchar string[80]; ) K; u/ K3 _* r @4 Q& F
/* clear the screen */
! i2 i, s5 V9 h, cclrscr();
1 R3 ?; z% I) S+ {- c0 N/* Prompt the user for input */
6 u1 D* a+ I1 B, ycprintf("Enter a string with no spaces:"); 5 G& n2 `" A, J9 }
/* read the input */
2 j# C9 k9 X6 T- [/ v/ i, _0 X3 f! lcscanf("%s", string);
4 {. n9 L* o# l' y6 {/* display what was read */
, \ ]' }( ~( Y4 Vcprintf("\r\nThe string entered is: %s", string); 8 f$ @5 ^4 Y. a z
return 0; J6 Q- s4 a( Q; b t
}
X5 ^9 r% n5 a% a& ~# r0 l5 Q
- T6 D1 }, K/ l: }4 T& @: f1 Q+ O5 o- P$ q- E$ U$ b3 o& ~, _
$ J) e4 T2 ?% q
函数名: ctime
5 L7 L4 m9 G/ |" J5 Q% [; w功 能: 把日期和时间转换为字符串 8 m8 d# ] t& X5 v
用 法: char *ctime(const time_t *time); ! Y% l. r1 b1 |: S/ y( b
程序例: % \) Z" G4 ?# {3 a j5 H
#include
3 I: H3 B" t4 H. K- w#include , [% Z- Y+ E% V6 k1 q* j: Q
int main(void)
4 j4 Y9 J- g- ?{
( p5 Q( x2 e$ p) g. k; Ntime_t t;
% V5 D0 ^, i6 xtime(&t); 0 q5 B" `3 a x2 @- e- `3 N/ |# a
printf("Today's date and time: %s\n", ctime(&t));
( ]4 J s' k" treturn 0; 4 c( z5 R! T" C! Y' ]
}
# I4 h* j$ e. y. O" x; @, i
0 b% w# W" ~: z7 d! l
- ?" |. X _* B/ ]/ M' Y J: E+ f
! O6 h6 ]) v" q( Y) E函数名: ctrlbrk 0 b7 j$ n L i. Y1 @ A) \$ p
功 能: 设置Ctrl-Break处理程序
. N4 i& s" j$ u% D8 E用 法: void ctrlbrk(*fptr)(void);
, E/ U3 U1 I) E. x/ T. H) ~1 A5 k程序例: - F8 M, b- r+ Q! z) G4 V' d+ w
#include
* T. {* f% P, v8 } b1 z+ J#include
. Z# h) _/ e0 s f' e9 n#define ABORT 0 * F9 c* i2 \' m2 \2 H
int c_break(void) 2 y/ s: }2 z+ J7 y
{
% _) Q+ P6 M1 Tprintf("Control-Break pressed. Program aborting ...\n");
1 p) \* l+ i/ Y3 B; ~2 b6 \( Kreturn (ABORT);
1 i+ }$ x- m/ J1 v7 L} . m, \) F# }( \ O
int main(void)
5 k5 R% R7 s) p& `9 X* U8 v' m h{
' Q1 s! e2 K5 b3 z+ gctrlbrk(c_break);
) d' ~$ v. {2 t, _, _( dfor(;;)
`/ Y/ ]/ B j" O& b{
, v( h5 F j( A6 _5 h& uprintf("Looping... Press to quit:\n");
5 j' \. E k+ n/ a} , [! P3 {4 f$ T3 c, A
return 0;
# b9 V8 y7 i! E2 R3 T5 W" o} |