|
函数大全(c开头)6 a; a; Z0 V6 L% h2 Y1 @
# I- |" F, `2 j) i2 C8 l" `, e函数名: cabs " i% n" `9 l, f8 c
功 能: 计算复数的绝对值 3 o6 V# b1 b4 l/ O; O+ w
用 法: double cabs(struct complex z); Q" }( D" W% C) \! l8 z q
程序例: 2 U9 t: o7 r0 m& X3 ]
#include 4 K+ m9 f. x8 @7 f( N
#include $ }# u0 W9 I' Q2 A- I3 x: y
int main(void) ; E0 H6 e# r* e% B) P% E5 u( a0 Z
{
1 I$ @7 I2 P; D: Y% a' `4 D' ]0 Kstruct complex z; 4 M3 n: k% Q$ |' y) e
double val;
" |, r3 `) f# l+ u2 L* o; `z.x = 2.0; 2 W1 s ~8 F& j2 n
z.y = 1.0;
. @$ \0 ?2 c% ?# A4 Xval = cabs(z); 7 q1 f* B- K2 ~
printf("The absolute value of %.2lfi %.2lfj is %.2lf", z.x, z.y, val); & [/ e% |# S$ z; V2 a0 X- R2 f# D3 M
return 0; + C+ ^# [1 Z: u0 T# i
} / G+ Y% c$ G4 c1 j3 i
: P" D% l( }# P s1 K- }! K
; P6 O, p% [& g0 }
% c) c- @- A8 O函数名: calloc
4 L& }# S% } P7 @- q7 u功 能: 分配主存储器 0 y! w6 | H, |6 `7 Y( a% b. Q8 h
用 法: void *calloc(size_t nelem, size_t elsize); $ |: n$ P2 Y' Q5 j& R5 o
程序例:
; V' J. J P' e# P( N/ X7 ~; p#include
P) U4 w' m: T8 R% S#include - j/ y9 ]6 O5 A6 l6 d2 Z7 i& r1 E
int main(void)
1 i: b3 ]6 T$ k/ n1 \6 F Y{
! ^+ d& ]0 A: [4 R7 n+ achar *str = NULL; + H2 ], L- r' m4 z, J1 {
/* allocate memory for string */ - p' V9 W8 i, [. f- _" Q
str = calloc(10, sizeof(char)); 9 O" o9 P1 Z. B- N# B
/* copy "Hello" into string */
! h6 t7 m4 R6 v7 y+ @strcpy(str, "Hello");
: C" l# ` {# V/ t7 y9 M/* display string */
; n4 v& ~% m1 s; S- g& t- S& kprintf("String is %s\n", str); / l; [& G0 r. q1 H
/* free memory */
& L/ V) O4 h4 V5 ]5 Qfree(str); 3 j$ e$ E$ Z/ m! O. Q2 \
return 0;
7 _/ U, F4 A+ S( A# q} G) t# b1 d) E4 f. Y; ]( C
; ~7 b4 T" o/ M" o3 G' @8 h4 _0 J3 k! X7 j
/ ?# u: N- i3 S& M函数名: ceil
! c6 e! k. v, v/ L功 能: 向上舍入
% G# Y G, X* L- h Z, l# w用 法: double ceil(double x); # d# Y+ K3 B. ?# W/ d+ R7 L
程序例:
& m! {( s8 k3 Y3 W7 B* i& `#include
( @- p5 ^" a% y6 i* C#include - L" l; M& P R i2 M2 |8 V# D
int main(void) - ? b6 C* R: ^# F9 `! {
{ : P0 A! }' }! G# B) _
double number = 123.54;
/ y9 D. Z' ~& \! ~8 U7 ?/ E( gdouble down, up;
- g l% y, H2 n8 j2 u- Ydown = floor(number);
* @ | k5 T1 }3 F3 s9 J% V; n& [$ s6 N. v, Zup = ceil(number); : i& H4 X0 f; V) x: w$ `
printf("original number %5.2lf\n", number); " M: ^) l$ O) B. P3 b }9 w
printf("number rounded down %5.2lf\n", down); % G$ r# s9 [( Z ` O
printf("number rounded up %5.2lf\n", up);
" B# j; ]7 D4 _* s vreturn 0; + Y/ ~6 w" r- B0 _2 O7 a
} + w. y @( [) Z' _
, h) V) N$ ^+ T& J; y8 G9 o" v0 Q3 R: b/ _, T" M
2 ?; P S* l- x( o9 C, l函数名: cgets
) l. C+ L( S v7 S, r$ {功 能: 从控制台读字符串 ; O! f4 R$ j; O# Q7 X( d
用 法: char *cgets(char *str);
6 D' t* j# w; `5 \/ _% @3 d程序例: * Q8 J( l3 Q/ b1 {9 x% j5 J
#include ' ?; J/ s" O& H' h' x9 ?% n
#include
) E0 q j2 G: d: yint main(void) ( S0 I+ n) F! O' n2 }: Y7 i/ L
{
+ u/ Z% { P5 Echar buffer[83]; # r9 s r; x0 q3 i2 p2 x2 [( {& q
char *p;
( b" c$ ^6 k% z1 H |/* There's space for 80 characters plus the NULL terminator */
- G) R% _* x( X1 V2 q5 Kbuffer[0] = 81; 0 L7 M$ f2 r8 Y& ^
printf("Input some chars:"); 5 G* N4 U- a% Q% v1 [! Y
p = cgets(buffer);
4 B3 L2 h8 C& f3 Tprintf("\ncgets read %d characters: \"%s\"\n", buffer[1], p); ( ]' M& e. C' u
printf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer);
3 Y# z$ D1 Y" _( {' q' [/* Leave room for 5 characters plus the NULL terminator */
+ t% V7 Q9 S! | _$ z0 h$ L8 m& bbuffer[0] = 6; 9 _% b; S* k, D
printf("Input some chars:"); 2 A$ c! ]. w7 c0 H
p = cgets(buffer);
7 v3 O; R% u/ |. tprintf("\ncgets read %d characters: \"%s\"\n", buffer[1], p);
1 I8 K/ w: H9 [& H& s& C9 hprintf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer);
+ w8 Q$ }# B$ P& L; ~0 t6 zreturn 0;
[8 E! l. o2 Z" [7 q1 k* W7 ^- z} - j% l( ?' y0 i5 K* C
; |- f# K* T, y" E
3 H0 r1 K) e! g, T0 C, D1 ^
0 t; R* m. c8 l0 T: Q6 |# K函数名: chdir
( a+ K0 y Z0 H; w3 [% G5 J功 能: 改变工作目录
6 I) H1 x# w, { a3 U用 法: int chdir(const char *path); * k1 z& Q/ l/ u6 H2 v& m- N- @4 B
程序例: - d# n. U: f0 _0 ^. o/ l* r
#include
3 U& D. [$ _' U3 {#include
/ W4 W& f. \6 q! j. x% Y% i#include - q2 r- X; p/ t3 r ~+ i
char old_dir[MAXDIR];
4 x% ~2 n0 R: Xchar new_dir[MAXDIR];
. L) `4 E$ D* o- vint main(void) % O' b8 U$ B# k2 r
{
+ t+ N. Y) G c7 r( h: sif (getcurdir(0, old_dir))
$ v1 b' ?* i# x9 }8 f/ ]: Q) q6 W{
1 `; i2 q! Z- u' w9 l) ` Xperror("getcurdir()"); A4 Q' z8 i F9 r3 w- x2 N: x$ f
exit(1);
% t3 q, E: a% `& G. m} + Y3 q1 n4 N. R7 f2 l3 G7 O# o
printf("Current directory is: \\%s\n", old_dir);
1 P% N- U' `" [" {& i% B6 x$ Q( fif (chdir("\\"))
' n6 X( b, D& N- y{ 2 w/ B' y- T% k) `: g2 g$ c/ _$ i
perror("chdir()");
; ^; c6 Z! g5 U+ g# ^/ jexit(1);
' [0 U0 b' c4 F2 v$ b/ p; R1 O0 Y- s} 6 j1 N$ e1 [9 m6 {! |2 u
if (getcurdir(0, new_dir)) 3 ^8 E" A; k& Z
{
1 z0 A& o+ j2 [9 s4 q8 M7 |perror("getcurdir()"); 1 J5 q% g) p; D' e+ M
exit(1); ( c+ S1 x2 ^ W- p% I- V% d) h
} / ]# P+ v: l: w8 e; e0 N
printf("Current directory is now: \\%s\n", new_dir);
* i2 h% f5 i' T* o* Dprintf("\nChanging back to orignal directory: \\%s\n", old_dir); " B6 M) [+ J0 m& _* p+ s1 s/ [7 s
if (chdir(old_dir)) 1 P( l$ O5 Q6 ?% _, A. P4 K& F
{
+ l' T) b) x6 H7 `" {perror("chdir()");
. X& d& W D5 A+ }: t" s, Jexit(1); . X% [ P! g; f, e" `/ h0 k
} " j! r1 Q/ [5 |1 o) S% h3 N j
return 0;
- y+ Q4 ~" c0 s8 h# o7 o}
: b" p9 H# @: b# |# a- b$ M$ R5 @( h. r5 ]2 {- A% Q; @8 c
5 P+ K A+ p' f' l
函数名: _chmod, chmod
: o( @4 I/ o( x L; H$ M& S' F E2 l功 能: 改变文件的访问方式
7 E; q; G3 T; g/ ]0 a6 w: H$ ^/ R ]用 法: int chmod(const char *filename, int permiss);
# a6 i# V" k# q0 v6 [0 @程序例:
6 L5 D1 l. a! c+ Z* u) X- C#include * T% o4 D+ U+ D! o/ v7 r- Z
#include
2 v4 p4 _% C' w+ F# V#include 3 q: u9 \8 F( K8 q" e; P" d; a
void make_read_only(char *filename); 4 l# a0 t# K% S/ a- W$ Z
int main(void)
2 v$ ~9 y; X& A; W4 Z( r{
/ s+ C0 n3 d0 H6 o) Y2 Rmake_read_only("NOTEXIST.FIL");
8 U/ X! T/ V# v1 Smake_read_only("MYFILE.FIL");
9 n+ k" g" y. }% areturn 0; 4 [0 k5 L- J% \! c* X+ }4 s
} * i3 o6 U% L+ E) `; m8 F* [+ j, s
void make_read_only(char *filename)
" {; u0 k/ S1 w. K6 A% x0 L: M2 O{
& K: s7 b$ r! J: y% iint stat;
; [: D- F$ k2 ?stat = chmod(filename, S_IREAD); 0 G" N! V: y2 ^% q1 ~! k
if (stat)
# g, d+ H+ U) Q+ z1 Pprintf("Couldn't make %s read-only\n", filename); 6 I6 x8 z ?9 c2 V4 x
else
9 t2 |% N5 r& T+ v& Q# X/ }printf("Made %s read-only\n", filename);
0 |; P3 L- X, v( ]' e8 }& }} 9 ^ O {8 h- Y
) A# n1 N6 A, I( ?
% y# S# l; Q7 i C
0 W) k4 d* s2 b% q* D函数名: chsize
& O8 z$ g, |0 M, c# x# h功 能: 改变文件大小
' o, y+ n0 m$ c, T3 _$ H$ Z用 法: int chsize(int handle, long size); 5 O( z9 j6 J6 c6 i& x
程序例:
5 i4 f. z* {" ~9 F* l6 B#include
7 ?! q1 n' g# L g9 v( s- n! F#include / w% W/ W0 `) O; |
#include / G4 M4 z* G8 Y- A5 A0 T/ K
int main(void) 6 N" k6 d" I4 ]7 _8 Z+ F/ J, [, t1 R
{
2 L1 Y$ E: f' M# a# B F* Sint handle; 9 |% M* u5 M7 f; h3 y6 Z
char buf[11] = "0123456789";
u. A$ R U i5 R" [1 a: V/* create text file containing 10 bytes */
- _+ E3 b5 l( F1 zhandle = open("DUMMY.FIL", O_CREAT);
) `$ h A2 c5 D) B1 f4 a: ^write(handle, buf, strlen(buf));
2 H B% F7 P7 a! K( @( @3 Y/* truncate the file to 5 bytes in size */
8 b1 F6 w3 k. Cchsize(handle, 5);
0 m3 ], v) |8 @7 T% J7 L% U/* close the file */ ) b9 y7 H: ~2 R; @0 A
close(handle); % S, d( p" K) V+ g
return 0;
: ^ t$ \: D9 S4 k6 n5 m} 8 Y: o' f* r$ Z1 v6 R7 L
# S3 d7 {' M) X( y0 d' }/ ] 2 G L5 k- E/ X$ M$ m. u
函数名: circle
; }, U7 N$ t+ b" P& I9 x功 能: 在给定半径以(x, y)为圆心画圆
4 Z) I: _ c ~; t用 法: void far circle(int x, int y, int radius); & X* k W9 O6 n" J+ o" e
程序例:
2 H8 b; x6 X+ L" n6 b) A#include 4 T: ^, R8 B: u& ~" g3 \% j9 |7 d5 P# x
#include 7 a8 M. P% a) i7 B, y' m
#include
/ I i8 l9 J. `! g#include 5 D+ g& |/ `* U6 M3 _2 Y4 E
int main(void)
# A6 K C2 ~ |" ?4 x3 O3 B# ^0 @7 x! @{
* o; `5 q+ E, k- Z% I/* request auto detection */ ( \( t( B6 U, T- B6 ` E
int gdriver = DETECT, gmode, errorcode; + I. I3 ~3 B1 [# W. k9 u
int midx, midy;
/ V" A* C; X. ]6 u& t' Eint radius = 100;
b1 }4 q$ @0 E1 V4 ?/* initialize graphics and local variables */
* b1 U j: b5 R+ @; k, |initgraph(&gdriver, &gmode, ""); & i$ g! f3 U( L
/* read result of initialization */ ; y" t, O) Y+ r1 }, S. R. L
errorcode = graphresult(); / D/ y( U- I: l( a* n4 e( Y
if (errorcode != grOk) /* an error occurred */
# [. G1 S8 O# y{
! V. M. }) k& d% |4 l& oprintf("Graphics error: %s\n", grapherrormsg(errorcode)); & E4 s/ f) G2 W; `$ E7 `' E
printf("Press any key to halt:");
% p$ D0 C( A# Ogetch();
) I3 H9 O, ^# I+ G7 ^4 Y- Jexit(1); /* terminate with an error code */
2 h2 n, q: m# ]9 e- u} . n2 k S9 `* O/ }' x
midx = getmaxx() / 2; 5 {; r* f. g9 z5 t6 B
midy = getmaxy() / 2; * K! r( h8 N; e( N% @$ s
setcolor(getmaxcolor());
1 g6 O/ a! J) W/ k" o3 p+ z/* draw the circle */
2 J. d6 A1 N @) ccircle(midx, midy, radius); % y$ ?2 }! ~( w4 B6 Q
/* clean up */
* M6 P6 r2 `& o7 O% Cgetch();
2 S( D2 M' d2 H& p: V. uclosegraph();
: `4 T i' @) N0 @- V0 }) preturn 0; 0 G4 P* e) K$ p
} / ^+ N G* J$ P2 U* V! ?9 f
5 _! `$ d* V4 z5 }% l+ f
% k: S5 A0 |/ \! W) v |, G) E
- Y' E" k3 Q9 j- t1 o! ]' X* L/ s6 p函数名: cleardevice : r5 f) W2 x3 W. n) W
功 能: 清除图形屏幕
* N {4 d5 Q; h" l8 D用 法: void far cleardevice(void); * E6 T$ r/ x; \) j2 V
程序例: / {' \: \" v1 X3 h
#include
t+ _6 Z2 ?2 O Q/ S#include
( X8 J% u* ~$ Q: Q) n/ }#include
^; T6 `% g; g7 k9 N; u3 t7 I#include
+ c6 ~3 X- f" T1 a# E6 N3 E% aint main(void)
2 A+ [7 I- |7 r2 F{
J3 S& ~$ ^: B3 m/* request auto detection */
% G" B. B- c( G, ~& `int gdriver = DETECT, gmode, errorcode;
+ m8 z0 s" ^) N4 cint midx, midy; + N. H& P7 J, M L
/* initialize graphics and local variables */
7 _2 n3 M' X& D6 X# d* S9 ]initgraph(&gdriver, &gmode, "");
$ r# a) u" D- H/ ~( T! G/* read result of initialization */
" N1 s8 @/ K9 Uerrorcode = graphresult();
9 _* J4 d+ d( k3 S$ iif (errorcode != grOk) /* an error occurred */
/ {0 {3 y, A9 V& u R5 @3 m5 m{ 3 e5 n, o( D8 X0 ~- U
printf("Graphics error: %s\n", grapherrormsg(errorcode)); B' t" L- D- a) y
printf("Press any key to halt:");
1 r8 a) @" l) ]% h9 x! C. Fgetch(); H o5 J% |* x1 B1 Q$ u( M
exit(1); /* terminate with an error code */
* p: M2 a. u# t7 `} 1 \# e E) Z1 l1 I |
midx = getmaxx() / 2; 4 Q* P, f) ^, `4 D4 |9 N& u# w
midy = getmaxy() / 2; 9 O+ t+ ^" l8 o* @6 q+ M* J
setcolor(getmaxcolor());
+ `/ d' L# L5 g9 o9 ?) E/* for centering screen messages */ - U# F: Z, E( k# ^- c. P$ r" i2 {
settextjustify(CENTER_TEXT, CENTER_TEXT);
5 b+ F9 F3 |. Q/* output a message to the screen */ 0 z( y9 Z: G: b8 Y0 g; Z5 B
outtextxy(midx, midy, "press any key to clear the screen:"); / P& H d8 |6 ]; K; P1 u& F
/* wait for a key */ " i. c! ^, G3 u+ X5 p9 n% @+ Z
getch();
7 J! @0 }: K) `! [3 m" E; `$ r/* clear the screen */
+ K+ A; E! R' M" l$ |cleardevice();
5 P- V$ A& w5 p d# u9 c/* output another message */
/ b4 i9 s; K( s, z4 T; Souttextxy(midx, midy, "press any key to quit:");
, e& F* y2 u( v, N/* clean up */ 8 b, y* q1 v/ N9 C+ p2 F% \/ O* k: l
getch();
6 g F& J. R N7 @/ l; ^! f" z5 Fclosegraph();
9 r4 Z: V) G: \return 0;
1 y8 k4 F; l- P3 Z} : b) h2 \- B, w" s9 c
" H5 S) `& x6 y. k4 x0 Z/ `
1 j/ ^9 O7 V( L0 l A
2 E1 B, o' ?1 W, _函数名: clearerr & P0 p- _( j" S1 c3 B; m, A
功 能: 复位错误标志 S& G( I+ j G6 P
用 法:void clearerr(FILE *stream); " I4 B! p% \& K6 [4 W$ A) o
程序例:
]3 S4 R n3 T2 M' W6 n#include
4 t# n! P4 P! K$ wint main(void) + S. m/ ^5 l4 R0 q: U; z) C6 s+ T" b
{
' Q) n5 N! E T% oFILE *fp;
8 Y" J0 ^# O# Jchar ch;
a* \* M7 N2 R, X5 O6 q/* open a file for writing */
: J" k5 w1 c& G5 }5 S; y# Z& ?fp = fopen("DUMMY.FIL", "w");
2 F1 A$ S# U4 P$ a1 K8 x' n7 G8 K9 ]/* force an error condition by attempting to read */
& [. d( G6 V) l. x nch = fgetc(fp); 6 C# f$ t7 w' r: A2 i
printf("%c\n",ch); 8 d2 q6 g1 O' ^! D
if (ferror(fp)) ! X- a* w" g" {* @$ m
{ + @/ K7 C" _6 l: [
/* display an error message */
6 ~8 P( }4 x" C4 {$ V3 kprintf("Error reading from DUMMY.FIL\n");
9 T0 a P" ~- U* G" S8 l, X/* reset the error and EOF indicators */
2 f5 }" E# I- g& [/ \clearerr(fp); 4 K1 q, P8 h% l, `- T
} 2 Z- U B# j/ I' [
fclose(fp);
( R d# k: i0 Jreturn 0;
. ~- ^) J1 O3 w+ e}
- S2 P! a- e z c7 r6 Z! U3 ]+ m
/ K; o4 _4 w6 P
+ p" I7 ]5 ^3 Q! T $ r- H9 V# h' B! k) f2 \0 h7 X
函数名: clearviewport , N4 q5 U2 u j& D5 C$ u0 N! Q# I
功 能: 清除图形视区
( F+ E. z# U& T2 @& \& b用 法: void far clearviewport(void); % i/ ~8 Z* h* \! Z0 O) y
程序例:
6 ~8 ?3 q. r# v4 d8 W1 t#include
3 k5 T! R* s' Y( y4 @+ ~# Z) L#include + u& u* K& n8 t" j
#include . M+ v7 }& H3 f4 \! e6 H
#include
9 r0 p; T3 o1 T, y& p6 G8 x3 H0 b#define CLIP_ON 1 /* activates clipping in viewport */ 0 r" p; m- f1 [4 q; g! K' ^5 I
int main(void) + l* A3 M. k. v% D5 x# ~8 b7 n! S
{
# w& s! L; s- l' k4 e- w8 F/* request auto detection */
/ G( R k+ m' x, Cint gdriver = DETECT, gmode, errorcode;
. c N, T' \! c9 Qint ht;
/ f/ Y. W4 v% O1 F/ Q2 v/* initialize graphics and local variables */ 9 C) n% z; h( n% H6 n- ^+ |/ V) c
initgraph(&gdriver, &gmode, ""); 1 u& c6 {, @' z
/* read result of initialization */
4 J* l0 b5 P# D& W- Ferrorcode = graphresult();
U+ m$ P, ~ D, i& Jif (errorcode != grOk) /* an error occurred */ ; C; ]* K1 ?# L/ }
{ 4 O7 r& o7 X2 @
printf("Graphics error: %s\n", grapherrormsg(errorcode)); J6 G/ w6 o7 S, i2 Y
printf("Press any key to halt:"); 9 q( e9 W' I# j9 ?% ?
getch();
, H4 J% N' {- iexit(1); /* terminate with an error code */ 3 B% K7 ?* {" L' h
} + M5 i2 }9 x6 f$ @9 J9 G$ w
setcolor(getmaxcolor()); 9 p* | p* F( q4 V; N
ht = textheight("W"); - \& h2 J+ n" s, H" n) _
/* message in default full-screen viewport */
' V/ f4 S7 S5 h, e! |outtextxy(0, 0, "* <-- (0, 0) in default viewport"); " ?& P; ~, p, ?( A
/* create a smaller viewport */ 7 m4 j) _/ }3 x9 p% A6 @
setviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON); ! u# G& P8 `1 B3 i8 m; V1 o
/* display some messages */ * _. s. |/ A/ s) w* O+ R, ?9 L q' D
outtextxy(0, 0, "* <-- (0, 0) in smaller viewport"); " H% y" B5 h4 g2 z0 z1 f
outtextxy(0, 2*ht, "Press any key to clear viewport:"); 4 e) I; i6 @6 R8 d9 ^+ t8 ]& }
/* wait for a key */ 8 I; @! }8 y7 X; G; [ P
getch();
& J1 X) V; K7 G9 h" z& L/* clear the viewport */ w9 A4 L. A$ t4 d
clearviewport();
( R4 h( d" `% s4 g7 B' U/* output another message */ 8 H( P1 j' O7 y w: \# @# y
outtextxy(0, 0, "Press any key to quit:");
! Y9 @5 O; K' M' @, ~ w/* clean up */ 4 p8 u) k7 ^' T7 Z
getch(); * \6 ~& N: m+ D/ S
closegraph();
4 s, O3 U; r5 j/ sreturn 0; 9 ^* N4 ^( ^ u& p9 Z
}
4 d7 |) o' t+ T% S0 }
1 s8 c, X9 {' A8 m& h' q
9 n1 U5 ^$ F, {6 B0 O/ c
- X9 d( c; V" y/ f函数名: _close, close 7 J7 t z6 D( @8 ~' i# H3 {5 N
功 能: 关闭文件句柄
7 s6 Q) |# c# n O用 法: int close(int handle);
. t4 f% U6 @ Y% s2 E9 Y0 Z程序例: ( W& b" W/ b5 c, Q) U
#include
, f( x* c# I7 B/ L! r* |9 G#include
7 o+ Z% u6 K) |8 u6 T @#include
8 F' T5 i$ \$ E6 Q" D6 x7 f) x#include * ?2 h7 P `+ E
main() , d0 N+ w ~* ?: M! N
{
9 w! R$ C/ y7 `8 _1 E$ Vint handle; * N+ f! @3 b8 z* `6 k, y' R9 n
char buf[11] = "0123456789"; & S* O% g# h9 K2 j* o: t3 B6 f7 J
/* create a file containing 10 bytes */ + _8 O% a$ O/ Y4 L
handle = open("NEW.FIL", O_CREAT);
/ l; V$ W8 F9 F" e8 ^0 Sif (handle > -1)
+ `- b9 S* c5 m4 w2 t$ R{
{+ O( T3 N2 \$ o. N+ nwrite(handle, buf, strlen(buf));
2 E. z( f g" w/ W2 j/* close the file */ 4 {$ I- X; u6 @; h- c" }, u1 @, y
close(handle); 6 U8 Y; i P( s2 N0 o; r
} 6 }% f3 p4 B) X7 E4 B! r
else
$ F: }- h9 I! ^5 x1 D! l7 f{ 2 l. s- }$ h) y3 o2 B- t
printf("Error opening file\n"); + d* W9 k7 M6 w0 J& R0 p! J3 s' K
} $ e4 R4 }6 [! [9 r, L* P
return 0; - [+ U/ {/ H" r" L2 {
}
$ ?9 d! A6 q6 g! P8 c. q0 C% J% }8 l6 x2 M( T
* m* Q5 j& z, J) |3 o* k+ }% N! T
. Y- \& e" t% E; @2 F0 j函数名: clock ' y# O. S- m5 a6 i' i
功 能: 确定处理器时间
) T( n" b* h# ^1 r. r用 法: clock_t clock(void); ( e3 g, z* ^& s, `1 O. Q* m
程序例: , M3 C: C3 T X: R9 `
#include
' J/ |7 L) i8 ^1 ]#include " m" w! `! `8 L* \% Z f1 q$ O2 t
#include # S. F, Q$ b6 P2 Y1 z' d
int main(void) * l8 v A6 w8 A- N2 U; D1 r: P; t
{
6 V' a1 E5 U/ @9 h% C3 {+ K* u, b) Gclock_t start, end;
% q9 b' O* g/ ^0 Sstart = clock();
7 Y1 ~7 [/ ~' I" edelay(2000); 4 }- W1 f. \# B& `7 N2 D
end = clock(); 6 W/ c8 l4 f1 S) ^& X' u$ s% ^
printf("The time was: %f\n", (end - start) / CLK_TCK);
& ^/ X# E- w# m7 s! o% o. ~return 0; ' f8 X. w9 n. w: q4 t2 E1 M
} * h# ^$ J0 V1 b' x
# P5 |& w7 C ?; G0 e8 O3 k* ~
% W% E# j+ ~/ q @8 o- I! d6 } ; Z$ i4 Q# L% q: d# l+ H
函数名: closegraph
6 Q0 P; O7 l2 X m/ w功 能: 关闭图形系统
X/ {: k8 b0 g' q+ L用 法: void far closegraph(void);
( ? S$ Y. `+ x8 b, F0 k6 ^0 b5 j程序例: + D9 S$ p: D6 X$ |
#include
1 ~4 S1 u6 H: ?( h: s3 g#include
6 |" ]+ f- M+ d. q( R8 v% M#include
0 F9 |) h3 y, t, c P#include {4 w- L9 q8 U% J& g! H6 E
int main(void) * ]* e/ e& T# b ]/ I4 Q& h
{ 7 f' r5 v7 p' F, o0 ?" `
/* request auto detection */
- U, W& f7 @5 M) F# y$ C, S0 Vint gdriver = DETECT, gmode, errorcode;
# _* w) P" _. t8 lint x, y; , \$ V& j, b# o4 b( D
/* initialize graphics mode */
/ p! V6 @% f* Y' v; W( h! E8 Minitgraph(&gdriver, &gmode, "");
2 l) r- k* n5 }3 ]9 y4 o/* read result of initialization */
- l! k; C0 }: @( n; \7 N9 d( Uerrorcode = graphresult(); , V' ?; ^# P0 {/ ?* R" I
if (errorcode != grOk) /* an error
1 T% q5 ]7 Z% Foccurred */ 0 T3 V% b9 M) c5 {
{ 1 I/ m$ e1 f. i u1 z
printf("Graphics error: %s\n", grapherrormsg(errorcode));
5 y: g m" }+ t% Jprintf("Press any key to halt:");
7 y; k3 a1 _' `" L1 m3 tgetch(); 5 L' h0 p5 f3 H6 Q( W7 V0 X0 a- [+ }
exit(1); /* terminate with an error code */ ) B3 M/ J( N5 Q3 k% W. M
}
. V8 a3 N( E, `x = getmaxx() / 2;
& _3 M+ E6 c* ?0 }" }y = getmaxy() / 2; - O( O9 o* u6 S4 q7 ^
/* output a message */ # R4 o1 v/ {8 N1 z# R$ I
settextjustify(CENTER_TEXT, CENTER_TEXT);
1 w: Z- B/ X# Y' R1 n( F/ J, Houttextxy(x, y, "Press a key to close the graphics system:");
3 _9 U4 `( w2 B/* wait for a key */ 0 T" {2 Z) s" d6 g5 t
getch(); 1 T* C; N. o( }7 ^+ o
/* closes down the graphics system */
4 {* F* g- W0 k1 m( t8 s! m9 y5 mclosegraph(); + ^% t* ~. q$ E" _. M u3 R
printf("We're now back in text mode.\n");
6 Y) z+ T5 R! T4 {: U! b: wprintf("Press any key to halt:"); {3 v$ s/ |$ _1 ?
getch(); # M# x% L- c" B& ?- m7 x# N v8 _, H
return 0; ( K; T6 W6 R. C9 w
}
& S$ C" A Q) h; r% k% w
, }5 E# n. e, V
8 D: {! f V! C ?2 P" z5 {* P
函数名: clreol + I* ]: ~9 K, J# r
功 能: 在文本窗口中清除字符到行末
9 u* S8 F- X- K用 法: void clreol(void);
& B: j: ?1 O, X程序例:
1 s/ s: e9 d& I, f9 j5 I#include ; L" N* |! ~0 g0 ^% a4 q
int main(void) l1 Q# s% E# J) q( E
{ ' ^% a( q3 n& P8 ]9 C# H' N
clrscr();
5 Q2 N* l; O7 p8 `; Ucprintf("The function CLREOL clears all characters from the\r\n"); 9 S: F) o# g4 q/ X
cprintf("cursor position to the end of the line within the\r\n");
8 Y' {' M3 E- K* ?/ [6 Ccprintf("current text window, without moving the cursor.\r\n"); & ]! T+ _4 W/ x6 ~
cprintf("Press any key to continue . . ."); " g7 \5 D$ X+ v, H
gotoxy(14, 4);
1 a) b+ w9 |% {; F/ o0 {getch(); & R5 ~4 @" p0 `3 ]8 u8 T( c
clreol(); ; `" t/ l; h: v+ c+ O
getch(); 4 t3 q- F4 s7 s3 e$ O( h* t/ a
return 0; + c# s2 x; h7 Y5 Q$ W+ d" c
}
/ a3 R5 q2 l _. F G5 ?" ~/ K6 O# r, |7 M
3 G# \% k' o- l2 \ | ' X) t* j7 U0 s# g
函数名: clrscr
( j$ q5 I, q0 L& G/ f功 能: 清除文本模式窗口
i( w3 T$ g4 N& r$ {. g* G) G0 Z用 法: void clrscr(void);
! [8 O0 X# @0 u5 D* C& {2 [程序例:
- ?% N0 r) ?* a4 O#include
; M/ ~& j( g/ Dint main(void) ' Z9 v: D& y. F4 q$ q: E
{ & N) @1 h, J; ^" Z; E
int i; 5 d% l$ r" _3 L5 W
clrscr();
- [2 l/ z0 ~9 G& H7 [ M2 lfor (i = 0; i < 20; i++) , Q% b/ @- G" k$ g0 J, {
cprintf("%d\r\n", i);
4 U' N: I5 ]$ ~, i+ r; d! pcprintf("\r\nPress any key to clear screen");
) }* C( D, \$ b$ y$ ugetch(); ) g3 Y4 O O0 h. {/ A
clrscr(); ' b) a8 u* i* |. Z. G' h
cprintf("The screen has been cleared!"); # e/ T6 ]. U; Q. U4 A$ T5 w
getch(); 7 k% G4 }/ A( x
return 0; , s% @4 j5 {3 p; m1 g X: D
} 1 _! g2 G2 J8 E) n
2 T; Y7 @% q# n: c( ? _8 s9 ]9 d+ `* f+ @1 {& `+ j; T
, `# J4 B+ R4 O函数名: coreleft
; r9 l+ W6 p3 Y; m/ y# [功 能: 返回未使用内存的大小 4 L, j, u6 q/ p
用 法: unsigned coreleft(void); / @9 g9 c4 D; T! a
程序例:
+ A u% q0 l4 G/ ]. v8 V/ M$ e#include * s" E4 I. q( Y8 `9 ], f' F
#include ' q6 R% o- V/ T- k* G2 ^' }
int main(void) ; R7 k4 h. P2 X! f, i
{
, X0 U$ J# m& a- o5 y+ T" k" [8 h9 @printf("The difference between the highest allocated block and\n"); 1 D }7 u `: h) c$ \
printf("the top of the heap is: %lu bytes\n", (unsigned long) coreleft()); % g! {6 b$ Y3 C7 v2 G: j
return 0; 4 G5 K1 {+ K5 K0 n
}
$ z6 f, p+ ?; H1 f2 n; @5 T" E S4 F! ]& t* u/ s5 O3 y
函数名: cos
* ]. F3 e1 v3 i: F$ m/ Z# l功 能: 余弦函数
1 o% u) g3 l1 v+ Y3 O用 法: double cos(double x); % t( A+ V9 e. T) `9 @
程序例: 7 J, \2 z9 a3 c7 M: y
#include
1 r6 a) p; N1 B, V4 }8 d6 g* X z#include
4 L% Z( g4 w- U% p: H% i8 ~int main(void) : s0 b4 j5 a3 c7 d" h9 p$ n7 ~2 R
{ 7 h1 y$ r% u1 p$ Y: c% ^" z* g
double result; 2 i6 X% Z& J5 n; R* r1 U1 F6 h
double x = 0.5;
$ j9 }6 | S8 Y% Y7 G Iresult = cos(x); - G6 |4 `6 d( F# T
printf("The cosine of %lf is %lf\n", x, result);
/ F2 ^2 D- x- ^5 preturn 0; * S5 h' y; `/ m- H/ f4 p, b
}
. T8 N) w/ h1 \
+ K' h3 B: e: }9 _3 O0 T2 Q2 d; z9 O% G8 r# }( e$ |
" A" y2 A7 K( d% B" w
函数名: cosh 4 P* V) N0 T; `* A' p- B* o9 H
功 能: 双曲余弦函数 & E9 r" {+ v2 s1 C
用 法: dluble cosh(double x);
4 u3 Q% v" \. c2 {$ R程序例:
/ Q" N K1 }" v% j#include * x; C- l: n6 Y9 w6 _) \8 A; B. g
#include
' k y# n2 j0 a9 Zint main(void)
Y5 \ a* y) o* x{ & t! D* I9 \& z5 F
double result; " o1 c. K P2 a2 T2 J4 a4 R0 `
double x = 0.5; 2 j% a( B( ?" u/ e, v
result = cosh(x); 0 ^8 L3 x/ {4 v6 L* S
printf("The hyperboic cosine of %lf is %lf\n", x, result);
! V/ o: i# O+ u9 Zreturn 0;
: j: D$ c8 T$ `5 b5 W}
' d0 e% ]" s4 ]8 e# s- l. ^
/ G! o8 n5 R: l1 R0 { K! ]: Y& Q0 ~+ |
$ I3 ^; q1 W7 M+ L" x Q
0 u! S4 n5 Y4 x7 U9 n函数名: country 3 I% C" f/ T5 n8 g5 E/ D! G ]" A9 Z
功 能: 返回与国家有关的信息
7 _3 D) w1 [: s' o) _' r用 法: struct COUNTRY *country(int countrycode, struct country *country); 7 E b( N, {5 V& d1 }
程序例:
4 c' e( S2 `5 R% @8 E#include
: }4 I5 }# a5 L) h9 d#include
# M/ I7 R2 C( r0 G, u8 x#define USA 0
l( x/ H; n+ ?. yint main(void) 2 I+ l4 P" G! |5 a
{ % ]8 E9 z% Q6 n# ?8 m5 ]$ h: `" i: V
struct COUNTRY country_info;
8 b4 i1 P, _3 n" v' G9 }country(USA, &country_info); 7 V( _( q: Q- A1 E; _, k
printf("The currency symbol for the USA is: %s\n", 9 Z4 {/ h, h, m9 p
country_info.co_curr);
1 ^# k$ r' J) s. |return 0; - L h% ?$ n7 V. X! t
} ! Y7 N2 l s& u* V4 S
U9 O1 ~& ~. e# n2 ]
0 @4 x/ v& X( g8 O. U0 E
, X3 s2 [' m$ U: g" X6 g% _
函数名: cprintf
' x" B1 F! x8 W v: V功 能: 送格式化输出至屏幕 ) ]7 ~* [- S5 w! O9 n
用 法: int cprintf(const char *format[, argument, ...]); 8 x: Z2 h% ~ L. w3 T" S
程序例:
, ^+ J: w2 [) B, a5 E#include 6 V# [9 ]) `4 Z. |
int main(void)
/ P7 c. @7 r$ f H( O{ 7 ^( M( F2 U+ A% d$ w/ r, A
/* clear the screen */
$ G8 h# v1 Y9 @) D/ r# D8 @/ w5 W6 kclrscr(); ' u4 r; _4 @: d* s
/* create a text window */
% E5 ~$ j! g/ }( ?8 Pwindow(10, 10, 80, 25);
& v! L, w! O. _! |- ?9 O1 J5 D/* output some text in the window */
' [% Y4 r t& S. Q, J4 n0 lcprintf("Hello world\r\n");
3 G4 h# H. [+ D/ y; u# ?# F/* wait for a key */
+ @- l; Q6 C! X, d3 `0 {- {getch(); ; b1 t: b' s; M' \! G
return 0;
7 c0 [5 |2 Y' N}
* @5 `4 O! U2 P
2 h. X: ?$ d! P8 l) \
v6 c7 m/ f) r/ o 0 g/ m3 O9 q8 O- E5 i
函数名: cputs & c* h% I3 _; i8 b# Z
功 能: 写字符到屏幕
8 q) A' @- [) A7 F用 法: void cputs(const char *string);
3 w) {' ~" Y& p程序例: * t. h6 a4 W' \- a! ?4 ^
#include
. \ K8 |. m' jint main(void)
! ~( p3 B) O3 Y, d9 Q B' m5 B{
: n/ y. I, X& t) P) y/* clear the screen */ . \ k+ n" X. E. u7 p
clrscr(); ^2 h% ?0 n# F: T& {. J
/* create a text window */
/ O* y% V& r; s( v9 L: kwindow(10, 10, 80, 25);
; g o/ a+ F+ y& C/* output some text in the window */ 5 m. L5 g3 k, i3 W ?
cputs("This is within the window\r\n"); `5 ^/ J9 J a3 G% e
/* wait for a key */ $ @; G2 o# D8 K9 W6 |( J
getch();
, T; F# R7 F7 y( Preturn 0;
) u- B' K* Y5 a}
! T: s% r6 [; H. ]! r& Q) `7 c6 u- h0 N& V5 Q; }
6 v9 z- Y A$ c% T: A; x4 v1 g
' t- C7 P9 T3 [' D6 A1 @9 `函数名: _creat creat : v; W2 q# b9 U* D& p& l" ]
功 能: 创建一个新文件或重写一个已存在的文件
& y4 N1 J% y' n- Q) p用 法: int creat (const char *filename, int permiss);
0 j* m0 S( e( v. A% o6 \程序例:
3 Z! F; w, o8 z- _7 b$ F, h8 J+ I8 T#include 3 S! j! F7 j& ]4 g* ?* B9 }; ~/ b
#include ! B* K; m& h$ y1 m: I+ @
#include : O0 {' {/ o* ~6 Z( |0 a- ^# s' D; j
#include " @/ R! T; [9 f- b. d; L* m
int main(void)
- E3 f' z, T1 Z- r8 z! O% z{ # q8 e H* \) H" n& Y
int handle;
" ]$ t& a& b S# {( nchar buf[11] = "0123456789"; # g+ O0 e; n: g' Z2 P
/* change the default file mode from text to binary */
5 s4 F- _9 `) |7 Z# ~_fmode = O_BINARY; ) M3 M) X* a$ g4 F" C
/* create a binary file for reading and writing */
' J5 p1 p2 S' D* M2 S! r! Zhandle = creat("DUMMY.FIL", S_IREAD | S_IWRITE);
9 H9 ] p, F* T6 I3 T( \# ?/* write 10 bytes to the file */
1 }: D# ^3 G+ _write(handle, buf, strlen(buf)); 5 q- ]* n/ I; `
/* close the file */
0 E* {) ^7 |3 q4 E# Fclose(handle);
/ R# x2 [3 _- k G$ J4 nreturn 0; + l' r# M' C! I
}
8 u& Z [ |2 a- T% ?
: u: {) P9 H& T5 F函数名: creatnew
1 b* X i5 h4 B2 z功 能: 创建一个新文件
+ {+ M* h7 R9 [& f用 法: int creatnew(const char *filename, int attrib);
2 ^ M8 F4 M9 j4 R程序例:
4 g- H# ?" o" [: n5 r#include 2 s+ ~; H$ K6 M$ }/ {: p$ C# l3 `
#include ( k5 l4 c+ N5 v* _- ]6 m; @
#include & M) C; k. `* C; l
#include + B! p( B" B- n3 p
#include
5 H. C( p* `; k) v0 I7 O2 _int main(void)
; @3 ?1 {+ ]$ e9 @) {" Q2 O{ + T$ w( W3 e7 c" U, | X
int handle;
/ R$ g6 f4 H( f: _2 }% c5 b, D4 Cchar buf[11] = "0123456789"; $ P$ h/ _: h5 q( U- p4 I! y. k9 t
/* attempt to create a file that doesn't already exist */
, h- u/ w. r$ Q. `: Zhandle = creatnew("DUMMY.FIL", 0);
" i6 P/ M; Q0 n' q/ H9 yif (handle == -1) . Y: d3 j! Y6 d, n$ S! F7 G0 A; C
printf("DUMMY.FIL already exists.\n");
G+ F' E. V* Y* Kelse
, K, A8 s; I1 X$ \0 b. g- y5 M; h+ G{ / s! x7 A' F- w' [
printf("DUMMY.FIL successfully created.\n");
# u; |! W3 ~6 ~6 V& ewrite(handle, buf, strlen(buf)); $ \( F% g. D5 A2 N
close(handle);
( n/ s0 X, C* D1 u0 ?8 k7 M}
, N! a* d8 H' J4 A. Lreturn 0; + c. K# E b4 b7 o4 m" C3 R$ u
} ! B( o" B& A. |# r0 R
( R- @; |, v/ Y+ O+ \% ]
$ A, m. U0 Z7 b7 M. Q- ~
! b1 `! a' E1 r函数名: creattemp
' m% o1 @2 \) o/ R T8 `功 能: 创建一个新文件或重写一个已存在的文件 : X) U. a6 b4 f
用 法: int creattemp(const char *filename, int attrib);
8 l, O+ F7 U2 Q5 |; ]& J9 W程序例: 0 x/ v; `) q8 @5 c
#include , q! c& N \5 s$ K
#include
y& H; e* H7 y+ y* U3 n0 l#include 6 D( n$ }$ h3 x; y8 M( _
int main(void) + J l; S; K6 o
{
+ g0 f- k& ]; P8 @* T) u3 hint handle; \ { I/ P( f) U/ s
char pathname[128];
% u5 v1 b) }3 p3 o* J5 Pstrcpy(pathname, "\\");
0 L$ c" ^. ^8 R5 [ e/* create a unique file in the root directory */ * n8 u# F$ @6 u* G1 q: p9 p
handle = creattemp(pathname, 0); . z7 y! N; t0 X; u
printf("%s was the unique file created.\n", pathname);
5 x7 F) v: i4 a1 k' v, Iclose(handle);
# O8 |# z& m: p# }3 W% [) w# L, X6 zreturn 0; 0 e0 W: s/ S, B F6 X
} ! }2 s# ^& E0 Y0 A9 Z
( F3 P0 H3 q- M/ h# k' U% z
" k/ W, d" D) ^9 Y
5 l$ Z- ^* @& N6 \+ ^. W+ n函数名: cscanf
# ~% n* L! G6 | I功 能: 从控制台执行格式化输入 ! i$ n! k1 M( X, C' E
用 法: int cscanf(char *format[,argument, ...]); \& H2 |( R* N
程序例: 5 p" f- |, P8 w, j6 {! d4 F
#include & j5 S% q! C& h
int main(void) / Y; M8 B0 C L7 T) b
{
: Q5 ^: a, r0 T) K3 }char string[80];
# \8 ` K9 R5 e: L/* clear the screen */ 3 B8 K! q* f1 K" Z5 k- y
clrscr();
1 k O. f& O3 t8 R4 b7 s8 J4 T/* Prompt the user for input */
8 m+ q" L7 Q6 g( i4 [6 Mcprintf("Enter a string with no spaces:"); ! c; j7 i- I$ N
/* read the input */ ! T; @0 U- U2 I5 d: s' M, L9 v
cscanf("%s", string); 7 D7 a* h. _/ [: u* q! A$ \0 q
/* display what was read */
. A! N2 t0 G, c, h8 Y% x# Kcprintf("\r\nThe string entered is: %s", string);
& ?- c4 s8 e% a# Z- N( m1 @* \return 0;
: D K) @! i% N: a, u5 }# j% o}
/ `* {) n( e, j! p J+ t/ C& D
2 Y2 F$ ]) h. }3 r |6 n
8 Y3 G9 F9 l2 P M# S! X& n 4 G# c3 U7 {& G" x1 k
函数名: ctime
9 i5 ~2 Q, W0 n4 b0 b2 u, }功 能: 把日期和时间转换为字符串
# U9 X' ~0 k+ |2 N% q0 m7 R用 法: char *ctime(const time_t *time);
# \3 X; a& }1 n/ _3 E程序例: % v- C$ @$ k; t2 w0 d
#include # \# C! @" S. n
#include ( u- e* T3 u) |; G1 x9 K5 m
int main(void)
/ W0 `7 C& b! M) F7 t9 T{ 3 E! Z6 m6 E0 G+ @* d
time_t t; - B! [+ `, ?0 }$ _# p" _
time(&t); - m4 ^" ^* o$ M) d( k* ^, ^
printf("Today's date and time: %s\n", ctime(&t));
2 ?- P7 `: a* U+ c; n6 Wreturn 0; & z/ Y) p: S c- N
}
: {1 j! J/ x% m5 L; I
8 t# Z9 Q/ B0 o
: w* f1 t# K' G. A5 a' o0 T: V
1 b. c' d9 B5 A4 W% b. `" u函数名: ctrlbrk / E B, M( K x4 t
功 能: 设置Ctrl-Break处理程序 3 O* b4 Y g+ P# s/ q
用 法: void ctrlbrk(*fptr)(void); ) p* p4 \. l' F' m+ n! D, H+ _
程序例: 2 g$ P" j! J4 s; B# w
#include ! W7 C H8 t6 e6 \+ A! Z* U& m
#include ! ^' D% ^ P* n! G4 D& n
#define ABORT 0
- X* ^4 i% y9 g0 ]' |! Cint c_break(void)
% i7 V, n: j) a{ # r- r5 ^1 L) M# f" ?3 [2 A8 ?
printf("Control-Break pressed. Program aborting ...\n");
; G$ x- e4 X, V, treturn (ABORT);
: e8 J5 Z- `3 C} 5 j" Y& H) C2 I5 E/ h B
int main(void) " c" Q+ B9 ?; E+ w5 S- k9 P
{ ' u- s5 F: I4 C( G
ctrlbrk(c_break); ) S6 r) p- H- S2 U1 v
for(;;) " K1 _$ y4 F7 d9 d
{
3 h9 z- q( L( k ?( C: |- n+ gprintf("Looping... Press to quit:\n");
9 k/ U& d( E R9 b6 x8 J% s. Y0 ^. ^+ @7 H}
G! p# R1 b* F6 k7 B" H# areturn 0;
. U7 q* R$ U$ p7 i0 |2 L5 }} |