|
函数大全(c开头): i$ e! d" @ V _5 M: X3 R! V' A
+ B+ u* f D' k0 C' c函数名: cabs
0 S, U. P9 U* w; ], S功 能: 计算复数的绝对值 " @: H* o! B% o$ e# e) t; Q! B
用 法: double cabs(struct complex z);
' [3 ~2 ?* E7 }0 O程序例: . U6 r( s8 M: `/ W
#include # J6 W4 r0 u7 {9 s. }, w
#include
$ S+ p8 c6 _ j% t6 _0 jint main(void) * k/ X( |1 k+ K
{
7 r2 B0 E. r; q% c/ X1 {; estruct complex z;
$ a0 @" \- g1 {" V0 h! Xdouble val;
* c/ q/ H7 J9 Ez.x = 2.0;
) O4 m1 \" B+ {8 [% V& u! W) K2 @" Ez.y = 1.0;
( ~) a6 O# G5 `% Y+ ~val = cabs(z); ! c' T/ B& W# D
printf("The absolute value of %.2lfi %.2lfj is %.2lf", z.x, z.y, val); , z$ Z. T: B- n" c% P% e
return 0; % h. B8 Y( b& t" m. ^) V
}
* }4 d) ?# [, q. @% h6 E0 r9 |$ a2 z. a
; Q# L: A2 R$ ?8 \) `! T! c# s! `
; n; Y5 K& g& O1 s9 M0 X1 C1 e
函数名: calloc
+ G1 G9 m8 f7 p功 能: 分配主存储器
8 s7 {2 U* {9 `. l& P9 A' \+ ^用 法: void *calloc(size_t nelem, size_t elsize); ( ?. g. o0 |7 [, K5 j n5 y& w& C% u
程序例: 3 S4 T0 z# _' { O+ @8 Y' f8 B
#include ' [. e, L Q w0 b j/ H R
#include + A0 ~ m+ i! C- A( @
int main(void) v' D- E b V. J! Q6 o
{ 2 A' u0 t& u; [! K& {
char *str = NULL;
; L S7 R, B. B. I* V( s/* allocate memory for string */ * r7 S& p6 [9 H) |1 X! l
str = calloc(10, sizeof(char)); $ _4 e' ^6 V, n/ r$ r
/* copy "Hello" into string */ P! t" H; t; x \
strcpy(str, "Hello");
) d8 x9 M1 J* D( s% u% q4 V/* display string */ 5 l0 X. `9 L" ^
printf("String is %s\n", str);
7 y& q/ W+ u; F& ?. n/* free memory */
6 E- p: Q, `3 h) nfree(str);
( i) V# E. o+ V3 p1 H' |return 0;
; U' S0 ?5 O1 a; y}
) ]% J$ Q2 k' e
& T1 ]+ {8 w7 ]' E& v8 G
" `$ t6 I' a$ }% d: K1 \ " F, \" R2 |# p6 V$ V% G
函数名: ceil
- w& Y) A# z' t# I. M, }功 能: 向上舍入
1 \4 ]# z' U$ s+ l; y用 法: double ceil(double x); - q; g# y3 K% p/ |4 l; X1 n
程序例: + M4 }3 c3 G( z. l
#include , }1 d1 H) P0 B$ P0 t. T& P- B: X! Q$ A
#include + m$ [8 M- J) W" h- {0 e; C
int main(void) 3 n, M Y- c$ ]$ Q% d
{
2 Q- p3 b: p% u$ hdouble number = 123.54; 9 R5 v7 d, l0 o
double down, up; % R" f' |0 t2 u1 w9 ^
down = floor(number); $ }# @ Q$ Q# `/ p! {/ O Q" G
up = ceil(number);
* @- Y; F* w! X+ Q( A O2 Vprintf("original number %5.2lf\n", number); " z5 B* X$ y8 y& j8 K# R" I
printf("number rounded down %5.2lf\n", down);
% K. _" \8 ~4 {) C1 q8 ~printf("number rounded up %5.2lf\n", up); . t4 t- ^! _: w1 d# i
return 0; 7 F9 e7 o- A* r3 m
}
1 M+ |4 @ I; Q
0 W6 |( @/ _% {
8 t, |! ~2 c5 i$ y+ w5 L
* m& S- `9 @/ Z函数名: cgets ) ?& g. Z2 V& b# l6 |) y9 I
功 能: 从控制台读字符串 & s% ], H% |; k4 t6 c
用 法: char *cgets(char *str); - V" J9 C6 j2 |2 z' A' ^ }5 c
程序例:
0 P; s x$ T; W7 r#include
% Q" F s- v) A. w8 ~+ f( `#include
% F6 S3 A% [' d6 t4 [% Xint main(void)
) m" X) J' F& k9 ^{ & j* P3 o% Y9 r4 k/ U @5 O) e
char buffer[83];
9 H& v! e' W$ @8 M. b& Q/ Ychar *p; 8 @/ v3 D. I$ k S
/* There's space for 80 characters plus the NULL terminator */
: z+ M$ J# V1 j, ^- K cbuffer[0] = 81;
, l$ p: s' p* ~, I# u+ iprintf("Input some chars:"); 3 B2 B- ]5 ]" v+ N- E
p = cgets(buffer); $ O w# r7 U* D: e' Q' q. p' w- r
printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p);
9 n9 r8 D4 l4 y4 G/ Mprintf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer);
7 a; R2 A8 g$ N/ h# e& f/* Leave room for 5 characters plus the NULL terminator */ 7 ?- V: E1 _( u' g H3 P0 S
buffer[0] = 6; 5 q- R8 }; F: v; M" d+ B
printf("Input some chars:"); + @* \; m9 G- Y* N) d5 ?$ b
p = cgets(buffer); ) I( r- B Q5 N4 f7 [2 @; M
printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p); / E( ^' K# ]) J. Z0 f5 M6 e
printf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer);
- J% z4 l& E9 t$ x7 g$ oreturn 0; ; ~9 C7 ]. X$ a; L# I! }+ f! I' |
}
) ~/ K- b, }# D- D- u+ h. @' R0 D8 u; x1 n" l9 `" ]3 p
% A3 F/ ?1 u9 u5 h' K% Y: W
& ?+ s* Y$ f: e) h& t8 Z1 J( I: p函数名: chdir
3 o4 G1 Y. g7 ?1 G9 k8 w功 能: 改变工作目录 / a# E3 V. ~: W" u
用 法: int chdir(const char *path); % F4 b( \# y' A. _" c$ H' c' `$ f/ f" b
程序例: 4 d$ k7 o9 R% v9 Z: ?4 Y
#include 6 K" y* c, f2 f1 m
#include
/ ]' U7 W. j6 G7 f2 @#include " s* f/ [3 J( ^
char old_dir[MAXDIR]; 9 F' Q( V7 }8 t$ C
char new_dir[MAXDIR]; 5 ~! V8 D# m4 g; Z
int main(void) / h9 r. ]- Z5 u7 q$ p
{
7 j; U! |2 B0 {. g0 _/ f1 N4 zif (getcurdir(0, old_dir)) ; e1 N6 E! S2 j5 {9 t) F1 N$ b
{ / D. B. x; ?: A; q
perror("getcurdir()");
* Q" u* f: m* rexit(1); 7 P& J+ W( j2 F1 L, b
}
/ _5 u% m+ k# Cprintf("Current directory is: \\%s\n", old_dir); % u& v2 R. x/ ?% ]! I8 Y, s8 W
if (chdir("\\")) 2 @ S4 ?4 F# T% X" f
{
; q; `' a3 y9 P0 z; uperror("chdir()");
1 t9 h x; a+ L3 J8 Y" Eexit(1); # m) e9 o+ q/ C' M
}
) G, r, C- Z: [) }8 i! K/ i! B6 rif (getcurdir(0, new_dir)) 1 R* ~6 l d" M& J. T
{ - o0 p; T/ L" S# |$ V+ E5 @
perror("getcurdir()"); : L" U) S) N) w R4 y2 T" W
exit(1);
1 a2 ^' ]2 F, c% W4 K8 l} ) H+ B7 g) j! D# }3 _: {
printf("Current directory is now: \\%s\n", new_dir);
1 S6 A W+ z8 k; J* n, T, [printf("\nChanging back to orignal directory: \\%s\n", old_dir); 2 S" W/ o. ?, h0 Q/ Z
if (chdir(old_dir)) ) q6 m) o; e. U% k
{ ) r+ @1 M( c( f8 d) h
perror("chdir()");
% ^ B4 c7 m# P6 b0 ]5 k1 ?exit(1);
# H U1 J) X* F# j8 _# H0 X, q} ' F* W& ~& I. E+ t
return 0; 3 j2 L& C* N1 i$ D' h+ u
} 4 y$ i. W0 B$ [. M5 F# L4 n/ d( ~
3 u7 a+ `3 [% F$ d% w O
) I" [' R. F1 O, B函数名: _chmod, chmod
0 A* P5 k. Z. B) @$ O功 能: 改变文件的访问方式
& K: \* L6 d1 m) P' ]' d# Q( G0 \. n用 法: int chmod(const char *filename, int permiss);
m+ p/ L! ~9 X" [2 n: l程序例: + G' e+ \+ U! I& i; i: L
#include
# `$ X t: l9 N# @#include
3 K; S a6 _% h$ W3 B. Y( c#include
: l. l1 k& p' ?# o& I$ j4 |' {void make_read_only(char *filename); ' B, w8 Q/ ]9 ?. Q2 X1 G
int main(void)
% G. |3 G# g) t: B" v. V: w8 |{
, |0 n) E% V# Nmake_read_only("NOTEXIST.FIL"); 3 B$ \1 Q2 K3 Y& i+ T$ H
make_read_only("MYFILE.FIL");
. u. b* i3 a/ i3 f! o$ C) ireturn 0; $ c3 G4 ^0 t" ?( ^4 d9 @9 P/ g
} 9 o Y1 k/ X5 E
void make_read_only(char *filename)
: x- Z: H2 w% K" L! g/ d' \6 k% Z{
- l3 C7 n/ M7 ^+ a4 U& B& Cint stat;
! K9 G9 D7 W4 J3 J3 n1 estat = chmod(filename, S_IREAD); - a! i8 F3 y6 T; H; l
if (stat) 4 d4 f. [ c/ @; o: s
printf("Couldn't make %s read-only\n", filename); # Q3 S7 b L1 w! C1 t! b9 ?
else
3 c. M! G/ N! W5 ^+ z9 {printf("Made %s read-only\n", filename); & n: M1 i. O3 \
}
$ l/ n# G8 }( {# j
6 u7 a* D4 Q6 @. Z0 L+ t. u- ^( g, `6 F$ b: T1 F3 M0 g
1 e6 }1 r: f' u$ G! l/ {函数名: chsize
! B% |$ D# G* ^! i2 E功 能: 改变文件大小
! S2 {" l+ g: [6 f$ T6 U, q用 法: int chsize(int handle, long size);
2 Y( S e8 q/ _, H程序例:
& C7 X" `" p9 x8 F( d7 P#include
5 G# ^6 Y& v- p, I: {% n#include 7 w d% a+ G4 d C* P
#include
& f# e& x0 F# E" l( bint main(void) 5 ^7 C3 b( J4 H$ |# s# B6 z i/ Z
{
$ C0 Q, i8 ~$ k% t; L8 @int handle; 2 a; X1 `5 _- y( x' s$ |. C8 J
char buf[11] = "0123456789";
. |& \8 }& ]* C- W6 G$ m/* create text file containing 10 bytes */
/ w! f+ R. c) V: uhandle = open("DUMMY.FIL", O_CREAT);
! N3 K, V6 Q( \% f& m0 N3 L zwrite(handle, buf, strlen(buf)); % ^$ k3 y3 ]5 A, `, ?1 ^6 {$ C
/* truncate the file to 5 bytes in size */
* d( f% d& p% l N: ]4 @chsize(handle, 5);
' D' k% i) j3 Z z I/* close the file */ $ t( B* J: w$ ^7 _
close(handle);
. D5 j/ M* T# M- jreturn 0; 5 ?: O; ?0 f$ `5 w
}
c- T4 m! H. A6 s: q% z. ]' G: P4 E/ E) L1 ]- y) `5 ^: y5 R
& F! R, q) t/ O3 ]% s' J+ F! O- z函数名: circle
* d& n; d4 K# F4 P) I6 g; u功 能: 在给定半径以(x, y)为圆心画圆
' j- U" v4 E) P( w用 法: void far circle(int x, int y, int radius); ) a m- P! V' J4 f, x9 N/ R
程序例: " _) {1 [5 h+ l; x* x
#include 4 t0 O* K! F/ n7 @3 I
#include # z! R7 V- M7 H% j. [
#include ' G7 y' ]: D& r
#include
! N; p5 S1 H* tint main(void) l, } V# J0 |4 h" V
{
& d3 H7 e( @! i0 p/* request auto detection */
, J2 {1 e- ^+ X, p0 Tint gdriver = DETECT, gmode, errorcode;
- I6 v, }2 T5 w5 ~. m7 Mint midx, midy;
/ t4 U9 P$ a, ?2 f& m; ?int radius = 100; / h0 ~7 y7 v7 \: D/ ~
/* initialize graphics and local variables */
}2 j+ T. I+ d7 linitgraph(&gdriver, &gmode, ""); . M3 U2 j" W3 T7 j; `" U5 m2 Z
/* read result of initialization */ 8 E0 V) M' _0 l0 B" c! t
errorcode = graphresult(); D. b9 Q) J' E1 M4 b8 t) L6 r2 W. o
if (errorcode != grOk) /* an error occurred */
5 o5 G3 k8 `" C1 x% G, ]3 n{
1 I- R0 R C9 ~. sprintf("Graphics error: %s\n", grapherrormsg(errorcode)); - q' Q& h! R( I @8 J$ J
printf("Press any key to halt:"); & o3 ^% v1 k8 }5 f6 i
getch(); 2 S- o: M* z$ P# x
exit(1); /* terminate with an error code */
) |1 z7 J2 \+ Y( R3 P}
: B' v+ E! m& Y2 w( |% S* I' fmidx = getmaxx() / 2;
{) h. P2 ]7 N; \; T$ Hmidy = getmaxy() / 2; ! a8 `2 h+ @. p4 o$ q$ H9 @
setcolor(getmaxcolor());
- q( W8 \' t) Z% T) e/* draw the circle */
/ J. p8 Y! l! I; [2 xcircle(midx, midy, radius); 7 m9 {* i: Z3 G
/* clean up */ - g2 f ^4 y& ?( A6 }
getch(); & l+ k5 l& W( Y& i7 U9 y
closegraph();
1 q$ i4 H# w" N" i* g& U6 F0 L; yreturn 0;
6 M% c' U8 B$ V: c0 H) V} % B$ T. ~! x1 x! J# m3 m' q( t
$ q- z- d7 y( S2 x# D1 j( c
' h' l& u) R; f8 c0 Z' }3 o, U
+ [0 g$ |( A# J函数名: cleardevice
/ H- o1 |3 D: g8 r% C0 B功 能: 清除图形屏幕
$ ]& ^1 M5 X, O) n# Q# d; D用 法: void far cleardevice(void); 4 Q, K- t7 V0 g/ \4 \. Y3 {
程序例:
" v3 j2 U A/ w" u/ F6 k- V#include 6 U1 _, V% i5 E+ ~) t
#include
]8 Y* K; w8 ^" X0 J% ^) Z8 L( }#include
9 l* y& y. ^4 }, O4 i2 w* W# V#include
6 v) H$ z3 C0 q* ^int main(void) 0 N; H& q! T% v, M8 F" s
{
' Y$ E( p/ G' z. \5 c" x* J% T0 }/* request auto detection */
( m4 k! M, g2 d# Uint gdriver = DETECT, gmode, errorcode;
" N0 U: _8 K1 ?int midx, midy;
) p1 c" ]2 Z! L/* initialize graphics and local variables */ 2 B* U8 f# F& \1 d9 o" a
initgraph(&gdriver, &gmode, "");
( \0 X3 _% H' q+ Z9 B/* read result of initialization */
+ L3 D& h* F# {5 C8 L! Kerrorcode = graphresult(); 1 q; w/ _* l4 R0 \6 {( B
if (errorcode != grOk) /* an error occurred */ , e/ k: B; P) r! e0 w
{
4 Z1 U6 O. ? _- S/ j5 ]2 ?6 ~) Hprintf("Graphics error: %s\n", grapherrormsg(errorcode)); 4 s8 q. E, j0 J5 A/ Q0 o) J" ^
printf("Press any key to halt:"); " F/ W' {4 h) R1 }4 Q. W4 i
getch();
; O2 ], ? p$ t8 o# C# S7 i6 X# vexit(1); /* terminate with an error code */ ( h" g* E& w9 V, y, z
}
& Y1 M4 J/ F' |) ~+ X4 q, ~; hmidx = getmaxx() / 2;
. G2 t, s& } U2 B5 O. }midy = getmaxy() / 2;
& `+ H! @$ G( K( Esetcolor(getmaxcolor()); 5 N" \4 _2 T7 x6 w6 E4 O
/* for centering screen messages */
0 X( c- a& r" u+ n9 Msettextjustify(CENTER_TEXT, CENTER_TEXT); 5 B& {! V2 Y6 @5 G; }6 [$ E
/* output a message to the screen */
1 v- O; l; \' Q) wouttextxy(midx, midy, "press any key to clear the screen:");
, w& A* r: l5 q- g/* wait for a key */
. R) T( M; \2 \2 g4 q& \9 q* z; agetch(); 2 [6 K0 v" X7 J5 g7 d
/* clear the screen */
7 C2 S7 o6 ]' |+ o+ m, u- ^cleardevice();
& W4 b8 {2 R/ `/ s/* output another message */
; J& ~5 b% J7 kouttextxy(midx, midy, "press any key to quit:");
1 d* G( E; `6 J1 u9 i/* clean up */
' e; |/ w; {. Vgetch(); % x8 p6 G3 a1 c6 A
closegraph();
5 }' U! S5 p6 J' T* I, @return 0;
7 Q" v# p+ S2 u7 ?" F+ y! t} # P) G+ ^; h' a0 _& l
4 h4 p+ N4 z# }1 Z
* |/ K1 G# W, y
Z, \. V& W: T% u9 b函数名: clearerr 1 ?$ c1 ?% m3 V$ j" F
功 能: 复位错误标志
$ h/ q2 [3 ^- m- m% O# w用 法:void clearerr(FILE *stream);
1 a9 t+ j+ u3 }程序例:
! o" }4 O9 I, U5 L$ c#include 8 g/ B( _0 g9 Y% i3 ^
int main(void) 3 G- v" z. s4 w' l7 B% B
{ ) ?9 |1 W2 Z' G2 T
FILE *fp; 1 k3 i9 F3 ~! H5 L; }! j4 n5 u8 k$ ]
char ch; 5 x% r* d# X; }2 \1 s; g
/* open a file for writing */
" n- y j5 k* S7 mfp = fopen("DUMMY.FIL", "w");
! M- \, q" N/ s: Q6 a; D) `/* force an error condition by attempting to read */ " K" z* n& W* X! T% w
ch = fgetc(fp); 4 g. y5 z" {8 {( s; t1 k- W
printf("%c\n",ch);
; ]6 r: m/ N$ ^2 h" G$ Eif (ferror(fp))
( t% o( U3 Q; |% z4 z8 ^{ % w$ H j; g. ?8 A1 S! y
/* display an error message */
: q& s. O7 i0 D4 H) F w6 z& yprintf("Error reading from DUMMY.FIL\n");
, L$ Q" B$ E4 e7 |; l/* reset the error and EOF indicators */ 8 a6 S! F1 E" w- I& s
clearerr(fp);
# W9 F1 V4 g# x1 C! ^. w} ! |/ B) P( o' w6 q2 j9 Y* ^1 |2 A
fclose(fp); / {1 M5 q0 w7 a. V3 _: p
return 0;
# o7 J0 s: c& r) \* q3 |) Q}
$ X+ [7 i% U6 \% V0 ]( s
1 t% M6 n q3 r! R
2 i/ {4 f$ a! K$ I( s. I
+ C* v# x" f/ U. H( ]0 i函数名: clearviewport
1 B( j" S+ t6 X1 G& _7 Q, a功 能: 清除图形视区
{& ?& A$ d( T0 V7 J用 法: void far clearviewport(void); 1 b X5 H, u! O8 |- F
程序例: 6 b, D7 R+ X: C
#include
. ^3 m5 m0 j: Z: i' e( ^3 c" K#include
! ~5 o8 d1 Y; s0 j0 |#include
- m( S/ y" I- ^ f1 T% O#include ; |$ K' z3 r: _6 V, T
#define CLIP_ON 1 /* activates clipping in viewport */
5 g$ X: _3 A: C7 v) z$ P7 {int main(void)
5 }; T6 R1 H, c# Z" s) Q4 V{
; _& ~& x) z1 f* y4 J/* request auto detection */ : a6 z: Q; @- x8 y" A2 ^
int gdriver = DETECT, gmode, errorcode;
2 ?! a8 S- u; d+ Yint ht; ! ~, l( [# o/ O! u
/* initialize graphics and local variables */ 2 \1 a# Q+ o9 b
initgraph(&gdriver, &gmode, "");
: P( \2 h5 [1 t/* read result of initialization */
( \% X! i4 |5 f. b$ }# Rerrorcode = graphresult(); I" U5 E9 X( _ A: S) \
if (errorcode != grOk) /* an error occurred */
8 u! ?. F. r! o, a( i: A{
j+ ]* ~: r8 t7 Tprintf("Graphics error: %s\n", grapherrormsg(errorcode)); ( |% @+ k$ E8 l6 {, [
printf("Press any key to halt:"); / [7 l+ G: d2 T R
getch(); 3 `# j( A) I8 D' R7 T5 |+ f# D: N
exit(1); /* terminate with an error code */ 4 p* @7 U+ H8 ^5 P9 F
}
7 r. K2 }- i6 Q- X Qsetcolor(getmaxcolor());
0 O7 {2 N7 t0 O4 [5 k3 `/ c4 G4 cht = textheight("W");
$ K& S" C/ o8 h+ h" x, G( V( A; ?/* message in default full-screen viewport */
/ _, K; M9 I Y$ x9 \" m2 A. k+ Couttextxy(0, 0, "* <-- (0, 0) in default viewport");
) ]6 Y+ G1 O. e0 f/* create a smaller viewport */ H) a( b1 H6 h4 r, A9 H4 ~
setviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON); 2 ~, M/ `. b6 H
/* display some messages */
$ S( l. X7 O$ T$ W" pouttextxy(0, 0, "* <-- (0, 0) in smaller viewport");
8 S- H% u1 `' nouttextxy(0, 2*ht, "Press any key to clear viewport:"); 0 q# i" X k/ F5 y; V% H
/* wait for a key */
; I: r8 d. D/ egetch();
! X8 G8 S- g0 M/* clear the viewport */
8 v' x2 r$ M9 O2 @2 h* I% |clearviewport(); , {7 `% d0 E+ Q0 k$ } G% M
/* output another message */
7 ]" d/ V; A: G0 Bouttextxy(0, 0, "Press any key to quit:"); 3 b5 A4 |1 X# |! h2 U( a, Y# ?; F/ B+ V
/* clean up */ 9 M5 l5 M% t% V) u7 `4 a7 s
getch(); ! x" E. e8 S- a z4 @. r, m
closegraph();
5 R3 ] J% o1 u& V7 s8 y! Yreturn 0;
+ z/ R/ M" |; L* F1 m, e u} 9 z) e) @! G, |
- i4 T/ t! S2 S+ k# G+ o1 ^. o
8 K0 a- s& l' b( e: T! C : I G" v8 {* f7 w0 m9 ?
函数名: _close, close
3 _- \3 u1 q7 R; _8 o6 A0 O1 a功 能: 关闭文件句柄
2 D/ g9 @9 ^' @+ Q% R9 ~: I用 法: int close(int handle); : w N2 \& U p0 U5 ~4 D
程序例:
Z! t& N. S8 C3 J#include
, I' x/ {( n W. W3 u1 `#include
" \* t0 V m, m: y+ t* K3 R#include 6 a5 Z; D3 B7 L- C# ^7 q& f
#include
( ~! C1 R/ r, A0 y. M0 b% }main()
) J* g$ L# h s8 T/ G1 |6 v{ 9 C0 ]. v8 V) G" D* ^: l
int handle;
$ T7 d. u, t) P' Q: A1 a9 ychar buf[11] = "0123456789";
9 h8 F# M5 d p+ Q/* create a file containing 10 bytes */
8 l- |6 _4 ~( \" {6 ahandle = open("NEW.FIL", O_CREAT);
) K& U4 w2 o1 {) K1 o n5 Bif (handle > -1) " f2 L; \7 W/ W
{ ' T& B; l% H4 M9 c% j
write(handle, buf, strlen(buf)); $ Z# E3 Y: y+ g% m' S+ G
/* close the file */
+ k" _: [0 K) H2 U) }8 |7 @close(handle); & [; x6 g$ b w
} ) @2 v* c% { R% }, ]$ Q& R
else 1 o' C2 i, a1 r/ h/ `
{ * I8 p& m' y8 [7 m/ {
printf("Error opening file\n");
. P- V! ?0 s1 s5 y9 S} 2 `3 ]* i2 V7 [6 J3 d; _& G# h
return 0; , q! F- j- B1 p6 Q& L
} , \! {) r5 v6 I& l% q. H& X
) L( B! _& i: ?7 }) I4 C. ^5 Z( m
) }# c6 Z7 F) i2 ?8 s% S+ u* ? * J3 O& i7 N, Q) h- J# Q2 o
函数名: clock
9 k6 _" T" r" s( z* Z0 J功 能: 确定处理器时间 5 q l" d5 V3 e, | d, p5 j
用 法: clock_t clock(void); ; \3 a4 Z4 G6 Q& W% I; i
程序例:
6 m" m8 {% ]4 R+ m# `#include
( { i6 ^0 ]6 l: g" O2 w#include ' o3 `* _6 Q! p2 X* M9 Y
#include
* p5 r! P1 g9 q: k; |# S7 Nint main(void)
( l2 d7 `9 k' A0 i' \$ W: h{
% {% Y: h" r. R+ Aclock_t start, end; / G: G$ s9 ?7 n: I4 @
start = clock();
6 t" K' P/ h4 f1 W5 h4 Gdelay(2000);
% `" q, s, s* T( Zend = clock(); c$ M% ^5 e% u0 D
printf("The time was: %f\n", (end - start) / CLK_TCK); 8 z5 O6 L& B. l" p D$ m0 p
return 0;
! N$ t" ~( L0 ~( P' O" h& b) D}
9 L9 m6 }% A$ |
* K: z. h- H8 O- o+ E: ]- h1 X3 P2 t. a- Z2 m/ a
# D/ `" l. q# L3 t; A! J函数名: closegraph
3 w5 ?" Z. K* S& f, a V- Q; G功 能: 关闭图形系统 ! M% U7 e; e5 x/ n
用 法: void far closegraph(void); . J: P- T% Z8 v; {
程序例:
" R/ f/ s V% a- a1 q#include
0 j0 M! C6 r; c' c2 Q9 b: P4 n2 V8 |/ d#include ! X/ E; D+ A/ j( u/ s! s+ _
#include 7 Z2 b" _1 ]* A1 e$ A- T
#include
5 ?$ A: [+ t. I% Q# [1 M9 b0 E2 b; _int main(void) 4 v9 x* o9 U9 H \+ Z+ R
{ 7 T+ j( `- I1 m3 D3 j
/* request auto detection */
: z9 C' h% X3 r# M% ?- Z! l& ^int gdriver = DETECT, gmode, errorcode; , E' @- y; b' G
int x, y;
; y5 ?1 ~3 p: w) r9 y/* initialize graphics mode */ / J2 q s5 ?8 s2 Y5 `% |
initgraph(&gdriver, &gmode, "");
, t$ R: b, _6 [6 V! ]/ L/* read result of initialization */ # S) [* F7 t |* \
errorcode = graphresult();
- k6 i0 S- {7 T5 F6 R; gif (errorcode != grOk) /* an error ; K3 v: S* I) r3 z6 A! X% R" o
occurred */
" x: q- x/ Z3 ?{ $ G( b! B# B! ~5 t4 m
printf("Graphics error: %s\n", grapherrormsg(errorcode));
: o* F+ v* `) _* K; Y R( M9 Eprintf("Press any key to halt:"); ' O i7 J" o7 |9 ]; o1 m3 }1 G" N
getch();
, H1 _. a: S9 ?2 r+ s* x, v& E: {exit(1); /* terminate with an error code */ 6 \ E; Z: O, t3 s. ]. D
} # g/ T6 K/ U; s7 J& g6 U6 S
x = getmaxx() / 2; 2 s! J* H( b) [
y = getmaxy() / 2; ' ?. }- V! A( t3 ^- E" H3 Q
/* output a message */
2 Z) u- p4 K8 u2 ^& H4 @# }settextjustify(CENTER_TEXT, CENTER_TEXT);
. a! e/ z- W: e6 ~( {) Bouttextxy(x, y, "Press a key to close the graphics system:");
/ S# D0 ?$ Y* p: h/* wait for a key */
0 h0 I; C( T4 \6 ~' Ygetch();
6 Z1 Q X" A" z) ?: r9 l9 R* W/* closes down the graphics system */ / Z! W) f! G9 {3 x/ K
closegraph(); 0 \8 }) {. k+ f' n$ z
printf("We're now back in text mode.\n"); , J' z# m( {. n% N$ f# p
printf("Press any key to halt:"); 6 c! y z, J: C4 D6 Y. [- z4 E
getch(); / d4 }+ l! r* y' C) E Q' y% Y
return 0; " J# E% f3 D l8 t; Y
}
; p- L" l/ f( R+ r6 \+ O0 @ B$ ~& \4 l/ q- Y
/ a* v( ?: \+ G- M4 ^7 C5 j( p
' o% e' P2 v& u# ^9 H+ b
函数名: clreol
; @8 i! q- ~. }功 能: 在文本窗口中清除字符到行末 % x) F) C' m! n+ @0 C
用 法: void clreol(void);
8 ]$ P/ x. P E; I/ e4 _1 A程序例: - |! L0 q3 p+ Q' v5 w
#include 5 l: u4 B8 c: O4 Y. q0 N( h6 h
int main(void) 2 B g5 {1 s) [! k3 Z& A
{ F7 v* g9 R$ I: P- V- T
clrscr(); 6 E) V, q, W8 B% m1 R* \' u* O8 @" E
cprintf("The function CLREOL clears all characters from the\r\n"); 3 T- W* r: n. Z4 c0 S, o
cprintf("cursor position to the end of the line within the\r\n");
/ B; S7 A/ D& h: z: ncprintf("current text window, without moving the cursor.\r\n");
$ |( D0 D& j1 t3 \9 q, W- \; H: ~/ Rcprintf("Press any key to continue . . ."); % z! n2 N' ^0 i- h5 w* E2 r0 k+ b
gotoxy(14, 4); 5 i9 A4 E. {- K* y. r# g
getch();
2 f! Y7 x6 b- }# P5 ^$ i9 b/ b& p/ dclreol(); 0 _1 ]" [+ T! @2 V8 n6 E; `+ D1 j
getch();
0 f) E/ x% ?( V( i* Breturn 0; 8 N! Q# R3 y: X4 D( h+ S+ U
} ! M5 W0 r( O; { @. K" X
8 X @$ l: t6 K" K6 h( Q& ^9 P
( b( v" R. `$ c/ y2 e$ A: W
1 o _( {* X( \函数名: clrscr * j. N# r& y6 U3 ?1 K
功 能: 清除文本模式窗口 ! _+ D$ e5 \3 u. w
用 法: void clrscr(void); 8 c6 a+ ~- U. ^- {
程序例:
7 b. @; p+ a5 i' ]+ z3 l#include : p; P; J# K/ p' w( Y4 W
int main(void) ; J1 a3 v! g" `: ~, O
{ & i4 M3 u- Q3 E$ _, a5 N
int i; 4 @5 u+ o% _- S R+ x, j
clrscr();
- x( a- z1 x% G, x, n/ jfor (i = 0; i < 20; i++) + \7 S. V/ J9 ?3 r$ Y
cprintf("%d\r\n", i);
& E3 M( m/ k2 e9 r9 U8 H1 L Rcprintf("\r\nPress any key to clear screen");
# ^* f t6 \$ u; Ggetch();
* T2 b- y2 e) X( i9 c+ y! Iclrscr();
8 d0 h/ ?5 ?* T4 @cprintf("The screen has been cleared!"); 9 y$ O4 w9 j) D Y' M9 V
getch();
/ W6 W/ z# a4 b9 n# m% ?3 s9 p4 ~2 dreturn 0;
0 ]5 l5 }: K) T$ E, U! C/ x# o}
; U* E( l9 |% ^4 M7 N4 W* }
2 b( `# S0 z; P7 t- H. C
* ?" ?: f- H! H7 [; c1 ?$ e% i 7 K5 C) n1 Y# k: O' j: x9 J
函数名: coreleft ! Q" x: w$ |- Y& g
功 能: 返回未使用内存的大小 ; o' Z" V9 V T @
用 法: unsigned coreleft(void);
) Y3 W* ]. ]% S! p9 d: k) h6 @: M$ a程序例:
, F' F; Y. S- ^" l# r#include . |% [ V% ^, _$ e2 }
#include
( }% q1 C0 G8 g9 S( u( B1 q$ Qint main(void)
% S; G; |8 l* `! e" \! i{ + J2 K4 Z5 Q1 L) ^- H' n1 E( H ^
printf("The difference between the highest allocated block and\n");
- f( [* w" ^/ n7 n9 bprintf("the top of the heap is: %lu bytes\n", (unsigned long) coreleft()); 1 g2 h; n, U: C
return 0;
/ p1 T7 W2 _/ C! N: q: X1 c, H}
& R/ }) E: ]3 w2 C
5 [# Q& o: e* P& }+ g4 X函数名: cos , `: m9 [0 m2 ~- D
功 能: 余弦函数
. p) G* t5 j1 E+ M' G* Q2 R6 j% z用 法: double cos(double x);
; T5 W) g: D% j F7 Y5 }程序例: ; U- E& u5 j: j, F# h' X$ Z2 B0 F
#include # B1 F' G0 n8 C( H
#include 8 A& |- y: `4 F
int main(void)
8 j( x$ U* A2 P% `- I9 W6 M{
8 L" m$ a% Q a) Z1 c6 u" i" fdouble result;
9 ~4 |7 A; [/ V5 kdouble x = 0.5; # ~; m; B# U0 Y* s
result = cos(x);
$ n+ l: f% m$ b7 A4 r4 {printf("The cosine of %lf is %lf\n", x, result); 3 s5 }% j$ G( e6 b
return 0;
2 H9 \% G5 q( C) n3 _}
9 D( K/ r( C+ [, R
, n& o# x1 O0 i2 G& E/ {( j# S3 S1 W ~4 i2 a! J
& Y6 Z3 Q: d9 W8 p9 h9 y函数名: cosh 9 {* F0 K X. s4 d9 z6 K: g# W
功 能: 双曲余弦函数 - p3 L/ D& K, D U3 S* H# Q
用 法: dluble cosh(double x);
: n* B$ G! D% s, R/ Q0 F3 x程序例: k' J# @2 K! C% c
#include ' ~5 }/ a; j1 l0 g6 \$ \
#include
% D0 r/ s2 \ S- Z, rint main(void) * J$ v* K% Y3 z# _6 l
{
/ `9 h4 N( V6 c' ]: P. l/ r: Vdouble result;
f v6 k! ~/ l, s! Y# a7 p, f* u7 J5 ldouble x = 0.5; % a% X1 U3 X0 \4 w
result = cosh(x);
2 _# F: \ D2 E8 u5 cprintf("The hyperboic cosine of %lf is %lf\n", x, result);
, Y' v; b7 B2 X1 Ereturn 0; r& _+ c7 P/ e+ e' ~6 }
} 7 v* {; t) B- X& q
+ O7 w& U$ ?2 m) |. ^; b* _) ?8 r" _: L! h& P
8 V: r' D9 N, P& O0 {1 a* @
函数名: country
- P& j- Q6 b2 U8 ]功 能: 返回与国家有关的信息 * P! r5 O" D; V- c
用 法: struct COUNTRY *country(int countrycode, struct country *country);
# R4 M3 V& H e% P& S( d' m程序例:
! f; L; _% `3 R# w/ a#include
# N/ p5 S, {, T. y4 j#include 4 Z& G& Q2 J0 W* ~( C) `
#define USA 0 ' X5 A; ~& x; N- i) j
int main(void)
9 c k+ {& S) x5 d/ j* }{
8 _) b( F$ {8 p: istruct COUNTRY country_info; $ @9 F: b8 V7 w6 |
country(USA, &country_info); 3 j3 H( \% `, ~# q0 m! l, v6 L
printf("The currency symbol for the USA is: %s\n",
: V2 A# Z1 A: b( p" r' |country_info.co_curr);
0 k5 `9 [0 t. J0 Vreturn 0;
6 v+ P& T& m- {; w$ i& c}
# g2 F: L# W$ x# b/ c6 O
- c, E- n) C4 S/ A+ r# Q# y
/ h3 i6 p' R& Z) F: Y : w# a3 G. h4 J$ p! k
函数名: cprintf 6 ^- Q( E: \" i% L- w# f+ _& q
功 能: 送格式化输出至屏幕 8 p& y: w0 S, z. A3 l+ o
用 法: int cprintf(const char *format[, argument, ...]); ) f3 C$ G( z3 G" {3 a: o4 Y+ o
程序例: : Q# o$ r. k2 u) r! V [. x
#include
! H$ W9 J: b: K2 F/ P4 i( H: wint main(void) 3 B% l* L: ~3 K- E
{
5 c# p/ U! z; j) a" x- G6 P& \! B/* clear the screen */
6 S% b+ w' ] bclrscr(); 3 o2 v0 c% y( C5 P, K
/* create a text window */
" {4 j, h0 ?7 c: Uwindow(10, 10, 80, 25);
Q# r# `- x3 r. O. D/* output some text in the window */
; ?* O, K/ y8 P; C- n$ I' g5 F% _6 [cprintf("Hello world\r\n"); 2 r" d- G6 r* r1 ` `) w' C6 q- c, L
/* wait for a key */
8 s, M8 B1 M/ m' ygetch();
! D4 c4 S5 F" W9 c% }9 u- creturn 0;
+ d3 F5 }+ H* B' q1 F9 }4 t; z} 5 O+ \" `( w- j' V& v7 E/ `
8 z' o- }$ v2 ~: i
) ~' v: P* d( l$ ~
, ` b5 t' M$ s( v函数名: cputs ( K1 L6 }! N0 @4 b/ [: [
功 能: 写字符到屏幕
) Y7 _$ n9 C& G$ A- A! V用 法: void cputs(const char *string);
0 O9 U, k$ @( i+ `% m/ C& y; L/ x程序例: 3 _* X2 t9 ~$ Y" Q1 i
#include
7 g/ l; @* B) i; y( _; p$ Lint main(void)
* O8 d2 d9 O6 x) h& S{ " k. e& A2 w0 G6 T$ u& Z( j
/* clear the screen */
$ M3 r- v; l( U Q) Fclrscr();
; j. M3 y1 }1 \/* create a text window */
6 O1 F% }9 [" C/ b/ ^ u! ^window(10, 10, 80, 25);
6 T8 K" q) j8 |/* output some text in the window */
* {" Z8 { S' K: f$ zcputs("This is within the window\r\n");
: S0 Z2 F5 [' {3 Q6 m/* wait for a key */ - s2 b: A0 e3 ? Y
getch(); % R2 n0 i2 }0 N2 N5 H6 M# ]$ Z
return 0;
/ J& `" Y+ }, w. W2 ~}
/ X- B) S* f6 s r; ^4 ?
/ G# d( g. I1 a* b* x# W
9 Q; |6 U, Q# F B1 c . a; I: c* F% R' n. U
函数名: _creat creat / L2 P i- d! Y7 J- \8 u
功 能: 创建一个新文件或重写一个已存在的文件
+ S' ~9 e! d, p: }2 x1 P6 l用 法: int creat (const char *filename, int permiss);
r: g% \- ?1 S2 w! K程序例: S3 {9 v* @& M/ K; l! l
#include 3 I- {+ A, d- N+ N4 { J
#include " s% t9 @9 H! f8 T& O: W; |
#include ! F5 O# E( G" K6 ~. `/ H) k0 O
#include # G2 M D) S$ D0 C1 k P
int main(void) ) T8 S! D9 p# |, B3 K$ W3 n
{
4 ~8 B- m) a7 e) z/ V% yint handle;
% ^4 D/ l, |# z3 C* ochar buf[11] = "0123456789"; ( Q) S$ }* v. Y3 N" y
/* change the default file mode from text to binary */ ?# \# h: g2 E
_fmode = O_BINARY; 2 U7 M8 ]& b6 k" E ]( |/ p9 G8 s/ X
/* create a binary file for reading and writing */ , N2 C- q2 l7 A+ Z$ x8 H. N6 ]
handle = creat("DUMMY.FIL", S_IREAD | S_IWRITE); 8 ?3 `& o1 \- U6 G: h1 e, u( v
/* write 10 bytes to the file */ & j& f# g7 T% L/ W& f
write(handle, buf, strlen(buf)); 2 j; r/ r$ y( b9 J# {) q& Z
/* close the file */
! |4 U7 ]8 j5 N3 Z( ]" n4 {! bclose(handle); ; a$ ]; g- U( o& o9 W
return 0;
# N. {! \: X7 g, M8 x- p} ' {, C, L& S. [/ K8 _
% C3 i4 q; Y4 P v
函数名: creatnew 4 I% }, L7 M4 y: Y4 Y4 F( t
功 能: 创建一个新文件 5 E% i# v+ n& j7 W
用 法: int creatnew(const char *filename, int attrib); . u% U# E% V& L7 ]5 H6 c
程序例: ! l, K4 [1 W5 C7 i! ]% l
#include
& d. l6 t: V9 B- D2 R5 l#include & p, x1 Y7 | ^6 x j0 m2 d
#include
8 \* d5 V( r. F: m. t6 m$ i6 T. a#include
3 Y' }& k9 U: g#include
. `5 Z5 V8 B" uint main(void) 9 f* I+ F7 o) q) Z- R
{
) a6 c. ?; x8 L6 nint handle; 6 j ?; b' K6 T! n f
char buf[11] = "0123456789"; 3 d2 Z8 s6 W0 r2 J! _
/* attempt to create a file that doesn't already exist */ 0 t& W5 S1 u, b
handle = creatnew("DUMMY.FIL", 0);
7 x9 F1 s4 X! `+ B; M5 v- }4 yif (handle == -1)
3 n2 F: s7 ]8 _6 K' x& `$ J# bprintf("DUMMY.FIL already exists.\n");
/ i! n+ `3 D6 P* } L: q) N" ?8 ?else : T, b. N$ e1 g
{
+ ^, q5 m5 L0 {( H& [printf("DUMMY.FIL successfully created.\n");
' J G+ C1 P! hwrite(handle, buf, strlen(buf));
P a* X9 I+ n; @8 Tclose(handle); 9 [- M5 ^. z: {; `4 h. O, n+ e. x
}
. K: M- D" P, Y9 d3 A' \" freturn 0;
- y; f3 d% f0 o' {3 G" T}
8 P6 u; t& y! H0 E- H
b7 f) Z6 D* ?$ g7 Q% G6 u, w. r3 ~+ s j; b! c
& S/ D( w1 z C4 K函数名: creattemp
7 @0 B5 n t. H功 能: 创建一个新文件或重写一个已存在的文件 6 d$ R1 \) z* V Z' T
用 法: int creattemp(const char *filename, int attrib); ( \8 P" P( ?. P5 Y$ R# x
程序例:
" T* T5 a/ x( n5 b. G, U4 a1 b2 k3 \* E#include ; ?$ M, |& H7 O) ^3 A) H( J
#include & R. [- K! f' N7 {/ H+ V
#include 5 t( R- Z0 D& [2 r% a
int main(void)
! L- E3 b( |6 h# M# M" Q( T{
u) s" m( R4 Eint handle;
5 L& L3 P0 s* b3 o5 f3 D- `char pathname[128];
e7 w9 F% o/ q7 G6 G& gstrcpy(pathname, "\\"); - o1 `5 \ ~+ U1 n; X- s* T
/* create a unique file in the root directory */
6 n& B+ z& h3 `# Q8 |2 i/ Qhandle = creattemp(pathname, 0);
; F8 [$ a. |/ u& o; w3 Mprintf("%s was the unique file created.\n", pathname);
8 l) j0 K6 _ Y1 Pclose(handle);
$ `, H( o% j7 I. Y3 m( w) Creturn 0;
7 I4 K2 ^! } X, f5 d5 |0 e} . H% ]& B0 o. z5 j. v
& ]: N9 v" t3 T6 s* }; v. q1 I9 G9 f! C: g
2 T/ V8 F/ f$ m1 M! p5 l函数名: cscanf
6 x G0 l; ?9 c功 能: 从控制台执行格式化输入
% v+ }6 y4 C# Z+ K7 f用 法: int cscanf(char *format[,argument, ...]); 2 ]3 A! F1 p: \- @
程序例:
7 l9 ?6 }6 N; v5 O& h; Y5 A: H; }' T#include 0 C! W* K$ n4 N8 D; k/ K- Q
int main(void) + ~$ r1 j, t9 y
{ 4 `4 d, |0 W. O( X+ Y( Y
char string[80]; ; ^( T$ B7 C( u1 {
/* clear the screen */
% a3 J+ Z3 e+ h+ S4 @1 k/ Vclrscr();
, w5 S$ _. W2 g) d/* Prompt the user for input */ ! r% v/ {* Y5 a# u
cprintf("Enter a string with no spaces:"); ' s( ^4 z2 i8 b
/* read the input */ & i0 S. A" l W/ w
cscanf("%s", string);
1 a3 S$ e3 B9 h6 A, v K/* display what was read */ . d" I8 R5 n6 M0 m& {* b) O# m1 i& Q% i
cprintf("\r\nThe string entered is: %s", string);
9 N, E% U+ J! O, b. W! freturn 0; 4 Q# [4 f* P- f' {9 v8 x
}
( o' Q: a1 h3 f8 k! P; j, E, D/ T# O/ x H) \* M( r
3 f- S9 d+ ?+ W3 n& w$ [; u
0 z i8 ~5 i4 h" n7 A! x! Y+ Y% q
函数名: ctime
# R5 B5 F& I" R功 能: 把日期和时间转换为字符串 7 {2 T- x; ?. j; N
用 法: char *ctime(const time_t *time); : F* l* h8 v6 J7 B9 y
程序例: 4 Q z) Y1 X, `3 i9 _2 H- n
#include
9 o; T: H9 u8 w, x+ M$ I/ ?#include
6 B- u- l, D' e2 r0 w5 ?. f2 j' nint main(void)
9 ]$ q) J& _) J7 T7 X8 W{
8 C% F( o) c& I4 e" Ktime_t t; , z% g' m* z/ [& I2 g& O: \
time(&t); T/ k# [! s R B) g2 i6 e( u! |
printf("Today's date and time: %s\n", ctime(&t));
2 c& {; ?6 H( ]( jreturn 0; 1 K1 b3 n- m& n' k& }! m. |
} % _' \/ Q! q* G5 b! f- z+ W
3 W/ ^% n# z9 m- }! r
+ e- ?2 K; E$ O
( X4 @( e% R3 S1 z6 N函数名: ctrlbrk & R* \8 C) j* \+ x n" e- b
功 能: 设置Ctrl-Break处理程序
6 H- ?6 i% j6 z) B用 法: void ctrlbrk(*fptr)(void);
" I0 n5 m- x: @5 p( g/ L6 s程序例:
" N m4 p$ U( @- S ]#include 0 G2 \- W3 r3 y$ v) X- b
#include
8 d- k9 O# b- S6 w1 s, J) B& }/ }$ r# ^#define ABORT 0
5 p' h$ }6 M% \9 X0 oint c_break(void)
e5 c$ M/ r# u0 x5 d{ : q; s4 }4 L/ U6 d
printf("Control-Break pressed. Program aborting ...\n");
9 ]" {/ U7 f% Oreturn (ABORT);
8 b8 f0 ^5 s& S4 D} # s8 S* J0 p- f) D$ }- r
int main(void) " P. _3 Y0 B1 b2 R- ?8 N; E$ a$ {
{
/ U P( m6 u+ M/ k$ v* }# Ictrlbrk(c_break);
& h; E/ ?: D, W# X5 Tfor(;;) + F1 z5 T8 f; K) w
{ / N+ ]3 g0 ]4 k6 x
printf("Looping... Press to quit:\n"); 2 s9 S3 t7 J6 F
}
4 y. l6 m; v2 Nreturn 0; 5 ~- [: |) k& H2 L. t
} |