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