函数大全(c开头)
, E5 L) t, f0 E
$ y1 d8 P$ N! g* c, D$ _函数名: cabs
5 f/ Y$ e* ]" S8 O5 m# N T功 能: 计算复数的绝对值 $ |& x* l: y, f# p2 N+ Y
用 法: double cabs(struct complex z); K! ^. |4 |& {! i6 r, x% g
程序例:
. S' h% u2 J" H3 q2 H; x#include $ z+ ^/ r0 f/ S" r
#include * D. ^" y+ s" V: y3 d1 f. ?1 a
int main(void) ! K& I' `( M$ y/ \3 c; g2 k
{
! @6 _2 _- U! F8 h4 pstruct complex z; + h. u k4 k! n" `9 H9 b6 S' a
double val;
# c9 o6 t8 F3 A8 K' N7 Z) W' mz.x = 2.0; ' o* J6 b. Y- n+ a/ }6 u6 D
z.y = 1.0;
6 ^, q1 Z3 c1 k" J5 Rval = cabs(z); + ? Q* f2 g$ j- r$ ~
printf("The absolute value of %.2lfi %.2lfj is %.2lf", z.x, z.y, val);
& d# k7 m; j- ^* O7 ?0 A) f$ Jreturn 0;
% u$ x' r1 T1 U9 l1 E T}
; [$ `; k1 t7 O2 k! |8 W+ ~! J8 c# Q$ \! f' H. B
2 C, p' m/ n% ?4 d% h; n7 o7 \: Q: ?5 W
- R- Q, U# F+ {7 [/ D) y; F7 d; J
函数名: calloc
8 ]) K0 w# a) n N1 y& [功 能: 分配主存储器 / x/ ~$ V6 C0 _) }7 O+ F' B8 G
用 法: void *calloc(size_t nelem, size_t elsize);
; ?: S9 ^1 Y5 L( t5 z程序例: " c5 l$ R# G# U; Z" B7 d8 T
#include * H/ o# g# X+ b. ^& J' w# L) K
#include
/ B4 v0 k9 g8 U* fint main(void)
) y7 @ K! w# s: y% R( q* {" Q{
! c0 S5 _3 z4 Schar *str = NULL; . E7 v# T# c- F5 ?+ J, \5 }
/* allocate memory for string */
/ e, I( j" [: S) _# M/ b% ?. Q+ z Wstr = calloc(10, sizeof(char)); . d- z" a& S3 O+ R- j
/* copy "Hello" into string */ 7 S$ P( C7 b0 d
strcpy(str, "Hello");
4 p9 K5 m! @) ?' ?/* display string */ 8 Y8 R0 r5 T; r$ Y$ ]+ p
printf("String is %s\n", str);
+ S' ~6 Y$ Z- \$ H+ a f/* free memory */ ) x/ l/ {: ~" |
free(str); & W; { _; e$ L& u! R! E
return 0;
% T% A6 a$ m4 J% H6 z& d% T} & w( `$ @. N' L0 E! P, t
7 S+ Z ~: t0 L# j- {
{. c# `# v( |
: _' ?, c% ] S2 k4 A函数名: ceil
/ q+ b+ n5 U( {$ o D功 能: 向上舍入
% L1 S1 O; F/ _/ X4 I用 法: double ceil(double x); 2 b$ X: @+ {, Q- f9 k# i; J
程序例:
; q8 _+ e* b# j#include , D) Q4 E9 ~. v/ y* D
#include , T" ]! F3 p* ]5 O
int main(void)
. c) z" M& S7 U6 ^{ 4 j) m* q7 B* b; a- g$ G) o
double number = 123.54; & D0 |/ w+ Y* G; j4 p6 B" |
double down, up;
1 u$ s, |3 O: b, Bdown = floor(number); : u% D1 @ A$ w7 V7 U
up = ceil(number); ! |/ N2 P1 J4 X% g' j
printf("original number %5.2lf\n", number); # A: z0 ?8 |2 f
printf("number rounded down %5.2lf\n", down);
" d' s3 H. o6 y" \1 Q) h/ c4 }0 C/ l) yprintf("number rounded up %5.2lf\n", up);
0 r8 ?7 O$ _7 {% [- C/ N3 oreturn 0;
" S9 U, N! X: U2 b. t, G* Q# E: P# ]}
. O7 t5 O d( D2 \: X+ V) F6 C0 F& {6 ^# }5 |, Q6 p% o1 W
7 J. ^6 k( S9 s. n0 R
+ N) u% E$ d4 _1 f函数名: cgets x; L4 G% [2 ~3 y, H
功 能: 从控制台读字符串 p$ R x- E- [& h6 l0 U
用 法: char *cgets(char *str); 2 W$ R c b& {# I5 j
程序例:
# H/ a8 w* |' \& }( C#include
( `: V7 v" A4 n3 r0 p#include ; f9 F2 x K1 s G* T& y! p
int main(void) $ {" m' G4 G! L( T8 X3 h; n- G2 k7 h
{ - m, Z+ k0 ~0 W& m+ `. c4 Z4 ~7 `
char buffer[83];
: b5 N2 ?6 [5 e* p! T* A. Vchar *p;
5 o2 I$ u, H9 _' T* q. A/* There's space for 80 characters plus the NULL terminator */ ) A- c% E& E0 h3 [4 G: h, @3 ?
buffer[0] = 81; ' S8 s, e* L* k. ]8 C: w- M
printf("Input some chars:");
/ W7 M* F. Q: k" Mp = cgets(buffer);
! c+ Y& R7 Q( }printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p);
2 w' v% q8 b# W) fprintf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer); 4 |3 r5 h; G, Z) ]
/* Leave room for 5 characters plus the NULL terminator */
7 d6 ]# a9 |, ]7 Ibuffer[0] = 6; & L1 e) l. E: m B |- J: C
printf("Input some chars:");
8 y* [6 U* d$ C0 p# ^# Yp = cgets(buffer);
# p& f1 i) |0 [6 ~! Zprintf("\ncgets read %d characters: \"%s\"\n", buffer[1], p); ; v% T# w7 K$ M- W( ]3 |9 I7 Y/ a
printf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer); * ?1 o6 W' \7 c. f+ ?7 Q
return 0; + u& x" ^# G i0 x& |2 ~
}
/ B6 }5 w+ x7 U. j
& @, y: Z$ u8 K! X. ?8 n' F. {! k9 i2 k" s2 k" ?
( S3 d! e2 e* a- B1 A) w# t函数名: chdir 8 n0 R0 U0 Z- q" X
功 能: 改变工作目录 % [, k/ t' z# Z
用 法: int chdir(const char *path); ; }! B( L/ q4 V) `: m) p4 @
程序例:
1 \9 b% i! d/ r7 W+ h* a#include 6 Y2 Y2 E: K/ j3 H, v9 C6 D. }
#include 0 \0 x' o8 p3 X9 n' r) d
#include
4 S8 B8 u- N3 S: Q1 `2 gchar old_dir[MAXDIR];
7 O' G& T6 a+ O/ Q, U* |char new_dir[MAXDIR];
: \ r' S# K9 n3 ]int main(void) 4 V3 ?; ~7 R% p1 {
{ 3 B2 T% X3 ^3 f* F+ ]
if (getcurdir(0, old_dir)) ; j3 H& U4 ?* H% f- [" C1 M) u
{
K) E% g4 X) i/ w4 @2 L. d. `$ _perror("getcurdir()"); # ^/ b6 H( s" Y
exit(1);
) Y( F: ~# m- f$ { z% g} 3 a! x+ i% D8 l4 \" a
printf("Current directory is: \\%s\n", old_dir);
5 `0 z+ V0 O; X$ P" }if (chdir("\\"))
% M+ L; O/ v2 d4 a7 h{
y( T# b; Z" }9 xperror("chdir()"); , h+ p) p2 d" J9 x* U
exit(1); 5 U6 r3 Q# L7 R+ l2 T
} : s( l. ?0 p% ~" c7 x6 p7 ^' U$ k; F
if (getcurdir(0, new_dir))
$ F2 s9 H! X! @. X k& D% i s# ]{ 1 L! ~. K# D+ x. u
perror("getcurdir()"); + r N! Y; o5 G, h" @- C0 U
exit(1); 6 T/ P3 t$ g; k4 y, a* y
} 8 q |/ B! u5 Z* O6 ^
printf("Current directory is now: \\%s\n", new_dir);
( V7 k" |- ^/ M- b0 bprintf("\nChanging back to orignal directory: \\%s\n", old_dir); ; i! m; K$ D E# a% K+ p, ?
if (chdir(old_dir)) 4 s) k$ I' j0 f( w7 [, J6 e
{ 2 _" \/ A* @( k& ^+ F+ _: g: |
perror("chdir()"); 0 Z0 B. `3 O# }6 C
exit(1); . F4 Q. P( I% \$ B0 p: ?% h; R* u
} & S+ `# Q; L! w# x( L
return 0; 6 X; r. ^& A4 V" a* f
} 8 _; n+ h6 E& @6 D0 _' t' D" [
! V5 S7 a1 u2 N' A m
5 g: }' g {. e
函数名: _chmod, chmod U' \+ L# x- P- C/ w) v0 V* e1 {
功 能: 改变文件的访问方式
7 @. T3 I( j" o$ i8 ]/ ?用 法: int chmod(const char *filename, int permiss);
! _# m( {- J% U+ m; N# {# k程序例: $ Q4 r5 M: s. R4 q7 }
#include
0 b# S+ P3 y6 M1 W/ R/ A6 k#include
1 S7 N5 l9 t/ ]; v$ P#include * a5 |* [& {& D/ i# O
void make_read_only(char *filename); ! Q" T/ l" ~2 E5 k: N N' }
int main(void)
& d2 \9 G$ M/ z) ]5 S; C# o{ * ]/ {" Y6 z( l4 \. B% @% t5 g/ q' ^
make_read_only("NOTEXIST.FIL");
6 N$ C; Z7 n I/ U1 Nmake_read_only("MYFILE.FIL");
! z" d( m: Q# ~' l+ ^0 K2 @" g% greturn 0;
* V; @3 ^$ k$ g3 |0 |9 M} $ x3 B& [! K" @
void make_read_only(char *filename)
& Y% l% A+ O% G8 {6 p9 l{
" m4 J. c u3 O3 L q5 uint stat;
/ O4 |9 e) M" I0 \stat = chmod(filename, S_IREAD);
' J6 H q9 ~3 X9 s/ Kif (stat) p! g+ Q1 u, t+ Z s: S* i
printf("Couldn't make %s read-only\n", filename);
( P2 U9 I7 p( r* |* `. yelse
! h; u5 L) L2 o/ Mprintf("Made %s read-only\n", filename); . A6 X& d2 Q1 d( t3 j% v k9 B
} 5 Z& N+ l+ C+ E% Y
, l0 d* Y! W; W$ l5 R9 m5 Z8 q' W8 r$ `
8 ^2 m+ v' M/ w+ P函数名: chsize
, R: J' p5 T* u9 s3 S功 能: 改变文件大小 / B( N1 |' Z( V; Z. }
用 法: int chsize(int handle, long size);
# S2 \7 r( R. g V程序例: 3 s; {3 d7 e/ ]2 h
#include
" a8 c8 N# w, w7 r4 A% c#include ( C+ p$ ~; R# F1 ?1 I
#include
) l* ]$ \. O' W3 N. r8 _# i5 Wint main(void) & S( t Q% A( X4 a; z. S* Q
{ + z3 w2 |) X. [3 y5 H
int handle; ) S. ^: {! [ y2 m" p5 E
char buf[11] = "0123456789";
) S _ o$ `1 P; @, o/* create text file containing 10 bytes */
/ [5 \/ K2 W( T6 ^; A7 shandle = open("DUMMY.FIL", O_CREAT);
+ l# c, p$ q" H' b% Zwrite(handle, buf, strlen(buf)); 3 `+ N! v. i% \7 @* N9 s$ F: e
/* truncate the file to 5 bytes in size */ ' n. m" z T0 Z* z1 |
chsize(handle, 5); , I, K _% L# h; _4 b$ }* [
/* close the file */ 7 w3 C/ u3 e+ d4 |
close(handle);
! m, Z5 x" i1 yreturn 0; " P& J* i6 N) N! ^+ E: z
}
( e. e. w" J* p) L9 L! r5 R8 b; h
: v+ i4 f- t3 h Z# w8 u0 A % R& ?4 i; T8 t! {
函数名: circle , V* H" g2 D/ A; ]
功 能: 在给定半径以(x, y)为圆心画圆
- L' f0 K- \$ ]1 e6 K) o用 法: void far circle(int x, int y, int radius); 9 f/ S' Z+ `2 d" E
程序例: 6 r- L5 ~& f \/ Z5 J! Z/ n
#include + }/ Z8 t( l- y! X4 x, o1 H( G
#include
5 w: i: w% K" a1 S$ A& B#include
5 [& \% b* T/ h& j#include
' \) `4 x! J3 s3 ]% ~$ vint main(void) 1 \1 v L' T' G# ?2 W# ~. ]8 N
{
6 Q# }. u! I$ ]: { `/* request auto detection */ . r3 w( d2 q- C- h
int gdriver = DETECT, gmode, errorcode; 2 p3 q$ b, v1 C& k) v
int midx, midy;
; T. n9 x0 i2 T7 K( ]( w! oint radius = 100;
2 [- @% G2 q3 d/* initialize graphics and local variables */
5 x, H0 j9 \. n% Q2 |2 Z2 @initgraph(&gdriver, &gmode, ""); ! j; Z& N& r4 |+ O5 d* K3 K" h/ r
/* read result of initialization */
; \9 R+ N5 y! N, i( z0 nerrorcode = graphresult();
0 T h. s2 T8 uif (errorcode != grOk) /* an error occurred */
- T$ ~' P N3 _5 G3 _{
; ? F' f! X& S) Q2 xprintf("Graphics error: %s\n", grapherrormsg(errorcode)); 6 g3 ^% n7 r5 S3 r: _
printf("Press any key to halt:"); 5 P2 G# B' I! L- r5 x, n5 S
getch(); 9 e d9 l# F: l ?8 v# j; |
exit(1); /* terminate with an error code */ 4 C' S; l7 F" K9 W
}
$ y/ v0 c0 T$ E; r" U9 x' Imidx = getmaxx() / 2;
( l8 q: s1 Q+ x8 m/ Lmidy = getmaxy() / 2;
2 n* J% g( C% D9 {3 K$ R& K7 J4 Lsetcolor(getmaxcolor()); ! F( ~$ u1 u1 G* S' {- s& X; E
/* draw the circle */ 8 O8 {# x+ k7 `
circle(midx, midy, radius); + w" L8 ^0 \! f
/* clean up */ 7 m) L$ D% p& P; i
getch();
* K" X5 i+ p( |/ lclosegraph();
8 V, j% S1 F9 O' U% K( H0 freturn 0;
# g! W% i7 x% u) q. C5 \} : T5 t) |& ^2 n" c9 p- e# Y) M
; ~6 K2 N6 {- w& q" n% j2 C" K
; }- a. Z' H2 E% E* @" P
6 Y& q7 J4 Q% H) V
函数名: cleardevice
+ E3 ^0 |" |* I/ h功 能: 清除图形屏幕
# g/ k; N$ P5 F T6 t用 法: void far cleardevice(void);
1 o8 Z3 D% l, U4 N% N+ D程序例: : X9 R1 x: b! \1 u& \6 l8 H- p
#include
) w/ R4 }/ k$ @. ]6 l0 t" {#include 7 F, O' E8 ? Y$ S! k- U: z
#include 5 f2 A/ {& C( w/ J
#include
( e+ c8 |7 V$ R* e1 ^int main(void) * [8 ?1 `* o4 r+ ~- y
{
8 k- l; G2 k7 q! b/* request auto detection */
e# [( u) S) y6 c0 Qint gdriver = DETECT, gmode, errorcode; 1 M' m3 f3 h6 }& f; X
int midx, midy;
3 R3 r- a U# g" M) H/* initialize graphics and local variables */ 3 J l ?# x' ~3 ]3 S" t1 ?4 m
initgraph(&gdriver, &gmode, ""); W' w. m1 N: O9 @
/* read result of initialization */ 1 m- I: l9 r" l: Z" U' w& n+ }
errorcode = graphresult();
0 r; \: G+ u: r5 @+ fif (errorcode != grOk) /* an error occurred */
|5 C: v; l# M2 P{
( M8 ^: j3 W eprintf("Graphics error: %s\n", grapherrormsg(errorcode));
* E& B" D" y9 e! aprintf("Press any key to halt:"); - h" G' \( ]9 {% r: W' g+ `+ R
getch(); 9 z" [' C2 o/ }* U% _3 M
exit(1); /* terminate with an error code */
7 |$ V% y& q" H}
E: m+ k& W P$ ]midx = getmaxx() / 2; ' T4 z/ @' U2 \: U; Q1 c
midy = getmaxy() / 2; 9 b+ W( V; _/ P) R! r5 d
setcolor(getmaxcolor());
' ?7 r. E! |; b; p$ D/* for centering screen messages */ % N! U! O2 L6 Z
settextjustify(CENTER_TEXT, CENTER_TEXT); - d' Z* r8 h! L3 l
/* output a message to the screen */ 0 {% F/ f8 ]- x( l
outtextxy(midx, midy, "press any key to clear the screen:");
1 n8 {0 r, m9 p# ^/* wait for a key */ 2 ~7 p' O; y# V' Q+ H4 Q
getch();
! N+ v3 @ n) y7 E, h/* clear the screen */ 4 M1 J: B+ x; D; t
cleardevice();
+ k) G, {: ~! x0 q) G$ q/* output another message */
0 p8 \9 o" k3 Houttextxy(midx, midy, "press any key to quit:"); M ]; u/ `5 w: F, j+ \4 _
/* clean up */ 0 G2 {( a S6 e+ ~( \3 H* ~- P6 V
getch();
; Y1 d5 {; A$ K# o) N* T/ S' c& iclosegraph();
& f; G6 i1 h% {+ k6 Hreturn 0; 5 E% j+ d' U; [3 E! m# x U; e
}
5 z% T/ s; b5 d6 @5 T; Q
* p: [1 r% e1 e/ p* h
% F7 r9 x# o6 V, f+ q t# M) k& i( Y
$ l: x T) e0 X4 c+ U. d8 `函数名: clearerr 0 `' q" {* j! v& }8 V4 }
功 能: 复位错误标志 $ A' q. a2 E5 L' N
用 法:void clearerr(FILE *stream);
* d% j/ {! o. V; z4 k程序例: ! e3 I3 ?6 }+ @: L: X N6 q
#include ) H# I0 T, N" L& }1 j
int main(void) 0 d! Y! w* q0 d7 a
{ ) b0 L) _. Y1 H" B
FILE *fp; : n" H k8 Z) W, V4 s3 O3 z8 Y1 L
char ch;
3 Q3 {5 v4 ~" ^9 a/* open a file for writing */
' K3 O& E5 M2 y Efp = fopen("DUMMY.FIL", "w"); ; \4 \/ u; }2 j; M5 M% G
/* force an error condition by attempting to read */ # B4 Z5 s1 o1 @+ o) D
ch = fgetc(fp); `# D5 F5 L y* D, ^% q$ V
printf("%c\n",ch);
& U8 s+ T7 j; [9 k) Wif (ferror(fp))
% X( W k" d& Y{
0 K ~* }; J7 i0 B; T9 G/* display an error message */ # Y% Q8 W/ T3 _% f' y7 M
printf("Error reading from DUMMY.FIL\n"); n2 I9 J1 p& }+ [3 ?
/* reset the error and EOF indicators */
9 Z' ]: I5 @" @% g: `0 c; b. [' sclearerr(fp); " f1 Q0 w! L# H& E& l
}
z/ c8 a) e- q2 y* K' rfclose(fp); ! q' e. g' v2 n- L) _3 [/ b
return 0;
) n& I& c9 a0 l$ N( J6 ^' U! g} 6 a" }! } o. v: ?* M3 }* s' W
+ y7 ^3 x% W2 C. h l- `
/ Z8 F7 V9 Y S
' t3 O, ]9 Q9 ^3 N
函数名: clearviewport
! ~& \9 l* p" n( K: y+ F" m6 o功 能: 清除图形视区
. L' z5 N+ E. C$ F% l- F用 法: void far clearviewport(void); M! N; j( I' ?; s' Z
程序例:
# g1 ?2 z& B; w, W#include : }) o8 b( b7 ]
#include $ l- V+ C/ a2 w- H: A$ z; I8 A
#include 9 W3 k4 c, t- L3 }0 J
#include 5 o" B& h3 n; Y9 J( N6 B5 ^4 K5 ~% E
#define CLIP_ON 1 /* activates clipping in viewport */
) H4 ]6 |) p0 c+ \1 O" g1 v8 U9 vint main(void) 0 _/ Y+ B% u0 A& e. ]* Z
{
/ h6 ~) U; g$ E, l. z; K/* request auto detection */ : g" ^0 P! c3 [3 A0 E
int gdriver = DETECT, gmode, errorcode;
5 R" V/ L# \$ v% F {$ g3 ~int ht; ; K3 w3 i9 t- E) n0 j
/* initialize graphics and local variables */ , r6 a) S& X4 K' [2 k
initgraph(&gdriver, &gmode, ""); 7 w5 H3 V, C) b9 u3 ~! I3 y
/* read result of initialization */ / @2 `* Q3 Z3 ~9 I% v: H# a% i* L
errorcode = graphresult();
& o: p# e; W% @# h B$ x# g# Dif (errorcode != grOk) /* an error occurred */ - c; N7 X$ O% _7 K
{ % Y+ y5 k, P: _- E% v6 U
printf("Graphics error: %s\n", grapherrormsg(errorcode));
; U. z; \& ^+ _1 ~0 I n" \: Mprintf("Press any key to halt:");
3 @/ J8 u+ I v3 M5 r' n# l( zgetch(); 3 J+ r& u/ w b) k/ @
exit(1); /* terminate with an error code */
: N+ b; c4 i+ N, F; E( L} $ q$ e( m' f- d+ X
setcolor(getmaxcolor());
1 f% J3 X6 k% Wht = textheight("W"); ) \/ w. q* _2 L. A5 W/ G! h
/* message in default full-screen viewport */ ' R1 A i/ W1 N! C
outtextxy(0, 0, "* <-- (0, 0) in default viewport"); 8 r9 }. w0 M1 F. E K
/* create a smaller viewport */ ) f5 J% f1 _9 Z. ^) e( y
setviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON); / c: p* H7 c3 r; o, G. q5 j- O
/* display some messages */ 7 u' s! r4 G0 h* q+ b
outtextxy(0, 0, "* <-- (0, 0) in smaller viewport");
9 |% `5 N+ @' Y+ iouttextxy(0, 2*ht, "Press any key to clear viewport:"); & T! k- Q; Q6 r1 y
/* wait for a key */ ! S J' `+ U% S4 M5 g6 {* E' U
getch(); % y8 ^( L P" X: R h+ w' R
/* clear the viewport */
& g1 B# U% |7 v0 ~/ r) Oclearviewport(); " j9 ?0 {8 c5 K9 G
/* output another message */
8 D$ T( o p9 q% ?outtextxy(0, 0, "Press any key to quit:");
1 i4 M, }( g* l( o: p/ K3 V& [* E3 I/* clean up */
, ~; R. Z$ P) c- D$ Kgetch(); / R' G R& U) f! w/ k8 |
closegraph(); ( t. k: ]6 N# H5 H( u& ]( A
return 0; 1 m( c% l& ?$ G# Q
} , ~; {8 p! `# V' _
6 _: V; `5 G7 u/ a
4 G/ {+ Y, ^. S5 J& t' N' |9 I ; q6 @) w0 _$ v9 V. B" y
函数名: _close, close 0 ^& `# Z% V) c$ c/ {# ]
功 能: 关闭文件句柄 * P* o) |; o+ w! U! \7 j
用 法: int close(int handle); 8 S1 n! C( W8 g% ~1 r
程序例:
* h' ^/ [2 m! g' R) q6 b8 J1 m/ L2 x#include
+ g& [7 s) Z7 e#include / Y4 r$ Z7 L$ u3 S q" o
#include
/ W8 c4 P$ N- ]& d. Z* ]#include
$ s0 A! ?& G. cmain() - I9 a/ G& \ M. H
{ " U* f7 o% _1 [- V! k
int handle;
+ X5 x2 W c0 Wchar buf[11] = "0123456789";
4 W( ]0 F) y+ W9 ~/* create a file containing 10 bytes */ / _/ q( w' U+ J) ^2 y
handle = open("NEW.FIL", O_CREAT);
( b" d, g a3 q7 s2 S. cif (handle > -1)
$ R2 `6 M/ q) |: i A% \& h# o{ ' G) _9 m0 D. g* }5 f4 V5 n6 {
write(handle, buf, strlen(buf));
6 p6 d/ h1 E' G* v/* close the file */ 0 R0 ^) Y3 M' x5 W
close(handle);
( ?$ i& t' v5 V1 l5 Z- t6 P4 | f. T}
+ | \ u* Z5 W& m& N* o6 Velse 9 E/ p: x( n a
{
9 \# o: u. S f9 i/ Vprintf("Error opening file\n"); * P3 v. ~% O, K& _7 U
} " x6 ~& a8 _0 l
return 0; . t& X# l( `- e, Z9 U
}
1 V, G5 s8 a: G G4 c5 q- ?+ ~$ j2 s3 t4 m9 s: W' p* j# C
- u' B, I/ M' q" R5 H% ]
, }5 |9 X b- p B) j- S7 Z函数名: clock
5 {) u/ h# v. {1 }& N% \功 能: 确定处理器时间
" ~$ l0 b8 ^1 e; P' D% A! F用 法: clock_t clock(void); U% w' F1 T* c
程序例: - o X7 Q- D& c9 n) w N$ W
#include
0 J* C6 {, y7 L$ L3 }* P9 H- h5 V#include ! G+ p# |" ]2 J0 O# S* p$ ?# b; c
#include
6 w. j* R( ~8 K) A4 Cint main(void)
& [4 |* Z6 B/ k" f( L{
. d) t: t+ n/ \/ k' Pclock_t start, end;
6 n" Z$ ~# N3 X2 e* U4 l/ o. x2 }start = clock();
# C+ a9 A e$ M* d% w" J+ Vdelay(2000); , x9 J9 h# Y6 S1 C* N& G2 w
end = clock();
) c9 L8 T4 b- w9 G+ ~printf("The time was: %f\n", (end - start) / CLK_TCK); 4 W% D, V+ [+ k( W$ X6 n9 u7 c
return 0; # `# ]: g: c) E! r6 y& ^% E# W& [
} 6 h; X! H- K9 f* M# p
5 U9 ]( r1 [- d2 W$ G* G. t
- r- m; l& |+ P6 u0 u& { . L5 G& J- \7 c/ z' B
函数名: closegraph & M, _; |5 @$ E' d6 O% V6 L
功 能: 关闭图形系统
: M) }6 a/ F, B用 法: void far closegraph(void); 2 A% q! m: s @2 [& K
程序例:
7 J, X3 e0 R t1 P#include 3 D4 h% p2 o3 w, Y/ L6 G" `
#include
6 x. F; S7 Q6 ^8 f" P" B#include : }$ E4 A! b. ]' S( O
#include
; K" E# O- i `* _- O- p- ]+ W4 Yint main(void) , s) ?0 ?9 I( M2 Q1 d7 ~$ v
{ - c% x1 |3 V( ?" D H
/* request auto detection */ 8 ?) e1 C% O. |) ]" f
int gdriver = DETECT, gmode, errorcode;
% g! q" r8 y; cint x, y; . M9 v( U0 ~8 h* c8 R# T5 s
/* initialize graphics mode */ : t! E) k8 }7 G( d& m O) {2 U
initgraph(&gdriver, &gmode, "");
( U8 o1 D0 D) B) j1 Y/* read result of initialization */
' I- h" K/ j+ P2 ]* e6 Xerrorcode = graphresult();
' i& h! e8 X$ V% B0 g, h4 Rif (errorcode != grOk) /* an error
1 @ ]+ _' \1 g7 F/ R" t1 R, @occurred */
4 @1 M5 x `& ^8 Q( o; }{ # w- [) A& ?! ^+ F- N8 |. V* f
printf("Graphics error: %s\n", grapherrormsg(errorcode)); 8 ], J; i5 M- Y# F
printf("Press any key to halt:");
: Q; F; `6 o j* z$ D4 F7 {8 `getch();
$ a0 K* @* V: ]3 E9 q9 E( T: Mexit(1); /* terminate with an error code */
. m; u( M/ I% V6 s6 z. [# i} : w) f0 K* A, C) A, h
x = getmaxx() / 2; $ Q. Q: |( x2 i, i$ w0 p8 L
y = getmaxy() / 2; 2 q& a* O- v! m1 m& e/ d. o
/* output a message */ ; O$ r4 X( M6 ^
settextjustify(CENTER_TEXT, CENTER_TEXT); ' s2 g1 G$ D, f2 {6 ~; ?/ f
outtextxy(x, y, "Press a key to close the graphics system:");
5 w; F. N7 @# D( j$ A/* wait for a key */
x- u1 o t+ ^& }4 q2 ggetch(); " X* \! R$ ?" H- e) |( {
/* closes down the graphics system */ + r- i0 O3 q/ W- i
closegraph();
) |' }! g% t4 y+ _4 Tprintf("We're now back in text mode.\n"); 6 ]. ~$ K1 Z( `) G6 Z% a0 T
printf("Press any key to halt:"); ! L3 D: Y1 b T. f& _
getch(); / `7 I# `" B) K; B, ?. ^
return 0; 1 \1 M8 j/ I# q* E! s, D
} 1 B) }4 {: d( b# o1 n
; c; D3 U6 g& v% \. y' G
7 F. v/ `. ~" b$ Z8 y" m- }
% {, [4 `9 u5 V# O) x6 F; x函数名: clreol
9 Z* |" ~+ ~' d功 能: 在文本窗口中清除字符到行末 / l7 D* I7 h: E; L( M! Q
用 法: void clreol(void); % ^& L' P# y$ s/ C$ H
程序例:
$ P. \: Z F/ |. T" q2 l#include " T7 J! N K/ c }1 W
int main(void)
2 m5 G8 J/ q: B/ f# _{ u% Q7 A# r8 ~8 o- g
clrscr();
/ R' F2 U' y2 V- Ncprintf("The function CLREOL clears all characters from the\r\n");
! Y( I& U ]: N& P% ecprintf("cursor position to the end of the line within the\r\n");
/ \- _; c, A8 u( k% c5 [" Jcprintf("current text window, without moving the cursor.\r\n"); % Z+ V/ y3 n- Z$ H% N/ [
cprintf("Press any key to continue . . .");
* x* o1 ^* j# b* I$ }& ngotoxy(14, 4);
6 u* o- w( C# U; `* w5 y! jgetch();
7 Q6 W4 r- F! yclreol(); * o! |; a4 A% j0 j
getch(); " z. I; \8 _1 F2 ^' \0 ^
return 0;
4 v' V9 _9 H! T. i}
6 Y& N) Q3 Z8 o; `: g0 w! J
5 Q1 S0 z5 H3 j! S" E7 h0 `9 Z: D& K8 j8 w) J" \$ {
% ~8 x+ q: U! M! b3 b0 ], b/ F函数名: clrscr
5 q" s( I2 d3 \: P* H) ?4 H9 ?+ `功 能: 清除文本模式窗口 % j% Z; s/ d' \: @6 n
用 法: void clrscr(void); 7 S" i6 q! L# F2 U+ g! l
程序例: % g! K- z" g! Q
#include " P) C' v1 n4 h! }
int main(void) # R. j1 V7 W& k/ `/ o5 ]
{ 5 O) {# g4 x$ @/ q
int i; 1 ]6 d4 k. ^- p
clrscr();
' ?1 ~$ z: H' n; `! w2 Z. r: e! u3 }+ nfor (i = 0; i < 20; i++) 8 x2 d5 ^2 N8 s7 [ q+ f
cprintf("%d\r\n", i); # `1 o7 J% ^" t" f
cprintf("\r\nPress any key to clear screen"); : Z! e3 v7 W6 ~% i0 G% N/ j m
getch(); & Z; f2 W" e! j3 z- u# W
clrscr(); - l9 n% l# P. ~+ ~
cprintf("The screen has been cleared!");
4 y& @6 y, Z; `- P8 C- o8 \getch();
2 h4 l4 z! `& d. }) ~6 N1 Oreturn 0;
$ O# Z/ Y ~$ v}
3 r4 S" D6 F5 U& @: U5 o
! J& {8 j$ m, ^5 o" ?8 K. i: I
) N. R! J% X/ J- \+ E7 H2 A3 T
: z' p0 B k& A! `1 z* O* Z% A函数名: coreleft ( l* q! h! N" b3 ^* q
功 能: 返回未使用内存的大小 , F6 B) `/ C' m( b+ z ~" E
用 法: unsigned coreleft(void); $ A! N5 W# T# P9 [; J3 u1 C
程序例:
: V7 P6 D& e- x. s9 x#include 5 I5 @8 g6 Q2 `- m/ R6 z
#include
7 v1 E$ M7 P" U& P8 f- t1 zint main(void) $ c; ^" ] b6 |* B/ V1 m
{ 6 e- i8 q! d& Q& ^
printf("The difference between the highest allocated block and\n"); 4 r- I# ~3 X# N2 T) d; h* A) K
printf("the top of the heap is: %lu bytes\n", (unsigned long) coreleft()); " p" A. b% ~$ u0 s+ g
return 0;
2 b* G- ]; E6 I3 i; z7 V}
2 u8 ?) B/ s! K% d % D" O/ c7 F- B3 f$ ^9 K
函数名: cos 3 Z0 E8 Y0 Q. F( r9 v+ m0 H
功 能: 余弦函数 ; `7 I/ p# m" R. P9 l7 Q: l2 G
用 法: double cos(double x); # f i8 U: T6 @
程序例: 1 h& T+ C) X. b
#include
* }& d0 {$ p6 D4 p#include
3 S0 J* t! `4 j7 e/ B: Gint main(void) & l5 @1 c" k* I1 P
{
6 Q( [. ?1 _( m: j0 `8 O/ fdouble result; * l" c9 }/ E- E% I
double x = 0.5; 0 Y+ e( u4 t M- n9 ~" a u+ R9 h
result = cos(x); 9 P, x( t( l- x9 F7 }
printf("The cosine of %lf is %lf\n", x, result);
, @2 q5 u7 R& W# ?; {return 0;
/ U+ b8 M' T; ~+ {5 X- v}
* J$ B% h5 z5 U9 A9 W# O m7 O) S
% f$ @/ m; J6 K: ^4 P/ Y ?* l! V! U! o& _/ l: ~" x' o( ]
7 \* u% b4 c( ?函数名: cosh 0 z- `: ^4 r1 C. f" q3 V' a
功 能: 双曲余弦函数
- l9 R6 F& A* h2 [# G) H4 p用 法: dluble cosh(double x);
+ b! J5 a0 m8 J0 K; y$ r程序例:
7 H, `3 V5 c2 Q8 \5 ^#include
4 ?- \! @! Q; ?( S3 k) r#include
/ v" [% i9 e1 h$ ^int main(void) . P) b) ?& x/ J( W S" G8 ^7 Q
{
9 W: O4 S/ ?, y% N1 `8 t5 zdouble result;
# y! W1 I1 N; h, Z' Z! Kdouble x = 0.5; . U; M' N7 T+ M2 G/ Z
result = cosh(x); 3 c7 V/ L9 {, v$ x- Z
printf("The hyperboic cosine of %lf is %lf\n", x, result); S- u C' c i
return 0; : I9 G! a* ~( l- W, e1 O; y
}
8 {2 s p9 l2 s% s! n! l9 x) b! |, H* J* w7 H4 r
' c& J( S- E* J# x& P/ ~( X% M' c0 z
p! X ]9 s9 I/ `# X! l函数名: country
% @: Z: a3 ]" ? @* K+ s. D功 能: 返回与国家有关的信息 4 u9 v! r- H% C2 {
用 法: struct COUNTRY *country(int countrycode, struct country *country); 8 g1 [4 {" U% e6 a
程序例:
) a7 z. A6 J& S1 N4 L3 I3 _1 H1 @#include
6 Q N6 n0 h2 X0 h8 A) M#include
; E1 ?9 J7 ^1 F% c" L0 x#define USA 0
: r: B) b+ n8 ~& Sint main(void)
4 [3 r6 o+ N5 n3 m* A5 I7 y{
# J9 j ~: V$ K/ ~9 ]3 {struct COUNTRY country_info; 2 t& _( S5 M: B% w2 J
country(USA, &country_info); . V( k$ s6 s' M- V. I
printf("The currency symbol for the USA is: %s\n",
+ p! I/ \& u, f0 @$ u9 tcountry_info.co_curr);
# j3 _3 g, X9 G; treturn 0;
9 ^8 x& w' g( I% `) l} 5 @, a* q2 R+ k. p( R! B; @
/ W5 T- H3 O/ U( X$ l! x
3 N0 R+ b% m2 k J X% F : J; ^, f9 i0 x# d& @: y% _
函数名: cprintf . J3 `0 c/ B( f9 |
功 能: 送格式化输出至屏幕 - |! N' n( A# d; U1 }& j
用 法: int cprintf(const char *format[, argument, ...]); o4 e2 k- M+ v( V/ v
程序例: . I$ P: ^* q( ^, _- b; s
#include 2 J/ X: m" [2 L. V7 R
int main(void)
4 B( F' o1 `. A- l! }) D& B g{ & w. }1 k5 D% i
/* clear the screen */ ) {' ~3 j* `0 ?7 h. b
clrscr(); % u* p/ j- M$ z) }' \8 {- W
/* create a text window */
3 q: \3 u0 u4 _window(10, 10, 80, 25); 8 w6 w2 v- C( C: R$ d* `% Y2 ^
/* output some text in the window */
, K' Q, U1 ?8 Q4 i s1 Ocprintf("Hello world\r\n"); $ i3 }7 B& Z, x* U! h$ @- B
/* wait for a key */ 6 X' f/ F# e% V7 \* Y9 C' x
getch(); 3 O2 |: H; M7 }' H/ H
return 0;
& p5 w2 ?. x) `" L2 {& f, A6 `}
8 _/ _0 X- s4 B. K' b( U. h8 X# @" @" M7 J5 {! l. |9 U+ y' e
6 y* c+ c7 ~4 ^- A$ O; X: ^3 g
2 I" n% g: l6 R9 D% p7 z函数名: cputs
7 S) c* S: X0 r1 T功 能: 写字符到屏幕 3 A, t$ l$ x7 m# T3 G
用 法: void cputs(const char *string);
9 s+ s' ?4 U2 ` F) T) m2 u+ Z1 g程序例:
3 a$ z3 L2 Y6 w#include
$ F' \6 t K! i" Bint main(void) - @# f) p4 `! T* B1 e
{ . `5 t0 d: U4 X) E# S* f. `
/* clear the screen */
$ i+ l0 a2 _" [, _$ Bclrscr();
5 j4 ]8 y: o# w( M* d! R+ j! M/* create a text window */
7 S* z" A ]" Zwindow(10, 10, 80, 25); $ A/ g/ _0 v' P5 z+ W: I
/* output some text in the window */
S- i( H1 G/ c' z2 H' ^' hcputs("This is within the window\r\n");
+ l5 D' `; G4 }5 _/* wait for a key */
$ z) b8 X I! y* X) [) kgetch();
: p9 B! W, g7 r2 f: @6 Yreturn 0;
) E! x. }9 ?2 m" ?" d C2 ^8 \} 6 G6 i" R+ ?8 o
: V( }0 U8 L. e' A9 Z& q# |% l+ L
1 t# ]+ ~% N- J6 y# \7 p/ C5 g
函数名: _creat creat
]( S6 P' `% Y( f功 能: 创建一个新文件或重写一个已存在的文件
1 `4 @ ^; ~) u4 w用 法: int creat (const char *filename, int permiss);
0 J7 t1 q& T& V) j( u程序例:
+ L- T# l) N: a; p7 r4 ^#include . g, l/ f% c0 x% z
#include
/ `' F$ p6 I$ F9 s9 `; G& @#include T$ ^: ~/ J2 i, z+ t# x/ `
#include ) G: S8 R& r$ O4 W! ]+ X
int main(void)
1 x5 g k+ E2 Y6 o0 M( L% p# R{
% \( l0 i$ [0 L* |4 n- }* `2 L ]int handle;
" i) k3 T5 h' X5 f9 W% f9 n5 o6 |: Ichar buf[11] = "0123456789";
m1 L1 t) v" T, u& P0 H- O/* change the default file mode from text to binary */ 5 N+ \0 a! e. F6 K+ q0 U
_fmode = O_BINARY; & u0 ^7 ?% p5 G
/* create a binary file for reading and writing */ 6 `9 g2 j9 x: o' ], R1 k! n |3 T) D
handle = creat("DUMMY.FIL", S_IREAD | S_IWRITE);
" P( s+ Y5 } X" R: ]6 p* Q1 E/* write 10 bytes to the file */
9 I, d6 J. |8 B! Bwrite(handle, buf, strlen(buf)); " Z; Q! m9 ?! F' E
/* close the file */ ( L1 @8 @, I: K( d& G5 l9 Z# d
close(handle);
( h& d4 U8 K/ e4 j& L; @return 0; * d# ~6 [) G/ k& N/ t' R; H! l
}
1 Z) e5 i" w7 v* c. l* }$ G 4 {1 J3 m5 c$ V$ H# e; U' i: \- x
函数名: creatnew
+ }' ^% g: q1 B, r- t功 能: 创建一个新文件
: H7 H4 d. D0 ^) X' ^8 A用 法: int creatnew(const char *filename, int attrib); 2 c- T$ z4 W; i
程序例:
; l% z+ C3 u7 c: n#include ) E9 O' g$ ^; Y7 Q' J; |+ e s* ~. E
#include # h1 O* }; T8 y6 p& J: D
#include
; ]. W _+ a' {+ O* n9 X#include " u; s2 a3 U( G& n& z7 p
#include , q z& Y% E! Z1 o( ?1 M
int main(void)
* c8 q, ]7 d/ ~5 }% _{
- {' n( @( q8 R$ Qint handle;
7 @: f5 d2 l7 A# Ichar buf[11] = "0123456789";
6 y- g+ i2 Z, \! i$ O/* attempt to create a file that doesn't already exist */
0 n8 W) a# f3 [handle = creatnew("DUMMY.FIL", 0);
4 _# s, U1 {4 c) z; \+ S$ O7 M1 C! hif (handle == -1) 9 I3 N8 R& W+ F* M/ T3 v0 b
printf("DUMMY.FIL already exists.\n"); . S1 T; L$ S+ ~" y9 I' t% e. ~
else $ n; b2 \- b+ `5 V
{
* y# g) P6 T; u: ?& f, iprintf("DUMMY.FIL successfully created.\n");
9 N& Y3 V& _8 f/ m" {, pwrite(handle, buf, strlen(buf));
3 R% h! V2 t. B) Zclose(handle);
7 N y5 Z$ p& N, O J}
! l u0 z: x- j; I# f! Jreturn 0;
" Q7 k& \- P$ z& C3 V}
8 H1 J/ F8 A" \8 A1 p$ l
: x0 i3 h, n b# S* U- j! ^+ ]) t, p, `
+ A& X0 a: x1 }% U: y; X
函数名: creattemp ; ]# O/ \( a& a/ C$ e* c$ L o/ z
功 能: 创建一个新文件或重写一个已存在的文件
4 y$ n- {, P- W9 q. a用 法: int creattemp(const char *filename, int attrib);
, L) z9 u$ Z' Q7 K( B% M: F1 K程序例:
) _' T& m8 I; S; Q9 S#include ! ?$ g+ w5 q# ~0 r+ }. q4 i8 O
#include 8 n6 A+ o% u: R0 [" e; A+ ?% E7 W
#include
4 r# {4 c# }2 N2 cint main(void) 2 {7 W' v1 P$ W" J' I/ y
{ ' [3 ^% X( C8 B) Z+ B
int handle;
' ]5 }' r* @- V/ dchar pathname[128]; 6 t6 ?0 n/ ]1 c7 s
strcpy(pathname, "\\");
- o( t; F& {: f3 K/* create a unique file in the root directory */ ) l0 n: X, h$ C) i
handle = creattemp(pathname, 0);
]$ A6 N0 x* h# }# g8 tprintf("%s was the unique file created.\n", pathname);
: E, M8 n4 T. ^ }5 S. G) Oclose(handle);
+ V H; ?: K' Lreturn 0; 9 e) J. ~0 R4 S3 \+ v
}
$ Z5 W& A3 d7 _+ a, P5 _0 I
5 V! K a2 s2 S4 m* n; [4 e. u3 T! a# n& Y$ |
3 V- {: H! a3 M; d6 W1 h; g1 D( F
函数名: cscanf % [; ]! q& J) N0 n. a
功 能: 从控制台执行格式化输入 " T5 ?9 ~( q3 f9 z
用 法: int cscanf(char *format[,argument, ...]); 6 a& J! H3 \; g# j! }
程序例:
3 f4 C u/ A# A9 e# k" R# Z' ~: X+ Q#include
3 B- U) @4 W1 P4 q- g8 Vint main(void)
7 F( s5 ?' y+ L, W{
) r# Q+ L8 l9 Cchar string[80]; $ ~# s! U; V% N, I7 G
/* clear the screen */ c* d% s' {. K
clrscr();
/ q$ v0 o, g+ y, R/* Prompt the user for input */ % {# L9 l8 b6 c# y, ]/ g
cprintf("Enter a string with no spaces:");
; b2 j! [5 W7 l9 _/* read the input */
/ B/ b2 y% A0 x) T4 q# d* P; I3 }cscanf("%s", string);
! P1 o1 |1 P+ X+ a& ~' d/* display what was read */
& _1 ]4 h( `' b8 B' i) scprintf("\r\nThe string entered is: %s", string); ! ^ s- J" v; {" w* ^% X
return 0; S8 ~$ ^* t: t7 p4 n
} / z( n3 |9 U( I6 s' K* a+ J( u
" ~0 s* D4 k1 p3 }/ F3 L9 B5 v7 P( T2 A' }5 f$ ?
6 T3 f7 _! k4 [5 R3 }
函数名: ctime - u3 b, i4 _' j+ e5 F
功 能: 把日期和时间转换为字符串
) ^" G2 k; d8 ]" T1 ?6 Q! B4 N用 法: char *ctime(const time_t *time); y" Z4 X! R* O' y1 n( c
程序例: . ?! l' a, O( r
#include , |6 C; {, I* c. Q( A! {+ U& c
#include # ~) X; q4 u$ _1 r, i( _% K3 T* s
int main(void) 9 a! j% _) i, S8 {' G: g: j
{ 4 a+ h. \" F2 \' t5 _# A, j
time_t t; ; n2 V, R4 f1 V# _' I( s0 F
time(&t);
# d' ]5 {* B8 B v( C2 W: `printf("Today's date and time: %s\n", ctime(&t)); 3 s" X6 j& ^6 y. S
return 0;
1 N b* Y( A- n" W2 d}
" c5 e, g! N- D) j$ I# r3 j* N( N6 _% _8 t+ H$ | V
3 Z6 H3 s7 a- t! ?
2 F3 e4 v& G5 X6 @2 }7 P, n) Y
函数名: ctrlbrk
/ N9 u9 z1 t+ V3 _功 能: 设置Ctrl-Break处理程序
0 G3 W2 [! c$ {/ e6 d用 法: void ctrlbrk(*fptr)(void);
/ y( c" R7 g' U( |7 Y! V程序例:
- Q" l! p8 W# k#include
, F# t: x' y# u/ r! j, {) a4 q#include
( D% ]+ c' K' H1 P#define ABORT 0 ! b) e" \. D1 B/ p. q8 h
int c_break(void) " |1 n) I$ T& F4 z. f2 \
{ 7 r' p) _# w1 I) I! Z* G9 F2 H
printf("Control-Break pressed. Program aborting ...\n");
/ G2 ], s5 L8 {& M! _return (ABORT);
7 W: I( N2 Z# A8 Z' \- ~) n} 7 L# J- ?% c2 d2 u' `2 x- m
int main(void) , p$ k/ K& V; j9 B e5 S
{ % Y5 i4 Y8 J# r% }" Q' m
ctrlbrk(c_break);
5 t d% L" V2 t8 Ifor(;;)
6 U- K- a% i* V; L$ L `+ p; n{
) X R* b( E+ U0 \8 s5 \8 ~printf("Looping... Press to quit:\n"); 7 x' Z: l1 E) ^8 H0 R3 H
} 9 n5 g% T: z1 q! h8 t! A
return 0;
4 q& b: }6 X4 q} |