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