|
函数大全(c开头)3 c6 |& u1 P# u& H
7 w2 L" W4 [5 ^/ Q7 p7 K
函数名: cabs
* ?( |5 N" N" W! V O$ }功 能: 计算复数的绝对值 0 p. V+ i V- Q: d
用 法: double cabs(struct complex z);
$ U6 F B, Z! R6 X程序例:
+ {. M+ s3 @# s; S#include 7 p- k/ b5 m$ }2 u: B+ Z: D3 r. O, L
#include 2 [$ t4 k- L) ?' d4 Y7 o4 I: P7 X0 E
int main(void)
% S1 y4 i2 A! r{
/ k' d9 F; n3 {% t: qstruct complex z; : A9 `5 P: S' a
double val; ! z+ D I8 q, a, v) o! c. ?
z.x = 2.0; - n4 J* N8 H, x
z.y = 1.0;
! C B: a! E- n, }; M. C6 `! ?val = cabs(z); 3 y% H$ C, d' n g
printf("The absolute value of %.2lfi %.2lfj is %.2lf", z.x, z.y, val); ; l& m, R1 f7 j
return 0;
0 Q* a8 {0 t8 k7 g}
P" D. i5 g/ l5 m" P1 w6 S6 h
1 G+ c& P8 y. o+ ^
8 r7 i( x% k: Z7 M函数名: calloc
$ z- ?$ ~5 G# `3 B$ S' ?# v) B功 能: 分配主存储器 8 F% n5 n' o* I/ `4 O
用 法: void *calloc(size_t nelem, size_t elsize);
- G5 V% q7 V: x6 L程序例:
\1 j6 B* U: h: K) E6 D#include
7 l$ _6 @ Q! u. B y6 Q% f% g7 u#include : q, a9 X; Q/ f( v: o7 k% G+ M* E# ^
int main(void)
: K1 w8 g9 p4 ~( i: I0 i. a{ . V; \1 _$ w' J
char *str = NULL;
* e( L2 u& c1 D& X0 b* ^5 _/* allocate memory for string */
9 D- a J1 L, c4 X$ }str = calloc(10, sizeof(char)); , b) r# F. C% D( y
/* copy "Hello" into string */
3 @2 Z& X ?- L* V( g7 E9 fstrcpy(str, "Hello"); # L/ x0 Y" G, a6 A) t
/* display string */
7 u L9 g! z& A3 s- t% aprintf("String is %s\n", str); 3 d0 Z, V; Z7 L t* C# d- f8 A2 t
/* free memory */
% m, p) I2 [5 q. Y( Ifree(str);
" \% t* l) O% {' U0 Yreturn 0;
1 K5 E, T6 Z5 e! v1 J}
' h) T8 U1 X5 a7 U! E' Q3 c1 `. d. q1 K! i* J4 a
' R8 Q1 ~4 \+ ?
; u- @, a- E* C2 f) e( l/ ^函数名: ceil 1 L% u7 S) [+ Y2 B( D
功 能: 向上舍入 7 O& f0 M; h5 q( I& x1 [
用 法: double ceil(double x);
h7 [$ ?! ^+ y. m: u程序例:
7 L7 w$ T' m% ?/ R6 g+ A' z#include
8 X, m- X( b+ J9 g#include , l0 ] l9 _3 F' b
int main(void)
; ~ ^- s: g; U4 `% Z. S1 M{ . e9 Q( {7 `( |+ n: ?+ ^8 {# n
double number = 123.54;
( t% y, V$ I, i7 a& N( ]" ]0 t# ydouble down, up;
4 A( p0 m* @! Z9 u W* Jdown = floor(number);
# S8 R: n: W; O% k+ c! `6 _4 Y2 Q( v+ bup = ceil(number);
- S; o: t$ @/ x7 lprintf("original number %5.2lf\n", number);
+ C4 N a% i/ {1 |9 hprintf("number rounded down %5.2lf\n", down);
6 U/ {5 v# P( h/ `printf("number rounded up %5.2lf\n", up);
1 ^/ s& J# j$ N, h- }: Sreturn 0; 8 q* ]8 _4 j# x9 [) r5 @4 P
} - h* {& R+ W! y
# c3 K* d' ]- m1 T: r6 k1 z6 P. o8 m! O3 l9 Z, j* N" ^
+ h3 V' U# \) m) ~. ~: I$ Y
函数名: cgets
3 s+ c5 ^' d; c* \& A功 能: 从控制台读字符串
# c/ c/ a y' A* Q: Y% b& Y用 法: char *cgets(char *str);
: K( }$ E7 Z& u: k& [7 ~1 |程序例: 8 g- t# r: `" s. C8 {
#include % A9 i/ ^6 M9 X* o, P z! j0 R4 Y/ z6 l
#include
5 U k6 J& o% u+ @int main(void)
5 f, m1 k0 h' B! ^' I0 e{ # z7 d% m8 u+ Q$ ^+ r) i. Y( i
char buffer[83]; ! o; e. q) E/ D1 q( i% b) r
char *p;
0 k3 J4 n4 Z! Q* E( @/* There's space for 80 characters plus the NULL terminator */
+ C7 g- c' q" S" hbuffer[0] = 81;
8 g; U9 K# z. N3 `' l% G" Bprintf("Input some chars:"); * ^9 [% k! G9 M+ H- m |
p = cgets(buffer);
6 ?/ v9 t6 Z* K/ q! J( aprintf("\ncgets read %d characters: \"%s\"\n", buffer[1], p); 5 c' @$ ?7 @. E! d7 x% e: @ v
printf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer); % }# x% F9 ?% u- {8 K) x' L0 V
/* Leave room for 5 characters plus the NULL terminator */ & p7 n1 M4 ?9 \) O8 W
buffer[0] = 6; " A, R! k8 x4 i* g Z5 Z) P
printf("Input some chars:");
- ]9 p/ y* O% i* V) j" Lp = cgets(buffer); ! q4 E: l2 j1 \) ~ _# V
printf("\ncgets read %d characters: \"%s\"\n", buffer[1], p); ( g* q) _. n. L& r# ^% l8 x
printf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer);
6 f" q( o. H0 vreturn 0;
! e, X: l% s' u- f6 Z3 }: i: ?5 d: B}
/ n( q/ h% [5 O9 y$ v \. c
- u. }& W5 `$ `9 ^, ~
9 D1 W- S# H0 z* U' k
# C/ l$ M# q9 Y8 p函数名: chdir : E1 y: K% Z; [" f4 g0 q! x
功 能: 改变工作目录
8 e Y+ D: Y8 u2 u用 法: int chdir(const char *path);
( o2 g2 L$ @. J& b/ n程序例: * t) W8 }% w* x8 Y$ C) Y
#include
/ K: W( T9 Z0 x9 I+ U) O$ Q#include * U* C* L: I$ x/ a
#include
0 r+ z6 w+ x9 P% G8 |char old_dir[MAXDIR]; ; Y' n4 Y ?0 r7 Q3 [
char new_dir[MAXDIR]; ) h5 g6 h9 C7 a5 `1 W' O5 P( C' I; ]: N
int main(void)
: U) P" I4 j, ^{ ( J) A$ p- m7 L4 v0 b4 m
if (getcurdir(0, old_dir))
" B* @* ~; o \- a{
5 C9 H6 ~0 c& r- f& Y5 _% Pperror("getcurdir()"); - h) I, I) u& O/ l
exit(1); 0 k) k0 [* l' d4 A6 ^( R5 P) U
} d4 G4 v# L' l
printf("Current directory is: \\%s\n", old_dir); 9 b' i8 Y" \. \/ B5 Q+ n9 n9 q
if (chdir("\\")) & }* \& x5 y8 n. u
{
7 B8 o+ f, p, b* V8 Qperror("chdir()");
8 T% O+ s% h5 ^; g2 rexit(1);
1 B9 `: F( u( Z2 R, ?} + z" O1 e% l9 x" J7 t
if (getcurdir(0, new_dir)) , S0 O* q$ j* \, J4 ?+ U
{
8 [9 m0 n T! y" j9 i) r6 pperror("getcurdir()");
5 s) f' H* \$ A7 H4 Jexit(1); ) x5 @" f) R& N8 ?
}
5 G* Y# ]# G5 G0 f5 S! l, [% iprintf("Current directory is now: \\%s\n", new_dir); 8 s( K0 G$ @2 l! x& }0 ]
printf("\nChanging back to orignal directory: \\%s\n", old_dir);
; n) U: @! g, hif (chdir(old_dir)) / [2 r! W% O9 b. L' C
{ : {$ F3 l8 y$ F% d# b
perror("chdir()");
' X* r: N' W+ p' V% Pexit(1);
7 D0 S0 d" c1 B: \7 S' P4 o' v}
; l# B6 s: M* i& _. ?' Areturn 0;
* ]$ I. i, ?" d% U& a& `} 8 P3 ~/ c0 ~3 @! d8 t) D+ ]
/ U* r, n/ V& S3 Z! X, h, L6 D" z b
9 @' Y) q7 L7 W% ~$ G函数名: _chmod, chmod
5 o* ^' ~% ~! J. \功 能: 改变文件的访问方式 / h R; i- [% T6 h$ d# z
用 法: int chmod(const char *filename, int permiss); ; v4 W8 h/ \8 u: c6 p
程序例:
M. o: B# x8 R& f& F% h3 A#include 4 X6 {- s* t5 E# O
#include " M% I0 [' _7 ]+ X+ |; m
#include ( h+ I; \/ C/ Y P/ Y
void make_read_only(char *filename);
" [2 q, ?: Y5 T3 f$ B: }8 Dint main(void)
' v% C* `" G9 T7 i{
& A' T# |4 z4 ~# jmake_read_only("NOTEXIST.FIL");
* P! z" \& e5 K$ nmake_read_only("MYFILE.FIL"); b. R/ }: R0 c, ]/ A9 e
return 0; . }* i$ W! X" T2 h! o. L
}
! p% S1 o1 o$ M6 r* y* ]void make_read_only(char *filename)
8 v$ z# x( _9 I0 ?9 H{
# w! b V: ^! G; K1 U' y; S$ eint stat;
! {& y6 _5 w [% H E& @) @; ?stat = chmod(filename, S_IREAD); ; G+ v* ]) N6 U" y2 u, s
if (stat)
0 }2 {: X2 i9 T* l2 c# xprintf("Couldn't make %s read-only\n", filename);
N' s8 o3 r. M r; Telse
/ l Z1 d% G. y& b+ m( j4 r+ Iprintf("Made %s read-only\n", filename); ! d+ s/ `, F" S6 u( a8 j
}
; m$ a# c) R+ b6 `/ _9 V* W, J+ M9 `: i" X2 q/ Q ^9 \/ H$ k
! Z( `/ O1 T! a # Z' B# b) E' \( X. j
函数名: chsize
" x' n1 K6 m# o' c功 能: 改变文件大小
0 v1 z; f4 i" z& `; u用 法: int chsize(int handle, long size);
: x. ?: N% B- Y( U- M; O! r程序例: % Y2 ~4 n3 d* F. f7 ]( R, U
#include
$ s/ S& \ u+ g3 d7 N5 ?3 { v#include
8 q; i! R. ^ M. a#include & @8 D* N3 A& e, i
int main(void)
: }5 j9 ^7 z& `- D p+ k7 R0 ?4 T{ + O9 X$ C) u2 I) o, N, S
int handle;
1 e7 f% e% G: Z! C6 E! Echar buf[11] = "0123456789";
! [# x v" ] r9 P) r q/* create text file containing 10 bytes */ ! l, {. v0 M5 X D
handle = open("DUMMY.FIL", O_CREAT); 6 z* E' r6 C' O/ \0 j! Q
write(handle, buf, strlen(buf)); 8 ]- V; P+ W [" S- L1 o/ ~1 F
/* truncate the file to 5 bytes in size */ ' Z& H; i) Y1 r5 S; R O3 d8 P
chsize(handle, 5); + \+ |8 U* M* A- J6 O
/* close the file */
6 {4 Q: K, M: a Y( D6 q5 yclose(handle);
$ W1 W' w% Q" ?; r* S. I3 treturn 0;
- [; @6 `) Z$ h5 u4 B} X% ?# \7 e y4 T
4 F1 |# m! W# z3 V+ Z- l
' v: U2 Q. L, V- A, S+ x$ b
函数名: circle
& z3 F$ }" i p. B1 [/ ]功 能: 在给定半径以(x, y)为圆心画圆 5 X2 m5 a" x0 d0 o- C, o
用 法: void far circle(int x, int y, int radius);
2 i! }0 h0 a, a- `: N: T程序例:
4 n% |. X8 m. |: g/ l#include 4 N- G+ a7 T" a+ o1 x
#include
8 o8 G6 Q4 N' W2 J4 H) P) Y5 K. T#include 1 c+ e: [% J3 N7 [* H, T
#include . f+ b/ W" W% {1 v0 {5 E
int main(void) 2 u( O9 \4 _ H$ I
{
) g$ d% U5 T5 W; F8 k/* request auto detection */
8 L% k7 ~8 L2 |! j7 Z" K4 |. R' H$ Cint gdriver = DETECT, gmode, errorcode; 1 |2 k4 }. F( P4 [( T& n# v
int midx, midy;
& ]( F: o3 ~2 _2 Fint radius = 100; & i' w) |0 |" g0 K
/* initialize graphics and local variables */ W( q% E; h# Y$ P) ?! j
initgraph(&gdriver, &gmode, ""); 5 L8 E8 ~, g; F6 O8 k: m' a
/* read result of initialization */
9 ^+ P0 u4 b: c6 h, Uerrorcode = graphresult(); 4 Y2 S: g K# r) G, U$ H0 p
if (errorcode != grOk) /* an error occurred */
# w: R0 I: D& I2 Y% ^! R{ % H. B$ m9 l! G+ Z$ a, x
printf("Graphics error: %s\n", grapherrormsg(errorcode)); 6 Y# E0 |! z9 h1 ]: D
printf("Press any key to halt:"); : |/ v; M z! G9 D' i& c
getch();
5 B* F/ u; Z! J3 r: V) jexit(1); /* terminate with an error code */
+ r/ Z" ` K7 l$ E; M} 9 ` v/ f' `3 Z! ]
midx = getmaxx() / 2;
4 Y- P L" S0 cmidy = getmaxy() / 2;
6 l6 n- Q2 u9 @& p6 ^5 u/ ]setcolor(getmaxcolor()); " q4 P5 A5 {7 t( C: @ l. K
/* draw the circle */ 6 S2 t0 x/ s: y/ B6 P
circle(midx, midy, radius); / b7 m, O% h# F
/* clean up */
6 O* Y6 ?% o* C+ agetch(); 8 K0 ^8 a; c- ]
closegraph();
/ x ^2 H4 W7 w$ Wreturn 0;
; d* k1 p8 W* Z/ o% M} 4 z9 j+ g3 I. s: B/ f( d* U
% e. P1 G; H$ r6 F% A
# p& d. B) d2 ?; [1 Q, ]' [
1 D1 B& d. U9 E/ x3 X! j函数名: cleardevice * m# `2 i0 o& e( ~9 t0 W2 q
功 能: 清除图形屏幕 / i4 Y: s/ ~1 B" ^4 X7 ~; u
用 法: void far cleardevice(void);
; U+ _8 O, H: T. q. `; g程序例:
- Z/ h% [ i/ [5 C7 \#include
6 V! m: a1 r8 |#include / @/ l) @ f" A8 U C1 |
#include " o5 t) D }# c: U5 @
#include 2 }. q5 O4 |% Q2 E5 H D# m
int main(void)
8 G0 a! }+ _7 N: \# J% ^{
3 z, M; [* W: x) ?/* request auto detection */
2 m& I$ |9 r) H# Z1 j) U6 g& M" tint gdriver = DETECT, gmode, errorcode; ) V; A, b( a9 `, a* x7 G% |5 j8 |7 V
int midx, midy; + a4 `+ `) M8 m# m& K
/* initialize graphics and local variables */ " N% G- c7 _1 j' M" S* Q" {& a
initgraph(&gdriver, &gmode, ""); ! s! Y4 ^. D2 ^5 j r, J
/* read result of initialization */ , s4 X; }( V% m) p2 d: l
errorcode = graphresult();
% d% b* w# U4 B3 m3 }if (errorcode != grOk) /* an error occurred */ $ C% B/ `: `& {5 m
{ ; L% o/ r+ o1 e4 g- r$ J3 X; ]/ r
printf("Graphics error: %s\n", grapherrormsg(errorcode));
; W, w, M/ O" W5 l& pprintf("Press any key to halt:"); : _! u1 [- Y* ~: u/ u3 [0 D. q; E- K. f
getch();
, Z5 x& ~& y7 O `1 Uexit(1); /* terminate with an error code */ " g9 _9 O! I( f
} ) T" w% Z' t9 a; I6 [+ f6 x
midx = getmaxx() / 2; * u2 J z5 U4 Q% |6 R/ l
midy = getmaxy() / 2;
0 V8 [% v6 {. n9 c asetcolor(getmaxcolor());
" d1 m0 O8 Z D0 ` t/* for centering screen messages */
! p$ H' a$ [6 P. t& Wsettextjustify(CENTER_TEXT, CENTER_TEXT);
# L! V/ z% T5 q: Z* w. l: ?+ {. `. y' f/* output a message to the screen */ + j* U& u* m6 q/ q6 l/ }1 b
outtextxy(midx, midy, "press any key to clear the screen:");
2 ]- V" q: e3 H9 q; m/* wait for a key */
, ?# f' r" X% p; _+ {getch(); 8 D# f7 N% A- G! e1 Q
/* clear the screen */ ' A% V. p' i- Z
cleardevice();
$ R$ v* p& I F1 v; B/* output another message */ 6 Z& R+ n( U) e
outtextxy(midx, midy, "press any key to quit:"); 5 G# u, h3 U0 t" @, V/ f: Z/ X. R
/* clean up */ 8 k2 x! `) j3 G3 X: ?2 i8 S
getch(); " `- u: q4 u# J; B: M- \- F( h5 Y
closegraph(); 2 F; A6 g4 `& d, z' P e% r
return 0; 4 A1 ?, G/ E: J J$ Q5 }6 q
}
/ N5 |: T+ j% A5 z8 P( E9 b' F4 N4 P/ t; [" y8 I5 G
5 H. |" k6 i9 F7 y' M
9 Y2 q4 X: W) V% A) g函数名: clearerr
: a* p4 `+ K @- d% \ l功 能: 复位错误标志
2 t: e" z+ ] k1 C+ H! {/ Y1 ^. H7 y; u5 ]用 法:void clearerr(FILE *stream); + W# W9 o# C% G2 L4 Q! B4 L+ ]
程序例:
5 C9 S4 u: O7 b/ \ z#include : h$ P2 t6 W+ ], B1 Y* h
int main(void)
! l7 F' V/ i! S) _8 q1 c) R4 E4 d3 l{ 9 k1 x1 L( ^* y& h* X$ P& [" D
FILE *fp;
: T2 i% F' m4 F9 a3 ]* x! mchar ch; 5 R1 |7 k9 l5 U! B) j6 J( ?9 I/ a
/* open a file for writing */ 7 U$ E8 K7 m" D9 Y/ A" o/ W4 s
fp = fopen("DUMMY.FIL", "w"); 6 D( I- S+ l% ?& ?8 X9 |8 Y7 h8 y" z- v
/* force an error condition by attempting to read */
+ _: c' G) z5 [" m0 x, @; g2 K- `ch = fgetc(fp); - l# L$ Y, L+ X m! t" R
printf("%c\n",ch);
- f: T6 r9 R Z5 H" y0 mif (ferror(fp)) 0 _0 I; }/ _" P- ~/ I
{
' f. |, I \1 ~( i; ^/* display an error message */ * Z( W; y5 G z& h
printf("Error reading from DUMMY.FIL\n");
: c0 N. N7 A. x5 z/* reset the error and EOF indicators */
" @- }3 G: }( l7 I7 ^: Lclearerr(fp);
; c' c; Y# N$ C5 E& ^, w}
( G/ C/ j) Q- U1 T4 V$ F8 z6 _! }fclose(fp);
, k) E7 v% W' R/ o9 L# h- Preturn 0;
- P. a1 u( I: e; p% j3 ?9 R" V/ H}
1 e& e% O/ M; r1 G! L* t- V& {8 ]# M3 s1 R/ w: W
( Q$ ~- v5 }* A9 p2 r- G/ ~
* A3 `8 k/ O X- _. g函数名: clearviewport
& b- i) A% T3 r功 能: 清除图形视区 # h- G" Z% O" A% D+ \$ A
用 法: void far clearviewport(void); - a8 \* k" ?+ c1 B, Z8 h
程序例: 7 y* Y0 t9 h7 Y# K+ v7 _2 U4 o: T0 T
#include ; u* B& r# q4 x0 b
#include , W% s) x# m# T0 ]# f6 S% x
#include
! c( e9 ?1 _3 W#include
3 H1 v$ e6 I1 ?: n1 b2 K% ~0 p#define CLIP_ON 1 /* activates clipping in viewport */ % I' Y, u# h# ]9 L8 D8 I
int main(void)
4 l6 ^& c) e+ E2 \. H$ b4 \4 ?{
8 I* S* _% A( @" Q8 [/* request auto detection */
# p- y( d4 c! B, m" }5 W) dint gdriver = DETECT, gmode, errorcode; : u1 I- }3 F9 [2 {& \
int ht;
$ ^/ T& Q1 l/ `' Y+ x4 ^8 Q# J/* initialize graphics and local variables */ - @9 [' O* a+ u. m; k
initgraph(&gdriver, &gmode, ""); . H- a# y1 h+ Q0 I
/* read result of initialization */
: X' j! ?1 O! P; z( |# D) G% Ierrorcode = graphresult();
5 Z' c6 \( F6 B9 i" U- Iif (errorcode != grOk) /* an error occurred */
# v6 e: y" o, Z' S0 T" Y9 l3 n1 u{ " ~( b+ D* w# S; I$ f% \/ @. }
printf("Graphics error: %s\n", grapherrormsg(errorcode)); . q- p+ w. i# h0 J5 Y
printf("Press any key to halt:");
, z5 \) r0 d, Tgetch();
9 q1 W) c( q' h' texit(1); /* terminate with an error code */ ' y# T2 c& F& t/ X# @; E
} ( c8 w- m& X$ l# f7 }
setcolor(getmaxcolor()); ( L7 a" ?2 y* I. }: [' `
ht = textheight("W"); 7 a A, L. K: F `7 i
/* message in default full-screen viewport */
( @ a' ]; P+ C1 qouttextxy(0, 0, "* <-- (0, 0) in default viewport");
8 U% S# r4 L: }; @: K1 L2 Z/* create a smaller viewport */ % _0 X, b* L+ @7 M& b2 P3 [
setviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON);
: y# z0 K: q: z- d( N! Z/* display some messages */ 7 Q" `7 o. A& I3 b: }
outtextxy(0, 0, "* <-- (0, 0) in smaller viewport");
* `9 G4 { t# I: M0 jouttextxy(0, 2*ht, "Press any key to clear viewport:");
" l# M: Q0 u" r3 j- x* u/* wait for a key */
# O$ L& e, h- {4 k& C: Y8 g0 l8 Agetch(); 2 U) h% k6 B$ u
/* clear the viewport */ ' r% h k* {- g. i7 w. v2 @: F) {
clearviewport();
0 W2 {, |* @; B% @) B c: Q/* output another message */
) a# y, }/ k" f( D5 bouttextxy(0, 0, "Press any key to quit:");
$ r/ D& C2 k7 X/* clean up */ 5 R) ^2 F5 k( Y7 O. x; ~" R# `; z
getch(); ! E4 n: |% v* x4 ~1 i2 O. e; W
closegraph();
! `( N, u; \# [% y& xreturn 0; " o( _* x6 A/ b9 A2 `! p
} + P( W, D' ]7 X6 Y- l8 j y* z! `
' P+ ]+ \# B+ a$ ?2 e0 |/ L7 b5 P2 Y% [- c3 {
! R1 n! R! ~" H2 E' G
函数名: _close, close 9 H6 G6 K ~% `" T/ t" Z
功 能: 关闭文件句柄
4 t( f) T- x M9 v7 j5 A用 法: int close(int handle);
p; s) M$ N s+ F( h( Q$ R程序例: * R% U& Y* y6 X% I' |$ ~
#include
& G; r4 b8 q9 A! A" S" l" }#include 3 @6 W# k0 Z) R# N0 [# t5 n
#include 0 ~6 `3 a5 Y5 O( c1 ?/ d
#include
5 S0 ?$ ~$ Y, c! f4 A' Nmain()
* u( Z7 u, I: P0 Q+ m{
0 D2 b9 ^, k& `; Kint handle;
4 ]$ u+ ?1 J' xchar buf[11] = "0123456789";
. t N" y/ ]( x' A/* create a file containing 10 bytes */
8 U* \# S/ |# K) @0 H4 ?9 X/ lhandle = open("NEW.FIL", O_CREAT);
6 N. v$ x) X1 x! R- b) Mif (handle > -1)
6 w* e! \1 G0 r; s+ x{ . y$ g) ^) n) R5 ^
write(handle, buf, strlen(buf));
9 X! O% k: u) M/* close the file */ 7 N; r# l% D$ c! Z1 {7 P) Q6 r6 h
close(handle); ! }8 B$ z- d: C9 ~
} ( L) F7 v0 R# N7 u2 a0 \: }" f
else
" ~5 i5 M, K4 n- ~8 V+ k{
4 _6 l6 O+ ^& a2 l3 m2 ?% Kprintf("Error opening file\n"); & I5 L0 }1 L% X* R1 Q- z5 H4 p; X
} ) B1 u" g1 {0 Z2 Q
return 0; . {; t* B. \7 N$ v" k" t
} 6 }' Q- P( W' d. ~( ]
& w1 J& U* w, y( R. A
) a3 M8 T: g7 x& P0 R* S5 x
& u" H5 f* r$ c6 U
函数名: clock 9 E4 X U; i( ^- p0 D
功 能: 确定处理器时间
, X3 ~9 [: H& Q' P0 [& w用 法: clock_t clock(void); 9 z: T# u$ T1 [! x
程序例: - y" S) n; ^5 C+ T2 F* K0 B
#include 7 o% P; p( L0 b% n7 W# U8 @
#include
4 ]- i, E* W* d4 G#include
% _2 c4 K. l( j) ~5 u. i3 fint main(void)
3 R9 p2 X$ ?4 m+ T) v{
# T* G) V3 r+ Q7 e- \7 I0 `4 Aclock_t start, end;
% B$ J8 F7 n( ]start = clock();
& N2 T9 |) p! e3 Tdelay(2000); 8 C- b ?; ]8 Y, v& T
end = clock(); 9 a9 d6 a9 Z5 {+ ^: [2 I" _4 n' o
printf("The time was: %f\n", (end - start) / CLK_TCK); 6 _7 O) t. h5 U: K( u
return 0; - k* H9 W7 S2 }) K/ X
}
- E: d7 Q& Q/ T1 p9 j' Y; ^
H( H$ j' s$ e+ {* ~% T; U; J, A' t9 h3 r5 i+ j$ b
4 J2 R. e, H% |0 k) H( H
函数名: closegraph
1 L2 t' j0 Z' G( L功 能: 关闭图形系统 9 b4 Y+ P2 a. f# ~* L! e
用 法: void far closegraph(void);
! M ]$ L$ m" \' I程序例:
& P* J& `& ^% V: g5 K#include
/ i: S0 ?' v+ P/ Z( h- Y#include
( @2 t9 P/ W) T; Q N5 ~#include + z1 P" Q# H I" e
#include 3 w" p; s0 ] d+ P& }
int main(void)
5 X: @* R' u0 y: a{
; _0 m8 g* C1 n& [5 |9 S, v/* request auto detection */ ( P) I/ ~) X, H/ i
int gdriver = DETECT, gmode, errorcode; / L, i C) ^7 a; \8 Z* w+ @
int x, y;
' a( w" U( R. C$ W2 `/* initialize graphics mode */
7 Z3 @& g1 r& L/ |. e# Qinitgraph(&gdriver, &gmode, "");
* p0 P! B0 z! X8 U7 D/* read result of initialization */ ! w, K( n% i8 M, @! B! |) A5 l
errorcode = graphresult(); ( L9 R* y& c7 S5 L: ~0 ]
if (errorcode != grOk) /* an error / X% p9 W) G$ s$ F; z
occurred */
2 W' K8 y( ]' c9 g# E: f$ K6 D{
6 y( a+ s8 r) ]5 o5 M& `printf("Graphics error: %s\n", grapherrormsg(errorcode));
4 A8 M: s/ }! h( jprintf("Press any key to halt:");
0 X4 Z. ]* b# L! k# rgetch(); ' B0 W; I& I9 C ^% \
exit(1); /* terminate with an error code */
5 ?# X) W4 k- U" Q} ' T; `, U7 U1 t& p L
x = getmaxx() / 2; + B% ~" _$ [6 r: I1 O* m( `/ i
y = getmaxy() / 2;
: s% p& {# a$ I2 i- l( e/* output a message */
4 W, J" F8 t( zsettextjustify(CENTER_TEXT, CENTER_TEXT); 1 R. _3 p& W- z& h$ n- y! _8 H
outtextxy(x, y, "Press a key to close the graphics system:"); % L" v$ J' `% y3 ^- }
/* wait for a key */
/ @( G. c# r2 M! L2 e5 d& [ K! ngetch(); 9 \* S% a8 d4 ]( X5 y' B# }
/* closes down the graphics system */ ) y0 {% P: G( u1 h8 @
closegraph();
. B3 Z# n3 t9 Y+ Cprintf("We're now back in text mode.\n");
* O4 q: ~1 V- Vprintf("Press any key to halt:");
o( [- l& [! k( [; U+ K/ `7 Wgetch();
$ ~4 ~6 X' c4 kreturn 0; ! B M! B2 E/ \$ J/ d6 ]& w9 K$ Y
}
& h( W- I a! F8 {+ [7 t
- g3 W( }- p' |* m: i. z; m4 q/ H7 ]6 Q0 z/ ]* Y# j' u( M
& Q# i) U6 e/ Z函数名: clreol
- t6 n( ?2 c, k功 能: 在文本窗口中清除字符到行末
8 P& q+ G X& i用 法: void clreol(void); 4 z R ~, z8 [
程序例: $ Z. s- _6 {! K7 W- J0 t5 e
#include & K3 M5 }9 _* N: L+ ~% l- g* v/ x
int main(void)
. Z, V: K8 w' T+ A7 J# y{
2 Q4 N1 m/ P: X! y6 u+ Hclrscr();
8 w" ^( H; e1 N0 _cprintf("The function CLREOL clears all characters from the\r\n");
5 B" t: p/ g* I1 j6 A$ qcprintf("cursor position to the end of the line within the\r\n");
]6 G: Q: h! I' X& {cprintf("current text window, without moving the cursor.\r\n"); 2 o7 ]" Q6 T# F
cprintf("Press any key to continue . . ."); # X: G0 [) q, N
gotoxy(14, 4);
; h/ D) ?) h2 g4 c4 lgetch(); 2 j4 @# D) \3 k3 ]
clreol();
, B( H6 y# l: P' N5 Q- Wgetch(); 7 o, Y& _- l6 A$ z$ |: t; x
return 0; D. f- I5 ]0 B* v- \
}
/ J8 ?4 D8 K! r O& s6 {" P( \% w' @- Q" Q
% b9 [" z. ^$ n) u/ {
/ ~3 w" u. @9 G/ ^6 t- _函数名: clrscr
7 m! Q. D4 l0 q0 v功 能: 清除文本模式窗口 , r5 E: n- L- C6 ^' R1 B
用 法: void clrscr(void);
T% Q% Q* h* G程序例: 2 e4 ?6 m7 X2 t: }
#include
, K& { n' Z$ Lint main(void)
3 L8 w$ n* i' {$ r2 I% v" m- b{
( q4 W9 j% @& ^4 J9 ?' f4 T( Iint i; . {5 C$ Q( y! J
clrscr(); 0 G+ z( v! d( \ C# a8 f( N
for (i = 0; i < 20; i++)
5 C" z: n7 W3 Xcprintf("%d\r\n", i); 0 K- k( H" k/ [1 N2 \
cprintf("\r\nPress any key to clear screen"); ( H1 x7 P( G% s g
getch();
: K6 X8 C t& s2 ~( ?$ ~$ ~clrscr();
2 n; i' ]3 _5 T; K4 Ncprintf("The screen has been cleared!");
4 R' g4 r! O. e' H7 v, F- k/ Igetch(); - F: Y; [2 o* n( c n! H, e
return 0;
2 V/ u8 R0 m% y; d# G% S# ?5 n& g6 J} ( x" j O7 E# F* x l, p
$ k0 i7 m4 j+ G. `! p# G! F; K
( @% o9 Q# L+ H" A; R ; y$ w/ |+ J$ I$ T
函数名: coreleft
- v& E' y) `& y+ O4 D1 b" X# ]功 能: 返回未使用内存的大小
( N! |: Y. w0 t8 q用 法: unsigned coreleft(void);
( U! @/ W. ]; z" r程序例:
8 U! `+ Z. E% ?$ z; l/ I#include
! k; O/ w0 l; `" T0 [1 R#include
- A j/ N0 n) D; \5 c( fint main(void) : t' B G: I4 D+ S/ ~3 w9 ?+ R
{ ! e) o0 Z, Y h: y
printf("The difference between the highest allocated block and\n");
8 j" Q' |1 c+ b8 z& t# K. `printf("the top of the heap is: %lu bytes\n", (unsigned long) coreleft());
* r1 l4 \/ t5 y; a* W b- ~: l+ C& Oreturn 0;
3 N: q( \+ _& E- e. B. H) u \2 ~} # Z+ _* z' C8 ~1 L
: r! k2 S: @. f6 E( M+ N
函数名: cos ; Z4 z8 i& |% V: u4 d5 p- H* Z
功 能: 余弦函数
6 ?' n+ G( \5 ?9 B5 q用 法: double cos(double x);
* l( T4 ~4 }0 x7 n程序例:
- f$ u! N9 v% B6 g: U#include
2 S- \/ G8 x4 E: T2 }! n#include
; B' r) y" _4 }4 M4 `/ Zint main(void) 5 \" `" e5 n1 u* T! K& ?7 `
{ ' J# P2 S0 y& p% i
double result; : o; d/ f# v( J% C& P$ N3 Y& P" f
double x = 0.5;
4 f: d" a, Z. iresult = cos(x); 9 A x V! W( J( H$ V
printf("The cosine of %lf is %lf\n", x, result); % V) n: U5 ^6 A
return 0; ; g1 @ d+ p3 o; [' C
} 7 h; P! {9 `( A) j& E7 |3 w
- k' A9 F3 U( m2 {2 y9 Z, B2 Y# l6 O: s2 |# E0 s, q8 W( g$ y
, V9 E. j7 Q) r5 h. D2 {3 Q
函数名: cosh
9 }. k7 V7 ?: a; w功 能: 双曲余弦函数
! s) ~$ o! u8 X2 X) X { [! X* q用 法: dluble cosh(double x);
N/ y6 p* \* ?" v程序例: ; e- ]2 U$ U; B2 q% u- ?
#include & u0 \* |! v8 z' R5 r# o2 N
#include
) n- {. u2 j% X4 D1 g' G! hint main(void)
2 v8 z& [; a) x{
$ x! P2 H' H" j7 c$ ~double result;
4 K R+ r# _& Qdouble x = 0.5; 8 Z3 {+ X$ ?$ d- Q t8 k2 N9 E
result = cosh(x);
7 r2 c% P( p- b& ^: A; c' q# Kprintf("The hyperboic cosine of %lf is %lf\n", x, result);
$ Z8 i: L9 G# f; m, v. hreturn 0; ( |; i4 R( X3 c- O+ z) J9 q! r
} / m7 p5 k9 g) ~, ~$ A9 C6 W
! b+ B5 v1 E" h. U2 O: f+ X
+ U' A, [) L+ O: x( I) R e
. a/ b0 W- ?7 w l3 P
函数名: country ( j2 q c/ y/ J
功 能: 返回与国家有关的信息 c9 s. Y6 ~# M* m6 c
用 法: struct COUNTRY *country(int countrycode, struct country *country); 1 I4 y( Y& y' I0 Q& ?4 k) {
程序例: 1 [6 {3 v9 c! y, t
#include
- \1 k6 X$ `6 n$ K#include
9 F6 d+ Z% @/ ?! H; i- F% B#define USA 0
i: ?& Y y0 I- B* C+ Pint main(void)
( B, ?" T" F% x/ k{ 9 G+ ?7 t- q6 U8 ]0 p
struct COUNTRY country_info;
# a+ Q$ ]5 z6 X4 d% N& hcountry(USA, &country_info);
9 o& @1 T! F$ Vprintf("The currency symbol for the USA is: %s\n",
( r! c2 Y. u F9 ?. y* _" L, |8 y8 dcountry_info.co_curr); 3 x: _% F; ~6 U6 u; A
return 0; # _/ ]; ~. O# I+ U
}
. o5 K o- c+ j
$ T, Y2 g- k8 ~$ \
) V8 h3 g) N0 k9 g% t 4 ]- _. f$ b' y( @4 f! H/ Q
函数名: cprintf
" ^1 j% J" v3 I6 ~功 能: 送格式化输出至屏幕 + {' D3 M) J8 p: b; C3 H" Q5 P
用 法: int cprintf(const char *format[, argument, ...]);
" g- g4 |0 ~. B$ M( P7 |6 T# |程序例: & S# u L! K' w; ^
#include 8 r5 Y% B [/ m! Z* [
int main(void)
5 z2 x0 o, j2 p" l. m7 R1 Y% k{
$ o! ^/ ~* |! _/* clear the screen */
& A. W' K: ?7 k& p1 H2 O) Dclrscr(); , A/ H4 d* F' q; K. X
/* create a text window */ # O8 L# H7 }6 T* u: y( ^* o
window(10, 10, 80, 25); 5 p4 M8 O( H$ Y+ z: ?
/* output some text in the window */
* P$ {1 N7 Q4 K/ _% R7 Tcprintf("Hello world\r\n");
- H" }4 Q: [) k/ l. s/* wait for a key */ . A4 ]4 M7 a& G+ f/ m, ^8 ~
getch();
4 G% M8 j" y0 ureturn 0;
4 r. D; k% T( J8 K% q& F+ S}
+ y. i5 K: C& G; g4 [& F7 G+ \* t) @3 k" c3 V: w. @" m1 o: H; s
; h4 J* g/ s) i # J( { Q8 G! h
函数名: cputs ; [$ l1 h! u6 Y! [
功 能: 写字符到屏幕
8 O( [9 ]9 F7 @; |用 法: void cputs(const char *string); , e6 C- E4 ?/ x1 H! x
程序例: / R. p& h* V% d3 w# T8 w% h2 {7 ^
#include
5 t/ X& s* K2 P a7 x/ g( Bint main(void)
$ |( i, v7 g/ ?/ ~{
0 Z3 \5 K$ i5 ~% s+ n7 _, K/* clear the screen */
5 J* s' T; \/ q: \& A& pclrscr(); , J8 ~3 N% u; e: ?3 E
/* create a text window */
7 w$ \ C) b) P5 ]window(10, 10, 80, 25);
1 ~$ w6 u6 O# t6 g% U, R- L' F* Q/* output some text in the window */ ) [' ~( a4 h( x- P+ A
cputs("This is within the window\r\n"); . u$ o) `/ x7 Z2 G% H. |' p- M
/* wait for a key */
% U1 p: C" [2 \" @getch(); 2 a) e, R( L8 G& e
return 0;
0 C* Q0 c. z4 t" \5 b+ W; w} 2 R' `* _: L% }; X! {+ M2 i% D
1 u% A2 {( [2 l1 ?0 ^' Q* v8 s& }) J
, U, S" G1 M* d1 ]& f3 V8 r5 ^
函数名: _creat creat % W4 g2 k$ Q6 u6 \' g* P& x
功 能: 创建一个新文件或重写一个已存在的文件 3 Y, h% f) z6 K4 l3 G
用 法: int creat (const char *filename, int permiss); 6 @3 [* B. ^" `% s* J0 U
程序例: 8 K1 X, F' \0 s7 Z. [
#include
; V6 y. X6 k8 t' j }#include 1 q; p- Y+ {9 G/ |9 t
#include
2 a! [7 C: b( z4 S, c#include
5 c+ M0 {2 Y7 e- F6 }" V1 `& K6 {int main(void)
- k1 s J. h6 M. g# w' o" g{
5 m; x$ Y! C" X, h7 e1 \6 A+ Cint handle;
8 K6 |3 Y% q9 y7 K/ [: u8 |char buf[11] = "0123456789";
2 o0 _% ^0 h# w' w% e& J: y/* change the default file mode from text to binary */ 2 H7 D# a, Z1 J8 v$ X) U% @
_fmode = O_BINARY;
" C5 s- Z" M/ V/* create a binary file for reading and writing */ . I& U: X7 v. m# L7 j
handle = creat("DUMMY.FIL", S_IREAD | S_IWRITE);
2 s- p& O" [5 ]3 O! s; k9 ~/* write 10 bytes to the file */
3 C- [% {& I# R Vwrite(handle, buf, strlen(buf)); 1 y8 h3 \/ F5 W# z: m
/* close the file */ : }. J# v# V) l _; t# Z; y
close(handle); 3 q4 P2 Z3 H1 n J; x
return 0;
: r- T- U1 A! g. g8 ?! d}
/ d1 e; R7 d, s: O: \. f; \& v 2 w/ M7 S+ Q, Z3 h% q6 h! S9 p V3 P
函数名: creatnew : ?0 {' B5 J5 K4 Z
功 能: 创建一个新文件 ! b7 A8 d8 U+ p4 `3 [# i8 {
用 法: int creatnew(const char *filename, int attrib);
7 h% V' K/ m' n! n+ u* j+ Z% r9 x程序例:
: |' M ?$ ~% P% D$ Z3 M. {#include
- `) \4 k& u1 P0 c2 e#include
) D& r" |0 c, J( Y#include 7 C8 y0 j& z6 n! `8 a1 v' n
#include 1 ~. H, g3 y3 P. _8 }" j. o! q
#include
/ G5 L! {( |+ S* C/ q5 M. i8 eint main(void) ! p. V }0 I. L( j. i I
{
6 N, g* _2 t* O$ ]$ P8 S% eint handle; ( ^0 L: z# O: R/ g+ F
char buf[11] = "0123456789";
; @0 O9 W3 \) _! m/ x1 d/* attempt to create a file that doesn't already exist */ D4 T4 o4 h4 r, u9 X
handle = creatnew("DUMMY.FIL", 0); {# }4 B2 \/ \$ a; ]. U
if (handle == -1) . J8 v+ m$ l& }4 Q; T; P
printf("DUMMY.FIL already exists.\n");
0 \* w4 @% Q: I4 j* r* k" Qelse
. C( b! g. K( ~- I, K; ]{
6 {) W6 a: z% \& o, a7 x5 J2 e1 c! wprintf("DUMMY.FIL successfully created.\n"); Y! a, q/ X. G
write(handle, buf, strlen(buf));
. R9 F6 Z) x+ Pclose(handle); ! q% I$ E1 S) ^) v9 }3 G
} / B; X$ O r4 V
return 0; 2 Z4 Q# |- w9 I7 }
}
* n- U$ k6 Z+ N# K `: A8 n' D
* {1 u6 O1 p- y. X- Q; q; M; j+ n
+ u7 f: O3 B4 X q( {9 J8 C6 l
函数名: creattemp
$ ?+ _" _+ D$ k% H' P功 能: 创建一个新文件或重写一个已存在的文件 " d) D; F2 m$ p. w# ?. w
用 法: int creattemp(const char *filename, int attrib);
- C- Y b) b0 |' h. P; m8 a3 W8 r程序例: 4 D- F% ]2 Z2 Y
#include $ h, H* ~/ k% \
#include
( W& p/ h: j# x; L+ c#include ' F7 R' s( z! g% I/ L. s
int main(void)
: Q" d! x: |! F/ j: G{ - B" d2 n" c7 p7 }1 }
int handle; ' D' n! U5 X% Q1 g+ O
char pathname[128]; - I/ L5 Q8 l$ S5 r& t
strcpy(pathname, "\\"); ! |; {3 m2 n2 u$ }. A0 A& \
/* create a unique file in the root directory */ ; D; A3 F8 R& i) u" z7 F
handle = creattemp(pathname, 0);
3 Z0 j: [. J; v. q* j. Uprintf("%s was the unique file created.\n", pathname);
. |. o3 [% I" [6 R2 uclose(handle); ; `8 S9 Y1 c. V* G2 `6 T
return 0; & e: R+ V. Y) Y4 T5 S% g+ l9 u& m
}
1 e i) ?, i& J3 ?! H$ v2 `. w+ p0 J0 k
' u7 c4 I' W% v9 }& x6 `6 m, ~" ^. J 2 O% T- A, f2 }
函数名: cscanf ! j) u0 B& v) s- ~8 _) Y' C
功 能: 从控制台执行格式化输入 ; |3 L) _- ~/ V: a& c) G. S9 U# @6 X
用 法: int cscanf(char *format[,argument, ...]);
, N) Y0 q" I8 j+ C程序例:
- S) c l' c; O#include " y* ^- d! R- _* \/ G! b8 | v
int main(void) ' _/ S4 ~- J- n
{ ; l* n5 P5 P0 m! U
char string[80]; 0 O. @% p( _1 c' x# ]3 j- g
/* clear the screen */
" d" s% m4 h# Z% b% Yclrscr();
/ }) |9 }/ I8 G n5 P9 e* a/* Prompt the user for input */ & }/ {+ R1 ]6 ^
cprintf("Enter a string with no spaces:"); D0 ?" F0 ~% E9 r# `! \% X
/* read the input */
* z: y, H0 m# o: Z% r1 Kcscanf("%s", string);
+ s9 M+ @5 j4 l/* display what was read */
# _7 Q9 T/ \* C. Scprintf("\r\nThe string entered is: %s", string); ) g: u+ C' V3 Q
return 0; 6 V! R: h6 b: h) M9 Y
}
! P7 n) C9 a+ H |: v2 o
4 I" K9 z! T$ S& G9 C7 ]5 `
* B, e% K( ~) x: } + ]2 ~2 f6 r* b( W$ L$ L" X
函数名: ctime & q0 F6 P% O4 W$ F0 _1 h. g
功 能: 把日期和时间转换为字符串
- y3 I9 k0 j$ h' V& G用 法: char *ctime(const time_t *time); / c* c( M" C% [# m
程序例: W3 x# z$ ]3 y5 f
#include . o! b5 U4 K# R9 B* Q" R9 B/ |
#include , E) X6 z$ T. t5 N) d9 U
int main(void) : ^3 W" f0 Y- I- i
{ - I, D$ u# ~' H3 t
time_t t; + C& D1 z: P2 Z- n U, n
time(&t); ! X+ R! f0 I& B- y( x4 L" }* [$ I! a {
printf("Today's date and time: %s\n", ctime(&t));
7 _6 F% m' y4 o5 @* freturn 0; `: ?2 G) e4 w5 F
}
* ~9 [+ G5 R9 o9 Y) Q6 @) _/ N: U% y; W# U5 J% |; Y
1 r1 `4 c- a; c
\* t- `2 Q3 g2 g0 m函数名: ctrlbrk
: @ f$ @9 N w, H7 n功 能: 设置Ctrl-Break处理程序
% z8 C# K% |1 K. M- ^4 I9 v用 法: void ctrlbrk(*fptr)(void); : H* S5 g# Y! Z
程序例: ]- F) Z: c$ w1 ]0 m
#include 9 Y; c: v% ~& g6 v: ~# q1 p& x L
#include
7 B; i( g9 ~ T3 D3 G9 U' j#define ABORT 0
6 E) H: w* v6 Aint c_break(void)
! ^' O& }6 D" k1 x6 S4 B{ 3 `7 [! h/ K0 f1 R* X! o: t
printf("Control-Break pressed. Program aborting ...\n"); 7 o" n: B3 Z0 V: S7 g" e
return (ABORT);
+ F% X8 E# @" N; D R}
, ^' E4 ^; u. I( T) H" | Zint main(void) ) N1 B. P4 g+ D+ t* |3 y, d/ m
{
, C# L; _5 A, J# R/ L" {. L- pctrlbrk(c_break);
4 A9 Q# Z6 T8 z: ^" ^" I2 Sfor(;;) - K( n- a1 ^! `7 K& {+ V* Y) h1 O
{
/ h- D: F6 |2 U; Lprintf("Looping... Press to quit:\n"); 9 o, I, s% n5 l' j/ P9 Q" I
} # ]8 M7 J- `4 a1 b; w, X
return 0;
/ G+ a! c0 C3 ^( T h) | z} |