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