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