|
函数大全(c开头)
3 O# _6 I& j! P n* u
' ]$ ?& I& u" j7 z函数名: cabs 7 N! B4 h1 k8 H" b: P
功 能: 计算复数的绝对值
3 x! z3 p/ s- Y用 法: double cabs(struct complex z);
. e, B/ {( a. Z1 Z# V7 ?3 H% G程序例:
. p5 }3 P4 h1 ~: Q: r$ z#include
% [: |1 Z7 ?, p9 H* z#include 7 X. h, G: l% {, w+ G% H! y3 A
int main(void)
/ V# ^0 X6 K( \$ z/ ^{
3 R$ p# M% I$ g% W) B3 P7 i/ Zstruct complex z; ' E; z& I7 W7 L1 l" J ^& T
double val; 4 e& r( j" W) n% E: P
z.x = 2.0;
/ r6 u/ K1 L. u X# s0 r% Jz.y = 1.0;
) J6 x9 ~5 ]: Hval = cabs(z); ' ]- s5 M' j' x5 o: u1 b
printf("The absolute value of %.2lfi %.2lfj is %.2lf", z.x, z.y, val);
7 g, b( ]* `0 q0 _* @6 c# \return 0;
* C. c- C4 Y. G* z% g6 w2 \} - Q) |) u; b. `; @, |" j
8 p$ a/ U7 e8 o# [+ Z' \
6 x2 a6 Z7 P2 @0 D + O; I6 L# G6 r: w+ f7 d
函数名: calloc 7 s# `! t( Q* W* L- f
功 能: 分配主存储器 2 t% c4 N) f$ q- E7 Z) P U- R* ]
用 法: void *calloc(size_t nelem, size_t elsize);
+ A2 @9 B: c; z* [2 \- V8 U程序例:
- o# w5 t. a0 a0 L! u) [#include 9 e: W: f% B3 Y+ W2 H! c1 f% \% T
#include # N$ E5 F1 ?: R& z+ H
int main(void) ' P! `5 U0 b8 h$ S; ~+ v2 B% D- X6 H. O
{
0 G9 i) Y7 F T) _7 C: U1 Kchar *str = NULL;
8 T5 G z# r# J) x# l9 } d/* allocate memory for string */ ) u Y% t& d0 v' x
str = calloc(10, sizeof(char)); 0 Z$ |, N: s3 R
/* copy "Hello" into string */ x$ `$ `6 n7 Y: D$ u
strcpy(str, "Hello");
; x6 m% C7 I7 m/* display string */ 1 m9 b A6 P* Q+ }. B% B1 O0 m3 B
printf("String is %s\n", str); 0 R0 o: Z! V! E( x7 ^5 Z$ S4 I
/* free memory */ - I7 `; t! B7 \3 i# u! T; \ U5 \
free(str); - ^6 N t4 A' w/ c$ e% J; g( Q
return 0;
. B0 v, b! l4 ]( X- A} : L# v4 A/ ^& d) o! v k- C# q, m) j
3 V& A: l( F' Y' F" n& C( a6 w: d
) U U( x! z. F8 B2 G- l) w1 u8 D
( p5 W( ] Y3 L% g: N! ~函数名: ceil
& u" ?, [# j9 r. `0 m2 h功 能: 向上舍入
! }2 B6 m+ C L7 v5 X用 法: double ceil(double x);
' t3 R# G6 b' E+ X程序例:
; e2 F; s8 s Q- {9 K#include 1 J: A: B: R% I3 `& T+ v
#include
" ?' F5 z3 X( h% V5 K5 yint main(void) 3 G1 O) s5 ~! L6 Z* Z9 D
{
# E" Y0 U) a3 Y2 X0 G: i6 d" Odouble number = 123.54; 7 X1 r: t: M1 l, p7 D
double down, up;
4 O: _0 M7 ?. P# H: Y& G6 adown = floor(number); 1 }. Z; ?: ~8 Z3 q9 W7 `/ T6 n
up = ceil(number);
$ x% M) n% @9 ~9 k. }0 eprintf("original number %5.2lf\n", number);
8 X. N9 H; F. Dprintf("number rounded down %5.2lf\n", down);
M, I7 A: _2 b% y6 bprintf("number rounded up %5.2lf\n", up);
6 r; k& F/ [) u8 D0 m& Breturn 0;
$ O" v0 K# x& U} 9 Q a9 x7 {9 b: K* y8 G
# _1 {$ c/ y1 r- e# \
8 C. S3 U$ n4 A; s 6 h- u+ Q4 p5 u- L' O+ m2 G
函数名: cgets
. q8 @( n% F* h3 L E功 能: 从控制台读字符串 & H5 \) i: x8 B( b
用 法: char *cgets(char *str);
' n: i7 y; [& R5 i& e0 |0 B程序例: # L, }7 P4 c& D( `' y |) p8 L
#include
! f) ]& q$ j8 O5 X4 f#include ! m0 p# a0 y2 ] E
int main(void) 2 D2 x5 R' p) v) E0 ^" V6 E) p
{ ' ~4 I' a+ S9 O/ g' T' h5 }% w: @
char buffer[83];
" Q4 M) x- o! ]4 y8 O/ X" jchar *p;
: @% `8 D' ?5 \: U3 c3 z/* There's space for 80 characters plus the NULL terminator */
% r! R; _6 g; u& O; ?8 _buffer[0] = 81; 1 G0 Z# r# m& n8 H0 K# p5 x5 r
printf("Input some chars:"); 3 d4 w2 F1 z8 S, i) J; i
p = cgets(buffer);
# P* H, l6 a6 w, j* V+ cprintf("\ncgets read %d characters: \"%s\"\n", buffer[1], p); - }4 [& |4 ]; y+ c/ `. T. i
printf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer);
+ T, p( p5 j# k0 e/* Leave room for 5 characters plus the NULL terminator */
x+ r1 ~) R+ ?, v5 s$ @( |buffer[0] = 6;
. w- z7 K/ }) W, z& x4 dprintf("Input some chars:"); 6 x2 y! ]" z6 w: R; K7 I/ a, x8 a
p = cgets(buffer); , Q# [7 x: ]5 }; `- ~
printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p);
$ i; `* u) r# ~printf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer);
; M# x% p. w8 W4 }6 J. l3 nreturn 0;
* z' d# r5 y5 n- v! i5 o} : B7 ~9 \; }4 C6 j5 q
5 [# i. h; [6 v
3 T! W* i/ C. _
& N; K" a' v( e函数名: chdir
! B9 I) L$ {- T) V5 t% B功 能: 改变工作目录
5 h) N( g8 E' E! [$ O: T3 G用 法: int chdir(const char *path);
4 y$ n( i3 l* n程序例:
- Z% n0 n; Y! L, l#include
! F1 B+ R0 p8 E2 c2 C- [#include
' c* `9 l/ U( n& w1 K* P' `. ]8 ?#include / ?( l K/ F0 {$ V' d
char old_dir[MAXDIR]; 6 L1 J b% |# f3 l) C7 [0 r
char new_dir[MAXDIR]; * I/ @; w- y* e: h
int main(void) : o/ j' R$ N4 v$ C5 ]2 l3 n
{
4 E1 B' f% F0 u* O6 |# wif (getcurdir(0, old_dir)) 4 C2 L' v! y2 ^2 F" [2 u5 A
{
a# O7 E2 n3 S6 r' H( Fperror("getcurdir()");
) ?( e+ Q$ j# Pexit(1);
3 T( G6 r7 }8 e4 D2 i3 Y) s}
# Q3 {/ T% G- pprintf("Current directory is: \\%s\n", old_dir); 7 k) y; L" h- g- W" ^
if (chdir("\\"))
9 d; @+ o0 X/ W4 L' k% A{ ' J3 g8 n( W' ?) k7 B& u$ K
perror("chdir()");
3 v$ g* w2 H8 w6 I5 G8 N4 texit(1); . t [* A) E/ d/ b$ @ s
} 9 b4 E% p- `, t0 r7 o" W
if (getcurdir(0, new_dir)) 1 F9 v, V/ R1 _0 H4 Y4 V
{ 2 s7 y5 F" C" L2 [: o
perror("getcurdir()");
1 W/ a( p; U8 `exit(1);
! E1 K/ n5 K/ Y2 J+ q- Y} 6 I1 o( \8 ^; \+ j& s
printf("Current directory is now: \\%s\n", new_dir);
. F( q3 k* e8 N/ `6 l, U2 dprintf("\nChanging back to orignal directory: \\%s\n", old_dir);
+ T' A9 l; W7 P2 O3 _% Oif (chdir(old_dir))
& n# W z4 i0 E% ]) U{
, a$ ]$ t7 R. A6 sperror("chdir()"); # F, m3 i% w: v& G
exit(1); ' g0 f n. {9 ~! F
}
* A2 w6 g5 b# B3 Wreturn 0;
* @/ k: U/ N$ I. M! q% O}
+ E4 Y! F' o1 g" k5 Q8 `5 I+ u; p+ ?4 Q3 }' Z3 f* A9 y
3 J, F! L- K5 B函数名: _chmod, chmod . z' s* e# C Z7 @0 q( `" w; R
功 能: 改变文件的访问方式 b/ S8 w% M S- z8 ], d
用 法: int chmod(const char *filename, int permiss);
& a! d( u. M+ |8 x! K6 p U7 j程序例: 7 K O8 ^5 ^/ [, a1 r. F2 j
#include / T. Z3 s9 f% E7 B! W' G
#include
2 C, P: h" \/ K# s; M2 S#include 4 l7 p6 T. w. x2 @! Z
void make_read_only(char *filename);
q7 }+ u' v: @# J; kint main(void) 8 X: u0 Q4 T) t# {& D* W
{ ' |4 j( m3 I+ d: ~
make_read_only("NOTEXIST.FIL");
D/ }9 }( g* D7 }5 Y8 c9 O5 W) Omake_read_only("MYFILE.FIL");
7 x( v/ ~, x9 q0 [return 0;
9 |9 {8 D C8 I, H5 h6 i} & C; _7 L+ [- W0 x
void make_read_only(char *filename)
/ L" F8 A& G" r. x. f{ 2 p4 d T/ e- m! [
int stat;
& L3 q/ Y, q7 _9 Q: L* w0 c W ?: Ostat = chmod(filename, S_IREAD);
) @: P/ e* p4 p: oif (stat)
1 D9 D3 m/ \, Z* Nprintf("Couldn't make %s read-only\n", filename);
! K6 s+ b5 q" B' ~/ m% W5 N6 s" ]: ?0 Relse
- F' K3 \; L' `! d6 fprintf("Made %s read-only\n", filename);
5 ]" Y) Y9 K& q8 R} : x: v4 f: A2 K9 c4 ?- J4 D
% V M- \6 n/ ~, ~" y) J& u
& N7 S: m# z3 P$ O. \ ( }2 \$ q- M6 Z3 j7 v# [
函数名: chsize ( M2 Q, _: R- S
功 能: 改变文件大小
, a9 H) @. A7 [& V用 法: int chsize(int handle, long size);
8 n/ q/ M( p- a9 t程序例: 4 i+ l2 P) `4 R5 J6 u
#include - V/ f- `5 j/ }- U) @& u
#include
, y4 r% q: A+ N#include ! @; }' }, l' n! h
int main(void) + X( _; p6 L) A: ?+ f4 z# T; @
{ & A. c; k5 _# V E7 h! y9 j
int handle;
6 K% o/ s' Q) }4 ]( o5 Ichar buf[11] = "0123456789";
' a- X" P% B6 A2 f6 U# t/* create text file containing 10 bytes */
5 i1 ~1 Y6 z3 i' a. Khandle = open("DUMMY.FIL", O_CREAT); # s9 n' P" |; f% i9 h ]6 R+ J
write(handle, buf, strlen(buf)); ! C% Q; V! W& h: H, r- t- \
/* truncate the file to 5 bytes in size */
" M+ h) S2 J2 q6 q# xchsize(handle, 5); * I7 c. O4 W1 H2 g3 J# Z' m0 h9 Y
/* close the file */
. I8 L1 D: N! G; L2 Dclose(handle); s" w+ A/ q( O" I' P9 A0 X
return 0;
9 Z, R% o2 z0 ^+ a}
: X- U$ P0 [3 I/ k S( z
( B1 L; G B6 }% D8 P * j8 j% u1 {, v$ W6 `' M9 ?
函数名: circle
0 e' q: Z; n& ?3 D0 F# m6 Z2 u# T0 U6 W功 能: 在给定半径以(x, y)为圆心画圆
9 _! r: p) S3 ^& F3 F用 法: void far circle(int x, int y, int radius);
; H9 ^6 b1 ^3 ?, S( D# a) I6 D程序例:
9 K1 G0 e; d, ], A" ?#include 1 f' @1 X$ p% w
#include * K, b+ h: L1 {$ c- Y0 M
#include o* \3 s$ ~' d, \
#include
2 x3 U( t* a% @* n2 i6 \int main(void)
; W3 ` i5 S/ p" B{ 4 ?2 e" r& o% g& k! t% J$ K3 y
/* request auto detection */
: \$ T- {1 p# Q1 Z) L4 |$ Nint gdriver = DETECT, gmode, errorcode; + m# T7 \/ V, f* u) ^
int midx, midy;
) ~! ~) t4 |: sint radius = 100; & D: Q4 C8 y4 r& K8 k: t# |! W
/* initialize graphics and local variables */ $ H4 u2 a: }* R! W
initgraph(&gdriver, &gmode, "");
( z! n5 u1 p7 a4 D0 F' a/* read result of initialization */ ; D' D5 x1 U* e2 q7 y/ U
errorcode = graphresult(); ' Y& \6 M% F% a' w$ N
if (errorcode != grOk) /* an error occurred */
, O) R9 [' U I8 M1 k( i+ W{
8 R& E6 G3 Y! v! ^8 r) w1 _printf("Graphics error: %s\n", grapherrormsg(errorcode));
1 f. J, i7 b$ v! S3 Gprintf("Press any key to halt:");
1 i) Z) c g0 m* K& Pgetch();
c+ a0 i0 l% A. `1 F/ @4 B+ ]0 _; Kexit(1); /* terminate with an error code */ % i; j+ U/ D( p: Y
} 6 D4 G$ M- z# N8 g; Z, I
midx = getmaxx() / 2;
% R v) M U" |* Imidy = getmaxy() / 2;
/ R1 c( n, P8 P+ A. O; E; {5 ssetcolor(getmaxcolor());
' }4 q8 |' u+ L3 F9 k3 c, q/* draw the circle */
, \8 o) `% [# \+ z D) u" Hcircle(midx, midy, radius);
! H3 e" d, f& H2 B# _2 _/* clean up */ ; g" O& p% G" n( O7 p# C
getch(); , t* ^# [2 }' M( v) N- T" v
closegraph(); : E6 G% R* j; P0 J
return 0;
. P T! @& i5 i! f) Z} 6 ?) i6 j+ q0 w; X9 W+ A. k
# ]2 w; ?3 X9 _' r
) ]' X/ z% |' U5 R0 C8 n. B1 x + B! Z# y& x& m, a0 {! y- N
函数名: cleardevice 0 ?1 m6 m7 c0 ?. ?3 L" V; Y8 G: k
功 能: 清除图形屏幕
* P" x, o3 s- `用 法: void far cleardevice(void);
: y7 J& r# L" C程序例:
) i; s. T9 |7 V" l ^, I4 B! F! b/ q#include
2 i: b! T$ u, R9 Z4 ~3 d#include ( E) u8 S+ E5 h. P- @
#include 8 w+ A) t4 ]4 h/ E8 `9 b3 O
#include
/ w F* T Q/ I+ J5 |int main(void) 9 Q! I6 ], V" E/ r/ k9 `( Y: s3 s5 {
{ + U$ s- P. D7 z; O! `/ x; ?
/* request auto detection */
/ u& ^' v0 b D9 E4 Lint gdriver = DETECT, gmode, errorcode;
' [1 |2 @0 N1 x# H6 P0 X3 Sint midx, midy; & U+ W; f0 L8 V9 @) l
/* initialize graphics and local variables */ ) ?3 V9 A) S% t4 L
initgraph(&gdriver, &gmode, "");
" i& ^( X5 J. L# }1 A$ p' i% p/* read result of initialization */
! i, L" e6 H. ] P G8 e6 R' U* gerrorcode = graphresult();
, v1 ?0 S# B' w/ R$ Iif (errorcode != grOk) /* an error occurred */ % B! @& [ o' a8 T4 j$ u. d% J0 G* U
{
# f: t% D- q1 X2 `- _ kprintf("Graphics error: %s\n", grapherrormsg(errorcode)); * [+ Y) x$ Y% {4 n; S3 `3 {# i6 K
printf("Press any key to halt:");
+ s8 H% t8 I: e$ `# J% m4 {! cgetch(); 2 }! [& t' p+ ?
exit(1); /* terminate with an error code */ V5 A0 L" [' C* v
}
* t5 D; t$ J* h: {midx = getmaxx() / 2;
( p0 h# z- e0 f% C% ^/ J* jmidy = getmaxy() / 2; , }2 ]1 J0 A( O9 S
setcolor(getmaxcolor());
1 N- {7 E9 F1 ?% w ^2 B0 Y/* for centering screen messages */
4 `, @0 y' r3 Q3 y" F0 b. }settextjustify(CENTER_TEXT, CENTER_TEXT); ! C) S$ d+ q/ H
/* output a message to the screen */ 2 q" J) T$ F5 w8 q/ v0 b/ f, X
outtextxy(midx, midy, "press any key to clear the screen:");
5 B2 a9 n+ n7 [( r5 S! V( V& ^) M/* wait for a key */ , A& _/ S+ G1 a) S* B% ~% B# N0 i
getch(); 1 {6 C* k7 ?' S: p7 c1 Z A
/* clear the screen */ 7 Q* L$ @4 W0 o# u
cleardevice();
3 N+ W C% B9 c/* output another message */ , U: C4 D3 x8 A& p: s( {, s( i% [9 l* V7 N
outtextxy(midx, midy, "press any key to quit:"); - `, |: K0 n+ Y$ b( [; ~7 R4 Z
/* clean up */ ; @' y, l" ?8 X0 P# K. \/ [
getch(); 9 D5 M' v. o+ \; a' p
closegraph(); 3 c1 Y5 k# r' t5 S1 F/ J4 T) E% ]
return 0; , K+ h2 e: m/ p7 H+ l5 @7 ~; H
} 6 y- T" A; m/ }% A0 i' O* K' s
V5 L- w0 `. n z+ L; V J
8 B/ J, F; V. [7 d! ]
+ ^2 @: _# b ]5 R' Y" v2 s( s函数名: clearerr ! z! M1 Y! J8 `5 _9 V, I& c, @
功 能: 复位错误标志 " L* g- x. a' p. K: n$ h
用 法:void clearerr(FILE *stream);
) i. w+ ?% t: D# O程序例: ; g' B' r- ]: d" B
#include : g c, C* m, Q$ z
int main(void)
5 b. |4 E n8 ~0 N# f6 h{
- K6 E" J/ _( v; a: I; RFILE *fp; # _$ E' `+ n7 ~& n- Z1 \
char ch;
+ ?- z G/ n- V/ D/* open a file for writing */ # {+ Q1 [: R) L2 P" C
fp = fopen("DUMMY.FIL", "w"); ; ]2 f) J2 u% q
/* force an error condition by attempting to read */
& b( S% }) T3 }. _+ _ch = fgetc(fp);
2 z1 U4 v7 k4 M& ^printf("%c\n",ch);
) M) Z& `3 a5 a1 x, Q( t7 k/ l; Pif (ferror(fp))
1 t# ~( k$ u4 j( B# S R0 e{
! o6 h' o7 P( O( M% j' U/* display an error message */ ) \1 m: G& b) f( j+ Z
printf("Error reading from DUMMY.FIL\n");
* s, b; v9 ^" B: h6 {/* reset the error and EOF indicators */ ' z- C0 j* O0 a. m) D5 {. J
clearerr(fp); 1 t) Z, M4 H' L5 e8 |# D# B
} ( K: V# [; \( g- f% \" H
fclose(fp);
5 g: T% q2 n) ~ K( ]return 0;
1 m! {5 N) l) ~" X8 K3 A: b} / P% p8 l2 e: n& ^% c. ]
/ b" N2 G d# T! g5 Y6 R6 g
- Y( S3 D6 V) b. W - _5 M' Y2 ~( x! a4 r/ A1 d
函数名: clearviewport 1 _$ f! ]* |5 g+ P
功 能: 清除图形视区
5 `) G7 V o2 r用 法: void far clearviewport(void);
8 ^. M& H# T2 V9 R+ F5 o程序例: " T' ?* A. C: a
#include
' P' u8 |' D( n) ~& b$ G6 Q#include ! Q t5 Z, n# F- X: T# p
#include
N3 c2 @% g; o#include
b, |) Z7 Z# F#define CLIP_ON 1 /* activates clipping in viewport */
& ^& g" ?/ g! u/ Z/ W' ~1 y. Wint main(void)
1 t0 ^$ u) h l- Y$ X{ * f- q1 a: Q9 K- v% v' h5 O
/* request auto detection */ / }0 t% P" |' o/ j
int gdriver = DETECT, gmode, errorcode; 5 O* I T* T1 k8 [1 I
int ht;
" |1 i4 _1 G3 c6 ]9 f* a1 c/* initialize graphics and local variables */
% {) `6 H& P9 b2 O, Q' finitgraph(&gdriver, &gmode, "");
% P9 @& O+ U1 v% N6 f# x3 t- d' G/* read result of initialization */ 3 s- u: W0 Z2 B: H' I
errorcode = graphresult();
3 l* r6 Y$ C8 a5 Y, T/ l$ ?) |if (errorcode != grOk) /* an error occurred */
4 R2 W/ K+ @0 p{ $ H# g ^; E2 m. q% y
printf("Graphics error: %s\n", grapherrormsg(errorcode));
8 @6 U! s5 ~4 Z# R/ S i5 ^printf("Press any key to halt:");
8 d8 H5 `3 I$ F, J! z/ Y) igetch(); + @* G( }& y# ^9 `
exit(1); /* terminate with an error code */
7 @3 S% y7 i1 Y}
7 t. }9 j6 z8 B# Ksetcolor(getmaxcolor()); $ s( S* v" B& O: `9 m. Z
ht = textheight("W");
7 I! d' i( C* B' G/* message in default full-screen viewport */ 5 G% o" K: A) S/ u
outtextxy(0, 0, "* <-- (0, 0) in default viewport");
9 f2 h# o. D& l/ N+ a/ A! k, v k. O2 K/* create a smaller viewport */ 3 \" o _0 ?5 x) T: }8 b5 F3 \
setviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON);
" z2 T; Z2 L0 {5 v5 |% s" C0 u( j( t/* display some messages */ . y% L7 v' P9 q0 e
outtextxy(0, 0, "* <-- (0, 0) in smaller viewport"); " O) _; Z Y0 Q/ A R( c& o9 P% p
outtextxy(0, 2*ht, "Press any key to clear viewport:");
! ?* Q" o, S) x- R @4 \$ L/* wait for a key */
" S: Z |: E, c( R s3 u! \" lgetch(); 7 T2 w s- Q7 D( q' Q+ e
/* clear the viewport */
7 l2 P" s! w0 C/ N' hclearviewport(); 2 G" J8 n) A$ s9 A& O0 L
/* output another message */ & ]1 ^# R& Y; ~+ j/ u+ |3 g
outtextxy(0, 0, "Press any key to quit:");
" A/ [5 p8 A3 v. `" ]/* clean up */
# L) i; p2 ~4 z# `getch();
8 o9 f. @3 c3 n5 G! nclosegraph();
- K( |4 v8 \- }1 E8 xreturn 0; * }. R- T! X$ P' [
} ) p8 [- y8 A0 N! ]( P+ M$ k
. N; B+ U) C" p( o% N, K
; y& j* e& m3 Z& p$ O- j
/ _: S/ _$ c% ^* V W函数名: _close, close
( U9 F+ a2 B2 i. G5 I( b/ e6 k: t6 F功 能: 关闭文件句柄
2 ]4 @% z8 ^/ N3 r8 M% V用 法: int close(int handle);
8 L# W0 P) V: V! x程序例:
0 N B% C5 G* l4 k! T9 r( P#include
- g8 F! D- t. o/ ?#include + z- Y& j) u7 Y5 w/ C$ a$ F I3 M
#include
& q4 j) C# q5 L6 Z* n( Y" ]#include ( p: ~; B2 d' a: Y- G
main()
' S$ y( Y$ E* w{ 4 `# R! k D+ U5 B* ^4 {6 q' E5 P
int handle;
# x5 D2 J! \1 t0 dchar buf[11] = "0123456789"; : b# e0 l6 R/ n" \) ^& v
/* create a file containing 10 bytes */ : G& @2 { x5 y0 n1 w% ?6 y6 B# O
handle = open("NEW.FIL", O_CREAT);
7 L+ S$ U; M$ X6 Z% Lif (handle > -1) . i$ m) }, S; C% [& M! D
{
( C5 w. I2 s# Hwrite(handle, buf, strlen(buf));
! _6 `3 u% P: \, A" P9 J/* close the file */
; w2 v' p5 i8 `: h0 D [close(handle); ; E, K* U" Y% `0 G* s* `
} ! P2 W( y# }- D- B- m/ E7 {
else
# C& J) k4 ]" W( @* z{
8 @% w* ]6 w5 o$ o( Aprintf("Error opening file\n");
; P* `7 M( X0 _} . G% Q& I% g: ]) c+ |1 U! |
return 0; ! C5 u/ S3 K3 t$ v; X- `
}
2 n' ] K F! P$ k! _( f( B: g4 s2 i9 m j- }4 d3 K; E, J
E, L* R0 a0 H9 o: {" ?
9 g" N- A# r) Q! A2 a: ]% e函数名: clock
# L5 c$ ~5 x; p& P功 能: 确定处理器时间 4 P) b) Y3 r9 R6 d. g6 Z
用 法: clock_t clock(void); & Z2 d* N6 I/ X$ e
程序例:
5 E& A- ?+ P ^5 U% L3 y2 [#include : \+ q( s* Z# @1 ` S2 i
#include ; k j* X( ?4 a+ L) ?
#include
6 a3 P7 w0 |1 b( aint main(void)
4 d+ R# u9 x( D8 Q9 Z! v' g3 s{
( c; G+ c: f6 H, x' [: D5 lclock_t start, end;
& @2 F) t! L6 G: z. S8 ]5 V; N+ mstart = clock(); 9 r* D( {4 L3 E. K
delay(2000);
% s# V6 [- s0 D% c3 d% Vend = clock();
0 Y, l8 U& r6 Fprintf("The time was: %f\n", (end - start) / CLK_TCK); * l0 k# P; d2 I0 l; v( y
return 0;
2 Z- j; m3 Q ?, j8 S8 b7 C; D8 B3 W1 U} ) t; t8 j: Z& t
( R" Z; V5 |- \7 g+ P& p# F A6 U) p
. X$ h6 i! g* o2 J' L; Y" U
4 K6 {# }% K" Z$ m, n
函数名: closegraph 4 q3 S4 k, S# |$ q- J4 a
功 能: 关闭图形系统
" K @! V6 s* |0 C- d0 @用 法: void far closegraph(void);
/ J5 ?! w1 ]+ i& c9 Z程序例: ! |+ C2 _! J: [; r8 ]
#include , m) l4 ]4 z3 V
#include + ~3 `! g4 w: b9 v1 A
#include * d8 B3 a6 c: D" S, {
#include ) W6 H0 H! z) y1 B
int main(void)
) s% v7 _" h) Z/ V; }/ q5 Q8 f{
* M9 O0 r. q( I2 p0 ~/* request auto detection */ % Y" `/ d8 [2 P' f
int gdriver = DETECT, gmode, errorcode; & `2 d- y' _7 A* s+ q0 k
int x, y;
& ]# y- x2 e% L- A1 k/* initialize graphics mode */
3 B& V9 g0 P* d! \0 Hinitgraph(&gdriver, &gmode, ""); n3 L. s$ z0 s
/* read result of initialization */ 0 ?* }3 F9 x$ ?0 G5 ^, G9 O
errorcode = graphresult(); 0 p, Y" D' k2 i5 b- D- a
if (errorcode != grOk) /* an error
' ~; X2 {: j3 ^* v8 u" ^. i0 X: Coccurred */
$ v8 B/ U6 n/ Y8 \! t" Q{
/ d6 ~! w w, M8 |& b; A. b* \printf("Graphics error: %s\n", grapherrormsg(errorcode)); ( ?. i# [; x2 Y+ L! k
printf("Press any key to halt:"); 5 \9 |) }& o# t; ~" U
getch();
! ^9 p" _2 z [) }exit(1); /* terminate with an error code */ 4 h5 I. g* L0 X7 D! L: n
}
' p- u' E1 E5 s' sx = getmaxx() / 2; , @0 L- W$ e; {% S) O
y = getmaxy() / 2; ) I! j0 u6 ^5 d
/* output a message */ 9 N5 O% g+ p+ ~6 E% c
settextjustify(CENTER_TEXT, CENTER_TEXT); 3 r* {0 O& f" G) `- I: i
outtextxy(x, y, "Press a key to close the graphics system:");
- G8 @( p. m: R) c8 m7 G/* wait for a key */
5 O0 L- q% y. Y" O- k! q4 fgetch();
2 b; T" R( ~+ Q6 E0 R/* closes down the graphics system */
& O9 F8 h- X; `9 a4 ]closegraph();
) S# j" c3 I) m5 E' Pprintf("We're now back in text mode.\n");
( p1 O. e# c( U1 T7 q) ~7 z: q, o5 ]4 } sprintf("Press any key to halt:"); 6 @7 L, z3 e4 w
getch(); " }2 @! P2 F9 w, G
return 0;
( D" q9 l9 l6 C* [2 i* X} $ P/ _$ m5 T- r/ A# M) u2 M. k
! f2 V5 J9 o, X+ M
& j! b& N: G/ J* w/ L& r/ N6 U, { % g' g5 F' F/ k, \& o
函数名: clreol
/ ^1 z/ i) U j8 I5 n8 g: N功 能: 在文本窗口中清除字符到行末 : j: ~/ C/ x, ?& a( l- N- C6 T0 Q
用 法: void clreol(void);
5 d p% R% ~$ n9 k( A I3 C程序例:
3 D, S' l0 N! l% @! R#include ) Q2 L* M6 I* P
int main(void) 8 k& p8 u8 }" Y0 S$ P
{ ( C x. I" H* w8 j2 b5 _) Y
clrscr();
3 n0 w" R U5 ecprintf("The function CLREOL clears all characters from the\r\n"); * |) T6 I4 L% p. O) ^. u( N
cprintf("cursor position to the end of the line within the\r\n");
7 K. n a6 j; Tcprintf("current text window, without moving the cursor.\r\n");
2 i! @& Q8 w1 |4 Ucprintf("Press any key to continue . . .");
: U% |% W' t- C2 K! hgotoxy(14, 4);
) \2 c5 k4 h6 N: k) k! rgetch();
. _# c' g6 b R3 y3 Nclreol();
5 E1 N; y1 M. k/ Rgetch(); 5 k/ ^% G7 Z/ k2 J
return 0; , a% U' T& R/ ]8 H* h3 h
} # V# t8 _; y7 O8 o/ @( ]5 n* b
. O7 V0 |6 z# P
5 ?3 [9 {! e6 [; I8 [8 y( Y; ?& ~
' ?6 n& s" r) c% q/ _+ s函数名: clrscr
9 i' O2 w: N" L功 能: 清除文本模式窗口
/ Q: e" ~( F3 N4 C5 D4 N, P用 法: void clrscr(void); * k C' D, l+ D7 D
程序例:
, V( P, Z/ P U' h( J3 p8 n( a#include 5 A8 e3 d3 \4 X% I( V
int main(void) 5 X& r, I0 Q/ T& a7 e
{
/ p- N3 o/ V3 b9 g3 i% {4 w8 Wint i;
6 l) K3 v+ M, W3 T8 O0 [3 @8 E' Aclrscr();
2 Z# j' R/ _5 ^) B$ J nfor (i = 0; i < 20; i++)
: M7 S& ? M2 T7 c3 R* bcprintf("%d\r\n", i); ( P) F4 Z9 W) s3 a% ?
cprintf("\r\nPress any key to clear screen"); Z! t+ X u. U2 [ l1 B4 E
getch();
4 Q: G2 u% i- I* Eclrscr();
: ]/ J5 p/ U4 w1 R/ T, icprintf("The screen has been cleared!"); 4 m3 m0 F5 o: i, \( _
getch(); 8 ]' Y; ^& I; ?' T4 o$ O# a
return 0;
' E4 n$ A# O7 _3 ^5 }: N4 \}
: E$ C9 |- {, M+ {6 v8 j
+ z6 P" e( b! G1 T9 T! c" T) b% w* A. o7 ^
% X9 B' V0 K% u$ L. S3 z函数名: coreleft
; I2 ]/ @8 v7 P/ {- R功 能: 返回未使用内存的大小
( b. Z1 w7 U! R9 @! j0 P6 T* s用 法: unsigned coreleft(void);
. k8 T1 A8 W( Q8 m& Z2 z8 u程序例: ! D; G/ y* w6 m* \+ P
#include + I. @, B) r Y5 P, I! |2 J
#include 5 y- u' O/ m% t R9 b, e- k
int main(void) / c) A$ u4 O- F4 Z
{
3 [. x3 b( N1 g. U$ @! ]/ nprintf("The difference between the highest allocated block and\n");
* [4 N* v) N Y) `6 Aprintf("the top of the heap is: %lu bytes\n", (unsigned long) coreleft());
( ]4 A# t2 i# ?, X# m6 O. }return 0; 6 ?1 v5 k- k% a$ c5 v0 z
} 4 F( c. t6 l8 G. k$ q
8 W2 Y- R6 ^# M$ W, ~函数名: cos
) w* C' w" S- D4 {功 能: 余弦函数 5 O! A; L8 S$ @ D# p
用 法: double cos(double x); & @+ ]' V" y6 G- j
程序例: # G) H- x7 ?8 U% l" {
#include
0 x' {2 Y* j; f#include
# |+ {$ G! e a2 u% F, Aint main(void) : y0 Y d& V+ l) H4 |! m& d
{ ! b& p8 D E2 n7 i
double result; # I, ~! ?8 F6 t+ d% }8 x
double x = 0.5; 6 f0 D4 u/ n8 M: U$ q/ T1 _
result = cos(x); 9 a) [* f4 c6 m% h! z. _, T
printf("The cosine of %lf is %lf\n", x, result); / @0 A0 S) N/ ~) C2 U
return 0;
: _6 M, `( d7 j; B. d}
* g% v- W4 J( P; ~ i1 t: k2 U! j6 N7 F$ j O4 y
/ B; q. \+ ]$ s
9 Z$ R7 K( x# {6 A' a
函数名: cosh 4 a! R, V$ k8 v4 J" ?3 a5 {( T
功 能: 双曲余弦函数 3 y, z# x1 y9 G% _# B
用 法: dluble cosh(double x); % }3 A& l; i; R1 b" K8 ~/ n- q
程序例: - P: B9 T& P0 @
#include
/ j1 i4 z) _' f- A& r7 o#include
]" p y r; aint main(void) # ~7 g' q% Y9 D) j; B% s0 ?
{
$ g y4 o- [ G F3 w' }double result;
# N I8 @/ i: q* {double x = 0.5; / c! m" _- }, M4 r& H9 E
result = cosh(x); ( S; J2 N! F6 E& f+ o! K' e
printf("The hyperboic cosine of %lf is %lf\n", x, result); # T8 {& ~% N) v; k' }3 C
return 0;
& ]" V6 g2 X' T) x" Z: e. ?}
. T( s# c# b% \% L. [
9 Y4 c% @! j. n- K, |' k
* E* W T6 f6 @6 a % E# G- K( n( B: F; ]$ \8 P
函数名: country / b4 P" O" ^7 n& O/ a2 G
功 能: 返回与国家有关的信息
' z" k; [& E; Z( e$ [用 法: struct COUNTRY *country(int countrycode, struct country *country);
( c5 \2 j& B {$ |程序例:
0 M' r& z$ y( g& ?#include
% d+ c% p2 u7 s K% }. u#include
. R' {5 d9 m1 B* F2 P3 M8 p9 q#define USA 0 5 c. R- V2 {2 m( T7 D0 W# \ i
int main(void)
4 Q6 s0 K. n/ G; \{
* _# ?+ h9 t, V# c; Dstruct COUNTRY country_info;
; ]/ T7 a" i6 m# ]' Z; W" Lcountry(USA, &country_info);
( h0 ]+ B5 {0 S, p0 T# _printf("The currency symbol for the USA is: %s\n", ) v8 B8 T( p! l. `1 B; [) \3 h$ d
country_info.co_curr);
& f8 I6 e. q, W. Creturn 0; 9 }/ i& [4 y8 Z4 v1 S9 e& B/ ~
}
O& ]! r, v# b& v4 d! a7 C* v
; |& p- \/ z3 Q3 T6 o9 Q& t, d7 @
( D2 _# K3 O+ X; O 1 u) o9 Q+ O# m, ~9 e
函数名: cprintf , s0 ]) B$ l- F$ J8 B' d2 n# Y
功 能: 送格式化输出至屏幕 $ d/ s* M& l& X8 @+ I
用 法: int cprintf(const char *format[, argument, ...]); ) O6 d3 m1 c7 y# C
程序例:
8 | q. U9 T, A6 W* o8 }: g#include 9 r2 O4 [3 e+ W' y. m+ B3 m+ G
int main(void) 3 d+ g2 c5 H4 U% W- T
{ ! J% \- ^& k! h! I' e
/* clear the screen */
D( Z; S) P( Fclrscr(); - a' x* I5 u' w# T1 }
/* create a text window */ 1 P: Q+ {. e4 K
window(10, 10, 80, 25); , B7 b$ m* D8 f9 L- w6 i2 ?
/* output some text in the window */
% ` Z M, ~* }$ W# Wcprintf("Hello world\r\n"); # T+ g; _' R$ [0 F0 z5 o2 m2 ^
/* wait for a key */ 5 _0 K+ C2 Y- N, ~3 a
getch();
( Z, f! c# \1 _9 zreturn 0; 7 ^9 W& a7 r5 F
}
5 z0 C: s2 z3 M0 J5 N' p: D0 {: O7 o+ D- }* w/ t9 |! H3 N
* G4 \7 {" P+ a( S$ Z' W, N+ ^ 9 {7 J' v3 M: q, w5 {
函数名: cputs ! a, R9 z/ i$ f6 y6 z0 }7 q3 F# }
功 能: 写字符到屏幕 . o8 i1 H7 a% c" L
用 法: void cputs(const char *string); * O" @0 q* N+ d2 u. Z, q1 @6 d* M# M
程序例: $ t# ?) h& p: E# n% K
#include ! k+ o- w) O4 w. m/ ~# G( s4 f
int main(void)
- B: ~* z }2 z9 k. Q$ |2 g{ 7 T# v/ u- b6 H4 ` O% @
/* clear the screen */ - l. v( @' {- C
clrscr(); ; A6 |) t& y- t4 y# G- s
/* create a text window */ 6 x: N! ^. w1 P6 t' J
window(10, 10, 80, 25); 6 Y9 U7 @; k0 n( z4 D2 L
/* output some text in the window */ : q6 f0 \! O2 K2 t- t9 e: A* z0 Q' k; f
cputs("This is within the window\r\n");
" ~. u" P# C. J- _/* wait for a key */
5 ~. {8 Q" ^! r# B' J; S) o9 n1 Ggetch();
9 s( u2 |" C' ^2 freturn 0; ) i8 j5 ]: d9 }- M% |! Z
}
: W2 T1 Y. A1 V1 w5 J0 y
0 g" ?8 n/ }" }/ x9 g$ X
# e7 Y5 `+ @9 E# k, `1 p
3 p) C; P9 ^8 T% w) `函数名: _creat creat
" J o/ m1 m. Z; i+ z功 能: 创建一个新文件或重写一个已存在的文件 . M$ d _1 Z6 l! B3 H3 D/ U* f
用 法: int creat (const char *filename, int permiss);
3 x. E% t; T: A% m1 X+ E( E# t程序例:
3 \/ ^& W. z4 C#include
& G/ _* M- Y0 L" {( n#include
* q" S% f+ h# Q0 f! J3 o3 f( a0 f/ W#include
# _7 k( |& H: U! S: k#include ' p ?: V) u* _5 H3 z* s* b
int main(void) 5 z4 ^* |! ]9 z$ }# _, G
{ - S% X0 f( A8 u, n
int handle; - W" |+ h: u& d( ^' i: v* T( M2 c
char buf[11] = "0123456789";
$ ]3 k: ?$ g* j( I/* change the default file mode from text to binary */ ' z+ Z I) v5 R; ^' I
_fmode = O_BINARY; ' I% y' N1 c2 L, }/ O
/* create a binary file for reading and writing */
2 x7 s. y2 m, g1 m0 w1 @handle = creat("DUMMY.FIL", S_IREAD | S_IWRITE);
& C8 v: f1 f3 p. L2 i/* write 10 bytes to the file */ 4 U* D" Q% y, S* [
write(handle, buf, strlen(buf)); 9 E3 F z0 c9 q0 f* c6 j) F
/* close the file */
- O0 f$ O: v$ l$ ]close(handle); 6 s& A1 _: W$ X6 F- q9 r+ i
return 0; 1 A6 \* ]) p* y- I/ ?, n
} ; N+ h( W* j" `# H7 b% a
- ~7 ~# B# T- [& L; z3 _. N7 k
函数名: creatnew ! L1 Q# H' U8 e- L1 M2 v
功 能: 创建一个新文件
4 u/ k, i+ D6 r3 T: V: Y: l用 法: int creatnew(const char *filename, int attrib);
/ c. L6 i' l+ O程序例:
5 l/ X% C) @3 |% S4 R- A! a8 l: g#include ! J% [% X7 u7 P
#include 4 K2 t) G# }4 p' \ J& R! N
#include 0 M$ ?2 ]5 `- X* k; |% Y1 K
#include
1 x; v; N; y8 D# ~) @$ S& }#include , k# L* L6 D. f, j4 a% S5 S6 ]2 f, X7 }
int main(void)
6 x0 \" V& B; V+ j# z{
3 [- [, Q+ U+ j% Aint handle; L5 [% Y% m J8 y# n* q2 z4 ~
char buf[11] = "0123456789";
* h/ a+ }- p( Q+ E: W1 E3 N ?/* attempt to create a file that doesn't already exist */
2 Z& @ J" c0 _0 Y3 B1 Chandle = creatnew("DUMMY.FIL", 0);
) a: [( j3 E5 y$ f7 @* h$ Z- V3 fif (handle == -1)
0 v* r+ k; B1 t: x. ?printf("DUMMY.FIL already exists.\n");
& _4 p' i; ^8 @: ielse
; S6 H, V$ T2 X{
; B6 R1 G7 l7 G, b }8 {printf("DUMMY.FIL successfully created.\n");
' b! n: e1 @# P& H# ~* Nwrite(handle, buf, strlen(buf));
# E4 v+ y4 }2 u$ ]1 `4 {5 W/ |4 s- w) x/ ]close(handle); 8 o4 P; r( j8 k
} 7 @0 `5 @1 A* P: E) c3 Q9 q
return 0; 5 _# w n. Y5 D/ p1 E; `$ j
}
7 o' v6 X; x2 Q9 D1 ^6 q k
v; t9 U6 ^7 [
' i8 r0 M' R f/ M: U0 g. w _2 B 1 Q- _2 Q% D) X0 R; H; Y
函数名: creattemp
% p( ~- i# ^; B& t9 `' m功 能: 创建一个新文件或重写一个已存在的文件 ; Q9 ]3 ?. k1 n' ^# e- |( ~
用 法: int creattemp(const char *filename, int attrib);
+ C" |6 ~. Y& H4 ^& y4 J程序例:
- H. U; c! E' M8 ]#include
0 c* ?) k# R/ l& y/ _' r0 e. S#include 4 {, I5 p8 Z: B8 a" L! |: ~& P4 Q. V
#include
6 I, d3 X) L. {$ K2 N5 `+ u7 f, Iint main(void) " k# I. s/ y5 b" {! ^
{
! ^ a4 C! i4 U! q/ G, Zint handle; / n2 l2 y1 _: y
char pathname[128]; & F- S! P0 v# G7 D
strcpy(pathname, "\\");
7 O; c8 T7 t5 A4 W+ p: m/ H/* create a unique file in the root directory */ 7 J' }' i2 }$ m, G* T& x. Z) \
handle = creattemp(pathname, 0);
2 [# G8 t- s7 u/ [7 Eprintf("%s was the unique file created.\n", pathname); ) Z* ]4 _' P/ j8 U5 G/ h% t
close(handle);
; M; r+ i! `3 G/ J% g! kreturn 0; ! r) |! ]" `8 r1 z* m1 c
} : H) p6 i; t* o4 L+ E. X
" k# i- i& [& A/ h: v- _5 J7 ~) {
# Q( H7 ]. n! g8 D( t7 Z3 Y Q
+ Z$ _; m4 ?. c, p- a函数名: cscanf ! E; u3 V+ M4 }# ~6 R' F0 L
功 能: 从控制台执行格式化输入
1 l& M4 t; M) O) m/ l用 法: int cscanf(char *format[,argument, ...]); 7 o8 Y+ ~& [$ q# v: z
程序例: ; U) A5 [6 _" L" W7 T) g% Z, X1 W
#include # h* j, U* ^; h D/ F
int main(void) ; p% c# j, d. p1 w* m2 c
{
. h7 \" C; g0 Ochar string[80]; / D5 s9 W8 q1 S+ O5 L) A
/* clear the screen */ : K$ e7 a, o1 M, ^8 K7 O
clrscr();
# }: ^; B' k$ l: q3 Q/* Prompt the user for input */
0 T6 j, q% O: x( I Q% H' }cprintf("Enter a string with no spaces:"); 6 c" g0 \$ \. L7 X3 z
/* read the input */ # Y; K' f9 C5 [" f7 K
cscanf("%s", string); # L4 |7 S- V- m0 y" v; @6 h
/* display what was read */ / T) D& U4 e s0 {4 z
cprintf("\r\nThe string entered is: %s", string);
& D* d! K. T' d* z# G/ }1 sreturn 0;
% r0 E* `6 d. x! _& C' O/ _: f} $ Y! [% q0 I$ c# i5 x7 d* t) R( v1 K) `
8 P2 s* R3 T! w2 M. b" u$ V. X/ _( N7 ^$ L2 h/ W @
+ X/ f, l/ a& L' k8 p函数名: ctime , B7 y% O5 k" a& ?' b0 q
功 能: 把日期和时间转换为字符串 % l% u* d$ r2 n4 \2 ]' {
用 法: char *ctime(const time_t *time);
* U6 ]& U; @4 X) W. ^9 G. I程序例: 5 _, t5 [7 I. d( g5 W8 n
#include . E, ~# a' j- S1 s
#include 0 c% E# x5 e$ M; E# p
int main(void) 8 j4 a. Z; r+ g8 M5 L& s
{
; r. D: O' [# G0 A1 ]6 Z9 mtime_t t;
. N! ?$ u7 ]9 }time(&t);
3 y- L# u9 l4 g2 F% nprintf("Today's date and time: %s\n", ctime(&t));
: f& {- n; D# r/ ?& t# w ?return 0; 4 k& p. g1 v- E4 Y9 y& t) X
} 7 N# e) p: Z1 l
( ]" ]2 `9 A+ }- C6 e3 G: u
0 o, Y/ L) y9 W" O 8 w7 e/ L* p1 q3 j( [
函数名: ctrlbrk , @3 c; y4 h; n& q- C8 t
功 能: 设置Ctrl-Break处理程序
: O" G9 f- @: G. Z用 法: void ctrlbrk(*fptr)(void); 2 [) ^( e& t9 t7 b8 ~9 Y; E* D" }
程序例:
l$ V, |5 ]/ }' a3 M#include 9 C$ W+ p; S8 Y9 l/ B7 A
#include 3 Y# g% N8 r# d9 Z8 P8 U \$ E1 T4 {
#define ABORT 0
9 C# N7 T& m9 [! A _int c_break(void)
0 M9 \$ h$ b* u" ~( ]6 A* h{
! s6 J% K% u3 @; [printf("Control-Break pressed. Program aborting ...\n"); ! `5 c' h- ~4 k% I; s9 v' i
return (ABORT); % }! W! o: L, m9 |! q
} # J) j! K8 H. Y( M
int main(void) + ~. z) s: x% h- m
{
m* Q4 @" o9 l# `# l6 n) C% `ctrlbrk(c_break); ( R! k) [, V! M% l4 B
for(;;)
, I, z. N; d: F9 i1 h{
4 h7 I+ Y& m7 T; z. M6 q/ yprintf("Looping... Press to quit:\n");
, E7 N: R. R$ O% u$ C}
! X! D) H0 K- t9 ?& {7 T, ]8 ereturn 0;
. @: z b4 j9 a1 J9 o3 F0 {} |