|
函数大全(c开头)
* ]5 W" E0 P2 g; \7 k( a# P
1 s J6 x) \7 Q函数名: cabs + |, l; i8 K5 `' q
功 能: 计算复数的绝对值 ) _% o$ e) q1 a
用 法: double cabs(struct complex z); 6 d: n, ?" x" U$ U# m
程序例:
, H( L5 W' ]2 j- b# ~" l& @' e; P#include ; F4 J- e' {7 ?% N+ \$ r h
#include + K' x& f1 f& k; R
int main(void) : y$ }0 U3 I0 P, Q
{
% }5 l' p1 l9 E& L, Z8 B, Sstruct complex z; d: W r$ M0 O1 Q
double val;
" U7 S3 L R3 a! Y7 Z, v1 c/ }z.x = 2.0;
9 Y2 v. U/ }3 B( U& |' x e7 Vz.y = 1.0;
- [3 ]6 d/ b4 x6 A8 E' ]val = cabs(z);
. x% Q# ^- k* H7 bprintf("The absolute value of %.2lfi %.2lfj is %.2lf", z.x, z.y, val);
[2 k: @; h! L2 h2 {return 0; 8 K: s- ?1 {/ [% A
}
2 g! t+ J* e4 K& [! H& F/ ~' ^. _1 @0 m8 [, e5 F B
/ ?8 v4 M Q% K
: i. N: k. D! C" E1 A函数名: calloc " o% r/ v+ [ c9 O
功 能: 分配主存储器 ( x/ c; b; l8 A5 ]
用 法: void *calloc(size_t nelem, size_t elsize);
1 B1 Z# g2 v5 T程序例: 5 x& t1 { Q" M8 P9 y8 o
#include
1 u% C) {0 r" q" e1 m. \( |#include
, b% U5 ]) Y( l/ j; i4 sint main(void)
. ]2 r' _2 ?1 d6 J{ % Q# ?& ~8 \' Z# K
char *str = NULL; B$ c# h/ h+ A% x# h0 Y
/* allocate memory for string */
; a0 @' i& H: X6 H. B! u* N4 vstr = calloc(10, sizeof(char)); ( ~* x" q( m# b7 Y$ Y
/* copy "Hello" into string */
1 b& M+ ~/ z. _1 M9 `7 Y' ostrcpy(str, "Hello");
+ \. C+ ?% M' B! [8 Y/* display string */ 2 `; B# O& P" s- D) M
printf("String is %s\n", str); 5 Y/ x6 \; E; A0 W- F, [5 X2 } I: K3 V
/* free memory */
3 w2 R& @+ O8 C( Z$ c, l! E8 Sfree(str);
. I# I8 d8 [" b o: \: l! F$ n* v- jreturn 0; y# N; T8 S3 l. W6 M
} * L+ l0 I' C# q V3 H
) _9 ~$ H4 U# O5 A
a, H R+ ]* a# [/ X
& a9 }- {' s" s, M$ e函数名: ceil " L1 Z( H) }' i1 F, P3 }
功 能: 向上舍入
# v3 \7 P4 G$ x$ W- j. @/ J用 法: double ceil(double x);
5 y5 x1 d3 N) l程序例:
# d3 F- O( |) B1 R#include - `8 j3 |$ y5 z3 n3 |
#include
# ^( Q- H" h2 ] b/ m# D/ \* A! yint main(void)
/ {* P: q; S* l I; V{
- @. {5 @, O. L8 `- h. @, s0 mdouble number = 123.54; $ ]) m" K2 M4 y- M Y
double down, up;
# ?$ B8 W4 @% w% K8 x5 mdown = floor(number); 1 `4 q% u, S Q* y {
up = ceil(number); . C1 l' l5 n' A' a" T8 I
printf("original number %5.2lf\n", number); ! L3 W8 B& f: U" s
printf("number rounded down %5.2lf\n", down);
+ z0 E6 r/ W7 M- L9 qprintf("number rounded up %5.2lf\n", up); 8 x) q+ z6 l; D; D2 c. @
return 0;
3 m N. S, ]+ W} ) W T6 a% G# w ]; w3 Q
( h0 H7 b" D Y, [5 J4 ^( x/ A$ Q" r6 V5 }# S# ~/ f
8 z0 f. t5 ^' a6 y函数名: cgets % J' x: \7 H% q7 j9 w5 `$ k
功 能: 从控制台读字符串
# a9 {5 d. S; i7 U用 法: char *cgets(char *str); % n; T9 |/ C0 |- Y
程序例: + b- I% i, [3 _1 M7 b7 @; A3 l
#include ) x( W1 t$ ~/ t0 \1 S
#include + K' t0 p: Y) h. k* e5 H
int main(void) ( k4 f+ v1 J9 C
{
+ b: n( V1 ~# Y) I4 J8 achar buffer[83];
\1 ^( Y+ M" K0 uchar *p; ! R7 ~+ d! O* p+ F* B) S
/* There's space for 80 characters plus the NULL terminator */
& K* ^% k9 H/ \5 v: q- q/ `buffer[0] = 81;
" O1 w" y; w9 {# h7 p" E5 cprintf("Input some chars:");
0 J9 a& Q# [8 p! u- \p = cgets(buffer); - `+ l8 o. ^) ~( x6 t
printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p); 7 ~) P0 b7 Y. e* X3 n. v
printf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer);
8 F( a; m) |, [# p2 P" E# J1 B/* Leave room for 5 characters plus the NULL terminator */
# ]1 d' @% q9 @ y5 }buffer[0] = 6; : d `( N* ^: B/ ^
printf("Input some chars:");
/ g4 d$ d& i( H, d% `* |3 ip = cgets(buffer); & R+ Y/ D$ D+ U& [4 K- X" c$ \5 J
printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p);
6 W. a+ U6 O6 Tprintf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer);
3 N3 r, B) }, H7 V; E$ z Ureturn 0; 7 ?4 E( i( o4 r0 N0 T
}
9 U5 w+ @; P3 ~" K T! q- \' ~0 U- O6 F& S
/ R/ c# r+ x n 5 W P; w( T* J8 y( L8 c
函数名: chdir 3 u! O2 _- ]7 A5 g$ v7 x* Q1 y
功 能: 改变工作目录
$ v$ m+ ~* b$ E' H& W4 Z用 法: int chdir(const char *path);
( _3 N! R% P/ s4 ~# P9 K5 _/ M程序例:
, t- M+ u7 k6 n6 G3 K( \. A3 M- j#include
) p# L8 q1 p L#include
]6 K3 z( P* x5 l#include " k" c1 T0 c, A# Q4 t7 {6 X
char old_dir[MAXDIR];
+ R' G6 @" ^9 f9 l3 A `: e T% tchar new_dir[MAXDIR]; % o2 Z' @6 b9 j3 m# s0 c
int main(void)
2 Z3 X0 o: H |* y{ - U) z% e5 W' g
if (getcurdir(0, old_dir)) 3 m8 n) O2 ~8 k @# \! P
{
' w! g& l, x. r" ?/ Z xperror("getcurdir()");
2 t* J8 P1 W p4 Uexit(1); r2 F- c% G7 v: f' e
}
0 ~ ?! [4 Q. |- S) A% Sprintf("Current directory is: \\%s\n", old_dir);
1 K% B. \) J8 ^3 pif (chdir("\\"))
1 p8 F% S# y F+ T }5 j{ 8 A: o4 l% s( f) k. x- ]5 j0 c- q
perror("chdir()"); / M* e+ H: f7 J+ I* e
exit(1);
3 ?1 B5 P* ] c' |4 n* P" m}
! l/ t! m* h" Q5 |5 s, y3 nif (getcurdir(0, new_dir)) 6 A6 H; w/ D. I$ n/ O
{ ( K! B) V2 p! A/ f8 B0 O$ u1 L
perror("getcurdir()"); % [9 R) E/ P- \. `0 L/ J, W
exit(1);
5 v8 U8 Q) Y) z) p4 O4 r} - O% I. b% H+ f% t6 R* h, A7 x
printf("Current directory is now: \\%s\n", new_dir);
3 L( h* t8 w6 t4 H# Zprintf("\nChanging back to orignal directory: \\%s\n", old_dir);
+ ~0 W+ S/ v( x* t) v' y$ r6 \if (chdir(old_dir)) ' k- |* m' K% O! @# U: w
{ - W$ _( q( b& B% j5 N7 U; D
perror("chdir()");
4 u) L$ t j' U! W- N |# X. b/ {exit(1); 3 O7 g) o) G* z8 w* F- i4 k
} 1 w# f9 n1 L r# c
return 0;
% s7 d( D7 P1 A; u9 ?' J- G} 9 @; V s. V7 x9 M1 | X( s0 L" V
# c* c+ h6 Z( Z/ Z1 d- _ ) e/ I& m5 z' u) \( `9 i
函数名: _chmod, chmod
4 G, g! |0 l9 |功 能: 改变文件的访问方式 3 m+ [. @( X, u J
用 法: int chmod(const char *filename, int permiss);
" ^; t; M. ~5 J程序例: 9 a7 o+ _" s1 x& d- {* j7 i! W4 o
#include
2 l# X0 F8 E/ N5 m9 I#include
7 F! N4 N, ^4 L2 O9 c& h#include
5 }7 B* t1 v6 }5 G5 Vvoid make_read_only(char *filename);
& A0 T" }4 D# _/ Rint main(void) % B5 V, c7 L. S& u0 z+ A# [
{ ) F$ k9 O0 i9 T% `% E9 @0 D' U
make_read_only("NOTEXIST.FIL"); 6 s& G: v/ y! k$ W! ]0 y: ~. i, y% O
make_read_only("MYFILE.FIL");
6 l# @! Y8 w2 F0 W/ u+ e: _return 0; # w/ R% C) i) X
}
1 S* S% i: d1 s" `7 B3 u( lvoid make_read_only(char *filename) : R# q" B) z- d7 r
{ 0 ~& t8 t+ q, e. Y" U+ M ~. p4 I* F
int stat;
( T3 z! j: H P6 |+ h: c0 P" I1 Vstat = chmod(filename, S_IREAD); ; b3 N1 w9 G5 h/ X
if (stat) ( `1 A* v8 Y. f9 j
printf("Couldn't make %s read-only\n", filename); ' i+ o- ?& O, m( N8 L) ~( |
else
$ \! s6 y8 z" J$ Jprintf("Made %s read-only\n", filename); 0 n$ o8 c; _5 V( q" R: S X
} 5 ~) T0 l7 w! V8 L' ^7 w% Y* p
! a/ q1 r- R1 @6 _9 @, F
M3 X+ b, _/ ~% v6 ` + ~7 r/ t) L6 o2 Q" g8 o8 v- e- ~# K
函数名: chsize
* X$ j6 G8 O+ v7 J- G/ B3 E9 b) z功 能: 改变文件大小 ! p$ W/ q& F% O4 E; W" y
用 法: int chsize(int handle, long size);
! r6 ~2 x% @. C1 s程序例: . M9 k: m6 \( |* V, z
#include " f# ~) m8 S! z% |0 {
#include
$ k( n: j" s& P' t; P% D" g( K#include
! y: `) A @6 L/ @' V/ o% N3 Qint main(void)
. p, b& \9 u% S u! L( G{
' s0 A/ g2 q& o* O( J, Xint handle;
7 j. O% e7 C, Z( nchar buf[11] = "0123456789";
2 G( h5 I1 X/ ]% \/* create text file containing 10 bytes */ ; B' @# j: j, W4 o( V) z
handle = open("DUMMY.FIL", O_CREAT); 1 D+ S1 Q: n" E$ S7 f
write(handle, buf, strlen(buf));
% M0 z* X; e& f/ `/ w7 F/* truncate the file to 5 bytes in size */
6 d, A% j9 P9 V1 f# ~1 gchsize(handle, 5); : _- W: Q3 s; L# e; C0 h G4 ^3 `
/* close the file */
3 f8 M5 B# D/ n: L3 jclose(handle); 0 h% w& K: y w2 M6 m5 C+ o
return 0;
6 X6 C; f* e; C) L5 u}
) ^9 V/ [) U6 ?$ {- G c2 c6 r4 f- g) }2 y6 V1 s0 o( ?
( R% `" Q2 m: X函数名: circle ( C, e% b& a1 ~: y& s$ I) Q
功 能: 在给定半径以(x, y)为圆心画圆
( ]0 G( X. c0 J* s* f* ~' l用 法: void far circle(int x, int y, int radius); 7 p) L+ {2 k6 X# W1 Q1 z+ |5 ?
程序例: " K/ z3 j* f5 w) v# P
#include # W, v! M2 y# J7 ^0 s/ D" R
#include
! V2 b/ s" ]- K#include
* N/ U6 R" Z$ _: q5 g: ?: c7 @4 `. G#include
# y) y6 U7 V$ B" E9 W+ }int main(void) 4 M" |- C" J- h% f% w: R$ o; C) v
{
1 t9 V3 J6 C! @6 v2 i( M& ^/* request auto detection */ + M4 G8 X7 F& ?
int gdriver = DETECT, gmode, errorcode;
- ]% Q) F8 ^; ?5 lint midx, midy; : e3 Q5 q. a3 t p& E+ Q
int radius = 100; 2 d# V0 W0 v6 q* F$ r
/* initialize graphics and local variables */ $ v5 ]6 ~. C% M# C! |, t0 g' n7 f. S
initgraph(&gdriver, &gmode, "");
) T- j) }5 b2 C2 `; x& N/* read result of initialization */
/ [. m2 b1 W) h" |( K6 w, }. Derrorcode = graphresult();
* @3 X7 L& F) w7 @7 uif (errorcode != grOk) /* an error occurred */ 7 N9 o+ D0 [5 ]; m
{ 0 A+ i5 j- E) R3 e, o1 P
printf("Graphics error: %s\n", grapherrormsg(errorcode)); . b' y7 t9 \- g1 ?8 k0 ?$ ~0 \
printf("Press any key to halt:"); + T: t7 N9 m) v8 o. s+ u
getch();
+ G4 y0 p0 `& F0 Y: Qexit(1); /* terminate with an error code */ 6 h+ U" Z& Z4 j
}
7 K" z' X" v0 k" ~, J0 Bmidx = getmaxx() / 2; ' R" N, a `- Q# x( F
midy = getmaxy() / 2; - W' e, k+ R, C0 P& ^# q9 h
setcolor(getmaxcolor()); b8 ^0 J J/ I8 U& e3 ?0 T" u! I5 k
/* draw the circle */
, l/ C( x# a- C) x/ Ocircle(midx, midy, radius); f) ~+ g. ?" ^4 v/ ^, s; K
/* clean up */
" v w; m6 @- i' F+ N5 Y" ugetch();
" \5 q5 m/ X4 C& @; z4 kclosegraph();
+ f+ k( ~ ]5 ?7 [' ereturn 0;
0 P3 S+ N: N" C, A- F- h} 8 d4 `2 w! e: c: |. A4 X3 [5 I
/ b: y& \8 k' q5 t/ h; ]
% t5 s; X$ M- T, b2 J+ o 6 i, h$ t5 l8 j5 e2 F* _! @# |, Y
函数名: cleardevice 4 n. ? h6 E: Z- G& A% M6 x, E
功 能: 清除图形屏幕
, ~. }& c$ A9 l用 法: void far cleardevice(void); / } z0 I7 K$ J% }2 A' _6 w+ a
程序例: # v B$ j8 R- o) ^* U
#include * }6 v1 J2 B: x8 ^+ L
#include
& ]/ {. [' S! ~# _) ^#include ' u7 I3 S( y' N) }
#include
& E j9 @* f9 Yint main(void) 8 Z$ }2 L" {- W) u6 i
{ % h/ g8 P1 `) w$ f: m
/* request auto detection */
: H# N) Y; ~5 I/ ]: c% C* x* ?int gdriver = DETECT, gmode, errorcode;
! T9 v' s! K, | G, jint midx, midy;
$ v1 p- I$ b, _, P/* initialize graphics and local variables */
& s9 \8 n0 o: q* Y5 Q5 Q! t4 Iinitgraph(&gdriver, &gmode, "");
p# J7 A/ T4 i8 k) k5 e# b, j/* read result of initialization */
2 \8 V5 H) f% M" ~. G' perrorcode = graphresult(); 3 N" _( K, w' x8 e
if (errorcode != grOk) /* an error occurred */
) m# o0 v4 m# R: [, |; F{
' m. K i. O4 w" y3 o: lprintf("Graphics error: %s\n", grapherrormsg(errorcode));
/ N* @# g4 j6 Y1 {/ wprintf("Press any key to halt:"); " Z/ `; k' {9 R( E" T {
getch();
& Y5 m; h m {8 I: E% ^1 L/ t- N9 @exit(1); /* terminate with an error code */ ! M( T2 M$ ?- h" ?- B9 ~8 K
}
+ j' B5 p/ ^- g% gmidx = getmaxx() / 2; 8 N( [! d: h1 }! b& A2 g
midy = getmaxy() / 2; , F. P" ]# {2 N
setcolor(getmaxcolor());
8 ]/ p/ g+ ~3 U/* for centering screen messages */
: y8 J6 l* |6 z6 B3 z6 l8 `settextjustify(CENTER_TEXT, CENTER_TEXT); 6 k( P1 r7 D" B/ T/ f9 {+ B) }
/* output a message to the screen */ 6 F7 R. p- y$ t% ~7 `7 S, v
outtextxy(midx, midy, "press any key to clear the screen:"); 2 W! h. b6 q& R% H8 q2 [
/* wait for a key */ " X- Q: b' j+ I+ f; ?
getch();
$ y/ f w' h, b$ V5 {/* clear the screen */
7 |/ ^# ?0 `+ Ucleardevice();
- i: s1 Z8 [5 ^/* output another message */
" b% s/ T$ T) z0 n. Qouttextxy(midx, midy, "press any key to quit:"); ( B, _8 ?8 E m! }& V l
/* clean up */
; \9 ?# K. u5 m8 c5 j* ?1 S( ngetch(); 5 j+ u, o8 f/ }$ M& O) k
closegraph(); ) C: h1 L8 q" G! r
return 0; 1 Q0 m+ w, Z* s9 }& u
} # Y; l) n& ?! J+ y) }0 m
8 s6 B9 W8 [$ u4 }
7 g& P( V2 \! F" ^9 f 4 x& j+ L8 ]' T8 D
函数名: clearerr
( h) a; d$ w$ R+ \: \% E功 能: 复位错误标志
$ ]- ~0 x0 Y Z4 b" v用 法:void clearerr(FILE *stream); 2 w! M* S7 ~: c# ~: [
程序例:
1 M, ?6 k6 S$ `#include ' h; l9 }+ F4 K/ @1 C
int main(void)
. u5 N7 d p9 ^+ O$ O3 ?" y5 Y{
! O# K) x" u, \FILE *fp; " p( S7 v( p/ z: O2 P4 E/ @
char ch; % {2 r y$ A/ k5 q( P6 V6 H
/* open a file for writing */ ; k3 D) [7 d$ T' z
fp = fopen("DUMMY.FIL", "w"); 2 s" @4 A: E" E5 q3 \) g$ L
/* force an error condition by attempting to read */
8 E ?9 J4 v. lch = fgetc(fp); 1 Z( q R: b1 ]2 ]4 s
printf("%c\n",ch); , O: l# a( { j5 S! u( ^) v
if (ferror(fp))
+ v, W5 {! t3 `' q$ W! r. B# i{ 6 a/ T/ i- f6 m+ _8 @/ U5 C
/* display an error message */
E. b4 x( S; b5 lprintf("Error reading from DUMMY.FIL\n");
9 A$ X' j0 f+ _; [0 T/* reset the error and EOF indicators */
' U4 T$ u' I0 X% I! o- s4 wclearerr(fp); ( S R" T. M* g8 ~, ]$ A# g6 E
}
. J4 G+ r8 F: j: x; b5 h) y) Hfclose(fp);
6 j+ D7 ]& @ c5 n) ` ?return 0; ( h9 J! S8 @/ k, V& b" P
}
9 v3 U8 r" r9 k' G$ J; ~, }/ ], E5 X# I. Z2 |! K. T4 v# N! V5 Q
' A1 h* E0 [5 U) r6 b
* O. f8 W s$ v q# E* P* w$ f
函数名: clearviewport + x- n: p/ y7 M+ l. V7 v
功 能: 清除图形视区 ) q7 l* ^+ x, \! f
用 法: void far clearviewport(void); " z( ^) P. R+ f- @) B
程序例:
% Y4 f4 }5 u/ M! n f: P, @1 j0 ^#include
4 c# J+ z8 b4 P! _#include
2 ]' o9 l$ {% ^% N" L#include # ]5 Z0 T8 \4 t1 P6 X/ J
#include - r; Z* C$ P7 |3 V c/ e
#define CLIP_ON 1 /* activates clipping in viewport */
7 f, k; U; `. gint main(void)
$ t: I. [3 B2 m& W+ s- U. H" q2 U{
, C0 A( C+ i( w2 p" K/* request auto detection */
2 y% j4 L @4 |int gdriver = DETECT, gmode, errorcode; B) n" Q& y T+ j- |$ A$ v
int ht;
9 K+ s' P/ H! g- v, [4 J0 N' z) k5 u/ H/* initialize graphics and local variables */
: h9 m! Q6 F1 r2 z5 ainitgraph(&gdriver, &gmode, "");
5 c. X! V; R/ U9 O/* read result of initialization */ " K% O2 N7 E: |' y
errorcode = graphresult();
3 x4 u b" @, v' G% ~* B2 Uif (errorcode != grOk) /* an error occurred */
; ?# ~/ Z% m0 v/ z; {# V% Z6 s% y{
- U% }" y+ _. M7 \& Wprintf("Graphics error: %s\n", grapherrormsg(errorcode)); $ t) `: J& Y: O: N* {: o/ i8 D: ^: d
printf("Press any key to halt:"); 9 ]/ t6 p4 e z
getch(); 2 ?/ l2 ^; J. g
exit(1); /* terminate with an error code */ * T! [! m, C! r ^* ?5 h( o
} 0 _; d2 b6 K# l- e# Z- J
setcolor(getmaxcolor());
4 b( D7 {5 M! U! J6 tht = textheight("W");
, _- H1 h) r [/* message in default full-screen viewport */
" S# I* O- H$ y( ]* Aouttextxy(0, 0, "* <-- (0, 0) in default viewport");
9 V$ k' h: b, k5 w7 V/* create a smaller viewport */ ; Y6 a' d D# o, C
setviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON);
8 S7 j9 V( _9 S; z9 e/* display some messages */
( B' y8 q* G& p: Couttextxy(0, 0, "* <-- (0, 0) in smaller viewport"); + t2 V8 L3 S+ N9 ^
outtextxy(0, 2*ht, "Press any key to clear viewport:"); ( F5 r2 e. y5 C( D& u2 a$ U
/* wait for a key */
9 i) B9 M$ v+ ^getch();
& d7 e, \9 j; ?1 E) L. Y y/* clear the viewport */ % \7 F; H. L l, N6 ^
clearviewport(); * l: _# O0 O9 C p
/* output another message */
/ E. Z2 M, O5 n9 }- ^: j) n( J# Kouttextxy(0, 0, "Press any key to quit:"); " i* O5 s7 ~+ T) H# v a; W( {
/* clean up */
% D' |! _9 I, Q* c0 \+ d5 q1 n. B+ Z% ]getch();
3 f- m# K; D' K8 t) k( Lclosegraph(); 4 W9 d9 S7 M+ n6 \
return 0;
) F9 g2 p0 r5 G( s( D5 ?}
+ q- r& @4 m! Y* g# j* E5 l+ Q# v7 Y H; T- D& j% ]. Q7 @
! z. Z1 o1 F1 _: S E, Z. D1 s
/ G: f5 A' Y# r4 a3 Q- I, C9 R7 [* E函数名: _close, close + a% q d5 u6 M1 e2 b3 V
功 能: 关闭文件句柄 4 U* p) f8 R# K) o( F
用 法: int close(int handle); % D9 a! ?% K% B; S' d3 ]
程序例:
* r$ f% K! p5 e5 i" N! I#include ' y/ J" ~( j' F' \$ o, o2 K
#include ( j- F7 z, b+ w% J
#include / y! O B! u' a# d' g$ F
#include % I. }6 C* g+ i
main() - v8 \# a! \6 _2 ^6 k
{
! u0 v8 ?7 X; g6 qint handle; 5 }3 r9 K% [& @' m5 d
char buf[11] = "0123456789"; $ g5 d) N4 [; M9 P# n# `
/* create a file containing 10 bytes */ , c: ?% V( Y$ x# Y& C
handle = open("NEW.FIL", O_CREAT);
0 g X8 C9 v' C4 N9 O6 S( mif (handle > -1) - l0 P8 W* \2 E. x |* I
{
; W9 U' T: f5 U# S/ ^' i0 Z8 Ywrite(handle, buf, strlen(buf)); # L' K6 A) Y6 c2 v; ?, X- @6 ]
/* close the file */
4 v% z) w1 _! C) r7 q" Pclose(handle); , o" i# H c# O' p' T9 J9 J
} 0 x- v6 j0 M7 }+ e9 q5 N
else
4 n Q4 i! {' ]7 Z{ ( j. {7 P. d0 x. ~2 f
printf("Error opening file\n"); 9 u* Z" j6 a) b. i9 s% u
} / @. D, m+ _, c! P) c' ^, p" s: j
return 0; # M, j D7 {) F: u- y
}
. j3 ?+ l& p& |( b' d/ K# |4 J' i$ V( I, e- Y; C8 `9 Z
) g: o: f$ \% H: Y% ^
! x8 g5 j5 R# r& A, O6 P
函数名: clock ) K4 t0 u. {% z
功 能: 确定处理器时间
; q" J$ e4 K/ `4 J用 法: clock_t clock(void);
, ]9 M7 q0 @( b* j程序例:
4 M0 y" ^+ M; ^#include
' V/ k+ |+ z+ l, O# D% ? G. d#include 1 J) b# D) _7 J1 ]
#include r+ j4 b* W( a4 A) w3 A
int main(void)
W: z4 g/ V$ q{
+ k0 m0 g! G% L0 K! k. bclock_t start, end;
0 Y3 ?4 |4 {: A, W6 P; r8 tstart = clock();
: M+ I" d4 O& Q; F* _4 `0 o% t8 P* kdelay(2000);
# Z9 Z2 ~, O% R3 N D8 W* Iend = clock(); & N+ D! A# {7 ~( f" `# B" }5 x; b
printf("The time was: %f\n", (end - start) / CLK_TCK);
$ W# S! u& [- ?" c0 ], D# w2 greturn 0; , T2 Y l4 j9 \2 ?! M* g
} 6 ]9 Y9 N: \0 j
0 |# j; V5 t& O* Q
/ x. P& c$ }2 F' t: F0 C 0 T- ^, Y1 E( G" }2 c0 O, b
函数名: closegraph . ~# v+ o9 l- @3 i) F n; O# |
功 能: 关闭图形系统 ' h) V8 y7 _/ i H5 U1 l9 T
用 法: void far closegraph(void); 7 C6 f0 K2 i1 ^& H) r: I
程序例: / {8 W# c. e( D4 }
#include ( {8 q ^# U6 e$ e9 r
#include 7 D1 d% z' \' h4 d
#include , q$ A: c2 r/ u1 d
#include
- q# m% } a7 R4 q2 f" ~& Lint main(void) , a2 g, k% i+ A! W* c) M
{ / A# A8 L2 D3 S, J! z
/* request auto detection */
" }/ z# J% ^& ?. z/ _6 }% Oint gdriver = DETECT, gmode, errorcode; 1 D" M! G6 \- P
int x, y; : C i2 H+ h3 s6 h0 W
/* initialize graphics mode */
~2 S$ T' l! ainitgraph(&gdriver, &gmode, "");
; [0 O5 i3 x4 K+ o7 P/* read result of initialization */
* M F5 ]1 J5 {* A, terrorcode = graphresult(); , h) G& J, ? p7 u
if (errorcode != grOk) /* an error + M8 |- y2 j2 C+ A+ P- A
occurred */
' q1 r9 E! R7 X5 y{ & B/ {1 Y3 F, `7 P& H' Z
printf("Graphics error: %s\n", grapherrormsg(errorcode));
' _! K; D# Z" Y6 ]' x/ ?printf("Press any key to halt:");
) k: x2 }: s* K2 j) i8 X9 Dgetch();
+ h* m ~, }& Q& X' I: b9 d3 Hexit(1); /* terminate with an error code */
$ f; J7 C" G6 F/ b} 3 ^0 m) f5 U! o, D
x = getmaxx() / 2; m$ Q0 Q' W" x Y5 h* O
y = getmaxy() / 2; 7 p1 U$ f# s/ m) {! K8 c
/* output a message */
8 w. K" F* D( I3 h" ysettextjustify(CENTER_TEXT, CENTER_TEXT);
' H# a2 y) I; L/ _+ K, @* _5 b9 u0 fouttextxy(x, y, "Press a key to close the graphics system:"); ! q/ M0 y; U- {7 j( l
/* wait for a key */ 4 {# E) ~) A! J$ @) u7 u
getch();
: i# l- e- F; g- ~0 v ]6 i/* closes down the graphics system */
+ O. D6 ]7 V" L/ ^closegraph();
7 e8 a$ }3 [) hprintf("We're now back in text mode.\n");
- e# o+ |9 n- a zprintf("Press any key to halt:"); $ f M( @5 \3 n# U
getch();
" {9 s6 ?! g$ M, Yreturn 0; # q- b7 j$ y9 h+ T
}
: ]' Z! `3 O# @1 P+ F
" E/ g: _. U) P% \! |
, h4 q, D; q% T3 @ W7 }5 w( n- v# q/ P
函数名: clreol * A: M. [- w' s5 y( {
功 能: 在文本窗口中清除字符到行末
6 p2 e" a4 y( D" `用 法: void clreol(void);
, ?* D. Q6 I6 ?' Q* z( [7 ^/ N程序例:
d# @) K+ E8 O8 T#include
& H1 b7 {- o5 m5 D& ~int main(void) " \$ ~8 I9 f* `1 n8 `5 y
{
x K' A6 a) b8 p4 h- ]clrscr();
% \( V' A, C; m% kcprintf("The function CLREOL clears all characters from the\r\n");
) v% ]9 O3 h1 a' rcprintf("cursor position to the end of the line within the\r\n"); 2 h. y4 o4 O% v; g+ V
cprintf("current text window, without moving the cursor.\r\n"); 9 v1 q6 i% i. \3 Q
cprintf("Press any key to continue . . ."); ' F6 a# F8 T: j2 C# j4 P% j
gotoxy(14, 4);
y) n+ j. ?# F h) f2 J# Cgetch();
) K3 L( Z3 l" N; k/ B* d! y; @) qclreol(); 1 }! w& K' { E2 |
getch();
8 e- F5 |. t# l0 Y8 n2 T3 b" |return 0; 4 z4 R% ?( x* r# P( I% M* \( B
}
- B: g# i& Y! I8 `$ @$ g D/ ?& q% X+ R! @/ O' y; @9 @
! K0 ?/ S7 Z& O4 ?# Y" g8 d . s6 `( H, B" ?1 t& I
函数名: clrscr
8 \/ ]8 c% R0 r+ i4 h功 能: 清除文本模式窗口
1 o/ M7 I" a" _7 |. s1 V用 法: void clrscr(void);
' N ]& J" p# J" @7 T; e程序例:
m1 j3 H( ]* G! y4 r#include
. N3 K/ I* r/ i0 c3 u; E) Zint main(void)
" A3 f, K- X* d9 k% G1 K{ : P4 [ e3 S& F( Q
int i;
8 x& F6 |0 O7 oclrscr();
- e1 ~# h" a wfor (i = 0; i < 20; i++)
: R0 Q8 M) _# I* O% Y! e' Vcprintf("%d\r\n", i); $ h' Z- @ J3 Z+ Y' ~, d
cprintf("\r\nPress any key to clear screen");
0 ~4 \/ }) ^$ `+ egetch();
- K+ U, g2 b9 k9 yclrscr();
4 S7 F5 @& w/ u$ ]+ {4 n6 A9 Q, V! Ucprintf("The screen has been cleared!");
8 f/ s. R$ d3 u3 Agetch();
0 d( Z- z! [" i- Y9 xreturn 0; - {& s0 o% h j7 [
} 0 f% \. Z: M1 {$ z5 D# E
9 [6 x& c% e( S, s* B. N6 ?
2 s. l8 h( G+ s. M. D( d
8 w: q: D7 S0 r# V$ R1 b, }2 h函数名: coreleft ) I) x" I1 Y$ _3 o6 @
功 能: 返回未使用内存的大小
7 U1 }1 O6 B3 s% ?. B' d用 法: unsigned coreleft(void);
7 o. r4 B4 t& H& E程序例:
& N- \. X. `4 B$ }& B8 b#include ! a' ]7 Z* f) r9 j: Z
#include 4 u- a- |8 |# v2 C& v6 i
int main(void)
. d# Z1 [3 ` B6 M$ o{ + q3 R! b/ m0 d
printf("The difference between the highest allocated block and\n");
2 w2 C1 s* B3 w' Y, L6 Q6 Zprintf("the top of the heap is: %lu bytes\n", (unsigned long) coreleft());
6 f+ P8 c: @4 I$ C1 K$ Ireturn 0; % p' d' a( E3 x
} & T4 X: x7 E$ q) U6 a6 D2 g
, e% `) G9 H9 @4 w- Z+ Z& b. ]函数名: cos # H2 A# k3 u, Z: S% D
功 能: 余弦函数 5 \9 s9 E" [( l, G
用 法: double cos(double x);
. ?* A% b% {# a* ^# I程序例:
% S: c2 E$ J* W8 p/ \5 {2 |* S7 f#include ; O3 L3 x2 M$ V# S. u+ e
#include
8 _7 f# t9 A! b0 ~0 L! zint main(void)
, z3 k; @/ R2 g3 f7 k{
$ N( U& f# q6 O- g: n$ u, zdouble result; , B, Q- u. y& v" m
double x = 0.5;
; ^* @4 Z; L8 qresult = cos(x); # w. V. |9 M8 p' s3 ^
printf("The cosine of %lf is %lf\n", x, result);
5 N+ f6 u8 R2 w% F/ X6 U$ Nreturn 0;
1 k% t3 K* L3 x4 `} $ r$ l# r6 g. ?+ I5 E9 q3 `
* t! t! `3 P. W# Z. }! P; W% G
' o! Q3 i, r. `- |
: Z+ _, W+ {2 ~0 J% r9 N
函数名: cosh - p1 J: V! G* Q0 y7 D
功 能: 双曲余弦函数
2 ?- N6 z! l. a1 w2 d1 E7 ?3 K% N; {- o用 法: dluble cosh(double x); # j5 p7 G! d4 k5 k3 K' `
程序例: 0 b& ^+ a+ W+ o5 G
#include 9 b: R4 Y. D& s/ V3 r: L3 A
#include
/ P& Y5 I) P7 r2 t7 x9 }int main(void)
+ d! f9 [( G# S( N5 k{
3 }" r+ K5 r+ w( H8 |# }$ F( j2 t% idouble result;
) Q/ \% Z! ~! j! o6 w# g* E4 v) Udouble x = 0.5;
1 x9 b1 H* r5 n; P* W7 @result = cosh(x);
5 E" G; o9 n& Wprintf("The hyperboic cosine of %lf is %lf\n", x, result);
~/ B1 r) a7 ?return 0;
3 @% J8 [( {; W1 T3 T}
# l- o. I. `0 N* c, Z8 |1 L! N- X: f4 ]% q) H% k1 d) D) f
1 P! v7 X. M2 }! f; t0 ?# U
' g9 S7 I/ m7 V6 v6 w# \函数名: country
6 N% n2 S [8 E* C6 B( _功 能: 返回与国家有关的信息 # k {0 y2 J( w3 }+ P* m
用 法: struct COUNTRY *country(int countrycode, struct country *country);
+ ?8 [8 }7 O8 x' l) A0 x, [) _" j程序例: * c5 ]+ P) \: f$ s$ a6 z! B2 s
#include / R, k7 o4 x3 {0 i, ]1 ^) B. r
#include
7 }4 z3 ~2 \* K7 X/ H2 u. g. z) p4 N#define USA 0 2 a H5 b$ z8 A% T
int main(void) : c9 b6 i) D K
{
) {' a1 v( R0 u% w# p' q* S6 astruct COUNTRY country_info;
2 s) d8 ~0 _ T/ |8 m$ p$ p. ycountry(USA, &country_info); 2 {! X2 J9 ]5 G& Q. l: c1 g
printf("The currency symbol for the USA is: %s\n", 4 L& u5 b! u$ h; t; U0 n0 J
country_info.co_curr); ) }( h" [) n; y: x l
return 0;
D0 J; a) Z+ a* {}
. M+ z! b6 q* ~- ^$ K2 g7 z- u' u
% v2 D+ s# H3 @# M3 ^* z, E
& D3 X4 {0 N8 \, k1 Y
$ P$ C9 a; |+ H8 I函数名: cprintf
* C a9 m9 e( `6 f+ j, {, [. y' f功 能: 送格式化输出至屏幕
& x( ?! n. O6 ?- ?6 G& D3 X用 法: int cprintf(const char *format[, argument, ...]); 0 y" d5 N" K' I1 b) j0 P
程序例:
7 G4 ?1 r* i8 ]#include # T; q9 B4 X% D4 ~" A
int main(void)
8 Q7 e, p4 j# ^5 T{ ( b6 j7 o% v) y6 k7 b' I+ M
/* clear the screen */ $ O( o/ R ~& K2 b. {; ^
clrscr(); 3 S% [9 p3 B( u2 f+ K, M
/* create a text window */
' \5 F. y3 j3 ?' I* x) t0 ]& Bwindow(10, 10, 80, 25);
; \. u0 M* z P) Q/* output some text in the window */ 3 s9 v9 a7 S! R" [/ t: s( l2 h
cprintf("Hello world\r\n");
; r/ |0 V2 n5 e% t/ s0 g7 ?0 {! _6 ^/* wait for a key */
& X6 r4 Z+ x8 o: M. agetch(); , c6 U. @2 ?3 Q" }8 f
return 0;
5 d9 ] v9 e/ C& F4 T* g} & |2 r9 g. q0 ^, X
9 f5 x5 Z. P. V8 G! t, {, {
7 L' _4 Z# u% N- [7 E# y( m( W8 [
: V( q0 k. K- h% O/ s
函数名: cputs
8 j) s, s5 O* S! K) ]+ N功 能: 写字符到屏幕
- s+ x H5 b: @% o1 v8 \+ R用 法: void cputs(const char *string);
0 F8 h3 Q3 n0 ?, u$ ?4 e7 F程序例:
& y; X0 J8 c7 e. F+ S: `( c#include / e4 v! ~/ t" m K5 V7 J
int main(void)
" h& W% d+ T3 M @{ 0 ]2 H5 V$ b9 [* d' N3 n
/* clear the screen */
1 k+ G6 Y/ ?* n- Hclrscr(); 0 v5 l! R& a" [# b7 C! N
/* create a text window */ / G ~' W; c. {6 P' L
window(10, 10, 80, 25); ' B. n; T7 {* W. L6 c! w
/* output some text in the window */
! y# V0 ^. N" P1 G4 H; ocputs("This is within the window\r\n");
7 R h4 Q; Q D }4 _' }+ e/* wait for a key */ * X$ o9 H- r+ C* r' a1 b0 u; Q
getch(); 3 w" [; H* }3 j2 p$ L1 Q4 M
return 0; " O0 s; k9 `4 w! n& K7 R
} 1 g: S( E a" y) n P3 a; F2 c- T
, f5 B$ R/ w; [' x+ j
! l" Z: `+ ^* u7 h 4 S U' L3 v0 ^7 E
函数名: _creat creat
1 h. k* T) z7 G# U7 X. ?功 能: 创建一个新文件或重写一个已存在的文件
3 l# M) h* k& G. d用 法: int creat (const char *filename, int permiss); 8 n* [* s0 p$ b0 h
程序例: . z3 S N! n8 ?! W( K" m3 [! X w* D
#include ; e, ]3 Z7 R/ k% P" D
#include
- C7 T5 r' U) d @6 J( H+ Q6 T6 P1 d ?/ Y#include
1 d/ B7 D5 f3 A2 f8 a1 F/ O#include - [+ }" @7 Q' N! i! I/ ^/ @
int main(void)
; n, |2 r8 D* \7 b+ W8 t{ & I8 p8 y) w- j! P. t+ t
int handle;
1 D. m+ N' c0 B; r rchar buf[11] = "0123456789"; % z- ]1 T- n: Z, ^
/* change the default file mode from text to binary */
% a0 M! m9 [' ~$ @( d2 {_fmode = O_BINARY;
! h4 h7 ?: D3 I3 [# T6 C/* create a binary file for reading and writing */ 0 K! c1 @7 l/ `" D6 Y
handle = creat("DUMMY.FIL", S_IREAD | S_IWRITE); g4 x' i; r, E& Q* ^ U
/* write 10 bytes to the file */
}* _/ l% H6 a& Q* _5 Qwrite(handle, buf, strlen(buf)); 9 ]( T( R: O$ Q* y4 {
/* close the file */
4 E( S* X+ f# _9 {" F* iclose(handle); . y9 l# w. E! P5 s
return 0;
* O$ O& H0 b a- a) _2 b}
) T! e/ j& w8 }0 {4 d( d% I" N
3 R6 n! B( F) K: y3 d函数名: creatnew # p; t/ |9 u, D. d
功 能: 创建一个新文件 3 q q7 w0 R9 ^5 A {% {7 U7 Q
用 法: int creatnew(const char *filename, int attrib);
" C$ A9 w7 A( \' b程序例:
' ~" a' e* b8 A( G. Q; b#include 3 A. Z- P5 W' n: X5 W% p
#include # k2 \ p+ [1 @* z
#include ' x: E, a @2 Z( |- {9 t
#include
5 ?" a8 e+ e9 k8 l1 L3 y#include ; i- {, v6 a1 X2 a. i1 d% a
int main(void) ' k0 U9 G) P8 [' |$ z
{
# g' F% S6 x% ]0 }int handle; 4 _) g, q( y8 n+ P3 l& r# |
char buf[11] = "0123456789"; ( c* \2 q! D; \: L2 J+ e1 o* Q
/* attempt to create a file that doesn't already exist */
) {: @! B% D4 J# `handle = creatnew("DUMMY.FIL", 0); % c; S' w! o* ]- l6 e
if (handle == -1)
D8 L0 }4 f* ^4 N8 ~0 h3 Tprintf("DUMMY.FIL already exists.\n"); * v! ~8 z% f ^: R& _4 u
else ! P1 P0 C$ m {) z, V/ d
{ ) V# I; ?6 c0 J3 T, h. ]& _
printf("DUMMY.FIL successfully created.\n");
$ U; P* Q3 i8 z! y& _' _$ Iwrite(handle, buf, strlen(buf));
. r$ j$ n3 d- ?1 |4 q# b0 `8 bclose(handle); + v& s( Y2 ?, G; o, N% h
}
1 ]( V G) K8 [return 0; 2 L% g5 n4 _3 e. F
} : f* C1 Y6 H' z
0 E- \3 c3 M' C% e* v9 o% O1 G3 h3 `0 [$ x3 z( Z* r
/ Y7 G5 A/ n: |% J/ j8 E' g
函数名: creattemp
$ q1 C9 e! t6 [% p) C! e+ B0 \7 W; ?功 能: 创建一个新文件或重写一个已存在的文件
# P( z @5 g* l用 法: int creattemp(const char *filename, int attrib); 3 n2 e% `8 t2 ~
程序例:
, ?. y( u* k. C. G* ~" l#include
# E5 X( X/ F) l q2 m8 C& w3 Q#include
8 c4 M+ Q p! P2 s. A3 R#include
; h( S! n: q! [! n3 `int main(void) 0 Q, O3 v* [, W" \2 i7 G
{ ; m4 E( g/ X# ^; ?0 z/ K8 V4 g
int handle;
9 `" ~% o. d; Jchar pathname[128];
4 j2 h# E# O/ Z4 \& vstrcpy(pathname, "\\");
& ?# ]% F* F, B) m% C7 E4 N* w/* create a unique file in the root directory */
3 c+ q3 ]* ?/ e; x0 W# w* Y) o3 khandle = creattemp(pathname, 0); ) `3 ~- \& k0 ?- O+ o( s& \/ H
printf("%s was the unique file created.\n", pathname);
3 z' @6 Y# d4 P. R3 k9 U0 C' n. qclose(handle);
/ |! X% ^7 u+ s; Lreturn 0;
. e7 K) _% w' L7 x} % D* c) R0 o7 J# e& B
: Z& N" b- h+ ]0 w" ^8 I4 t ], }& W# p) n; p& {
* a$ m! J0 ]$ V$ ~& x
函数名: cscanf 3 B Z7 I9 D& ?8 N
功 能: 从控制台执行格式化输入
7 G7 F, g, v! Y用 法: int cscanf(char *format[,argument, ...]); 3 d& x% u9 t; E0 x: Q5 R3 @9 _$ W
程序例: 2 O' _" P/ E5 W2 g5 N' ^0 M
#include
6 C s0 q3 Z( {" f/ s# U2 Bint main(void) 8 P6 m$ q, ]$ a, b5 ^) j+ {
{
5 l T2 [2 ^3 s0 [! x# l6 |% U) q+ Rchar string[80];
/ I# D# ?" U3 k; Y/* clear the screen */ ' J4 x1 F3 t9 m* u
clrscr();
+ F7 Q$ X& m+ W$ V0 J/* Prompt the user for input */
' O6 Y# R/ O( v, k4 Z7 i' fcprintf("Enter a string with no spaces:"); , a9 Q/ ^- `% ]* s; E4 _9 p* \
/* read the input */ , }9 @% V, T; E0 D" k& Z
cscanf("%s", string);
" I: q5 x6 Q' u0 n* ?/* display what was read */
9 h4 h, B1 Z! |+ wcprintf("\r\nThe string entered is: %s", string); 0 T6 X6 v2 x' j1 @4 T
return 0;
( D8 X3 q" F- Z; Z* C3 g+ ^}
; p' Z0 H. a9 ?- v: p
4 {/ l Z* _! ]3 o! {' H; K' v9 I- N
/ A3 {$ G. Z5 C0 u
函数名: ctime ; C. `* C1 G1 V" d( A
功 能: 把日期和时间转换为字符串 - D% ~! F3 H( ~- }/ j+ b
用 法: char *ctime(const time_t *time); " q$ n( V: u; n, ~7 b$ p
程序例:
7 P t& Q- k7 G% O/ h* y#include
. ^1 T0 J( V: L#include 6 U7 f& Z! l f9 ]% D1 C2 A7 y
int main(void) ( @ J- ]0 e) f# C" O* \7 ? n
{
+ N& L9 @! E- Ktime_t t;
0 T! \/ s" E0 ttime(&t); 3 C% `' ]/ P+ U- ]- ]0 k/ e) T
printf("Today's date and time: %s\n", ctime(&t)); ) S3 u3 U7 @) Q1 ?& |# ~ W1 J. X
return 0; 8 k! t1 T- c( ~5 l: ?
}
* P O( X9 E( T( V% Q6 A$ b6 m
( f$ L) B# }% N6 e- U. K3 x* v0 s1 m* S& [7 z5 \% W( p1 C, b- x4 t
$ V9 K# m G& L2 |6 W. S
函数名: ctrlbrk ) w3 u* M1 J ~0 N' w( y2 \1 ]' K4 R! ~
功 能: 设置Ctrl-Break处理程序 " ^9 ~, r! Y$ V! a! Q8 r6 h. r$ k
用 法: void ctrlbrk(*fptr)(void); / i2 W* Y4 }; d4 G2 K
程序例:
" R3 }- I/ X% g, i8 q6 T$ _#include
: G! `& V+ O: T& `+ c9 P#include
d, q/ M, O: ~& q' [$ J/ ^#define ABORT 0
. ^; Q9 o$ J9 J( ?! hint c_break(void)
7 h, e* m6 p, K9 K+ P{
0 k+ l# z: v* i- \% F B6 Oprintf("Control-Break pressed. Program aborting ...\n");
* k3 u5 T9 I1 I6 breturn (ABORT);
6 M! I& }0 O* v& U}
4 W8 H9 T' j$ F( I6 g# [) \/ {int main(void)
0 a) K' E" |( d j{ ! e" |/ Z; u, J0 a( t/ X- w
ctrlbrk(c_break); ; v% K/ X, S$ B! h
for(;;) & R7 j0 @3 M% j
{
1 w" y0 j& b+ U/ x( R: Z8 F5 F: vprintf("Looping... Press to quit:\n");
4 X4 F5 e: c1 I/ L7 R2 Y}
) z. U- C9 q; C: C, W, greturn 0; 1 b/ h& Y% P" L2 _+ e& ~
} |