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