|
函数大全(c开头)
; i) E) K2 `' k * f# C: T3 I" k( P/ T% t$ H% R) ~
函数名: cabs
# w# d* Z4 L+ }& k4 @. {# \; G功 能: 计算复数的绝对值
* I' ^2 [5 _* Y* t' j4 r* X7 G用 法: double cabs(struct complex z);
! w2 f' ?7 S4 v5 V' g5 C程序例:
0 V+ Y0 T: F6 z7 M5 x+ f& ^#include ; A4 X: ], c5 L
#include
+ t" p9 S# ?9 D$ jint main(void)
2 H8 e% V- u$ g# K5 _! t8 u{
8 ^3 l: t& u" {struct complex z; & h! d" r$ Y+ e1 V( s2 l/ G
double val;
5 V% @& I; O+ }" D7 Gz.x = 2.0; 3 n8 m$ I9 X( k
z.y = 1.0;
7 @" n. p" ^* ]1 [ jval = cabs(z);
; ^) b9 R% X- i/ _# Rprintf("The absolute value of %.2lfi %.2lfj is %.2lf", z.x, z.y, val); ; D7 Z8 [: m/ W( ~2 u2 ]
return 0;
7 @1 O6 ^" Q! {# i# i7 }/ ]$ T}
% V; x7 F5 ^' g. u7 ]3 T7 H- q9 ?& X' _3 z! ~1 A
* [6 G) ?: {$ O' i6 O9 e7 Q2 K
7 j* Y, M# h$ x9 B+ b2 g
函数名: calloc + N h5 Z% X' b, j, i' A
功 能: 分配主存储器
5 _5 F$ t, L7 p用 法: void *calloc(size_t nelem, size_t elsize); $ o0 o2 y" }2 A/ E G1 _& O! R/ ?
程序例: 2 g" Z, o% c/ B% @, k, s, I
#include M$ |: w: X" k- N% n& G
#include
' F/ x8 p& k( O+ \3 S% Vint main(void)
* b/ V' k8 r% p! U& a) F{ ) L8 v d1 ]% K% d7 m
char *str = NULL;
( |3 f: A3 g5 b, r: g/* allocate memory for string */ 8 z" l5 l' R; q5 z9 _" V
str = calloc(10, sizeof(char));
" A+ P: w3 T6 {/* copy "Hello" into string */ ' [) J9 z/ @. A
strcpy(str, "Hello"); 1 Q' V$ o* I& x/ T* g u% c b% u
/* display string */
- y m9 n+ ]! R* W# @ t: _7 [4 Nprintf("String is %s\n", str); ! W8 h3 ^! x S. d; q+ |
/* free memory */ * X" A) m5 h+ o# a
free(str);
2 w+ `- N4 ^4 l1 zreturn 0; ( i: f5 |& G+ z: A1 t. l
}
3 @# ]; x/ }. F+ I" U8 @/ z) j! h; y* a- V4 r
/ \0 S$ s, U2 L# G' U& T
$ p$ Z# v/ }% j: k. Z+ K) O. f函数名: ceil
$ C" Q$ c, _1 y% s; n" \1 F4 W6 Q. a功 能: 向上舍入
/ a- Q3 N" C* F3 s用 法: double ceil(double x); 7 u0 O% d. \0 q
程序例:
# `4 {* r+ k$ B: ~& W#include % N+ h+ f7 S0 [& b- _" C+ Y+ B
#include : q ~* o/ V& R4 [
int main(void) 4 L3 X: R; i3 H0 v# j4 T
{
" _, W, E8 M/ T, Ndouble number = 123.54; ! T* O4 @0 {5 k. g$ h
double down, up;
& I* S2 |# N. c; Rdown = floor(number); ( F7 \* c( {% V6 P$ }6 E) u
up = ceil(number); 0 b' }: u) e) z. i& {) p4 f8 ~5 n
printf("original number %5.2lf\n", number);
4 f- u0 J% J$ g' _2 l( D% uprintf("number rounded down %5.2lf\n", down);
# Q7 T8 c+ q/ H' x$ S1 M# gprintf("number rounded up %5.2lf\n", up); 0 m8 n( V$ ^& F; _
return 0; " ^* W( T+ N* M& f' ]$ p) N
} 0 s! j1 U/ d* H. }
- ^, A% Z. e& q( U9 U* W
6 g( L8 p; Y- R7 x% \) J . }0 S9 {3 j- ?. ?; k
函数名: cgets
! K5 g8 D4 y* q( k5 o& F功 能: 从控制台读字符串
4 e7 ^8 Y0 I- p. Y用 法: char *cgets(char *str); 5 p7 h6 |" @. X3 I
程序例: / a8 f- K' Q$ c8 ]
#include 7 C6 m' j$ L7 b \
#include
5 r t- O- }% M5 _4 I# K* q. xint main(void)
3 i4 B( W3 A0 r. k{
5 _7 S! v2 T8 C, b& I! p3 ^# }5 Gchar buffer[83];
+ C, O. t- Y/ s: r) d, [ Rchar *p;
0 _2 s ]9 e+ b5 i, C9 ~+ k6 O2 L6 Q/* There's space for 80 characters plus the NULL terminator */
! I% c' B- F! i5 m) t; A5 obuffer[0] = 81; + H$ B6 U% o' I8 ?9 U: A( U
printf("Input some chars:");
3 c% R% [+ M. }. f6 Q6 Gp = cgets(buffer); : g6 Y5 G) F7 {0 T3 E
printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p); 7 w/ N' K3 X7 q
printf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer); 8 M) t w) H V2 X& Q3 r4 f
/* Leave room for 5 characters plus the NULL terminator */ 5 t @& C! ^7 M$ {
buffer[0] = 6;
- L0 S8 Z% N: ]+ J4 G; oprintf("Input some chars:");
9 }$ B+ @: g9 P2 a% F$ S) ]p = cgets(buffer); 7 _7 w3 N0 j5 z% C- E2 q$ s! u* q, i
printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p); 3 V( s8 V" U1 X% j' N1 e
printf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer);
: H) u' J5 g Z$ E# }$ A' c( breturn 0;
" f2 U, F( P8 M, E} " _9 H, a9 A0 f) E& C/ C( x
" Y. h2 h- t. @
7 F, E) k. j/ n6 _( Q9 \
5 W. j* ^/ u8 U8 V2 j' v/ j函数名: chdir ) Q7 D5 n' t$ H3 r/ s8 i
功 能: 改变工作目录 % ]: ~# U* ]) j) ?
用 法: int chdir(const char *path); 4 A: e1 P/ H) K3 ~8 J$ K+ S
程序例:
# O2 G7 l7 v/ U: n6 A' |. O#include 7 ^2 K% }9 `# z4 o |) s' |
#include
4 L8 A. ~% l; v% {# H% c* t3 ~7 d#include
8 l- J' Z( c/ p8 T% echar old_dir[MAXDIR]; + I1 C, u" j$ X8 ~, B% W: m8 W
char new_dir[MAXDIR]; ' d8 {2 |2 n! z
int main(void) : F% Z4 _% [. z7 r+ s
{
% ~' Z" _" ]' a- q5 sif (getcurdir(0, old_dir)) 0 l! I+ E) A2 P1 _, H) f$ s) C+ ]
{
( Z' }' R1 u! V, `perror("getcurdir()"); . } \$ U- s* R% q8 r
exit(1);
$ M, x3 a, E0 t2 z}
, e" z) R$ L+ [. W3 j1 G2 wprintf("Current directory is: \\%s\n", old_dir);
2 ^& ?# J; K1 s/ n7 ]if (chdir("\\")) 6 V" \6 @0 O0 V' ~: j+ t
{ 6 }( ^) y: `9 X& J
perror("chdir()"); " j$ B3 p8 o$ {+ \" ~
exit(1);
6 v3 l) x2 Q& M& x6 v( K! J} / d$ D" x% h! D" Y- F; e6 N
if (getcurdir(0, new_dir)) ( B; X4 R! j) n2 a- K8 k7 o
{ / ~2 {" Y3 ~/ u3 H3 V
perror("getcurdir()");
* M! t3 w1 }" R: [$ Bexit(1); ! }4 \) N$ Z7 F
}
; n0 m+ A2 F3 n8 x r2 mprintf("Current directory is now: \\%s\n", new_dir);
0 S' K8 B9 }+ ^6 `- t, M. B9 N1 Qprintf("\nChanging back to orignal directory: \\%s\n", old_dir);
& Q9 l8 W$ S7 r+ gif (chdir(old_dir)) 4 C- ]4 O# x* T+ h1 j
{
7 l, ]; z. R! p' tperror("chdir()");
$ C! c( J+ w/ _4 S% U1 `/ N: aexit(1); 3 K5 d+ }, K7 h! U" C) l5 R
} * S3 p1 q! s3 a$ I' \
return 0; 1 P: b' k: @3 n3 }$ O8 @" B
}
- Z& b' d5 _4 `
+ z: o8 J0 a j% v* @2 F
% O) f: ~# P, A3 O y函数名: _chmod, chmod 5 M8 v7 M. i- \5 M2 U3 m! I
功 能: 改变文件的访问方式
- }4 O( R( n. X) {6 [' j! h用 法: int chmod(const char *filename, int permiss); ( a) M" Q/ Y9 b
程序例: * c0 J' D+ w) ~
#include
: F X$ W; j$ S( G- T/ L0 K/ X#include 1 P8 m4 f* Z$ y: H9 O5 U1 n
#include
E7 l o; q3 ?( b4 g& }void make_read_only(char *filename);
9 u; K4 }4 N* g: x" O% Aint main(void) 3 o" K4 B) \1 O: j8 k
{
/ u% p: B& [, W3 z0 b" }% g |make_read_only("NOTEXIST.FIL");
6 L; M4 F6 Z/ f3 mmake_read_only("MYFILE.FIL"); 0 H5 q& F9 d" B4 q0 ]! F
return 0; % P& B5 `' m+ C' F0 d: s
} " l& G; G2 w& y! n) D1 J8 T# K" ]
void make_read_only(char *filename) - |7 I% j/ [) J* t6 @, L5 C
{
" l) U5 H. [/ s% P+ z5 Q& aint stat; 5 n, K/ f6 `# @9 D+ z7 O u9 u
stat = chmod(filename, S_IREAD);
" j1 w5 e, u. t! @& |if (stat) 3 _) ]5 p" }( h
printf("Couldn't make %s read-only\n", filename); 2 W U' w( j& N$ @$ w
else - c( M* Y' f$ `/ t+ J- ~. j
printf("Made %s read-only\n", filename);
3 h5 t0 N5 U% R; B) L1 l} , ` N, U* Q+ K( A) n. O
3 m) V9 x& {! n0 c& c6 R9 G( \
3 l8 p3 K4 J: z, |: q2 ?
- v' Z# V+ w2 Y% Z5 w函数名: chsize
, I. m6 b# E7 M/ p- ^功 能: 改变文件大小
4 r" D+ v# {: ]( l' T. d* z# C用 法: int chsize(int handle, long size);
8 {# b7 A; H" ` p5 w程序例:
. D; W6 L; o; \" t( Z4 P w#include
" s& o) u. c% B3 L v \#include $ l2 {5 U4 t5 q
#include U5 F3 W" b2 e( T
int main(void)
' l6 Q4 i: k6 j, i2 \{ * Y: r. T9 t3 }, O0 m$ g) t
int handle; 5 q' V* w* q) D6 d* T0 H
char buf[11] = "0123456789"; " A4 w! V( E* _9 Z8 L; K; p
/* create text file containing 10 bytes */
; L5 }. |. \0 } d. r3 N% Qhandle = open("DUMMY.FIL", O_CREAT);
3 q; W5 G# T9 b- n% h( y% ~write(handle, buf, strlen(buf)); ) ^. x- e E/ R
/* truncate the file to 5 bytes in size */ ( D* i! r3 T2 e" z* [8 h" |
chsize(handle, 5); ) B6 Q( s: e, j
/* close the file */
: q' ]+ K- @" Zclose(handle);
6 [' M& i: J' p$ g" _7 C7 Xreturn 0; 7 T8 g2 G4 s3 g. C
} * o% C( x2 N, [! Z7 I
8 V; X. f1 P5 u
, _0 W, N4 f9 q1 k" z% T- Z函数名: circle 9 @3 ~, e4 ]" y2 K+ D0 F3 \$ M9 ]
功 能: 在给定半径以(x, y)为圆心画圆 1 w# N v2 `# V/ q8 ` P e
用 法: void far circle(int x, int y, int radius);
* c1 D/ S* v/ N' Q ?% _8 @. s, ]5 _程序例:
: r3 R7 r( O5 O: u; m#include
! Y' N% \0 z5 v l+ J#include 5 M" [, T o, F% @8 Y0 c1 u
#include
% a3 \6 ?! [# c- G#include 7 C6 N# [1 e& V; g* V* [: I
int main(void)
* K, g# J5 k3 t L( I; ]/ d4 l{ + r$ ~% f& x. |5 {% ?+ O* @
/* request auto detection */ # ]9 S# D( O8 e D8 M
int gdriver = DETECT, gmode, errorcode;
- l4 |7 A' |" ?4 D- q7 d+ bint midx, midy; $ H( x8 Q2 e6 ^# c- I% [
int radius = 100;
( U3 r" q' O6 m" Z2 y/* initialize graphics and local variables */
/ |/ L! y4 v: @# b3 _# r& kinitgraph(&gdriver, &gmode, "");
2 u; `; }2 G/ ^( D/* read result of initialization */ 1 v% c l, u, T5 l# y d' W
errorcode = graphresult();
/ ]4 u- D' }' j& Q' z1 c0 }if (errorcode != grOk) /* an error occurred */ 4 l; l8 h9 Z( @0 R& g# o
{
! B8 N: |! g/ hprintf("Graphics error: %s\n", grapherrormsg(errorcode)); - I) q% I( K* H* L& Z6 J! y" V
printf("Press any key to halt:");
5 @4 p }% }5 w4 c, }! Xgetch();
# g- Y. w* Z; K: _exit(1); /* terminate with an error code */
/ M2 ?+ P2 y3 Y% ]) d8 I} ; I$ g% E" d! f- l( `' Q/ U' G
midx = getmaxx() / 2;
4 D( T/ M9 W+ Z3 o: c8 Umidy = getmaxy() / 2; 4 Q, v3 D& u0 Y+ k3 W- O7 d' h T: S7 A
setcolor(getmaxcolor()); 0 t4 R, i" H" G% c% y- c
/* draw the circle */
! [+ Y1 O1 a6 ^; M7 }& {5 Icircle(midx, midy, radius); % p+ V) f) E/ |9 x
/* clean up */ : L* v, S: A ]. j& n
getch(); ) x1 a' ]! _- ?
closegraph(); , A9 p' K7 @5 P# D
return 0;
2 n4 [2 {/ D- x5 T; c2 m" @5 O: |} / ?, n9 J" W \; F+ I& \
1 r' S1 }, A/ K/ B6 o% z6 T& |! J/ m- e% t
( f1 e& a' |6 A& s$ p, |
函数名: cleardevice , I& u) B! S; b* q
功 能: 清除图形屏幕
& P' ^; d6 m/ i% Q8 [% K; ]! T用 法: void far cleardevice(void);
6 O9 K. q3 v$ j- b* q: k e程序例:
P9 _4 V! {8 H4 [8 ^/ K/ \#include ' L4 o, ]2 w$ k1 [0 X- G. k
#include 6 d) K4 M6 k4 e. F
#include
8 f% T( X! C7 I- T5 W9 V. p#include
* k3 A* _5 ]' u, x/ V/ cint main(void)
; I* |- b4 O$ ~{ ( L- t2 {4 i g0 e' Z: |8 q* F
/* request auto detection */ 1 \1 u$ |6 Z3 p; Q7 K; }
int gdriver = DETECT, gmode, errorcode;
' W. e, V6 E* n6 k0 C" Fint midx, midy;
9 v* j4 p. E5 ? L# ?# b/* initialize graphics and local variables */
! j2 x5 J, x. j: kinitgraph(&gdriver, &gmode, ""); & s& Y) [1 ]- K8 L
/* read result of initialization */
% p; ~( E& Y& c6 X1 H+ @6 uerrorcode = graphresult(); / l1 A+ d7 {% `) L n
if (errorcode != grOk) /* an error occurred */
! O2 }, l- z# G* S0 L{ 7 J8 ~+ ^6 n/ }9 g& T
printf("Graphics error: %s\n", grapherrormsg(errorcode)); ! b. K+ P. j8 C4 I m4 q9 l
printf("Press any key to halt:"); 3 E- |: n% n5 C/ O% l
getch();
7 E: P! b2 U5 Y) J Xexit(1); /* terminate with an error code */ 8 X1 p9 e/ B1 b" e# t
}
h- k y5 a( ]midx = getmaxx() / 2; 0 q+ F# _" t7 U/ {6 u
midy = getmaxy() / 2; $ R5 e: v4 m0 \) _2 s$ |
setcolor(getmaxcolor());
# Y+ {# Q# D7 j& E/* for centering screen messages */ 1 ~+ s4 h. I: w
settextjustify(CENTER_TEXT, CENTER_TEXT); ! f9 Y( l, {; u0 n$ N
/* output a message to the screen */ 0 U9 }8 |" z4 D( u
outtextxy(midx, midy, "press any key to clear the screen:"); 1 R' V$ ~" S8 D: }
/* wait for a key */
4 q7 u9 c3 r' y: }# jgetch();
2 Q& T$ I; c$ c7 ]6 _7 K. {9 e9 N: z- [- C/* clear the screen */ ( }, O& @; L- ^# i5 u9 u0 X. ?
cleardevice(); % N# n0 V7 w* Q4 L. s( K$ {
/* output another message */ 5 x0 q' K: q' V
outtextxy(midx, midy, "press any key to quit:"); ( o" R& K9 |1 J0 \
/* clean up */ . X4 T5 D- r, z; m% s
getch();
2 B& k4 H' t) I! X" Q$ Xclosegraph();
/ z$ o( S. }5 ]3 _! F6 ereturn 0; - X! |; s0 n% b; R6 B5 ^. e$ `
}
6 v8 }5 S; V r, {- n, T; P ^6 d! N8 V- O
2 P6 r4 n/ U; y4 j
4 C7 r. a# ^2 q$ n/ I9 _* P9 \; V
函数名: clearerr
. d: u" g0 Z2 o功 能: 复位错误标志 T4 D/ O' Y2 r# l
用 法:void clearerr(FILE *stream);
0 o L* x+ [' V' b. p ]6 S0 Q程序例:
% [+ Q! O/ h9 a; y2 N#include 3 _" D% }! `) F4 w
int main(void)
( C+ f/ k q/ b/ X- G# O$ {: f{
' s) o( P1 m3 I4 |" }& JFILE *fp; 0 K g6 k7 B; E. ^0 s: A
char ch;
4 t! P2 e8 ?2 b5 u! T/* open a file for writing */
# r" d; p8 f# V# s$ U1 L' P" D; \fp = fopen("DUMMY.FIL", "w");
, O4 x& @1 r g/* force an error condition by attempting to read */ , c* w n% L% ~. e& l
ch = fgetc(fp); ) o3 R2 K, S2 F
printf("%c\n",ch); ! L% c8 \5 d3 s) `6 C! [
if (ferror(fp))
) c' Y% m: k/ h1 I' B/ l. Q{ 9 B9 v! H( J4 {; F
/* display an error message */ 1 f+ ?; ]# c. g9 d0 X8 ^
printf("Error reading from DUMMY.FIL\n");
- `7 R# C& E: w* h1 G/* reset the error and EOF indicators */ ( Z `& |. X! U' P
clearerr(fp);
9 h; i6 D* V* D} $ T- v. g. [+ `5 |7 X1 b6 Z5 ?+ h
fclose(fp);
8 K) H3 i; z8 z( @5 V5 lreturn 0; / b4 G) q6 [5 C+ U: U0 q9 S4 V
} * b0 U4 \5 f e' f) `; Z) Z4 s
' S( ]9 e. ?5 {0 O
1 y a" z7 H, c; `9 A7 u * K) @6 j1 g, s0 g# F
函数名: clearviewport
6 ]# O0 b$ f4 t2 U9 x5 `, d* h$ W功 能: 清除图形视区
1 _* V4 S' s7 y4 w9 V用 法: void far clearviewport(void); . i" R, Q; @! f6 z; m
程序例:
5 i5 X$ ^. x( w; {#include 7 n' w3 f& e2 _; _% O5 ], z
#include ! m; B( _' q: R& l) A6 S# x
#include , |2 W$ x& L/ }, s1 |! e5 @
#include
$ k: E9 _& N- z- x#define CLIP_ON 1 /* activates clipping in viewport */ 2 u3 b/ ~5 T, [0 w ]
int main(void)
; e6 N, c; E! L8 ]/ Y! J{
) U. _ r( c$ B/* request auto detection */ 0 r& l3 N! ^3 P# T/ {& W
int gdriver = DETECT, gmode, errorcode;
, l" J: B/ h, }* X& u8 Zint ht; * `8 x% k% H- a$ k# P8 K) _
/* initialize graphics and local variables */
% w- m- l! ]6 {( Jinitgraph(&gdriver, &gmode, "");
/ M% t5 D$ R/ x/* read result of initialization */
. j1 P. ~% p9 U; j4 \5 Xerrorcode = graphresult();
! S: h I2 z+ a# O9 A! M# jif (errorcode != grOk) /* an error occurred */ 4 \: H; i$ p1 k0 S
{
6 G3 j6 T2 P/ V( X8 N' t K- lprintf("Graphics error: %s\n", grapherrormsg(errorcode));
" J' p# T$ z. b2 I" q0 S1 a4 F0 X6 Mprintf("Press any key to halt:"); ! S% L: ?( w0 M' o- a) G( O
getch();
( o! Y! L0 a7 i" t0 wexit(1); /* terminate with an error code */ / `; a" T; R; Z5 P5 z
} ( c4 u% J& l+ {; Q' J7 W
setcolor(getmaxcolor());
3 A0 L, g& u: bht = textheight("W");
' `1 X1 y- J: ]7 I# p: |/* message in default full-screen viewport */
8 T8 C7 [3 j) E" Q# Mouttextxy(0, 0, "* <-- (0, 0) in default viewport");
. |: U) G- G* w2 Z- B N/* create a smaller viewport */ w- c, t6 w. K
setviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON); * V/ a& c, r; d9 E
/* display some messages */ 0 j3 h* B2 L/ s! N/ {$ e
outtextxy(0, 0, "* <-- (0, 0) in smaller viewport"); 5 `7 p$ F+ v8 T f
outtextxy(0, 2*ht, "Press any key to clear viewport:"); 4 Y% ~6 q% y* D' n
/* wait for a key */
3 l# ~6 U S! C( N! |/ B7 s. q# J8 Wgetch();
; M3 ^2 l4 g) n% d( N/* clear the viewport */ # |# }$ K+ S; S% C# f: v' P
clearviewport();
. r* p* ~) C' s0 N5 L, G7 a9 p/* output another message */
3 L4 K' N- Y: ~outtextxy(0, 0, "Press any key to quit:"); - \. I5 E7 h7 h) L6 z% a
/* clean up */ 8 \* s& q3 o, y2 L+ t
getch(); 4 t) u/ Y' C* I
closegraph(); 7 \) u- O- @5 m4 N; v) O
return 0; 9 |/ n1 r0 \( t% \! X( i- H( A
}
; Q" ~) c4 A% X; z7 C; X
5 [6 @3 N7 Y- r" w$ E4 A4 _
) u( f$ I# C5 _6 L, a; I/ F- f
- u& f/ q* Z6 I; P, P# K' o函数名: _close, close - _4 c1 Q: L/ z( \. c z) G4 i( P# w
功 能: 关闭文件句柄 7 ~8 ^# y) r5 r% @& y+ u' W
用 法: int close(int handle);
- X# d% ^& J1 f7 u$ B w程序例: ' c3 M, n; X( F( o _" b6 Y+ P: \# N
#include
: C% e. F1 ]- f4 q: h5 B) s#include 2 n% d( w5 ]9 _
#include
" M; L, o; y( u% B5 B+ m#include
: G) ~$ E! @+ ]' g3 Ymain()
" H0 \ c4 x% L, b ?# Z4 O9 H{
! `" P. P# ~# Z/ R% ~int handle; 3 N4 C# `8 ]+ }, c6 _# [
char buf[11] = "0123456789";
7 z; Y- E! R# e8 _( m: P6 i/* create a file containing 10 bytes */ " m8 _ y5 m3 V5 T" H' d3 P
handle = open("NEW.FIL", O_CREAT);
6 n2 w( \1 e7 G2 aif (handle > -1)
J- a; [# V, j7 D, N0 e: t2 j, H8 ?{ 1 |" X! a1 [* U- l2 Q
write(handle, buf, strlen(buf));
3 F i- U. I1 E$ F/ h) Y1 Q9 ^8 k/* close the file */
) p g: c. i. g: Xclose(handle); 0 {1 q0 Q' e, R }
} . A0 g: G1 D/ m3 m, y( |0 ]
else
; a! r2 D- t( H: U{
* T3 x6 E( x0 u" T- \printf("Error opening file\n"); + C. f2 a4 Q( p( O
}
% H: f a9 W" L2 j7 i, ?' nreturn 0;
! J, i; }5 v6 O2 g" d2 X} 7 v% M1 Q8 U9 ]' y) B( a8 X
9 }# S$ ?% R9 M; f7 t
7 a- w: r( M: {. n T% H: Z
( ~2 k4 b) I! b6 \9 u$ ]0 \( @+ K函数名: clock
1 h$ M/ y) `( O% @* z5 D1 F, T功 能: 确定处理器时间
- s2 t* Y! z3 d6 ^用 法: clock_t clock(void);
- h6 n- D D* |程序例: . `$ n: o4 D: p
#include 6 U7 e+ q4 U! |$ s
#include
1 l! M% m" F Z0 y0 h7 W# W/ b. e, [#include
9 ^# t6 Y2 c, t: ]" Rint main(void)
7 j8 v/ O! u- f{ " X6 m8 b6 R% r. s! X+ [
clock_t start, end;
0 d B. a9 ^7 F9 j$ v; Ostart = clock(); , {1 @7 n; @1 \
delay(2000); % t7 S) v0 u8 e, _& d& ]
end = clock(); 0 K# g7 b9 _# k3 A! L' z* W* t5 s
printf("The time was: %f\n", (end - start) / CLK_TCK); / e' t4 L( y- K$ g6 d
return 0; 7 Z! K4 i7 `$ p4 M& Y
}
5 j- i+ @0 J# F$ m3 y5 J6 k( j% L& N6 @2 F
, G) i. F# ~- m3 T( d: F
+ w2 c, m: z( t( N
函数名: closegraph . B# ^8 y) V7 d3 g
功 能: 关闭图形系统
$ P% {$ v* I- q9 z! o: l( C& y" \用 法: void far closegraph(void); 7 e4 ~4 |* m" }( B' @# y5 C0 ] q
程序例:
' r/ k0 k& ?- P6 z9 r#include 3 I6 H. B1 F8 k+ T5 G5 _1 o) d% O1 M
#include
2 S$ _$ |% y8 j- A#include * o) T/ W: W1 K/ }6 H P+ C
#include 6 c# ]5 ^+ ?2 o
int main(void) * w- v. Z Y4 A( x
{
# [* t' V+ U6 ]/* request auto detection */
6 d4 m) ~4 v/ R' Y2 Sint gdriver = DETECT, gmode, errorcode; ; h, Z! [: Y% w9 M8 o
int x, y;
6 l* v+ _9 |2 u" ^5 X/* initialize graphics mode */
. K+ D( f& ~/ }" Pinitgraph(&gdriver, &gmode, "");
`* d$ s- }3 I7 h9 x4 m. A/ x. Z3 W" E/* read result of initialization */ 0 c" X9 Y ~4 E, t! T( T
errorcode = graphresult(); 7 }3 j9 V) j! u B7 k1 J
if (errorcode != grOk) /* an error
$ {7 A8 {' Q! X& c# p6 u% n* Woccurred */
l4 _3 G: V$ [( n- t7 ^* Q{
6 P( A, M3 U4 H3 L! `' pprintf("Graphics error: %s\n", grapherrormsg(errorcode)); 8 P/ ^9 U! b2 }2 u9 ]
printf("Press any key to halt:");
( s5 Q. p d( H& t T2 Igetch();
! x. i# J3 O$ Z0 b4 ~exit(1); /* terminate with an error code */ / m( \1 D. ~! G0 L5 ^( \+ A9 p
} , o* S9 j: y; d# ?- x/ d6 @
x = getmaxx() / 2; ! m% L# l8 f1 ~$ `
y = getmaxy() / 2;
( O! T6 n5 H/ @. U! u* b3 b d5 p/* output a message */
% [' i( e! S1 b$ f4 A" Bsettextjustify(CENTER_TEXT, CENTER_TEXT); 5 @+ {+ j1 k+ A
outtextxy(x, y, "Press a key to close the graphics system:");
8 R1 {4 A: G% A3 _/* wait for a key */
; {4 |4 j8 h, E9 _getch();
4 c/ Q! L" B/ b* |8 ~/* closes down the graphics system */
5 j% x* V" X1 L3 d7 Nclosegraph();
1 X( c/ i v0 r! |* i6 X% ^. z8 Xprintf("We're now back in text mode.\n"); % `2 n* y- y4 H2 E
printf("Press any key to halt:"); - `+ J9 U) V. L- M) B9 N0 q" k; ]8 p
getch();
) {' ^6 e- V5 D0 [( P/ xreturn 0;
+ ?# k& \- c' z; ^. W} 8 K: L3 u. p7 b: \
9 W8 Z. H: V: y# _6 f- _" l4 U
+ ^) t" T4 G# T- I+ s 0 n6 Y* U7 G M$ t9 r
函数名: clreol
- }4 H* a8 F0 g1 R5 I功 能: 在文本窗口中清除字符到行末 . [4 t1 \( H' V0 ^/ e
用 法: void clreol(void);
6 {- \' [0 h: f# s程序例:
. y6 B! ?1 t2 P#include
. E, {' U- C, x) E/ Qint main(void) 0 b' E( G/ i+ I% U
{ 7 S* K, v4 V+ {( f: r, K
clrscr();
* \# q; |' B# W# ?5 j2 r: E( rcprintf("The function CLREOL clears all characters from the\r\n"); 6 G+ w4 |4 k% I- k6 B
cprintf("cursor position to the end of the line within the\r\n"); ' A- e; o# {8 B. A* S
cprintf("current text window, without moving the cursor.\r\n");
& x0 G. ^4 s: Y+ e9 j9 ?# W: Lcprintf("Press any key to continue . . ."); * Y& x; ~" u# f- r) W
gotoxy(14, 4); ( [, b/ K5 i9 R" y8 Q3 q5 @- M2 v
getch(); @; I+ U' {( d" p8 ~7 h/ M
clreol(); 8 I" P' O; B: T! E7 G* Y
getch();
4 l5 k: t& `$ o2 h! Oreturn 0; # D) a# a+ ^* G+ U& f% ~
} 1 L8 ]3 Z( Z+ l1 H
$ P1 R& E8 Q) T6 E7 _5 q7 B
4 V8 s3 S4 y8 Q' `
' U. O; D0 [' u函数名: clrscr / L4 u! ?1 d7 h- f
功 能: 清除文本模式窗口 & }$ K% M) p. c9 Z- n
用 法: void clrscr(void);
) A+ D) n8 U7 Y& U# e V程序例: ?, l8 [) |8 U: m6 \1 K% U
#include 0 ~: _6 [( H! X# D" g
int main(void)
, h6 L0 k" i9 g* y+ C{
+ C$ ~( H% f+ p( Sint i; , B8 Y5 h- H$ f
clrscr(); 1 h# \, w+ E+ E5 M$ k
for (i = 0; i < 20; i++)
% P4 j% p! t$ F2 Lcprintf("%d\r\n", i); ; X) ]0 X7 D( y8 \1 {3 l- Y+ m
cprintf("\r\nPress any key to clear screen"); 3 k3 ?# V8 s" @7 Z* _( ^
getch(); ! E l. ^! K; a E
clrscr();
+ u' v5 a4 ?( U: o* t1 J& Rcprintf("The screen has been cleared!");
4 X4 z( Y2 l- c: ?* C% i( J) |, Egetch();
0 [* d f" T0 _" o& Oreturn 0;
7 w' o/ g, |& }' r& }}
8 x7 ]9 `* ?/ \4 W1 P/ `( F
+ t! c! Y2 }* W( ?4 D4 v
. \5 ~0 x* ] W" D* j ) ^6 s/ c4 K; O, A, }/ ]
函数名: coreleft 9 p- h9 {5 E! Z) Z3 s- D, g
功 能: 返回未使用内存的大小
1 l4 j ?8 ^0 P) J) v5 O" \用 法: unsigned coreleft(void); 1 |3 O/ I0 Y4 U. N! t, B
程序例:
* T7 d) L+ N6 D3 }/ o S#include , n0 n- @# I, `: z1 M/ B0 L
#include
; n& H. M6 @0 \6 uint main(void)
& k6 Z; N5 m# H0 m1 j: r{ / K6 S' w2 X9 o7 `, F
printf("The difference between the highest allocated block and\n"); - N1 Y) r+ ]1 T
printf("the top of the heap is: %lu bytes\n", (unsigned long) coreleft());
]9 v: |% m4 }3 ^5 Xreturn 0; I5 d3 n9 J! A) q, b+ Z0 J
} : E* v! K7 J, v1 C$ n
- m+ u! @4 L6 U- Z函数名: cos
& R( D1 M- l0 _: |) y" r功 能: 余弦函数 4 s Z% b' `3 r: c8 Z* K
用 法: double cos(double x);
$ Y' D' }) Z$ c; d) T程序例:
! O& S* ?# c7 n, A; o9 n#include - r; d! P# E8 y9 o& v0 N
#include ) M" m7 l6 Q! `
int main(void)
, t9 b; @/ @% N) R{ . i( u+ M9 Y( U9 d& i" @' |) u4 l
double result;
$ I8 t- h n0 @$ sdouble x = 0.5;
/ V) @- O. }# ]2 Bresult = cos(x); ' I$ l, ~- N" s" Q' H! ~
printf("The cosine of %lf is %lf\n", x, result);
" A0 ~5 m( O" B- ireturn 0; % Q/ X% e+ l' _+ S" ~( }
}
h' m% C" T: F1 m/ v$ G0 X
; M. ?1 O7 p! a
! j, h0 k2 X! q% s5 t5 ` + d3 g& e, L4 Z4 i: A
函数名: cosh " C# m6 I8 H; ?: n* m
功 能: 双曲余弦函数
# o3 E: Q* Y: @0 Q用 法: dluble cosh(double x);
% v9 U% |' C, \, Q- O, }程序例:
) |! ]+ h$ H) Q8 ^% N/ `, t) j* r#include
2 o) w9 m" d/ K" O# p7 S$ i#include
, G8 c) U/ E% y+ T& w- r3 fint main(void)
' k6 ~9 v7 j& b/ j) f{ , g7 k6 \! i- [0 I6 _' k- O
double result;
+ q9 t5 K+ ^6 v" l+ Q1 Tdouble x = 0.5; , F! e8 c' p' y% I: @' p
result = cosh(x);
% k, d- w2 v" hprintf("The hyperboic cosine of %lf is %lf\n", x, result); ! o( ]# G& u5 W( b
return 0;
: X, Z* A4 B3 ?} 8 h/ ]; B& t4 D
: _4 }& G1 N* H- ]/ r' Y, W7 F: ^' z; U8 r- C& I3 c5 K
6 u0 {% s# L. Z g0 J, Q- w函数名: country ) A% j `; H0 X% L: N5 `
功 能: 返回与国家有关的信息 # X& L1 ~$ ~) U5 v
用 法: struct COUNTRY *country(int countrycode, struct country *country);
, A u7 m7 Z4 o2 V# ~- h4 r. e程序例:
q+ q) G5 ]9 K: p* ~. y#include
r- v9 k0 V r+ f#include 8 X) _+ O2 a# D; Q$ M( x( G
#define USA 0
- R- x% m: Q& v5 S# qint main(void)
5 [+ W; b% i1 Y3 t{
7 u" m# T! z2 R7 T3 ustruct COUNTRY country_info; ; r3 V- E1 ]6 X) O: L& t
country(USA, &country_info);
3 V) ]1 |4 ]5 j. N4 c* Qprintf("The currency symbol for the USA is: %s\n",
' y1 W" {* L, C! s0 ]7 t1 s( Scountry_info.co_curr); ( F* ?3 C# l! b1 E6 ^7 e# Y1 @0 e
return 0;
8 Z N: ~. x! i# Y}
; J5 s6 t5 K. r) S0 t/ V' V& y9 o5 o b0 R3 V
3 J ?0 e! m' }& p. c
% N# k; I- O+ E5 E) w9 |函数名: cprintf 7 ~* [% |9 g- W* j
功 能: 送格式化输出至屏幕
2 l, }6 N4 u& B用 法: int cprintf(const char *format[, argument, ...]);
. K2 U |$ p2 e$ ~% d$ Y- Z% v程序例:
( v1 Q/ y, w! g* s# H#include
& C. ~# O: g& bint main(void)
3 Z% L/ n; [# y{
) Q$ o3 X* a. D4 |+ F/* clear the screen */
+ R# y; s& y! e5 P, ]$ dclrscr(); 6 X" G$ @ |8 K& f
/* create a text window */
6 v8 N8 l2 I. K1 Twindow(10, 10, 80, 25); ; r$ Y1 k) x! L% S
/* output some text in the window */
" k% S3 A$ c% E6 @cprintf("Hello world\r\n"); + q0 U4 }. T% r
/* wait for a key */
, p: u' X8 V! J( Ggetch();
6 Y) |/ \' G( q7 q4 x$ L, Rreturn 0;
' Z' _) |- \! ~2 a1 }/ I& v5 v}
# T0 T4 l& ?, N2 y. `/ c) Z* \! }
: E" I' `8 }" `' `5 @# b2 e5 l6 J2 a! ]
9 M& S0 F3 Z( z0 M函数名: cputs
9 ^5 `! X% ?8 n: Y0 ]0 x功 能: 写字符到屏幕 % _4 p- ~3 r5 P V
用 法: void cputs(const char *string);
# r" ~3 x) M6 O: e2 t$ b程序例:
6 m3 u1 B9 _) d0 g% r8 L( A2 }#include ! A8 m- v& u6 P5 L' D
int main(void) % T: w, M1 }/ d- q! F. G5 s1 I
{
1 `0 H$ g6 @2 Q7 w$ Z' D/* clear the screen */ + `2 p$ K& G2 F& D* w/ T6 h4 [
clrscr();
, L4 i- h$ Z! `9 |/* create a text window */ # j! \, I7 u1 r1 [7 T, e) V" b
window(10, 10, 80, 25);
2 z/ c( m# z5 J3 B* b5 h X$ b/* output some text in the window */ * |1 ^! V# g/ [
cputs("This is within the window\r\n"); ! k) H3 M. w2 V# G9 S
/* wait for a key */
+ t7 J8 E# W# ^, Pgetch(); 6 |# Q0 F2 m# r2 n
return 0; 3 a9 \0 s& l- S; G
}
! ?7 f7 k& d+ X6 R: a1 w, O. G) Y) q4 g
) `$ F5 c! Z, b7 _8 y: O' ]$ p6 D 3 C5 K: F4 m8 Q; E0 w) O
函数名: _creat creat
- f( H3 e3 A$ f y/ L! r Q功 能: 创建一个新文件或重写一个已存在的文件 ; m- W3 U9 S' X6 B5 g# l! l2 s
用 法: int creat (const char *filename, int permiss); 1 n! @: \' L. ?! l3 t- f# U1 {! F
程序例:
! E; K* M' G a#include " N' u; s8 \% @! k
#include
0 R/ `' s3 Z+ ~3 U#include
) o" t5 f$ Q& k _% H#include 8 s+ d6 x1 l' R% K7 f5 ~
int main(void)
" D8 F& C$ ^( n( G/ {: q{ 8 M* V! J. ] e0 P3 ?- e/ H4 D
int handle;
. r4 S' Y1 n; @$ U& g3 L0 rchar buf[11] = "0123456789";
0 o) O2 G. M8 S7 Y/* change the default file mode from text to binary */ " u- }! s( T: t+ I4 f) X; \& R
_fmode = O_BINARY;
" G# v) N& a& `0 ?7 y6 y/* create a binary file for reading and writing */
* o7 E9 V; w) f1 c& u: Fhandle = creat("DUMMY.FIL", S_IREAD | S_IWRITE);
2 a! ~+ S1 k: v J! n9 d# J/* write 10 bytes to the file */ & _; m+ |* B7 E: i' g' ~
write(handle, buf, strlen(buf)); 3 k2 P V1 z# y/ u% p
/* close the file */ - V; j# U4 {& E3 ~8 ~
close(handle); 5 k1 K3 R) F$ M" y9 e
return 0; 9 ~0 Q6 S6 o+ ^6 B( S
}
/ x. D) b; e1 U$ F, A
6 h2 f$ m* e8 N7 c; N函数名: creatnew
% l' H' Q6 a, j功 能: 创建一个新文件
$ }6 {7 r: g8 u用 法: int creatnew(const char *filename, int attrib);
) k: \+ U* T# i7 {' i& b程序例:
+ }. g# B- m, k/ L& C5 h# F, F#include 0 @5 {; Q, b4 x0 m) M6 c
#include
% R& n' \1 p _( }* x#include
, h. `( n# ?( q" r( u#include - T+ P7 h& U. ~* [" K) s1 @& f8 {
#include ; b* _2 T6 B( x$ O
int main(void)
* G' I7 [0 N$ ?/ B6 `2 B{ / f z( O' n3 B; ?8 h* s: T
int handle; ( C8 a2 Y" O. h! }
char buf[11] = "0123456789";
" g9 y% S; C) X! Q' X6 j0 M/ n/* attempt to create a file that doesn't already exist */
" |1 |) Z/ `1 s3 Bhandle = creatnew("DUMMY.FIL", 0); ; W* m+ Z) n, w
if (handle == -1)
. P% F o) s4 Rprintf("DUMMY.FIL already exists.\n"); - N( \1 n' Z2 W# p `6 d6 l* c% |' E* A
else . s. F$ \3 W+ e3 p7 U% t8 T6 m
{
' J x2 W' o6 M, m+ K* U2 Jprintf("DUMMY.FIL successfully created.\n");
2 U$ |" r7 u- I, f- F- F" S! R' dwrite(handle, buf, strlen(buf));
- f# U' S7 M# x* ~close(handle); / y& j# P) {+ q z0 e
} 8 H: p* i3 Z9 P$ {! ^
return 0; 4 ?7 y6 R! Y C r! M
}
( S) w9 ?! g2 i! {0 {! X7 s$ L7 O
: r' I$ `( I/ G/ p
4 y: k+ N( U; O1 ]9 u/ e函数名: creattemp . T6 Z- S; o- X/ J/ E# |
功 能: 创建一个新文件或重写一个已存在的文件 3 |" ?* I: A2 F. F# ?7 n( b
用 法: int creattemp(const char *filename, int attrib); 7 T; ^) ^* {( _! ^
程序例: , n- C$ D0 E9 X) W
#include
% E5 k2 b. v! [: y g" @% D#include
6 M, _0 d$ ^$ K- ?* e#include 2 g& p$ J( p* }
int main(void) # o# U) ]9 H) z4 ~1 H8 r5 H: H" h
{
, d) m8 _, O+ F1 \+ w, r# {* B7 x. Gint handle;
4 l5 @ W5 D+ h' W1 ^2 i+ ychar pathname[128];
8 x r5 F/ Q) K' F8 D1 ?" S& Astrcpy(pathname, "\\");
, d& L5 J( h1 h1 `4 Q/* create a unique file in the root directory */
( E- B) _' v; B, Q7 t/ i# {- E( J/ U5 Qhandle = creattemp(pathname, 0);
; d# v! x1 P( }" b2 J3 S+ S4 p7 mprintf("%s was the unique file created.\n", pathname);
. {* I' Q5 C; X1 gclose(handle); ; f) F7 H# ]$ q8 H3 z# t
return 0; # j+ `% u- w5 a
}
/ M. w b/ v$ D3 x& R
& q& ~ l# _- z a' b. l; V' b* ?" o8 M6 U
& U! \3 `9 @4 M3 z5 {, t1 k函数名: cscanf
2 w+ t3 Z; _+ \, T功 能: 从控制台执行格式化输入
# [ F4 `2 @& p9 _, X用 法: int cscanf(char *format[,argument, ...]); 4 \/ F; v# c7 d
程序例: ) ~$ y' d& I) O7 A0 Q( ?) {
#include ' o% E) X( F2 \7 @# c* G1 {
int main(void) * e0 J& g- H& k9 ~3 {
{
% I- n- a3 n( ]) C8 Gchar string[80]; N4 C1 d! n* M7 |4 e- C4 O, Y9 l, v
/* clear the screen */ 4 a- s# U% W- |. F5 N9 e
clrscr(); 2 Q; l% y) |" F$ z s
/* Prompt the user for input */
* l2 }2 T- G7 f% w M, v/ Z) icprintf("Enter a string with no spaces:"); 1 I) T+ \8 j* k7 ~0 `; W9 @8 D
/* read the input */ : L4 b9 `, ~ l) p- I4 r2 g. S& V
cscanf("%s", string); 6 d6 R1 Z- t! N) k
/* display what was read */
% e2 y W: {6 f1 f3 n- p; _: v# n$ gcprintf("\r\nThe string entered is: %s", string);
2 n1 m# G: G% P [return 0; 9 f- c% g! V5 k5 `
} / `$ H w2 v& \0 X* l& z
( [: b. q" I7 Y* W9 |, J: ]3 x
* d- i7 D6 m( M3 e% v 4 @ R' v8 J8 `
函数名: ctime
! Q# Z: ~" R1 P- a9 X功 能: 把日期和时间转换为字符串
. M( e7 T* h" b) H# m+ f用 法: char *ctime(const time_t *time); ( j0 K' c @7 u. _' L! F1 \( d( U
程序例:
7 r* [+ R, y1 ]$ f+ s$ h, F#include
F5 `) x# Y6 M' G4 e9 l#include 0 I# u" T( g1 }9 O: V
int main(void)
t% F g, e4 }- i( I$ Y1 i( A8 _{
. i3 i) _: I" r2 o$ Wtime_t t;
/ Q* M0 R& U7 ]* \8 d( ]% Ktime(&t);
2 T4 C. N6 B9 x: Lprintf("Today's date and time: %s\n", ctime(&t)); 8 ~$ G3 H! O* L- c8 M+ N( w6 [6 P S
return 0; 5 F3 z: n% h" a- n+ y' ?0 h
}
3 H" Q. _- D2 V4 P8 b0 E6 b4 r
' b3 F. H8 [& f% S; ]: G* N! Z8 h* D' ?3 @7 J
: k V# A" u) I& c3 q函数名: ctrlbrk . U" ^6 Y3 i5 O8 A( x
功 能: 设置Ctrl-Break处理程序 " U9 X& l& q" V# i( E. G
用 法: void ctrlbrk(*fptr)(void);
3 T" S; s; s6 T' a) d3 x' K程序例:
o3 D: u5 J+ ^#include 0 h" k- M- |. J1 @$ n
#include
8 a6 P; T1 A& k$ }$ N2 M% b3 C#define ABORT 0 : m5 K' s; \" G" y3 O; `
int c_break(void)
( O6 P! k$ f% [, [4 u{ - i' Q2 v" R1 C- B7 o/ h( P% N
printf("Control-Break pressed. Program aborting ...\n"); ' a2 U# r9 U( l0 {4 p$ q; N
return (ABORT);
( h6 V% w5 b# G" G+ P! {6 o} x9 C8 H$ L- E1 Q, e1 F& ]
int main(void) & `2 R) x8 W! e
{
, J) x0 Y' O, I4 ?ctrlbrk(c_break); $ _; C1 E# M! I9 o( P6 n
for(;;)
+ ]2 V/ m* [- F9 S) s{
8 [( g/ U$ c+ R7 b4 v) Jprintf("Looping... Press to quit:\n"); ' ?% V, q3 [' g# K
}
: R2 t, e f8 S1 q5 j5 nreturn 0; : `& D' e2 x2 N; s$ T/ T* M
} |