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