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