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