函数大全(c开头)
. y% }2 f. j( |* p , C6 Y/ B- E: v# g) L' s# h
函数名: cabs
/ O+ @8 e! n. d k- M) A' [: j功 能: 计算复数的绝对值
6 e2 M1 }. r5 [) S" B用 法: double cabs(struct complex z);
' s ~, } q. J8 W程序例: - H Q2 r7 x4 D+ u
#include % q" c& s! d8 C- d9 ?( F: Q' N
#include
! M/ x$ H, N- r' s! v# [int main(void) ( a/ T0 C5 H( v# N, \
{
+ r5 L2 @. `. \3 R1 z8 Hstruct complex z;
0 `* |: z* H' qdouble val;
. H/ u1 p& B$ Nz.x = 2.0; 1 F4 |( |6 d% L) |4 X# G+ f
z.y = 1.0;
Z8 V: Y# z( l* zval = cabs(z); " z7 M; s i* ] y
printf("The absolute value of %.2lfi %.2lfj is %.2lf", z.x, z.y, val);
1 W4 H& E! r$ sreturn 0;
8 \$ B) ^) ]) W} $ ^; @4 ~; q6 V" C& A. F$ D
6 y7 d( [9 n0 d5 N. c4 y- H5 u6 m" |! ~" `5 U& C
7 p3 d$ U4 s: t& Y函数名: calloc
9 f( F* h+ n3 P1 A6 B% g9 l功 能: 分配主存储器
3 ]2 W# i( i: }' Z7 X用 法: void *calloc(size_t nelem, size_t elsize);
+ e4 O+ [& C5 A K程序例:
% S2 V' ~4 j0 l" u#include
7 k1 ]' D, M' O9 L) S#include 0 J! {! b3 H+ H: w5 P6 k$ R q) L+ I: h
int main(void)
2 ?( q' S. |2 M# F1 T, B0 Y* }; ~{
6 b8 r7 O( a% m. p& ^char *str = NULL; 4 ~4 o4 x! k0 S" M- [* B
/* allocate memory for string */
) _0 S. L0 E( w: p4 G" y8 ^str = calloc(10, sizeof(char));
& d* \" W& ^3 T! V. W. R) w3 B$ n/* copy "Hello" into string */
" k9 N. x$ a* Z5 g& E1 k) V9 d' Y! s Ystrcpy(str, "Hello"); 1 g; Y* U5 a* z9 o8 T. B
/* display string */ . ]9 @7 s9 k0 [+ [, Q% X9 Y5 J
printf("String is %s\n", str);
% ]0 {; G. ~; n/* free memory */ 5 q& z8 p& |/ r
free(str);
2 ~* O5 s, @" ^" ^9 W& Rreturn 0;
, o) q/ K s& m4 I! u. Y7 W3 g}
9 D1 ^, n: L: i6 e1 w4 {/ A2 V3 J' {, r
0 K D3 |0 U/ ^ $ ]! G5 A# u/ Z) c/ h
函数名: ceil 7 n: n6 A% w+ i- O: {
功 能: 向上舍入 # ~6 y" Y0 a( d2 z- u
用 法: double ceil(double x); ' J& \* x% _$ O- p9 b& E/ C. _
程序例: : t! W, S6 @9 L2 J- A
#include & V. e* m) U% k* i+ W ~
#include
, \6 ~' h [: jint main(void) ; X* W% A- q% o( x5 W
{ / A, J5 v% S3 h' C4 K, q7 X
double number = 123.54;
5 {1 v$ n% `" u: M7 a+ U' W. pdouble down, up; " G2 h5 s$ h8 d. e7 b
down = floor(number);
+ ]( T; p* D5 \+ t; gup = ceil(number);
" u" P: Q8 R; R$ d% c1 jprintf("original number %5.2lf\n", number);
3 ?- E' l) ]$ B. U' E. W6 ]printf("number rounded down %5.2lf\n", down); 2 B! R; q ?$ m8 ^$ }4 O* G" b
printf("number rounded up %5.2lf\n", up);
" K3 T- V: W* G5 G/ i/ X2 wreturn 0; 3 D( S3 q; A9 _
} + X: z3 n. ^4 Z( N* b
8 l# {0 l0 {# S+ i. {% h! G, D6 C) b
Y4 ?9 e; f2 L* k$ j函数名: cgets , V! m) |5 r( ?
功 能: 从控制台读字符串 : q! M' h+ K/ d/ A/ C! D0 W
用 法: char *cgets(char *str);
: S2 A, S& f7 X4 l T程序例: - f6 X( a9 f8 W4 @# l0 `
#include ) x& I X( Y' ~) e/ [
#include
! A. ]3 e1 @- \6 @' k mint main(void) 3 r. R& T0 |% N* h, v* T' @
{
# g2 [: d* W" o7 X6 t1 }char buffer[83];
! q2 z/ m" a" L5 Tchar *p; 2 M5 p; L- l( ^- j
/* There's space for 80 characters plus the NULL terminator */ : Z3 o5 e' y1 f6 m
buffer[0] = 81;
. h& B2 u; C% r$ v- ]! mprintf("Input some chars:");
8 W0 ~4 q( m- I; Y0 N: Xp = cgets(buffer);
5 y: b) I2 b3 {printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p);
9 n) r+ ^4 O+ G/ }% p. h$ Cprintf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer); $ X8 Q) Y4 J" a6 F4 [" ^
/* Leave room for 5 characters plus the NULL terminator */
" j3 K" o5 Z* _$ T% ~" ^& B- lbuffer[0] = 6; * v5 n3 b( m5 y/ ~& c7 `
printf("Input some chars:"); " V, |- k6 h/ J; I9 E: D: G
p = cgets(buffer);
7 r+ W5 S8 z/ w" p6 t7 `printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p);
' d/ ]! b" _, Mprintf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer);
7 k" t+ j+ ?1 |# A- oreturn 0; 0 b( P# `) ^# e( s8 q7 J! Q+ m( {% ^
}
6 m5 S7 r! {) O6 K: K' T: \! R3 V( p# H# Z7 U
, T! }$ [' Z0 l
% j2 H" d# P) ^+ l" O函数名: chdir
: U1 |4 C) R( W7 z9 @$ E功 能: 改变工作目录 , }& |' O& ]& }( ~$ ~) {4 m
用 法: int chdir(const char *path);
9 K: X6 p- o* Q6 L, Z. y程序例: 0 J2 W% }) E# x
#include
1 e v8 y; T0 k8 j6 U# X: O+ T1 E#include 0 L+ R* l+ q7 a' B
#include
% c. b V; ~5 G2 O, R/ _char old_dir[MAXDIR];
; @7 \6 m# M1 Y' t6 \char new_dir[MAXDIR]; 5 \- o! p) Y$ x' S1 f" o( a
int main(void) # s; H* h2 r/ o6 a h3 F' o
{ 7 V/ a8 R& x+ o5 d
if (getcurdir(0, old_dir)) 8 k1 Z: N* Q- w; W
{
: D; f: c+ m6 P! ]$ o, ?perror("getcurdir()"); ) b2 v/ Q/ A3 Z
exit(1); 1 o; U. q# E) T7 h6 F
}
; Z/ s$ e3 d9 e- t* Gprintf("Current directory is: \\%s\n", old_dir); $ j# d5 y, j R: |
if (chdir("\\")) 8 l* o2 h& {8 h. O
{ ( D; y" C- z7 {: w; P
perror("chdir()"); . Y. ^: u9 d0 ^% p$ v
exit(1);
; F9 p. |8 u8 J}
5 r$ U( w j0 i( e3 E; p; |! N8 Z) Xif (getcurdir(0, new_dir)) , g3 D. b& y9 ]& K; n
{
, ^5 F% S: p' M$ k* F d) Gperror("getcurdir()");
/ D& O4 j5 \( g: U" dexit(1);
# f: R. S7 U) b6 _# t} ) Q" F- Z' |+ c5 u3 ^0 O; v
printf("Current directory is now: \\%s\n", new_dir); ' H1 M) v* K8 N6 j( ]
printf("\nChanging back to orignal directory: \\%s\n", old_dir); $ v( J# g* _( ^' r, u
if (chdir(old_dir)) 2 R9 R- y/ K2 o0 s( V+ V
{
6 r/ X+ T x' V$ Wperror("chdir()"); % y K, T: Y ]; a* l! b
exit(1);
, T5 W* x2 _! Y2 i7 Q* T}
" u# s: \9 j$ Yreturn 0; 1 t; h* ?' j, ?6 A# F9 X
} 1 |8 k! ~+ N( ^
0 a, x5 Z) J4 {5 |% m& t* F: h
* b1 [: s" M; I8 V1 \: l函数名: _chmod, chmod
0 e" C* ~# p1 u2 O. q功 能: 改变文件的访问方式
- h! Q5 C! t Q b用 法: int chmod(const char *filename, int permiss); 3 ?- ^; u" w f( F9 ?# c4 O7 S3 q
程序例: $ P+ W. F# k7 f5 Z T# E* o
#include
9 }( ~. B* x% G/ I9 W( |+ _#include
' L6 _& [0 X: K/ q3 u/ O$ S3 {8 a& v#include / D. ?. _' s. {" \$ K. X/ L
void make_read_only(char *filename); . \( M o2 j+ K9 j
int main(void)
8 F2 {0 U% \: z- A8 J{
X5 A G( W2 z* D3 y K1 ]; Gmake_read_only("NOTEXIST.FIL"); 3 R3 k" a4 u1 @/ f
make_read_only("MYFILE.FIL");
3 V0 C8 p) K0 L! d4 `+ V. p2 yreturn 0;
K6 I" F2 J8 b5 i} : U# W' w! e( b" p: ?6 P
void make_read_only(char *filename)
6 N+ B# e# |. ?{
9 n& l+ X5 ? [3 o3 m. {int stat;
3 g9 @. E8 y' F: Rstat = chmod(filename, S_IREAD);
; T2 f+ d; |3 U1 Bif (stat)
" x* l$ h. s' {. H! g6 m. wprintf("Couldn't make %s read-only\n", filename);
]* h" i* s8 P2 t4 f7 @& o# G7 Telse + M4 ~6 H) i: [; k) K3 F
printf("Made %s read-only\n", filename); 0 i" j, c6 T9 I' g! o- V
} 4 x. k: E. c# q5 _6 h
3 O7 u' R4 A7 D+ b1 W) q- @4 s6 [' G {5 z3 V
5 G- x7 d- ]2 W0 a函数名: chsize 5 i9 }6 Z B& ~2 X6 z- h
功 能: 改变文件大小
+ @1 ?8 x0 t' b# E6 F0 c6 D8 l- Z+ C用 法: int chsize(int handle, long size); 5 f2 F! H+ s5 B/ K# p, m, f7 U
程序例:
& o. g5 e! C) K* v5 M2 k7 h#include 9 x/ X& i! q' _' v: ^8 h/ C
#include 1 k0 j+ I1 X5 c3 k8 R
#include
( l6 T- O. B6 d( kint main(void)
! s# W# [1 W" ^& }1 `{ & \/ P; z6 x, }% m$ u* U$ u* ^- x
int handle;
, U0 t6 Z" B- ~7 I- d" u8 Xchar buf[11] = "0123456789"; ! i. |9 z/ I0 R" H
/* create text file containing 10 bytes */
& n X2 z% G* _) Ahandle = open("DUMMY.FIL", O_CREAT); , Q* J& V5 m5 G" d
write(handle, buf, strlen(buf));
8 i4 B/ v9 ]2 a4 |: t: T/* truncate the file to 5 bytes in size */ 4 @4 c' F8 s* w2 @3 Q
chsize(handle, 5);
: q) b9 Y6 Q/ V9 u5 P/* close the file */
" n# P* C3 }; s# G) p" e8 N0 gclose(handle);
8 Q* M7 C. R+ a; freturn 0; + C! f" R2 V' b8 J0 E
} : ^8 @' w2 l: D0 }7 \" t
$ ]0 M. P! U6 h* ?2 y9 r/ I $ l5 b8 x5 b; H; g
函数名: circle 8 k8 v1 ~9 X. i# W4 @
功 能: 在给定半径以(x, y)为圆心画圆 % Z5 k* n8 K |/ V9 [4 Q6 s5 p+ k
用 法: void far circle(int x, int y, int radius);
$ c; T8 c4 N9 a# N% s程序例:
2 q: b- `) q0 e9 f5 U" c4 z$ n#include
! E6 v- {% w. \: j+ ~7 }6 }4 f3 @#include
% A" T. d9 e1 Z: ^6 ]) S3 L#include
( l8 H( N& z: z/ L9 p1 } H2 b U; n#include
, n/ N" L/ U+ Z8 Q: k7 Q; y* kint main(void) 7 ^. }" B* q2 j4 U
{
5 G) r+ x& ~, I) k! g/* request auto detection */
; `7 P1 u" w7 @+ w, @" f/ {int gdriver = DETECT, gmode, errorcode;
+ T! u& B7 l) z5 D1 u+ `: Kint midx, midy; . R. Z/ f& \6 s- N ^5 F
int radius = 100; 9 e4 g! z" n& B& G& m$ T
/* initialize graphics and local variables */
- @/ b) I4 s5 o& Finitgraph(&gdriver, &gmode, ""); & t6 X, s. ~3 w2 E" g
/* read result of initialization */ ) h( e: h' i6 B- D- W: w
errorcode = graphresult(); ! @2 o0 Q* r0 L' j9 z/ [
if (errorcode != grOk) /* an error occurred */
5 i- U8 q; O% [% ~, T% J+ X{ + d) Y9 u% {/ J) Q/ C2 r2 ]
printf("Graphics error: %s\n", grapherrormsg(errorcode));
) }6 O; D- c+ r1 ]& Mprintf("Press any key to halt:");
$ d5 Q4 r# u% n; d* qgetch();
- t: Z0 S! d7 W4 b5 ~' P, N7 Zexit(1); /* terminate with an error code */ - f7 {% u# [2 s& q# L+ q# q
} ( t0 ]! Y7 B+ ~ i- t0 I5 T) _( {
midx = getmaxx() / 2; % K& U5 p, }0 k+ W8 O1 e8 h4 z
midy = getmaxy() / 2; 9 W# }; X4 Q4 v
setcolor(getmaxcolor());
( p* F) U. C$ c1 R7 Y) R/* draw the circle */
1 t7 W; J3 k# o. t: k( \- o u6 Ecircle(midx, midy, radius); , R" |9 W e6 d# \7 c9 h; G
/* clean up */
$ B0 P) A, y4 q; Sgetch();
% M6 U4 d) a( y2 wclosegraph();
h0 ? v* r3 D" Oreturn 0; 3 l+ i, @" S4 ? c
}
9 ^ F9 N4 _3 _/ g
`5 Y; R( C( x6 _! `) e
# e! v2 }3 N. @5 ] z " S6 c, `+ k9 ^% c! y: W8 q
函数名: cleardevice 1 ?! `2 P9 G% o" m& g$ H. C
功 能: 清除图形屏幕 + d i m4 c/ ^, ]) w6 H! T
用 法: void far cleardevice(void);
0 X% v3 B- N' i" h' F# J/ E1 t% A程序例: - s, ]/ k! q0 f! j. I& |+ l6 N8 n
#include
/ `& j4 M; ^! C- Z% H5 {; N" U#include
& y8 J6 o% ~( X/ J$ O#include
% \7 U. D, ]( }" p: E& y0 z5 t; T#include
1 q* S/ j" ~9 S6 G hint main(void) ; R$ `7 I( l& r w# e7 E4 k3 [
{
& e0 \" r, P V( l# L/ I( ?8 z/* request auto detection */ 0 Z% m7 v* }% f& ~. n
int gdriver = DETECT, gmode, errorcode; 5 u( Y. n9 x7 r% i8 B' k
int midx, midy; ! l z' b" Z2 Y6 p! O, v
/* initialize graphics and local variables */
- t: d) x6 l# v; G, O5 einitgraph(&gdriver, &gmode, ""); 1 I& `9 y% c/ c! Y2 C# p( Q
/* read result of initialization */
6 ~% m& s1 A- i" W& S# R8 S4 @errorcode = graphresult(); $ d6 L& |. m4 B9 R# B6 F; v4 M# D! n; r
if (errorcode != grOk) /* an error occurred */
K. I+ R2 H8 L5 r{
+ s) [, i5 m. ?* B6 g3 J) yprintf("Graphics error: %s\n", grapherrormsg(errorcode));
* t, T. D! J7 g# E! Gprintf("Press any key to halt:"); - n( V: \4 \9 t
getch();
9 R3 W: u( P2 }% `% u' \, K: @exit(1); /* terminate with an error code */ N+ }! N M7 R; Z
} * P2 T* Y1 a& \( n
midx = getmaxx() / 2; 3 f5 R5 O# A+ T ^, w' _
midy = getmaxy() / 2;
) h# h0 S% o, W+ V, y7 e3 B& C# {setcolor(getmaxcolor());
+ w' K; c3 J- t4 D: E/* for centering screen messages */ : h7 _+ Z. l$ Q2 u. u
settextjustify(CENTER_TEXT, CENTER_TEXT); 9 m* T* o5 Q8 U/ |; q
/* output a message to the screen */ $ K4 \$ p8 I* |6 F
outtextxy(midx, midy, "press any key to clear the screen:"); $ b7 ] e4 W& Y9 w. u6 k
/* wait for a key */ ( }1 @+ `7 V2 r) b! z
getch();
1 H- T1 m$ x( T1 n P, z. l8 l/* clear the screen */
& U8 j6 N' W' D1 Ncleardevice(); 0 V2 p0 c+ n# ^
/* output another message */
$ V' X, N% @' P6 R9 Douttextxy(midx, midy, "press any key to quit:");
4 \+ p9 E1 F8 U& {& H/* clean up */
. W: f! i0 D6 ygetch(); 9 c( ~3 } h8 ?' j4 J) t( X
closegraph();
4 H6 @0 j4 n/ C6 L; B9 ereturn 0;
( M( o( Z. v# |; n' _1 i}
0 p6 {7 z. d& g" r& M
" x6 D$ S2 y$ _
" z8 U8 n% v* m9 |3 u8 V; C : w+ w+ r0 Y. V( U" P& M
函数名: clearerr 2 k7 \( h! h" W" U
功 能: 复位错误标志 ' x3 I0 v: ^6 A8 ]) O+ [0 y. \& t
用 法:void clearerr(FILE *stream); 9 ]' m1 J/ Q# z" t, L
程序例:
9 o9 V# _" d/ v2 w' f7 F2 X#include
# U9 C: M3 b, X Q6 tint main(void) ) J2 i2 _( U( n; N
{ 6 }. M. u6 m/ m3 c
FILE *fp; 4 z( `* b+ v R/ e) g2 q' W6 Y# s
char ch; 3 V- H/ |8 d/ \
/* open a file for writing */ ) B; E7 {$ k2 k; R, E
fp = fopen("DUMMY.FIL", "w"); 7 Q, ~0 v* U0 R" X8 p
/* force an error condition by attempting to read */
- M: s# Q1 F3 h' ]) R; L* Gch = fgetc(fp); + I( `6 }8 R( Q0 ]- r; R. B
printf("%c\n",ch); $ b+ [& d" ?7 n ]6 C. j& x/ F
if (ferror(fp))
5 x, z' v$ L1 \3 G{
" b: [/ I" V$ \1 \/* display an error message */ ( Z5 a& z" ]# c
printf("Error reading from DUMMY.FIL\n");
4 c0 ?- \. K! L c5 b5 [* C/* reset the error and EOF indicators */ ; q; b- l# M9 C: t0 l' X# N( }$ {
clearerr(fp); , A8 I9 z' `2 [5 u2 I
} 2 Y1 b5 i0 J6 O% Y4 p
fclose(fp); : O. c3 }1 v- {( L/ x
return 0; 7 M- v R5 p* x: i9 I
} % U0 Z' d$ D) ~/ I# | r+ Z' {
. Q" T, a+ W9 h" X' d6 V$ k. o. C, [" z+ v$ x0 v ]
/ J2 ^+ ]3 c* _. S- f, J, d函数名: clearviewport
. ~6 r2 g) h0 Z2 N5 H功 能: 清除图形视区
/ g7 q1 p" K/ e+ h+ ]5 g- T$ v用 法: void far clearviewport(void); - u& I4 }$ |( h" S6 G
程序例: % V2 _: R, X4 T, l" D; T( d
#include
& @/ `& k! h& }4 b; O$ ]#include @5 I: @# \9 W) [9 v" H K) h5 X
#include ; u' f! ~) X% {" ?* C4 s
#include
8 K% W- ]$ t$ O#define CLIP_ON 1 /* activates clipping in viewport */ / y- N& Q1 ?/ ~) g* ^
int main(void)
. `" q2 q, m+ {{ - u* [3 g6 O4 C' y1 D
/* request auto detection */
: c6 w* A* t4 eint gdriver = DETECT, gmode, errorcode;
" ]" p" g1 u, W; K& k! A- }int ht; $ y; `" w9 O# F J9 }
/* initialize graphics and local variables */ 3 z1 T. D+ y; I* }8 G& e6 i% J
initgraph(&gdriver, &gmode, ""); 3 X3 `3 F# B* T2 Z
/* read result of initialization */ 6 a% u( H: E! N. u7 D: `
errorcode = graphresult(); 0 V, a5 V k2 ?, ]% P$ ~! {
if (errorcode != grOk) /* an error occurred */
) F* w: {7 M5 T! e{ ( f/ [8 W' P b; ~5 ]( E
printf("Graphics error: %s\n", grapherrormsg(errorcode)); 2 D i ?+ G2 b* b' S2 P
printf("Press any key to halt:"); % t4 Z9 b3 Q; \- W. B6 b) t
getch(); 0 G0 A. w- N6 s* K
exit(1); /* terminate with an error code */ 2 ]2 V; ^) f" W, j! X) ~! n/ W' X
} + Y& y% Y' p) T K
setcolor(getmaxcolor());
% S$ {3 M$ G, D h. l k- \ht = textheight("W"); " r# ?, J v7 F
/* message in default full-screen viewport */
" y/ |5 k) {/ p, K% [3 W3 \outtextxy(0, 0, "* <-- (0, 0) in default viewport"); ! c# Z: d3 _0 I' q
/* create a smaller viewport */
1 \" R8 J8 C. @9 n& Zsetviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON);
/ d$ n% i4 S6 P, h5 k: e8 m/* display some messages */ + l& i1 Y; x* l0 U
outtextxy(0, 0, "* <-- (0, 0) in smaller viewport"); : J* C2 D# L. J+ I) S: @
outtextxy(0, 2*ht, "Press any key to clear viewport:");
* f* S+ V U# c, {* H/* wait for a key */ : e7 G1 V3 }, F! H. m% W% V
getch(); [- E) B1 u9 z/ V; t- K
/* clear the viewport */ 5 E: T0 T3 U5 W& l
clearviewport(); 6 r$ S& ^4 _: ^! @
/* output another message */ ' l# I4 u7 }" R3 L8 q4 M! k
outtextxy(0, 0, "Press any key to quit:");
1 i3 O9 f, w6 ]! n$ y" W3 e/* clean up */
u8 ]" N, h9 T* V! G" A2 tgetch();
7 D/ q6 ~! O, h4 O# Q! I3 hclosegraph();
% Z/ q' `4 ~; |3 v h: sreturn 0; 0 `/ k/ l5 D F+ L
} 5 f+ L9 ^( r8 t, P, p
1 ~6 `8 I6 k* o1 W$ B. g$ c% g/ I$ d6 h( C3 e. E5 ^
; e& U' |6 @' H8 }8 l" s4 V
函数名: _close, close
2 L. o; @: O8 b3 k9 G/ l8 \功 能: 关闭文件句柄 0 s/ o" @/ H9 t9 b# E0 w
用 法: int close(int handle); ( }! N, N# i/ A* d( Y0 I' m* i# M
程序例:
) ]; D7 A" B' e; X#include + E$ E0 g, R, C! I" K H
#include 6 w L" L, h* Y% D8 `3 R3 f$ V
#include
' c A7 |6 Z9 |* H#include 4 d6 y* ?, ?/ u% x
main() - S. o4 b' n( }* Z2 z) Q5 Q/ Q, a$ ?
{
3 W4 v0 y$ Z! Vint handle; 7 f' v3 o) y8 J" \
char buf[11] = "0123456789";
9 H3 W$ |& j# q" P2 z7 C6 j [/* create a file containing 10 bytes */
/ z* f, a8 G, U- F8 p& e' k" Vhandle = open("NEW.FIL", O_CREAT); 0 I. j0 t' |, v( }/ v
if (handle > -1) 8 i e8 C$ p. O# ~0 t- W
{ 6 ?5 |$ W4 B3 n2 U7 C
write(handle, buf, strlen(buf)); 7 s) S- e! N* i* Q$ {0 \: g
/* close the file */ ; C1 e; p! @6 U6 y% H
close(handle); i& B2 @" x# }
}
9 g# |& h2 T: ^8 E- welse
: {" G/ u7 u. y0 B0 j7 d) L{
) P. K1 }* v& Oprintf("Error opening file\n"); 1 V6 n6 f& ^: y& F
} ; s" n# P4 g& C: H5 N8 c$ [1 B
return 0; ! G4 g2 l; K9 i- ^
}
6 }7 V( ~# Z2 B/ t
7 h7 o! n( b$ x. r, B9 A
! h! p+ ~1 C4 U2 ? i8 Y" a3 s
) }2 ? k, W" q9 n7 q9 J) D函数名: clock
9 F0 o7 u! `; x* U功 能: 确定处理器时间 5 w+ `5 M- Q$ u f$ ^
用 法: clock_t clock(void);
3 Q2 Z: K" u8 \& n" X程序例:
( |; w2 S0 K" [, y2 X/ A#include ) v- H/ @4 M2 U1 A: O
#include ; m" o& _7 I P, [
#include
2 I2 }3 [- s0 E7 H; F/ o9 o# D( r* T tint main(void) # ?4 W0 b2 W0 J6 y4 o2 l# |* D
{
! R: s! B% r0 cclock_t start, end;
( g' U) m3 E% wstart = clock();
4 _+ r" {/ \) P O7 A' Edelay(2000); * Z; o! D1 G2 I; A) q
end = clock();
' i6 C0 \0 S( a( D" p% B2 Iprintf("The time was: %f\n", (end - start) / CLK_TCK);
. {3 r5 \& m2 N( k& y2 _return 0;
0 y" g6 p; D* F) c} % J% ]* J. N! C; u3 p1 B( \
5 h! g4 ?5 x1 k. S
$ d0 ]6 y# J5 {) ?+ I7 m7 a/ } ; [" v1 g+ f S( O- ]2 C! C8 r
函数名: closegraph 3 ~2 w$ k8 L* g9 s4 B: D" c
功 能: 关闭图形系统 " h8 N% M+ f% C3 J2 I. Q
用 法: void far closegraph(void); 4 [& ^: \& K) L) F, V
程序例:
; p3 b: `) m- E# t8 s- w#include # r9 K# Z9 G' b, }6 v
#include 0 S- q4 I/ N) C m3 w2 w5 Z, `
#include , s. [5 S* l5 z; i
#include & L* U; i9 y' S+ l, G- j# }
int main(void)
' J+ r( I% @, D% |" X9 g+ X; B{
8 K, c4 g4 ^$ u& g+ y, Z4 P) l" C( S/* request auto detection */
6 v/ U0 k& I8 C1 v* ?8 w/ cint gdriver = DETECT, gmode, errorcode;
3 l7 ]1 y4 y9 q+ h$ A' Q* {int x, y; 8 v# L+ v9 k, ^: h3 b+ `( k
/* initialize graphics mode */ 6 N1 O T: [0 X9 K. e
initgraph(&gdriver, &gmode, ""); - `2 l$ F# {6 d1 w! p6 u0 F
/* read result of initialization */
' b% K o e% x; n4 I3 derrorcode = graphresult();
4 j! c* G. P3 cif (errorcode != grOk) /* an error " q/ M, |0 f% F7 R" A
occurred */
. X# x/ U( c) `6 m{ # ` C" l4 q, R% I' W! A) l
printf("Graphics error: %s\n", grapherrormsg(errorcode)); * N9 N9 r. y8 P. w+ C
printf("Press any key to halt:"); , f" n9 o: x( e: d8 R' x
getch(); " a$ A; o, z9 T/ Z* P% p" o$ E1 Y/ a3 X
exit(1); /* terminate with an error code */
9 V4 i* w6 T! L! `} 9 x, c+ p! k1 j* H' w
x = getmaxx() / 2; 8 N5 \- M. k2 U4 y. E: z
y = getmaxy() / 2; # e( z, S, K0 S/ i* L
/* output a message */ 8 B" e. K* B$ D) l O
settextjustify(CENTER_TEXT, CENTER_TEXT);
) N& ]3 ]: r9 e3 routtextxy(x, y, "Press a key to close the graphics system:");
! _: D4 S) m. J3 k4 ^! N* {' D" E/* wait for a key */ ' t' A5 c9 W' [9 o" U( D) t( f
getch();
. ^& E( U8 Q7 V- f* F* ^0 s/* closes down the graphics system */
$ J& ^3 G* A" {closegraph();
C% y' U% _, J) H$ Nprintf("We're now back in text mode.\n"); 1 h4 i# o# w0 l
printf("Press any key to halt:"); a( t' V, R+ F* T
getch(); 2 D$ r7 ^" c( r( A1 j# u
return 0; ; Y- `+ Q: O. [
}
$ x1 O) C" H! H; `4 _. s b' i7 R; H1 q' V1 J8 \
j/ G! g! q. h1 v7 t( X c) r
- P$ ^2 F- G: ` |& |函数名: clreol
+ H* r9 ]: B6 h: U' R# l功 能: 在文本窗口中清除字符到行末
; m! V: t1 T6 w& e- T9 d) }用 法: void clreol(void);
% i* @3 u! B4 _$ ~) u3 G _程序例:
" T& V- z; j( U#include - X7 Y2 r1 d7 g0 r
int main(void)
! {, q; y0 d* f9 }* O5 g{ . B' T, b! I8 _8 F
clrscr();
* }% R& Z6 M }: B; y) Lcprintf("The function CLREOL clears all characters from the\r\n"); 8 ?' ^$ `/ U$ q* _* h
cprintf("cursor position to the end of the line within the\r\n"); : n5 }/ J: R K& @
cprintf("current text window, without moving the cursor.\r\n");
* m2 B, |$ B: ?* D+ }1 ]5 Pcprintf("Press any key to continue . . ."); % w5 i$ O; V9 s) N
gotoxy(14, 4);
: x# [. F9 s6 s' U1 `. e. q' ^getch();
. M$ z) ^# Y. _: K& K# v' F! xclreol();
* U' ^& ~ }: P2 egetch(); 2 @0 x3 W; D( o9 U- X f& l6 b
return 0;
; r0 O" T- O: G) |, S+ c}
9 A* f" R* x3 p
0 M: M% r* V0 ~1 s! h H) v' E& R" \* W' P" T
$ o) M8 ?4 y$ [函数名: clrscr
2 _! z- b) \/ ^3 o& R( A功 能: 清除文本模式窗口
; v0 X1 k2 g0 h6 _0 C. [- B用 法: void clrscr(void);
1 j6 O$ E- R; b) t1 i/ q Y+ h程序例:
* x. b, d- _* \; ]* u' @& u' i#include ! v! _/ C: v E- \# L
int main(void)
* ?- d3 D; e; d ^+ _# _/ o{ 5 f% f. m9 V; b/ B7 l& ]3 ]$ y
int i;
4 r N3 U2 I+ l0 u: C dclrscr();
, J. D! y# N( J3 k3 N3 @for (i = 0; i < 20; i++)
& y4 ^& W, w2 X, ?3 h' ~: S% b" vcprintf("%d\r\n", i); 2 l O! N8 |: K. h6 X }
cprintf("\r\nPress any key to clear screen"); 4 X( v s1 f- J; v9 \" u
getch(); " L' S" S8 x5 Z+ W' z# ]& {
clrscr(); 7 V2 G) l- i0 G; B8 d3 `8 I+ S/ b
cprintf("The screen has been cleared!");
0 a8 L% |, h) S, m8 {0 Y9 r9 W! wgetch();
6 ~5 Q" Q# \6 x4 V0 m8 treturn 0; 9 f) k' e/ x& Y! i; ~5 n
} 4 z6 ?7 v8 X( e* c
, T' L) ]% x/ A4 M+ m9 `% O3 j7 H
: c$ k& o$ ^2 M6 r4 N$ p" j. \$ [ 9 `# s# F; \- {. S" R
函数名: coreleft
% B% `6 k; C( w功 能: 返回未使用内存的大小 8 d: }! L; d9 [' }0 @4 q3 I5 I ~/ ~
用 法: unsigned coreleft(void);
' v, u" w+ a8 I' _程序例: . w0 T0 \0 Z9 N: ^4 a
#include
& h0 {6 T. h5 j% G8 V( d- W! C#include
/ V% h0 E. ]* `/ Z' d( G/ S" E, uint main(void)
y. ^2 Z! f8 W& S* z{ 2 k6 @( J3 @1 Y0 \. T: Z
printf("The difference between the highest allocated block and\n"); S4 r3 Y6 `0 ?0 }- c( y
printf("the top of the heap is: %lu bytes\n", (unsigned long) coreleft());
2 x" k- B/ v0 Y, Y, U8 y" }return 0;
0 k) }5 v1 k4 S$ u& u}
2 o5 |+ t* X* N. M+ l - Y% ]: u& o, G$ W5 E* O4 z* X: r, }$ A
函数名: cos $ A6 }" D5 i1 Y3 A5 w
功 能: 余弦函数
9 ?, a9 w4 J" x8 m2 ]3 I4 U+ M用 法: double cos(double x);
- Q% T9 y* U9 ]# x; ?+ v4 O! Z程序例: 2 f. c# X3 b' D2 w
#include $ A( {. ?" _$ e6 i
#include " p" E! u6 O3 R' B' k. a2 C( R
int main(void)
3 b! l$ t# @9 z4 P5 E+ v' y) X{
4 O6 P6 u% p7 r6 D1 e1 X5 _) }3 p) kdouble result; 5 V* Y7 S* T! L5 ?/ w Y
double x = 0.5;
& m7 c1 s H; ?: fresult = cos(x); ( w. l$ i( P s1 S/ y% H5 h5 b8 W
printf("The cosine of %lf is %lf\n", x, result); 3 D+ c+ d. m5 Z/ e) p1 p+ J
return 0; $ N, V( }* @2 O% ]$ r# E0 f* I% T
} & z' K8 I8 K/ F5 y9 Y/ o
+ S7 ?7 b, W& L! g2 N
1 U7 E3 h. M# a/ M3 a) i7 M
; l& }0 q2 X5 j# K函数名: cosh
/ j b9 d- S% A: c& h' z1 X功 能: 双曲余弦函数
; P1 S. X# P+ l" m& S/ {; I/ d$ E用 法: dluble cosh(double x);
# s5 U4 @) j9 i程序例:
6 y" N, A% {4 I#include 1 C' o( S8 [: A% d; s V
#include ) K0 _$ d: |) |) Y/ T- m
int main(void)
7 M# d+ d8 Y3 g q1 x- x& E{
+ X) M7 ?7 T% q0 C4 y0 k8 P) ]double result;
5 Q9 y+ u! u0 U+ I8 M- Mdouble x = 0.5; / X+ w& H1 `4 j. N
result = cosh(x); 7 d+ [7 f2 v' x1 D9 x7 @2 a
printf("The hyperboic cosine of %lf is %lf\n", x, result);
# j' z& _+ F* B3 y/ i4 ureturn 0; ( ^5 `; Y" `, T `, |3 ]. S
}
, c5 N5 t5 N: ~
" P2 s' {7 A @) t0 w T2 r" Q( B4 B+ d; p7 r# X
0 M- m! E+ {- k5 ]! S函数名: country / {. ~, W5 @8 x+ s. Z
功 能: 返回与国家有关的信息
, o" e4 s: M. Y' V/ x用 法: struct COUNTRY *country(int countrycode, struct country *country); / q) `: q# @. s% V1 Y- `* p( @
程序例:
- }$ D/ R* D% W4 t( } m#include
W+ t8 ?. q3 {) a& P& V6 f#include 1 x7 ]) h! j0 E5 }% o) `- Y2 \
#define USA 0
8 F$ ~4 o% B$ C& E9 h% {$ H8 `9 s$ u9 tint main(void)
9 Y1 `- ?, I5 q1 G9 A; x# S{ 8 U( R+ c5 m- C2 e: C( {0 I
struct COUNTRY country_info;
$ R/ n2 a2 ]* Ocountry(USA, &country_info);
( ~! Y6 ?4 k) m; K/ n3 wprintf("The currency symbol for the USA is: %s\n",
/ _3 ^6 D( T d; ?country_info.co_curr);
0 d8 N T+ z, U7 M5 h R( hreturn 0;
" i& ]% H( b4 S& q3 h$ e& a, e} 0 q6 m) ^0 ~6 z" Z) T
/ H/ [% l% B0 k* P1 K1 ]+ n( h6 {! Z
2 ?. x4 D1 F; [& Q; ~. V2 ?
函数名: cprintf - B( c- D' O0 v! w
功 能: 送格式化输出至屏幕 $ Z1 {- T7 Q' T* e& Z' n5 Y
用 法: int cprintf(const char *format[, argument, ...]); # b( ]' R6 \ \9 V, `
程序例:
) m% {1 ^5 U. e8 Z8 G3 ?3 z0 Z4 q#include
( G/ N4 W# _- C, t6 Iint main(void) , @( B; _& u( d8 A
{
T4 h, w C/ c/* clear the screen */
" a1 v0 o; P; H2 n8 Q" Kclrscr();
$ j6 c- P2 p1 }) R/* create a text window */ ; J# F9 f7 G: C) o7 \8 s0 R6 F
window(10, 10, 80, 25); : v* l- M; Q! x1 M. ^# d* X5 o
/* output some text in the window */
% |% _' b4 y9 W$ Z3 Ycprintf("Hello world\r\n");
) h) e% M5 g |. P* W9 w/* wait for a key */
1 h0 [5 ~1 f2 k7 x5 _! O. \getch();
( o* P6 r% f$ E6 L- ~! Q s3 |' ~: Dreturn 0;
8 N: L$ D, B% ]$ K ~} x& O( N# V& d8 p' Z
& W& q$ `5 a) s L1 w: ?* A9 t5 N, \0 W( }- \" O
/ U8 a) m9 Y; v! z8 h# e函数名: cputs
0 ]3 W; v" D0 S- u: ?* K+ {6 \功 能: 写字符到屏幕
. T6 z1 B' |# K# H r1 D用 法: void cputs(const char *string);
- M: g3 I# q& z _程序例: " i/ j+ X! O* a+ l1 |
#include
3 U+ }' z- l, Iint main(void) ' h9 ?, T0 K3 R, m4 n3 I
{ / L7 X2 M" K/ l L
/* clear the screen */
# W1 ^4 w9 v s2 f# n* v$ zclrscr();
5 k& Q; {& x( d/ s+ Z; x% w/* create a text window */
$ A, s9 f" j( S- c, L* Cwindow(10, 10, 80, 25); / Q2 p8 p4 X" q* b9 I( C( S, l1 S
/* output some text in the window */
4 k% C' l/ @# bcputs("This is within the window\r\n"); ) t1 k9 G2 }) U' K5 L
/* wait for a key */ 0 N" e$ \9 w9 |
getch();
5 U S+ N0 c4 X/ b, k" Ireturn 0; % O- B0 _% d+ C
} ; W; T: L* I# a+ q/ J0 D
3 b& g( V- m, Y/ X- l" @
( V6 f/ ]9 g- @5 Q$ k# C
7 z, \' k4 b$ Z* l函数名: _creat creat
3 z% m; k3 s: \# s/ w c& J; k. E功 能: 创建一个新文件或重写一个已存在的文件 . R g1 Q3 x, K# K
用 法: int creat (const char *filename, int permiss); 3 X O" e" M) b0 ]0 N5 A
程序例:
2 m1 x; _2 z: ?9 K( {+ _#include
1 Y0 }- P( i0 _& y7 H4 |5 d1 f/ k#include
5 v. i9 T7 @: k2 D& u/ {9 u6 G8 C, n#include
, g8 p7 g2 M' w% Q0 [+ b#include
+ {) }9 o0 V; Y1 `# z- Fint main(void)
" ?" }. t& R0 `3 Q5 J) @ p{ 1 o6 U5 z& ^+ |2 M. _
int handle; % `# F& U+ K; l! U
char buf[11] = "0123456789"; " i. S$ R1 G! v$ ]: X* t+ p' f
/* change the default file mode from text to binary */ - u, ^8 }6 Y( ~# R* Q0 G
_fmode = O_BINARY; 9 E0 J5 ]# ^. n: K2 @" x- W
/* create a binary file for reading and writing */
5 o' j2 X4 w% q/ k; G8 ~6 D5 jhandle = creat("DUMMY.FIL", S_IREAD | S_IWRITE); $ p& N% @' H3 G8 m8 J
/* write 10 bytes to the file */ 6 ]" o: m D5 z* N/ O7 B
write(handle, buf, strlen(buf)); % y1 {; Y' r9 q, y/ B. G
/* close the file */ . Z* I+ o/ ^5 }
close(handle);
. ]8 _( O. d$ o+ c7 J% Q( \: yreturn 0;
, Y: ]: j+ n8 @ ]$ @8 I} + n+ L6 A l0 H2 n5 e, V" O
. O, n+ f! J; F8 T函数名: creatnew 0 W( S }. z7 ^/ d C5 H+ T ?8 L
功 能: 创建一个新文件
: p! A" t5 c1 T3 [) Y) V: M用 法: int creatnew(const char *filename, int attrib);
2 @$ i9 d3 b4 S5 k程序例: ; Z7 w% z" {# c5 s% a, T4 k
#include + b1 Z# K4 p) ^9 h' g$ J
#include ) i; z( n( f( u# @) t9 Z7 D
#include ( \. T+ w4 m8 B' y9 k2 F
#include
- W3 }" ^! Q0 ^8 @#include
1 X1 a* a* u6 d3 ]5 @int main(void) & E" w" f2 `8 _( V, q! D( V* O
{
* Z6 ?0 b+ W9 V. V# H, ^int handle; ! ~# e' l- w7 m/ z5 Z& ^6 ~
char buf[11] = "0123456789"; , |+ p1 j( o% W! @6 z, O( ^
/* attempt to create a file that doesn't already exist */ 0 c% G$ V( K) ]* a/ G) Z9 |
handle = creatnew("DUMMY.FIL", 0);
' O3 A* l6 \, R: R' dif (handle == -1) * L9 V9 b9 }2 y4 k* c3 ~4 Z
printf("DUMMY.FIL already exists.\n");
( c5 X2 A$ C: V8 Y, l# A3 relse
* B6 I; C/ W$ ~. H{
3 y7 S3 _5 Q7 \1 vprintf("DUMMY.FIL successfully created.\n"); % t/ H% R9 ^. g
write(handle, buf, strlen(buf));
& `- f( }" ?, ~$ [0 Iclose(handle);
+ W# v+ T# x- z# o* O}
8 z, I! ]6 t/ J) o2 areturn 0;
, p) t, y3 D9 P1 c. k$ H1 ]6 v}
9 U: l7 s' C' x1 m$ s; S3 Y% c
" i/ U# u+ ]- r) u6 t) W3 O
9 j- G! n+ B8 i) r$ x9 O! b' R / e ?+ z% `! H+ l) \0 D2 B a
函数名: creattemp 0 H9 s! F# ]' s
功 能: 创建一个新文件或重写一个已存在的文件
& m4 S2 ?2 B0 J) o% o" f用 法: int creattemp(const char *filename, int attrib);
& h; Y$ B3 J+ Z' I2 ?程序例: 7 i6 t. D, A4 F A3 k% B9 M+ o
#include
2 [4 l& x/ a5 U% s- }% ]#include
" `- [& v# _, Y; ]1 d. ]#include
( z$ g' O1 O- b1 Pint main(void)
4 S5 R% K F) _5 _1 F, Y8 k{
2 l. c8 {) V' a8 }/ e4 E8 p- jint handle;
( y( ^3 ~% D- kchar pathname[128];
/ M G1 o9 r. C) Bstrcpy(pathname, "\\");
D! r& b6 O$ B) u) L/* create a unique file in the root directory */
, v& g) {: y/ I( s2 @3 Vhandle = creattemp(pathname, 0);
% s1 j* E W# z: z3 ] q9 A$ _printf("%s was the unique file created.\n", pathname); ( t5 X" `3 W5 n: K$ e9 |3 l6 r& s
close(handle); 8 }) M! I: B$ m6 p! p
return 0; / r9 A" g/ d# Z# Y
} $ M$ R' [2 h7 K+ C% e- i3 B
k" _0 o6 Q7 y' P! @7 Y& V' I6 O
: b. P" u2 b6 p / R# O; p* ^' Z6 x: A! g
函数名: cscanf 0 [& }$ J2 B3 X* o
功 能: 从控制台执行格式化输入
8 ?1 T7 \5 V7 s1 s用 法: int cscanf(char *format[,argument, ...]); & n; Y. \' J- t- n* X6 N
程序例:
3 R, ?7 }9 A7 d( }. r' S9 E Y#include
, w9 V3 y0 T; O/ G9 X8 a- y3 _- j) Fint main(void) 6 u; k! j+ g, i. ~1 {' e" A
{ / ?( Q% |: d, I- i, e8 C
char string[80];
2 A, ?; F7 d' Y5 m- a/* clear the screen */
1 T0 L6 f+ R. Pclrscr();
# t+ N4 y6 S& z% ]: c/* Prompt the user for input */ 2 F8 Q8 j) z1 z, m. K) ^4 [
cprintf("Enter a string with no spaces:"); * W7 F- I# l5 o) L1 H: Y* y+ A
/* read the input */
8 |. y" a# q. Z2 Hcscanf("%s", string); . S% F9 ^* p* j
/* display what was read */ 4 B. @5 G" T" _: U. c$ g
cprintf("\r\nThe string entered is: %s", string); & P7 n# N4 ?+ q
return 0;
7 [" f( d$ {1 k) L2 [/ x} % t z5 Y \, _3 ]
9 L/ H- `( H7 r v) B3 B8 k+ |4 I( d. U
% X: l1 E6 t3 F8 w6 G. K函数名: ctime
) j6 `8 @' v# x+ S' j功 能: 把日期和时间转换为字符串 * l+ h c7 ^+ `" u" ?
用 法: char *ctime(const time_t *time); # K2 x9 \. G P* ^& U6 [2 H+ D% ]
程序例:
# Y. @8 ?; X2 w$ p#include
2 A: c& J9 o, g* ~- G. b, E( S#include * J/ m) s* Z1 J) H9 q& G
int main(void) 8 J' e) }) [+ O7 \. Q9 Q
{
+ i7 _! M: u; {1 Q6 \time_t t;
! H8 W* j* M# a: @+ o* j5 E, ptime(&t); g9 W7 ~5 m1 l' @4 C R' C) z+ Q5 B
printf("Today's date and time: %s\n", ctime(&t));
@7 `* L; N3 M# R, d; Mreturn 0;
* s$ @; S5 J% |4 F D} : r9 f5 k$ `( M! p" Q R2 l. n c
; h3 s/ E+ C0 N
" R- m: X* V; `* H& u/ j. Q
' |4 |: k5 y- \: s1 ~) X函数名: ctrlbrk
2 E& N9 I. S4 T1 y/ M$ x1 M功 能: 设置Ctrl-Break处理程序 0 S5 V7 ?# o3 ~) G, ?" G- |
用 法: void ctrlbrk(*fptr)(void); 2 @6 x# q6 F+ _# {
程序例: 5 R& O3 h! ]3 w1 g5 c5 i+ o
#include
! [8 Q/ j3 b" n4 `- n; M. j# W#include
/ K7 [; E1 W7 o3 g' ~' Y+ Z8 v2 n% q, E#define ABORT 0
, F& F n: Z' @2 Bint c_break(void)
1 f/ `! M" \8 Y4 s# T, i0 [{ 1 z5 q5 h. ?# H! T
printf("Control-Break pressed. Program aborting ...\n");
/ r7 ^* h! m% R' s8 {$ P% Ereturn (ABORT);
. L& D: m# p; K {( R% [}
. O: i' S/ I7 q; a g' kint main(void)
2 |7 Y9 [/ h. u. j0 _3 A" s' P& a{ ' M" \. _# ?5 p" G+ j4 p7 |7 I
ctrlbrk(c_break);
# b- J7 }# s3 T5 ]6 qfor(;;)
4 d8 U5 V, I* h7 x9 H, M7 N{
, H9 l6 t# b2 l# z! d% r7 Rprintf("Looping... Press to quit:\n");
' h' S; ]- {& T6 y. g}
3 D# G& ~- r, B8 i) e7 `& Sreturn 0; & e, D) A1 T% d. @6 a
} |