|
函数大全(c开头)
, W" B& m/ T" I& u7 Y0 ?, T# I ) p {2 S- L4 b: v6 E. r
函数名: cabs 5 J5 @4 B V+ J8 A2 W
功 能: 计算复数的绝对值 " s/ ?+ k+ J- w( u8 P0 r
用 法: double cabs(struct complex z);
" v6 b& c1 ?# I+ u2 u程序例: . @* ~- s% F' ]+ X! ^5 s9 u
#include
& Y7 S: M7 M* q( k#include ' K/ [* J' W6 F( B
int main(void)
# w: q0 K! [. T& {{
% e/ A4 f- u1 B, U% d4 e1 h, ~( nstruct complex z;
& U& o7 }$ @4 k3 gdouble val;
( D6 |: i O3 sz.x = 2.0;
4 ?( p8 a. q0 i% A6 ?z.y = 1.0;
, e9 t; Q# s( s. N" t Bval = cabs(z);
) [( K* T3 z4 T0 o# _ T( O( @% N& Sprintf("The absolute value of %.2lfi %.2lfj is %.2lf", z.x, z.y, val); 5 q) i; z# V, }. w
return 0; 6 ^% \# O% S) b% Q; R' _
}
( B; `! G8 l7 v. _
1 N1 m7 Z0 W4 g) w
2 H5 M4 k( J2 Q7 I0 T( t3 n! m
* L: y2 f. t0 r/ d4 E函数名: calloc
% x9 O( o* p1 M3 H/ b* D; Y9 E- {% Q功 能: 分配主存储器
}& j, U# p4 D% j8 n5 ?" @用 法: void *calloc(size_t nelem, size_t elsize);
+ W6 H7 l# t: h# i ~ k/ ]) Z程序例: / F: j( Q. J5 [1 N3 ^4 q2 P- k! V
#include ( G3 U) x. t8 d# Z x
#include
: N8 U) {- H* [# Wint main(void) 4 H' p) ~5 b; w6 z5 }
{ 9 V1 K0 b R/ g: A# {& U r$ z* a$ \
char *str = NULL;
' Y* s1 W. Z& G' M6 N/* allocate memory for string */
/ c- y! v! B7 Rstr = calloc(10, sizeof(char)); ' g; ]) A) ~4 h1 ]0 M# s
/* copy "Hello" into string */ $ t$ `8 O6 E2 s$ ]+ m5 D% h* {
strcpy(str, "Hello"); + p6 r% P! b6 Z3 j: D
/* display string */ 5 J: g0 m- O$ u" D2 [* _! Y5 {& P# R Y
printf("String is %s\n", str); & V$ y2 @; y6 c( J; s* v8 |: E! O
/* free memory */
) V; e. D6 J' {$ s' I% D+ j7 Kfree(str);
7 w/ D6 S& \0 ~2 t/ r3 E# freturn 0; 9 r1 B. Q: [9 P9 }& C, H
} & A$ w+ f, e; i( }0 E
: Q, l5 u3 S/ t" R
' O: F" B% Q4 V2 S/ A
8 ^$ ?% c) J4 Q函数名: ceil
% q. W1 @0 S) g$ x T" v功 能: 向上舍入
' I( U I4 h |. o. ], ~用 法: double ceil(double x);
5 y3 v6 h: Q1 D6 T程序例:
3 h6 u8 p* S3 K8 C#include
$ ^8 [* Y; ?8 z0 R1 ^. l#include 5 H# T) o7 o H
int main(void)
: _; H2 a1 D" j{
! l0 c' N" w$ f& m) j& A0 Kdouble number = 123.54;
2 U3 D* M' e( c# edouble down, up;
0 b2 V0 V3 h/ ~6 C) R- d. [ l- |down = floor(number);
! r7 p% o* Y9 A; i6 `& i( R/ Wup = ceil(number);
; G1 P( H4 i) M& @: i8 cprintf("original number %5.2lf\n", number); ' S |2 {' H1 r* s, P
printf("number rounded down %5.2lf\n", down); ; o7 ]! r6 m, N5 K% f
printf("number rounded up %5.2lf\n", up); 0 A7 {) ^2 s# \5 g* I" b
return 0;
; P7 T, V4 V! f5 z3 B}
- _' ]) w- d w" K* v+ v/ J' b
: w3 e+ Y2 i! k' R# T \
2 h& W. Z) h$ Z' i7 |9 { ; a( p: t8 S; J1 ?7 v5 N/ W
函数名: cgets % }% E8 e& D' B* J1 L
功 能: 从控制台读字符串 ) ~! u0 D$ |+ c& r0 }
用 法: char *cgets(char *str); % z/ L ^. x4 v/ Y9 E0 g" i; \9 {5 q. R
程序例:
# ~: \: v: |% j5 P% I1 @7 s* h. p; }#include ! M4 z. ^: u. K, E
#include ! v- h/ J8 P- F) d) a& z, m3 U
int main(void)
6 U7 J8 O6 M* A- \{ 1 H2 e+ S3 m. T! U7 ^3 o
char buffer[83];
/ F6 o4 |# |, ]5 s( i" C1 ochar *p; : @9 o% {6 d# I- r& b
/* There's space for 80 characters plus the NULL terminator */
6 q- Z3 {3 J8 N2 |buffer[0] = 81;
( [; X- \8 m& G( b; e9 mprintf("Input some chars:");
7 s& m9 D- y) e9 cp = cgets(buffer); 5 k1 Y1 ~! `+ ~
printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p); 9 E; {, n7 o ?. O8 X
printf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer);
3 M2 v+ s- a s0 g3 f/* Leave room for 5 characters plus the NULL terminator */
0 F( B% c; J7 T. Vbuffer[0] = 6;
2 I. N5 [: e6 l5 Uprintf("Input some chars:");
. F. d2 F! {! s5 A' f0 ?0 X1 ^+ [2 ?7 up = cgets(buffer);
, M+ s* |5 N& ?! M; {( yprintf("\ncgets read %d characters: \"%s\"\n", buffer[1], p);
0 t3 y& z5 Y: V/ U1 k% U! Eprintf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer); 3 f$ B! c8 O m, H
return 0; ) b" P3 u2 i" a( \: q
} : z3 N% V0 a4 j( h- q/ p5 C
. J2 A7 S- w: o* S2 u d& V3 m, z% t3 s# T9 p# S3 Q$ n
2 l( ~( W7 k5 q函数名: chdir ) u- K0 L& x/ w7 j3 N
功 能: 改变工作目录 # [- w: P; b! L) F( C/ k! G
用 法: int chdir(const char *path);
! R9 B* I& J2 B5 m程序例:
- g g Y( g$ s c#include
5 t3 H w! u. p8 d4 ~& Z#include . T Q0 i/ R; i- p% ^
#include
' [1 _7 @ G0 O. [char old_dir[MAXDIR]; % w1 w+ a9 X" B: ~$ W% U
char new_dir[MAXDIR]; ! |# ?! J- E# `, U
int main(void)
5 r$ a! l5 _3 @: z5 J" G' p: c{ 3 A8 y% C$ F- _
if (getcurdir(0, old_dir))
3 _$ L* n `5 G8 _& S{ " e( H) C& M3 K9 S1 J& _
perror("getcurdir()");
5 |- `4 Z! j. d' W5 r9 I! W* Pexit(1); - b6 y8 H$ S' r
} " l" M. Y+ G7 B1 a
printf("Current directory is: \\%s\n", old_dir);
& D6 ^8 b, [ h' W# F# gif (chdir("\\")) 2 {' c+ B4 L, R4 R; a2 M# r+ v5 m
{ $ {/ `* G) Y2 ]& r+ i5 q
perror("chdir()"); % v6 A, {: `# w2 \
exit(1);
# b+ k$ y8 ?- j1 v% u} + v& c( _% k7 r" k' ]1 c4 g, \1 c
if (getcurdir(0, new_dir)) 3 N g- s2 `3 |% F( Y
{ ( {. x {. X6 H1 H
perror("getcurdir()");
7 ]6 d% p9 u9 D+ u& H4 Jexit(1);
& e& `6 P1 w" d6 _+ h `} 4 ?- |5 U. m$ q
printf("Current directory is now: \\%s\n", new_dir);
A o/ a& Z L6 ?5 {% O& _printf("\nChanging back to orignal directory: \\%s\n", old_dir); , E5 x( D o/ [/ D, \
if (chdir(old_dir)) 0 w6 S7 I+ z: J$ Z2 K3 m* a
{ ; E4 B, ^6 M2 G, s" A2 Q, `
perror("chdir()"); / q4 r2 n: K: x" a
exit(1); / X8 |8 k2 X9 D! Y1 |7 Q
} ) S T/ @: E5 P7 Q
return 0;
4 D" Z7 E1 R+ p; B9 Y e8 J} . F; E: ^6 V* Q+ a& u4 B5 L
9 J- A7 |. f. Z# s; J
- @% T+ c3 ^; X8 l函数名: _chmod, chmod
4 R! v& i% {6 ~ J( _3 V. A功 能: 改变文件的访问方式 / c3 n& `1 s$ t% f- Y
用 法: int chmod(const char *filename, int permiss); ) ^% x3 G; q7 {- e7 }& a
程序例:
+ J! @, }# Y0 {% H#include
0 F, }- B" U) B y: ^#include
4 y7 P; U& m0 r: v- I+ f0 ~#include
# ~8 S* K- ^6 {7 u. Wvoid make_read_only(char *filename); % W4 w5 {' `$ d4 B
int main(void) $ B& o' ?2 Q) {. d+ X
{
7 c9 }4 C+ e, _' [( `make_read_only("NOTEXIST.FIL");
$ |3 i% ]' G- V+ Wmake_read_only("MYFILE.FIL"); / R& d/ N" ?3 a7 |8 |
return 0;
) X m" _2 }& ]4 F, q0 d. v} ( p3 k5 P" F( a! E1 j7 [
void make_read_only(char *filename)
8 ]9 r `1 H4 M' `( ~* b* m{ ; ?+ B( r( [0 n( Q0 F' I/ g! I
int stat; 6 U. i# O8 Z" ~; j+ d% M
stat = chmod(filename, S_IREAD);
1 w* S3 j' X7 W0 ^9 v$ s# y/ w g" V! xif (stat) & ^; p/ U, \/ N, y
printf("Couldn't make %s read-only\n", filename); : L+ r% C# ~% k2 x
else
$ K/ O: p/ C Tprintf("Made %s read-only\n", filename); & L: s" i3 m) B+ z
} & ~& Z- Y) J* \# i" ]! P7 i
6 t. H7 H2 f9 k# k1 g8 w% Y9 u
2 j% K4 L3 L7 F6 L) {. T0 B
3 g3 d7 g/ f9 V: o5 W$ f- K0 x函数名: chsize 9 k1 t5 t' n, O4 z9 I2 ~9 D
功 能: 改变文件大小 / f! i% v8 g3 b' a
用 法: int chsize(int handle, long size);
. U# S" k4 ]5 j程序例:
) o* d2 W6 } O3 y#include
' o4 c: i% l+ ]#include
. {+ ]4 O% O' a1 G6 } Z2 Y#include ) L6 ?- h4 |. P) O: I
int main(void) ( |8 E0 ^# W0 J0 ]1 P% D
{
; j* P$ s s U% D: u- M* D* H: xint handle;
/ y- \2 r7 O/ `: ^char buf[11] = "0123456789"; % w" c& q/ U$ w+ w/ v" r+ o, |
/* create text file containing 10 bytes */
) S3 ?, t8 Z; m3 H1 @* K# j$ }handle = open("DUMMY.FIL", O_CREAT);
6 M, w! I9 \$ A! {+ L' K. bwrite(handle, buf, strlen(buf));
: v# }* [/ Z$ n% ~3 \/* truncate the file to 5 bytes in size */
* E" a3 o5 x" v) k2 m0 Echsize(handle, 5);
" c5 L! T0 I% e$ n$ d! F/* close the file */
7 r8 l. g1 E5 xclose(handle); - P# O. f# R: w; ~
return 0;
$ ~; [$ y* y8 D( N; d} % I6 `2 B4 B& o8 N/ b! d
. Y* Y! Z& `6 X+ T4 L
) @: K8 j; P; f) b% C2 ~4 i
函数名: circle : k8 k7 H+ W: E4 ?4 P3 U7 P
功 能: 在给定半径以(x, y)为圆心画圆 4 Z$ A( G1 q& l; }7 x B
用 法: void far circle(int x, int y, int radius); 4 A! ~! @8 [- T$ y- e
程序例:
/ Z# m! O, ~* N# k$ U#include
: z+ T9 h, A: W# E2 Z2 _' y. D$ F9 w#include
- q5 {0 G4 Y5 w) I) {; T3 ]#include
7 Y2 F1 N4 I8 j+ A' s#include - W9 v+ r' G. b
int main(void) 2 x( y# E! M. f+ G! A* v
{
1 z2 X8 U5 ~3 }% M" K4 }; Z/* request auto detection */
# v0 I1 A) Q% y; Zint gdriver = DETECT, gmode, errorcode;
6 q \ l3 ~. Q3 g ]1 Dint midx, midy; & P$ b( _* C- z
int radius = 100; 9 P8 e/ {- w5 J( v6 D
/* initialize graphics and local variables */
* b1 L& j. ^! D- \% }9 Uinitgraph(&gdriver, &gmode, ""); , C k6 X& V* P% f1 N
/* read result of initialization */
& R& m2 ]5 m) \2 A2 @- i W' gerrorcode = graphresult(); ( T" a4 [! y2 H7 u O4 Y
if (errorcode != grOk) /* an error occurred */
( q2 U t* ~: k( o" b{ # \4 O9 W& u3 T6 q' R! F2 ?
printf("Graphics error: %s\n", grapherrormsg(errorcode)); 1 X7 e" W/ d, H1 Q+ f2 I
printf("Press any key to halt:");
# }/ v' A3 @* s0 H: i: x: ygetch();
' ] }* s8 z zexit(1); /* terminate with an error code */
& y+ F4 Y0 w7 y4 x* C" K" N0 j}
, f2 m/ a( V. {, ?& X: `' R9 Amidx = getmaxx() / 2; ( v# A3 S/ N9 w- W
midy = getmaxy() / 2; $ J$ }# h: x, ^: p) r* f# |
setcolor(getmaxcolor());
5 S* Y8 | F* I' y- h9 b' B$ I/* draw the circle */ ' ~) Q! D9 A( S+ u5 h ]. B
circle(midx, midy, radius); % {: L! R5 |. K4 S* I n. N
/* clean up */ 1 O) }& }8 N6 }% z* q3 `. o
getch(); . |2 j1 ]# o2 [
closegraph(); 9 A5 N8 _) q9 u6 O0 }0 N
return 0;
|% ?$ e# a/ W3 {8 C; j8 h}
' N* b) L$ ~: \! ]; v! O* }' U) b, p" H2 ?6 O
' f0 H' z0 D) H- f3 F1 S) Z" t
5 {8 O% q5 h7 W( {1 s/ m" T函数名: cleardevice
) F" j) f6 ]7 k C功 能: 清除图形屏幕
- \$ a, Z( z# s" y8 q用 法: void far cleardevice(void); $ r. D# n6 W. |
程序例:
# F: `! A2 ]6 l& Z/ ^#include ' B) u; r1 Q! d3 ]4 b8 b7 S, `7 R
#include
1 I# Y3 J. s! X" q( @* w9 E#include " H4 c0 w% Q/ s
#include 0 |+ q. Z: D1 w$ W' \
int main(void)
: w- g7 y2 j m$ `4 S{ 7 [+ a/ _: |+ U% x0 d* \. o
/* request auto detection */
" E; m& J) ?0 N. A6 Y9 a. nint gdriver = DETECT, gmode, errorcode;
& s. E! k1 u G0 [int midx, midy;
6 c+ e# D8 h0 h$ `8 ?/* initialize graphics and local variables */ ( H5 q' L& ] V
initgraph(&gdriver, &gmode, ""); $ K: \/ ^# v$ D
/* read result of initialization */
/ \9 ~1 T- L# {errorcode = graphresult();
7 Q9 Z1 c- }/ R, e Y0 U3 Fif (errorcode != grOk) /* an error occurred */ / r, s% s, i S* z+ ]- K
{
& e$ f7 x Z! x% ]& j2 Dprintf("Graphics error: %s\n", grapherrormsg(errorcode)); ; u9 Z; |& e6 \9 I& J- i& G
printf("Press any key to halt:"); . i/ ~$ t1 i* g7 W
getch();
! O5 ^/ n5 | r9 n' D, s, {% @exit(1); /* terminate with an error code */ c5 R( ]# {1 d0 `
} - ?, c* c6 T: E8 e( |, \2 k. H
midx = getmaxx() / 2; * ^. M4 C+ |& d4 z5 \+ W
midy = getmaxy() / 2;
$ R9 O; L$ Z5 O2 ~) g" X9 l* }setcolor(getmaxcolor()); ! ~* v7 ]% B" ^( ?# D
/* for centering screen messages */
: L8 L" n" A" [; Osettextjustify(CENTER_TEXT, CENTER_TEXT); & d8 R. D3 E9 \& U8 W
/* output a message to the screen */
6 t5 Y% U+ b# m: v6 I& \outtextxy(midx, midy, "press any key to clear the screen:");
' V# _* \( N- a. n5 n. @/* wait for a key */
+ A5 {! B5 j& ~ O! ?getch();
; Z$ c' d# f5 R( a; k( @! u/* clear the screen */
: P6 S* l$ }. r0 w+ s, P4 p: @' rcleardevice();
0 B! y, K) |/ D7 X' ]; V4 T/* output another message */ ) S0 ~, d* U' C: Z) t0 r4 k
outtextxy(midx, midy, "press any key to quit:"); ' [) x3 |+ }- S& U
/* clean up */
6 e8 t' P" A- q* Wgetch(); . M4 k0 Z* i3 C
closegraph(); 7 H) S* p0 J- k `6 `
return 0;
@. o3 d3 O( I0 u0 j# j} , h5 V7 g! o+ [- Z) o
6 G! [* A5 l( [* |1 w1 C
; {+ f7 J9 v( y- o4 I
* M. i, }! X9 A8 U2 \/ }函数名: clearerr
) W' F6 [9 K) Z3 o8 ~功 能: 复位错误标志
+ x: u2 f- o6 I用 法:void clearerr(FILE *stream);
: `; H1 Z& }; R0 ~4 ]程序例:
& G' [$ s A; Z7 C#include
6 s, h( V0 h& s* Y- w% v% ]3 tint main(void) 5 P% R/ k9 p: W* H, z
{ V. P( p/ h) @' I. s
FILE *fp;
" q/ u' b u4 s4 B9 \char ch;
' Q& f% H8 b1 S0 L# I8 o% |/* open a file for writing */ " L0 @' V& z+ P2 Z
fp = fopen("DUMMY.FIL", "w");
1 W) Y& I5 I3 b$ E" A0 j) P. a/* force an error condition by attempting to read */
. U4 P0 a* o' ]$ N2 m: R' `ch = fgetc(fp);
) \6 M4 F8 K; q( p; N9 j0 ~: w9 _printf("%c\n",ch);
, p% B3 e) T2 f- r6 z0 Yif (ferror(fp))
w+ R6 ]! I- a; o' p6 M8 u{ & l" [" ^% d6 D8 r" ~3 F, [: |9 p* G) d8 f
/* display an error message */
: q% p1 ]7 f; I8 q) t7 f4 `, wprintf("Error reading from DUMMY.FIL\n");
- `: [" L5 s Z. k/* reset the error and EOF indicators */ $ D# n& [; S- E( W* l
clearerr(fp);
$ F/ Z$ E' _ O% K1 q}
, M! M9 f9 ~1 w, ^) @fclose(fp);
1 J9 i/ }' {+ Q5 |! Treturn 0;
3 b) k. f9 ?5 M" U+ X. w} 4 H! \& I# T. n$ G% b3 M
% W# l6 o: ?4 Q( c4 ~; ]
; F8 f9 ~, S' V
7 r# p# L) @6 W5 j/ P函数名: clearviewport & u' `, m3 g2 V( Q( z
功 能: 清除图形视区
* p5 [9 L9 K' d& D用 法: void far clearviewport(void);
% n) |4 }9 [4 M5 W$ q! H程序例:
; v n7 P: b8 w#include 2 i/ R0 j5 S/ z7 H+ d% p5 H o
#include
0 _) d. |9 H8 G0 x$ b w% P8 x3 y9 u5 D#include 7 Z; _3 k, s) h3 X2 c: N( I
#include / t3 C; e7 M4 O7 H/ p
#define CLIP_ON 1 /* activates clipping in viewport */ ; D/ ]8 l A6 G
int main(void) 0 }5 \6 Q! P5 W3 A7 A
{
6 p% M8 D4 X' T, ~/* request auto detection */
& N8 D) L. r3 |1 jint gdriver = DETECT, gmode, errorcode; - ^* z/ ]- n7 {6 L" ^
int ht; ' s3 E3 Z: g/ `+ }
/* initialize graphics and local variables */ , c) o2 T5 x$ N
initgraph(&gdriver, &gmode, "");
% b! r- @' ~2 Z3 h- B) ~1 o/* read result of initialization */
; G( @7 S: Q5 T3 W( Q2 l, ]+ ^& Jerrorcode = graphresult(); . p% P9 X; @( M, ]! @
if (errorcode != grOk) /* an error occurred */ 5 [& `' E- G& |; q
{
% O; G/ D1 d0 F. rprintf("Graphics error: %s\n", grapherrormsg(errorcode));
3 f- F8 J: v% F4 I, zprintf("Press any key to halt:"); % v* S' ?+ a' P
getch(); ' S( _, M( A# r1 X
exit(1); /* terminate with an error code */
3 _# A) B, B; I# |# a# N) \} 5 w5 ~: V% d# f6 P! K
setcolor(getmaxcolor()); 6 V5 ~+ u7 f. m7 A# N2 n4 S
ht = textheight("W"); 6 Q0 x# I" R; J5 {) _0 E
/* message in default full-screen viewport */
* K) h T# e, d6 Douttextxy(0, 0, "* <-- (0, 0) in default viewport"); ( [$ K' p6 r* E8 x: f: }
/* create a smaller viewport */
# d. X5 G' t1 T c9 P& ~* ~, Msetviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON); 8 g0 J0 l) \) q
/* display some messages */
. a) V. f7 q# X% i* C1 M* Souttextxy(0, 0, "* <-- (0, 0) in smaller viewport"); ' \- K" d; l, F0 ^+ v
outtextxy(0, 2*ht, "Press any key to clear viewport:");
3 Z& w3 ]% g( ^9 l- ?+ Z. z/* wait for a key */ 6 C, R9 c, Y5 Z0 h* [1 v
getch();
9 R! G; {+ g8 S5 J6 r/* clear the viewport */
8 Z( H% ]4 k% [8 x H! K. r8 pclearviewport(); 9 j8 V' v1 Z3 a" A% c' N
/* output another message */ * E8 A# R k! p) s2 e
outtextxy(0, 0, "Press any key to quit:");
r) k( o2 @; z2 _6 P4 n5 ]/* clean up */
* E2 ]' q; y5 ~$ ^' x. [ M) bgetch(); 7 D4 f! P1 O8 h- w8 _
closegraph(); ' R( W8 e, j1 o; T* K9 Z
return 0; 1 T5 ], K2 F2 d1 P# m/ J6 |. _. A
} 8 j8 @. ^/ V: }9 }! A" I1 `
8 f2 L2 Z) J! u0 }) ~9 b2 @
, w, e' n; i! U1 s7 D: X9 U i
8 b& W, o1 a; o. O4 D# J# ]函数名: _close, close - Q' _' z+ v3 Z8 q: F, x
功 能: 关闭文件句柄
# n) i2 ?% ]: O$ l, R" t用 法: int close(int handle);
/ b" F( z; X2 z- E6 L程序例:
4 [2 v& W. `0 D, B#include 0 S3 w; ]; g7 J5 U# m
#include ! c: H; K1 L D. T! d
#include " V' c' I8 j( h
#include
6 `% S9 \! ^: Mmain() " i+ T3 C- w3 D( a+ K. Q2 H" G
{ : b# e! s {% C
int handle;
+ I. p% y" K9 z; xchar buf[11] = "0123456789";
. h6 n& d; ^* E* v/* create a file containing 10 bytes */
& a0 r+ _8 F- p7 _! Ohandle = open("NEW.FIL", O_CREAT); 2 b1 T7 R3 ^( |9 L7 S
if (handle > -1) * ]6 F3 u& t& q7 L: B
{ 0 A% L- `- F6 R1 y* z
write(handle, buf, strlen(buf)); 9 F% \; ` | c9 l5 Q5 |
/* close the file */
" l. ~& _9 j$ |: Y& v4 rclose(handle);
/ k+ d' h; e5 R" Z}
% J0 h( Y: B* r% }else ( e7 K% [2 s1 X3 l0 s& M
{
9 \% V1 l' j1 ~# ]; n; yprintf("Error opening file\n");
5 x4 F$ l; D: ]+ q) Y. O}
" v) y/ N0 v { I kreturn 0;
/ W& a" x& ~/ R6 U7 E& S9 v# e& C! c}
0 p+ y/ h, C8 O& o. v1 A' Y4 Z% N& m/ T
4 o$ w# h0 @& y& s% P m
) u5 P2 n8 q' w. h5 T* F函数名: clock
) ]9 @' n1 u- u0 t) y4 ^功 能: 确定处理器时间
- H# h+ @) I' {! t1 q- h用 法: clock_t clock(void); ' \) S" J7 ?) c2 r% r
程序例: + r8 ]) A* S5 B1 N, q: t6 D3 J
#include
# q# Y+ J. l: ?% \; y3 ?#include
/ f7 j2 C) \+ ~2 z#include + {4 A7 f; P* \% ~9 Z
int main(void) ' V& h7 N. G4 M$ J4 M7 u
{ 8 r9 Q. ^! H: X. x, t
clock_t start, end; 4 i" w( _* Q7 l+ z- t
start = clock();
3 S! f5 x: ~: ^' D& G0 z5 cdelay(2000); 9 Q/ T1 d% ~( |% x3 }* G4 _0 M2 H# |
end = clock();
) _$ U+ m2 x$ cprintf("The time was: %f\n", (end - start) / CLK_TCK);
" v _( E. c4 }# E5 mreturn 0;
& a( z. L6 }9 s1 g! O9 E6 |}
( i/ v8 p. y9 O
; y5 n% @# L" Q3 Q6 p( k# E& Q4 }4 K, l! r8 ]4 f
; g/ `5 g: j3 T7 ]函数名: closegraph , l; H: ?6 W7 F2 Z" ^' }' p' a
功 能: 关闭图形系统 . R* z9 }7 P' P2 W u
用 法: void far closegraph(void);
K! [8 y0 Y8 y. O3 t- S0 M' F/ E程序例:
* \' X6 V& D% u#include
% C8 `# \. z" ~' @% j1 `#include
; z6 x; q% i# A2 ]3 i( Q#include 6 z' c. r( @4 } @3 {
#include
" g+ c4 g/ c% K5 yint main(void)
8 M* _5 m2 k. k{
. _, o" h! F/ H) e6 o) _/* request auto detection */ ( Y$ a: F4 `. o; D6 j* `" C3 G
int gdriver = DETECT, gmode, errorcode;
" J, N7 @: R9 m7 e3 r5 }: T8 b; @int x, y;
( p' c! M% |! Y3 ]2 j0 J/* initialize graphics mode */ 0 t$ w7 ]* {1 Y6 t1 ]% X4 a& \
initgraph(&gdriver, &gmode, ""); - y% o! {& N% H6 [/ L- s$ l
/* read result of initialization */
! i9 V2 z) k% z0 }: D* Q U/ ]/ }errorcode = graphresult(); 9 S9 O6 k! b- ^6 C
if (errorcode != grOk) /* an error
+ B( G8 d. p( w. ]occurred */ : u5 g7 J& }) L" n9 Z& w
{ 9 e, X1 s3 O: t+ E8 m2 n
printf("Graphics error: %s\n", grapherrormsg(errorcode));
( h/ c# J) v4 g3 [$ ` [ Rprintf("Press any key to halt:"); 0 u% ]" W2 k, O
getch();
1 _% \5 H1 R; c: w& R, i3 `. oexit(1); /* terminate with an error code */ + ?! [$ b) I6 y0 t# G' a
} 5 \! G9 ~; r1 q* U- U+ _
x = getmaxx() / 2;
7 ?8 L4 O) f2 b* G1 Xy = getmaxy() / 2; 7 T$ P5 b- k5 Z% V% a# o3 c% Q
/* output a message */
# d m6 S% a3 _. ?settextjustify(CENTER_TEXT, CENTER_TEXT); 2 n6 p) S1 D9 K( ^5 u8 \" T
outtextxy(x, y, "Press a key to close the graphics system:");
1 x( v4 F% Q' E# U. A; I/* wait for a key */
* h" y, _+ ~1 B+ }getch(); 8 j0 T: y: f3 o8 y% z" H
/* closes down the graphics system */
7 ]8 g" O2 b; R/ ?" `' \# g4 Jclosegraph();
4 x8 U1 j+ _8 O. P Sprintf("We're now back in text mode.\n"); ( }" G) X7 P T) s9 w. j
printf("Press any key to halt:");
* c4 r+ d9 Q% {3 p: n0 d2 p6 \) kgetch();
* L; V! a7 I! D1 J) Greturn 0;
/ r0 h0 b2 |8 |+ C}
; w% w* a: j% E7 B" o2 o8 g3 \
J3 B" y( p$ ~- E1 G0 n" Y1 [8 |- _# @& A( |) l3 r' S Q
: n1 Q0 E& p/ L) b. t5 s3 d1 w
函数名: clreol 8 D3 |% L/ p- H G
功 能: 在文本窗口中清除字符到行末 0 I" c; R0 r$ Q) t7 s6 T) k. Q/ a* }
用 法: void clreol(void);
* k0 L7 k3 P9 G6 `% D0 X6 g: Q程序例:
+ ?& U4 T9 k) q3 I#include
& E) A/ e5 }0 jint main(void) 7 l o8 e5 i% v
{ 4 U4 O$ ]7 P9 V c4 O& k8 X ^
clrscr(); & x. _# m4 X. f5 o$ w, {8 z- t
cprintf("The function CLREOL clears all characters from the\r\n");
) L2 L, ~, A4 V F6 wcprintf("cursor position to the end of the line within the\r\n");
5 M! E4 ?1 }) u+ F% a mcprintf("current text window, without moving the cursor.\r\n");
* H5 n' a' y- Ocprintf("Press any key to continue . . .");
* j; E7 S+ |* y ? Ygotoxy(14, 4); 7 m# X, L* w8 J
getch(); 0 R, p4 y9 @! B7 B% G0 U: F
clreol();
, S1 p) I5 x) E# r$ [getch();
8 i v' y3 `0 ^; f) o+ ^5 @4 Preturn 0; ?) d. Q0 B* I, r$ H
} 1 C j: C8 B# `) [5 o
0 X" J0 b& Y& t# s8 K* M% f+ O! \( Z) A$ l1 W6 \( u
$ _4 u4 m- b0 s( S+ s函数名: clrscr
, [' R& b$ |% A' I: {# c功 能: 清除文本模式窗口 8 f. X( G9 ^0 w: ]2 z
用 法: void clrscr(void);
0 x0 w) A+ ~9 j' v5 Q( T程序例: $ W; G ]* L0 x" y" k
#include # X: T, `0 s# l* ~
int main(void)
% |9 H( J; w4 G0 J7 e) f{ + q8 F$ C% T9 \4 [
int i; # }1 q8 ~: K- s# E, T) f4 r" ]" Y
clrscr(); ; y( s8 b. \/ m5 |, _
for (i = 0; i < 20; i++)
+ F- b( k* _. |& D# Acprintf("%d\r\n", i);
6 b; B2 @& e4 J! K$ P! s. Q1 T; Z2 U0 Acprintf("\r\nPress any key to clear screen");
' c3 d, D. | E; L: d$ {# Cgetch();
) P+ ~/ x9 Q- e. f- |2 c) ~clrscr();
+ N$ z$ z) a( l+ G2 gcprintf("The screen has been cleared!");
. S' B' L5 L! I" I o- F* F8 b3 l0 Wgetch();
: ~% C$ ^4 y$ \& yreturn 0;
; _# @' y, n( [} 3 z$ U! B# f. w/ t* y
- c3 D- ?0 C- R2 u& P7 S3 ?4 |3 Q
; \% A" w" k2 M$ B+ G 5 l: I0 R' m3 g5 Q6 ^
函数名: coreleft & B) S n' X/ ? ^3 C) O4 S! Q% Y
功 能: 返回未使用内存的大小
- o$ h7 h1 ]" q+ w用 法: unsigned coreleft(void);
5 l' \; P2 f! Z程序例:
& S& x6 D0 r' ]: ?4 Y% Y#include
2 E3 \" Y3 `; ?6 y& r" @#include 5 I4 t8 u `8 G! n0 c8 I
int main(void)
' O; g& y! \- J# @$ _' v# g# `& o- I{ ' }5 R' V) h! a% R. o! @" w% s
printf("The difference between the highest allocated block and\n"); : X8 U$ x% f% x
printf("the top of the heap is: %lu bytes\n", (unsigned long) coreleft()); " q% y L( [' A- D
return 0; 2 U6 O. w! u/ K. o u
} 1 |/ l! m& b4 n
8 e a1 `/ W2 _ v, J v6 n' B函数名: cos
. K" G8 ?9 L& ^0 |! K功 能: 余弦函数
5 F+ O% a- p E3 x用 法: double cos(double x); 6 R' h& G2 u& w' _
程序例: : D6 _0 `' j1 [: I U
#include
$ r1 e( D& j7 e p#include ! G% K. k! {( O; R& w
int main(void) 2 q+ T! M8 E) O1 ~1 }
{
, c6 x; W" Y8 R; Gdouble result; 3 A4 F* }! o; K& r' U
double x = 0.5;
; |7 l3 f) Y6 D" jresult = cos(x); ! \2 O1 s. d6 {
printf("The cosine of %lf is %lf\n", x, result);
3 G, ?5 ]( ^! n" Areturn 0; 5 w2 Z3 Z' j! h, X
} ! t2 w6 o4 O' L r4 T
5 z6 A$ d8 |9 H$ ]* N+ ]+ \
, y1 L x7 I( t
' C a( D2 W! r a- Y! P6 r# e
函数名: cosh
1 u0 a- P' m# q. g+ n功 能: 双曲余弦函数
+ }. y [ L1 ~7 S. L* R |' P用 法: dluble cosh(double x);
6 q# p1 L3 U7 [, Y7 G程序例: / A/ P3 T3 r% i2 K: q. x! m" P
#include + b: q$ R. u6 _& y1 Z. [+ u# _$ ]
#include ) l5 {+ I% o& E' v! X$ G
int main(void) , C" t, N: P! p* x- C
{ $ N' F/ z- K0 s
double result;
_1 ]8 I$ I6 S {' {+ \* q+ W/ L7 Gdouble x = 0.5;
4 O" j3 R2 z6 n) Q+ Wresult = cosh(x);
) r1 s3 E; b0 G3 }3 wprintf("The hyperboic cosine of %lf is %lf\n", x, result); . p- p! C, Y( b6 W* T
return 0; " A$ ^: P7 y2 O8 h! M
}
3 ] ^0 O# x0 T! R
4 I5 }. ]# j6 I( z/ J6 p) B1 v6 n5 J5 G: c
# o5 r/ b" S, C8 E函数名: country $ a) o* o$ O; M
功 能: 返回与国家有关的信息 & Y* G( z' p- D P
用 法: struct COUNTRY *country(int countrycode, struct country *country); 1 S4 |: a8 F' i' |) c5 [
程序例: 0 O4 D+ f+ R" Q6 Q
#include 1 H+ `) W. z- ]" f; E" S* f9 Z
#include
& P5 G# q! I7 O* i6 {) A B3 M. w8 n#define USA 0 5 x/ n: h% S! \- Z' T+ a8 U
int main(void)
) [5 Z, ? q$ |1 s{
; m H" |4 a/ m7 q! ]+ ~; W" Istruct COUNTRY country_info; ; x; M5 y. y/ X3 H# ?2 k
country(USA, &country_info);
& A- B# v" s$ K! C1 K; r# \5 |printf("The currency symbol for the USA is: %s\n", 3 b- G& ^3 e+ |9 O
country_info.co_curr); . Y/ r2 B/ H. S
return 0; / R# b. c0 ?6 c. Y
}
& {! F, f$ O1 l5 }+ ]& i6 _1 y1 h! q/ p+ I4 _
: D" v1 p% [$ x, y: }
5 k- g8 p' W2 g% R' r8 H函数名: cprintf ( U; A4 b1 w& E: {8 d+ E$ u
功 能: 送格式化输出至屏幕
0 o& ~5 g d$ t0 A# s, j用 法: int cprintf(const char *format[, argument, ...]);
2 q4 W( L* o& \ [6 G+ M程序例:
1 {& u; k, [) }; C* f. j; b+ B#include
; l2 g: x; q k0 xint main(void) ! k9 y) J9 M6 }' O t
{ ! p5 D0 x: h6 o! F
/* clear the screen */ @" d. y+ ]+ \. c( z% J
clrscr(); 9 z$ b. I! T) H% D( y1 ?
/* create a text window */
6 _, |- o) h' h# N5 S9 bwindow(10, 10, 80, 25); ; Y: w6 R$ @+ m& B( L
/* output some text in the window */
" k2 p7 q: i% T: I* i' Y! {cprintf("Hello world\r\n"); # j" j9 |2 q' ^, A% D% j2 V
/* wait for a key */
# _) B0 D: G1 ygetch(); / J8 J% Q: x0 B, \9 y: t6 g% e
return 0; 6 k# y- C$ u( |. p
} 8 v$ J5 ^8 s: u8 O! C
/ E0 U& y( w7 {
( Y6 o. c. Y* _& \# l# A; L
. M+ Q5 u; j/ x& x% X3 r函数名: cputs * l; W% f8 `) P2 d! f! v u
功 能: 写字符到屏幕
' f1 y' O3 |% u P; d* M用 法: void cputs(const char *string); 8 h' z% W9 I* _2 W
程序例:
' q. {) h( u; G) e5 [: F/ ^1 @#include 6 T% _- y# s/ s3 E* s* b. l
int main(void)
, x; B! B1 J. ]% G" h4 V- J! j{ 2 c& S, _! f/ N
/* clear the screen */
$ T: u* Q% j _! Q+ |1 _# `1 Sclrscr(); % Z/ [6 y5 O6 r- ?7 z
/* create a text window */
9 L0 o) k! z7 ~' o, U9 _window(10, 10, 80, 25); - f& A, s3 P* w+ G2 \
/* output some text in the window */ ( D0 H6 |3 f+ Z* j$ ?0 K
cputs("This is within the window\r\n");
9 Q, H- N1 U) Y, R+ h+ Z# u/* wait for a key */
7 N; _' M, @: Cgetch(); & p1 t, U0 ? Y/ I- [9 s
return 0; ; K6 {( E5 M$ L$ w7 `& \
}
' M# i$ P# F m3 n2 ]6 N! @: R
6 h8 B" }- ]1 T9 H# |2 n' Q U {% B
# Q5 Z4 I! \/ R `. ~. X" b( q
函数名: _creat creat
1 q4 I: u! O; I功 能: 创建一个新文件或重写一个已存在的文件
( R( E! [" I9 l用 法: int creat (const char *filename, int permiss);
: N, \2 y6 m! U& a! a程序例: * W/ y) m4 P$ s" s
#include 3 C9 x' g/ j/ n: L* _
#include & G1 a/ |" d, k- M
#include ( | S' @' f1 s
#include
9 S- D0 X7 u# Wint main(void)
3 \6 m& G' h+ O* Q0 R) k{ ' f% ?9 e- ?8 i* K- U$ C9 y
int handle; ' S! D* D. q& c4 V1 H. A1 @: D* u
char buf[11] = "0123456789"; # \* M1 A0 }4 _
/* change the default file mode from text to binary */
% t, o; @& f6 Z" __fmode = O_BINARY;
% f! w7 u+ |- a3 q3 S# ~/* create a binary file for reading and writing */
& H4 \3 A* b, s8 g' [handle = creat("DUMMY.FIL", S_IREAD | S_IWRITE);
: M4 n' i, e b' X4 _. d' O; s/* write 10 bytes to the file */ ; m. v# N( d7 z a! O' a& n# k6 S. I; i
write(handle, buf, strlen(buf));
! E7 Q+ T$ a& d( G# E/* close the file */ # M0 |* ]7 X% ]+ k/ f# E; X' [1 G# I( |
close(handle); 1 G3 z0 e- I/ O7 @+ M" _6 R! M/ ]
return 0;
5 K. Q; [, ~# Z: @% x} + U) b7 g- \+ @$ E; o* X
. ~( S4 s6 q) d0 |! ^7 ~7 W
函数名: creatnew
/ \3 U9 |; b% z- ^功 能: 创建一个新文件
! X; n( u. {: ~3 u+ p. E用 法: int creatnew(const char *filename, int attrib); 5 x/ c K* Y, I
程序例:
% b8 w1 @% P& v& Y P$ f* k. L9 B; g#include ! h% p5 I7 P1 o4 q/ P
#include 6 B* B2 K: ~- E
#include
, R1 A8 p7 M2 K+ ?+ [7 ?#include 8 c/ N, c, u+ W% y1 V! Z" I6 D: T
#include
7 G+ H' P( I8 }7 ?& cint main(void) $ g* i9 M2 J, m
{
7 X$ Z B% w" N4 b/ W4 t/ ~int handle;
+ M* h, f3 {; p7 schar buf[11] = "0123456789"; 8 }% O8 [* k- u% m; P
/* attempt to create a file that doesn't already exist */
/ t2 j( y1 Y3 j- nhandle = creatnew("DUMMY.FIL", 0); ! L5 H( V/ K( w
if (handle == -1)
4 r* ^3 w# [* `" Y6 _4 a; V( g0 eprintf("DUMMY.FIL already exists.\n"); / q: w) k- b) D# l2 M8 ?4 C
else $ ^- E h: n( ~) w$ |7 {
{
' W4 g: _6 \! X: e; t6 f/ }printf("DUMMY.FIL successfully created.\n"); 7 n7 Q. ~5 H, R) h/ ^$ h
write(handle, buf, strlen(buf)); * f: M* b) u% d" i& I$ Q' z
close(handle);
/ H5 ]# X9 h$ }. K}
, T6 } L& f# G; `$ Hreturn 0;
, [+ g$ G9 w& }. c2 I7 d3 ]& r} 3 F) b3 c. n7 v; B
3 f) [8 ?% a, B" b Y0 y% C* |8 M+ A u0 u* O; k- i( N
* t z9 J, Q- J, F/ D* ?6 ~
函数名: creattemp
) a' ^" ^! ?* Y$ R2 s( z功 能: 创建一个新文件或重写一个已存在的文件
2 F" t; G5 u( b- ~% S+ Z用 法: int creattemp(const char *filename, int attrib); 6 p6 z1 s3 P+ N! |. r: H
程序例:
l6 ~: p" @- q4 G5 I4 {; b#include ' M; ~/ T B* O7 p# {
#include
' ^4 z" L+ k3 P( l( h#include
, x; u/ H! E& Jint main(void) * g' h, T6 j& K3 w. b8 K; p
{ l! E/ C J7 B) W& J2 W/ N4 G3 A0 o
int handle;
0 ~3 a. g! D0 G9 E) Fchar pathname[128]; K" j" m1 l; Q) y: u5 L$ {! X) X
strcpy(pathname, "\\"); ! V- o3 ?1 E& L
/* create a unique file in the root directory */
' R( F h+ {# @handle = creattemp(pathname, 0); . P8 J- o1 x( T/ y( x" f, M& y
printf("%s was the unique file created.\n", pathname); : n6 l3 `% X' l$ D/ [: F- Q
close(handle); . ~6 v) C! E, |; U4 K
return 0;
% x+ [7 P3 m" v" i. p1 q} " Z- ?( `2 z; L
- h" t% _* Z' V- a0 j/ r8 }
5 N2 H7 D8 ]' R* p9 B! O
# U! G' C2 J2 H/ ?函数名: cscanf ) T' C% \& I! ?; x
功 能: 从控制台执行格式化输入 7 Q# s1 W d7 K4 W. j
用 法: int cscanf(char *format[,argument, ...]); % e' {9 ~5 m Y% d w
程序例:
+ d9 M; h, s) T4 Y#include 2 P/ S6 ?! v# E" A3 k3 D
int main(void)
9 s0 V. J1 Y, C' y7 a9 c5 _& \{ ( [- P- A" V' M
char string[80];
) [9 l( G& V" s' g8 Y5 Y/* clear the screen */
& ~6 e& z6 a1 G6 I2 }clrscr();
) L* f) g! q* y& @4 f4 r3 y/* Prompt the user for input */ L, C& h7 G o: J, e) S0 f( J+ ]
cprintf("Enter a string with no spaces:"); % l' P8 J4 s* v8 s
/* read the input */ ; `; K& P' O7 o/ C/ Y: ?
cscanf("%s", string);
% i* G% W b9 `9 e$ S; l/* display what was read */
4 g0 }( W. I! I: m! t: k) ~+ l" tcprintf("\r\nThe string entered is: %s", string);
* a/ U! n/ H& J( N7 y$ a' \) nreturn 0;
) Y% u$ x5 }1 o' w}
7 m7 q, b3 v# I5 b! s$ F f0 B: Q$ H1 {- l. }6 B$ H
$ J( L4 E# b0 \! \( X q: e( r
! {* A' \2 F3 S* r4 ^函数名: ctime ; q; A/ x. ?7 Q1 a
功 能: 把日期和时间转换为字符串
* P J( V. h3 f# L& ^用 法: char *ctime(const time_t *time); 1 T @3 _; h& y6 I9 R. R
程序例:
/ H3 A+ d9 R0 l/ w6 @# Q5 L; M#include 0 s7 U5 w' s- Y4 J: [+ j
#include
$ C# t' V6 H' N8 ], X, X$ U6 j/ aint main(void)
$ `+ Z/ X) _' ~) a7 Z! f. w{
3 F k6 B, F0 [+ Ztime_t t; 6 Z0 ]9 }0 R- x3 n/ o2 K
time(&t);
2 k' A4 v6 s3 h6 m) Z9 Yprintf("Today's date and time: %s\n", ctime(&t));
. Z; t. C% Z2 P! J7 mreturn 0;
& i: ~* J0 y+ f, ]- m7 R" k3 _. D9 D9 ^} 9 ~( ^; E) s9 \& q8 Y/ Q- o& j
; z' h$ B; Q+ E9 y. D- b$ B' ]/ M# l/ p' P7 T& h Z$ ~
; x9 e% a" A5 k$ _- w/ G M
函数名: ctrlbrk
0 S- }2 V( a1 H% z' c$ ~功 能: 设置Ctrl-Break处理程序
8 b3 u1 }9 G# p8 F用 法: void ctrlbrk(*fptr)(void); + ]6 _+ l* D1 d- p
程序例: ' p5 k3 G4 {$ @0 y( r
#include
2 E# e( i7 _/ Q#include
, W4 E! k6 {0 \2 m#define ABORT 0
+ v# E$ u. _5 O+ f! h9 c. ^int c_break(void) , I$ p$ r. ~ o$ I9 X* o
{
, K; u9 q9 b3 E9 J3 ~' ?! Wprintf("Control-Break pressed. Program aborting ...\n");
7 j$ ~3 I5 C: C- [5 ]! x, Rreturn (ABORT); " X- u9 S q5 `4 U% f% o
}
% n3 s7 P F: b1 n8 m6 Pint main(void) # R# r& R* z, [* |+ Q- f
{
: ^% l! Y. `) v; yctrlbrk(c_break); 9 O, v% K" n2 u) g& Y6 w
for(;;)
- j% G2 B; k0 @& X8 E( b{
5 F3 M$ t8 g; p$ {8 {9 Wprintf("Looping... Press to quit:\n"); i9 b- B' ?" n. N' z
}
; N4 r# r6 d6 t' S8 n8 y/ H) p. A# kreturn 0;
( y E3 H6 ]; b} |