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