|
函数大全(c开头)
$ w: Z9 D( S' Y7 L
" ]1 O; X* b7 j7 T. Z函数名: cabs
- h5 F7 V2 z! t; d% l6 T/ M3 O功 能: 计算复数的绝对值
1 w1 J/ L* i5 n用 法: double cabs(struct complex z); 0 ^& ?( S( N" k+ \
程序例: 3 R. P! c# I6 S4 C) H$ i$ [
#include
! {8 Q8 t# E) V" p2 c9 d1 k2 j9 D5 ~" l#include
3 b9 E4 ^7 G3 l$ fint main(void) " W6 |6 B+ ]4 |
{ & q# }- r; v J/ ?
struct complex z; " ^. X8 X7 T1 A- C( d; Z
double val;
) V y3 @, L* i1 H; J8 Tz.x = 2.0; + C; r R% ]+ @' M+ R. t
z.y = 1.0;
7 j7 f/ k7 y5 qval = cabs(z);
* s- m: J- s4 N4 O0 F U6 xprintf("The absolute value of %.2lfi %.2lfj is %.2lf", z.x, z.y, val);
& L. J2 P& _" @! yreturn 0; - o( v5 y; |7 ^ [/ i
}
2 E p/ c: C T, h
0 l& `* k. T% X* H" Q! R6 c) J
! [8 m8 J; r- [% Y + i) c+ A3 a: o& I8 V# b
函数名: calloc X6 x4 W6 B2 i" O( U
功 能: 分配主存储器 ; q& q8 n" Z- W. `( q U* E
用 法: void *calloc(size_t nelem, size_t elsize);
- C6 C# G/ G4 ~+ L6 A, M程序例: ; M! r. ^' o1 }
#include
4 e/ D2 S% X9 k2 V#include 4 H" O" ]" v/ S% i/ U5 j
int main(void) 7 J) `) C7 y3 m. V9 f
{
% L. o: b# |/ \! s; _7 Achar *str = NULL;
. F! a- [1 g, `9 N# M/* allocate memory for string */
( R4 y& g* H- h! Y1 m% `str = calloc(10, sizeof(char));
- K1 Z" X8 r5 @1 t/ |/* copy "Hello" into string */ 2 f3 t3 e5 f3 l6 z( s7 f
strcpy(str, "Hello"); $ Y% [- k0 ^7 L* }; u) E Z
/* display string */
2 \9 z& \) N3 |& t, Lprintf("String is %s\n", str);
9 V1 |( l' ~* R* U& }3 F( h% k, A/* free memory */
5 i: A6 B: K I6 ~ y. {: J( [3 Vfree(str);
6 } ]# ~2 u& g B% ~! g6 Z! L; `% S, f' ireturn 0; 7 c* g5 ]2 H y K6 V
} ^% D3 d+ I5 }! X4 k* F8 S
4 l% J" P7 p/ A! D
! L7 C2 p8 T: Q% w8 j & [7 O p9 g. y. X/ s( o
函数名: ceil
/ b' C4 z$ s# I- e功 能: 向上舍入
5 }* ^$ M, t& |2 K用 法: double ceil(double x);
& ]) b* @1 K7 ]7 C% A' I' G- d/ f程序例:
4 k0 Y, {' Y" A. {#include $ m$ v( d8 W! l4 f
#include 3 b" w1 a! Z8 J& z
int main(void)
Q* c$ L4 p/ k$ ?9 {& Q{
# x K1 `/ ]) p2 t2 [double number = 123.54;
& ]+ y0 E0 }- _# hdouble down, up; , E9 S# |. T* k+ Q8 i! c2 @
down = floor(number);
) d; b6 j- u( R3 o4 eup = ceil(number); 4 J! |" v0 t! p! o' a D2 a
printf("original number %5.2lf\n", number); ( y! f* x0 F3 Q6 Q
printf("number rounded down %5.2lf\n", down);
" L2 a% L6 A9 z! a* cprintf("number rounded up %5.2lf\n", up);
8 `% o& a4 c) ^return 0;
/ K: O' x1 `' h5 Y+ c: _4 O} & \3 K' a5 V9 y4 F# U& ], |) q
! X& v5 X4 k: V3 e' Q( ?3 |8 \' [5 { ?! Z0 j3 x- D! R, i
# Y, Q' ~: C5 ~函数名: cgets 1 Z4 g' U0 h d6 U
功 能: 从控制台读字符串
: v. X$ p) Z6 s: t$ n9 f0 r: t用 法: char *cgets(char *str);
0 |" Y. J8 t S. w( f: K4 Q程序例: 7 @2 e. _2 J1 f. L
#include
% h) u6 C7 F# I Y#include
* T: o5 T+ p/ R7 t& L. g. g% c9 a" Tint main(void) ' B) I% W0 V" W& X; s9 x/ B5 C. ?3 M
{
* K* [1 u r8 C, L: q4 |% K* A/ Zchar buffer[83]; . l# S4 @5 V% M. ]
char *p; : Z2 f$ X% {" w6 E8 V2 P7 [
/* There's space for 80 characters plus the NULL terminator */
4 E) X( M+ b1 H& h! jbuffer[0] = 81; 9 Y" Z! m8 m" O/ h' e0 }: t" |' c
printf("Input some chars:"); 3 k* ?% ~- i8 J# y' \ v2 S
p = cgets(buffer);
7 U, R: J( f* Nprintf("\ncgets read %d characters: \"%s\"\n", buffer[1], p); # S1 X: l/ I8 U/ q2 ^4 C/ H. U/ `
printf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer);
4 w$ D8 m( P5 k( Q4 F( e+ F1 m4 R) Z/* Leave room for 5 characters plus the NULL terminator */
8 {6 q4 _' x+ p6 F) Nbuffer[0] = 6; 3 ~3 x5 }& r- w0 E3 C! N8 Z b8 M& ` k
printf("Input some chars:");
! J; s# s: _$ f5 qp = cgets(buffer);
0 D; n3 s* i1 e9 Sprintf("\ncgets read %d characters: \"%s\"\n", buffer[1], p); E" a9 I- y- R! @$ r0 T- u/ [
printf("The returned pointer is %p, buffer[0] is at %p\n", p, &buffer); 6 v8 B3 h+ _; {, ~5 ~
return 0; / y* d: c: w1 P# i, c7 C* g
}
8 \2 O/ k2 L" {
* ~6 j9 k0 X: c6 |4 ]9 |- B' F l P, s p
+ U X% i! E( S2 W- |/ d$ U0 G$ o函数名: chdir
: ]0 \( r6 R8 F$ p* ]功 能: 改变工作目录
6 s6 r- h; V" O9 _4 G' u; [0 A用 法: int chdir(const char *path);
+ T0 @* L( ~* D' g程序例: 0 F' ^/ T; q6 H% Q8 a6 f5 l
#include
/ C, y1 I; a6 i+ ^#include 9 a6 r( K8 E E7 i3 A
#include
: [+ j! I1 E; _( W$ d' v; Xchar old_dir[MAXDIR];
4 [1 p0 y- s6 T. S( Ychar new_dir[MAXDIR];
/ E8 x8 q! V6 b% eint main(void) % h$ h$ f+ ]' u; N4 o2 o
{ % J1 \2 j2 h. \7 t* l7 y3 o5 j3 \
if (getcurdir(0, old_dir)) " q ?& ]* J* P8 D; P9 q" U) h4 G& l
{
! T3 M [- Z8 s6 t) k8 U5 Eperror("getcurdir()"); / T3 q# {9 |9 T& X% D/ x6 w' u! _
exit(1);
3 X! u! @6 l2 Z5 g0 ?( ]8 x}
2 j" z' x2 b9 M: A: wprintf("Current directory is: \\%s\n", old_dir); 0 ]% b$ r; ]3 w O0 _, W
if (chdir("\\")) ; H+ D1 d/ ]! I; Y( o6 J
{ / v: S- a* }9 `, ^/ U+ a' m
perror("chdir()");
K4 K: r5 {) w# G% l4 ?" l0 Yexit(1); 4 b" e- \! P4 |. E: ?2 p6 v. G: Z! H
}
2 e% y$ A8 g% a Uif (getcurdir(0, new_dir))
$ T* l. I% T* `/ n% u3 t9 l{ . f8 Q9 }4 g0 T; Z4 Q, n
perror("getcurdir()");
' G- W' I7 |! texit(1);
, @+ |: B# f+ P/ W R. \; P# S} 8 B. y5 m# t# L2 A" s% e
printf("Current directory is now: \\%s\n", new_dir); 6 C& i: S# w: N! B/ r( N+ n1 R
printf("\nChanging back to orignal directory: \\%s\n", old_dir);
) Z* h' v ?8 Oif (chdir(old_dir)) * h; I* f/ f9 L5 a0 J# C+ N+ \% W U
{
7 ?0 x; }2 t. j A g4 L' bperror("chdir()");
$ I( U! c! K a* o( c) A6 }exit(1);
- P5 }" X0 @1 t1 g}
+ i- c$ h2 X# M/ S* S. Y Mreturn 0; , }8 z. T, \; r/ H
}
8 h4 l0 O+ U* W
! l! s$ _0 }' a7 b
1 g: S! d& ]. H, N函数名: _chmod, chmod ! r5 k( @' p9 t, S
功 能: 改变文件的访问方式
d: s1 h1 I5 N% H$ K7 \( C+ R用 法: int chmod(const char *filename, int permiss);
- \$ x7 _9 }4 _ _0 q0 k4 W4 ]9 _程序例:
( d. b- D* S3 Q! q/ q#include
$ n5 X3 X2 M% `+ Z# C#include 5 L }' A' ]! b
#include
; u: y; v4 `* F N, D$ I# U" qvoid make_read_only(char *filename);
: ^: J7 Q( g2 ]% ~& ~7 |: Gint main(void)
+ j8 ^8 J% f* e1 p4 ]{ : ]; G' ?0 q4 p' `, n" i0 `; X
make_read_only("NOTEXIST.FIL"); / L: p: O/ L* ?1 h/ t2 _
make_read_only("MYFILE.FIL"); % n- t5 o9 A' L- h, ?. a- i7 h
return 0; 3 h: I& f' w+ z$ r4 l* i2 N5 m
}
! I- r9 M' U8 t, N4 d; b# Evoid make_read_only(char *filename)
7 O5 S! p# h' D- s% A4 I) ^{
0 s' @/ S& T7 I$ [int stat; / V7 l* \9 r3 ~5 F' N
stat = chmod(filename, S_IREAD);
2 U" ]9 `: w9 O( l" t/ Sif (stat) - v' t! j1 q2 T/ |+ C% z9 t9 D2 F3 g
printf("Couldn't make %s read-only\n", filename);
. G. Y- f& O# x; k3 V# j, @' C) Telse 0 s+ P7 t5 w2 E8 ~2 C5 F: |
printf("Made %s read-only\n", filename);
. c. G5 D' p, [& v}
/ }0 Z2 z: z% k8 q# [# A3 t5 X3 j
% D/ E5 L( I8 ~0 f" X8 a
1 m; Q) h' o2 v* A/ l
函数名: chsize
& d- i1 y1 K0 Z功 能: 改变文件大小
# }3 j& t+ \6 v9 h7 X7 i4 V用 法: int chsize(int handle, long size); / W e2 d4 N' H9 `
程序例: 3 P' E7 r" L# \, N
#include
: I, ?/ R3 H" \1 w* G/ p8 @- w#include
/ q9 A1 a1 V: D#include ; T/ p5 G# r) q [7 C) j
int main(void)
2 H9 o: T) i) L% a{
; B' D% b6 P* Q6 ~: O/ W1 yint handle;
/ a9 S1 A% I* {2 k3 a: l/ W# |* Achar buf[11] = "0123456789";
$ I) b7 v. @* `, I. Q* h/* create text file containing 10 bytes */
& S1 d1 K, _. ]1 U! K5 [handle = open("DUMMY.FIL", O_CREAT); ` i6 w5 Z1 c' ` H+ P* @
write(handle, buf, strlen(buf));
* S3 e/ t+ K% Z8 w) P1 g( ~/* truncate the file to 5 bytes in size */ M& Q8 l; L, V4 u3 m: u9 K
chsize(handle, 5);
/ ]2 l+ v4 e/ i; o; }$ W/* close the file */ 5 H h% |6 O$ N( o; f
close(handle);
3 r$ c7 U% Q6 ]6 Nreturn 0;
5 x* u0 D, e4 [# b) Z} 7 Y% X" x0 I, T
5 F& ?' }" y. }, }' Q J, }/ E
/ r- D5 f! B; r1 F1 K I5 B3 q函数名: circle ! z) e* D. q" ^& }9 _! `+ c4 _
功 能: 在给定半径以(x, y)为圆心画圆
! o0 z$ p) z' S5 q用 法: void far circle(int x, int y, int radius);
! }5 q9 c, V: L* _4 }程序例:
5 s1 V" D8 g- T% Q#include * ] G/ A6 V) g) n' V! W) _
#include
- H. R" y. P2 ?) h#include
2 M: @5 S. Y2 w* j( g#include ; A; E$ v9 r1 h
int main(void)
, M: m" @/ c t0 w% W$ ~( q{ 6 S0 u( Y F: |# f/ |
/* request auto detection */
% }* u f, w; j$ ~( V/ n! Oint gdriver = DETECT, gmode, errorcode; " X+ J- R7 N/ N. h* y
int midx, midy; , u- s: W$ E1 [+ w( H
int radius = 100;
% x0 w$ z. W# g; a8 U3 m+ @5 D0 L/* initialize graphics and local variables */
; A% ^3 Y* t( U) }) ainitgraph(&gdriver, &gmode, "");
% v: L2 g% d, Z/* read result of initialization */ 6 {( d' A6 N+ Y8 E
errorcode = graphresult(); ( U. q! _% Z8 q: m
if (errorcode != grOk) /* an error occurred */
& |; X0 K0 B3 V e* @{
n0 r( ^* U2 Y6 g' p, h0 @" Dprintf("Graphics error: %s\n", grapherrormsg(errorcode));
4 w0 r. v9 y) I) eprintf("Press any key to halt:"); $ Y( z7 o: p3 o8 n
getch();
- J' j% t. {6 ~2 N- `4 I9 _' ?exit(1); /* terminate with an error code */
$ G) i1 {/ n7 M} ! ]$ ^! y3 a; p) G" r9 [( M
midx = getmaxx() / 2;
$ \ b! x/ @. b6 e6 d; |midy = getmaxy() / 2; . d% L- c' k0 Q1 V
setcolor(getmaxcolor());
' V# W. w. j8 k0 ^# A) {8 @# W/* draw the circle */ ) C* ]. h' _9 u! `6 J3 l' n) i
circle(midx, midy, radius); $ N4 D2 s) q3 d1 u- A; O1 ~! O% P$ r
/* clean up */
( V9 a4 F+ w% G9 D, zgetch();
: L0 x0 B7 U5 [+ O. O( uclosegraph();
& h5 p0 Z, k$ Y) v: a. Areturn 0; 2 ^% S8 O) S$ [! y& N
} 6 F9 o! C" a: ?! t4 C5 L9 _" W
2 O8 V/ l+ a" R( O- [+ }* d
) I+ C& \. {$ \& V, y7 K O
; ]/ f1 M+ @, v1 h函数名: cleardevice 7 D# ^2 {, V) x) i
功 能: 清除图形屏幕
5 H, ?9 V4 u6 {$ E, Z8 l用 法: void far cleardevice(void);
" ] T; a3 K+ w; e! A程序例:
. B9 U: ]9 a; r2 j2 h }#include 4 F" q) m1 E2 \4 U5 x8 c# `5 k
#include / f F# q( C& F8 H. d7 D9 }9 L
#include 4 G! w8 @; O9 b1 p0 t$ `
#include ) p" l% B# D$ i9 L
int main(void)
' V# N# B0 B: {: S0 F$ u{
P6 K- A& W1 b% C: E1 h0 g( j; }/* request auto detection */
% M* [0 E2 U1 q7 K% p' \8 m5 [, u2 m8 ~int gdriver = DETECT, gmode, errorcode; * ~4 Q& _% _# e+ k) ~. H
int midx, midy;
/ I6 j% L2 k8 f/* initialize graphics and local variables */ / [! a! B) u8 m) c- E/ Q) X
initgraph(&gdriver, &gmode, ""); + ` ~7 P5 ?7 t( S9 P, z* \0 r- g
/* read result of initialization */
* _7 `: @8 M ]" I6 b. |errorcode = graphresult();
& G: K( B: ]$ r1 O- p7 jif (errorcode != grOk) /* an error occurred */
' L. M- [( O; O* ?{ 0 a% Q( W$ D3 W
printf("Graphics error: %s\n", grapherrormsg(errorcode)); 9 K$ ?) R: |0 J3 A# f! {1 R6 S
printf("Press any key to halt:"); + G' n$ t2 p) w$ F
getch(); 4 E5 R- Z" i( B8 Q
exit(1); /* terminate with an error code */
4 h% k& z8 t O/ k}
( g% B0 A" W/ q% Z6 a5 S# kmidx = getmaxx() / 2;
0 u) y$ i' N. `+ K" V$ Rmidy = getmaxy() / 2; 5 ^" ^5 R& [, a3 V+ W* g/ e: k
setcolor(getmaxcolor());
! F7 L# k: ]* C' a/* for centering screen messages */
& v! g8 I8 z7 ?$ Isettextjustify(CENTER_TEXT, CENTER_TEXT); : U) J& e& @: m' \' M E# d
/* output a message to the screen */
4 r% E! J5 R1 Routtextxy(midx, midy, "press any key to clear the screen:"); 6 J: J7 Y, t( y4 O [+ J
/* wait for a key */ 6 i' ]+ g( K! P
getch(); / I( Y, f+ y# T' Q& g
/* clear the screen */
9 m( t/ K @) G& F, l2 r- gcleardevice();
! G1 r- T. }+ ^8 k8 K/* output another message */ $ Y4 L/ t2 ^2 f& _2 |
outtextxy(midx, midy, "press any key to quit:"); ) x# B2 k2 p+ Q8 L% }
/* clean up */ 1 ]+ r8 |3 |- ?' I! }$ G
getch();
( `, U- p5 B0 u! A5 `closegraph(); u5 A8 q) {/ n8 D% R
return 0;
r& e: X' ~. I, n}
4 I3 t g. f9 J8 z9 ?1 `
/ ]! j! B" e8 w3 Y6 a1 S
% H! Z/ z" i) ` 7 N4 l1 a) W6 |0 Q
函数名: clearerr B. U$ Y# [ t( I/ ~
功 能: 复位错误标志
3 n* F& L( Q' B用 法:void clearerr(FILE *stream); % u0 Z6 |2 Z6 \2 x) \0 C( z
程序例: + r3 E6 @' w, A s
#include 8 `2 ]3 h) [/ b& W6 j o
int main(void) 8 N/ z( X, A a$ f+ ]5 O% l
{ 5 @6 N6 Z9 C) S b( g. T8 p- \* ~! \
FILE *fp;
2 N, T. b: c( P+ g h- M& zchar ch;
8 E8 K+ I0 h* Y1 J# }/* open a file for writing */
) |+ k* X! t. S. w7 |0 kfp = fopen("DUMMY.FIL", "w");
. F0 U/ M4 P% q! @( E/* force an error condition by attempting to read */ - G0 Q6 ?( l3 o) G4 V! ]0 J
ch = fgetc(fp);
; B1 C8 m- h9 K" {6 z! m8 sprintf("%c\n",ch); 2 p2 o4 T$ `6 k- {6 W" l- ~: _
if (ferror(fp)) : N/ N1 e; \- y1 f8 s! T% O, X8 w; x
{ * F4 D( i3 X& K& c! ], T3 ^$ b* t! O4 ^
/* display an error message */
* q1 T( E) a+ [3 P8 @ Jprintf("Error reading from DUMMY.FIL\n");
( ]* g2 M, a2 P( v1 `/* reset the error and EOF indicators */
5 u9 j2 ?- J; k* a4 N$ O3 hclearerr(fp); 2 d: ^* ~! V0 R6 d+ C& m
}
0 ~# t$ c+ Q( v" m& z$ L) N+ Jfclose(fp); # s6 L0 s0 ]3 @
return 0; 9 U! V( r) h2 z. l; T0 R
}
2 H- C( W0 @0 Y7 U/ j/ {% A" @8 h3 m$ z/ V( c
- @; V( Y9 u; {9 u * _$ k8 n3 T4 f' ^- Z0 ~
函数名: clearviewport
, n/ ]4 x- w5 L' }; X- ^) y& \' O. o功 能: 清除图形视区
$ j/ s6 E/ B& t1 I3 I1 A用 法: void far clearviewport(void);
/ B/ K( E" @3 ~: J' H0 K/ j* E程序例: + d2 B% l! U+ z& p% E
#include ; ?9 b$ X5 [" }, a1 }6 f% k
#include 5 k! g% |( x- t8 T% ^. j- `( Y7 D& U
#include
% j: ^0 A: q8 h+ e, p/ _: e# O* o7 \#include
2 t' v* C8 P# y- k# M7 z: o#define CLIP_ON 1 /* activates clipping in viewport */
' x$ s e" P- _, o. @% y7 }3 Mint main(void) 8 Y. j$ H% w5 [( x( g! \- h$ q4 w
{ 0 @! w$ M' b- e( i! b
/* request auto detection */ $ @/ h3 r _+ v8 k2 @
int gdriver = DETECT, gmode, errorcode; / L' \- H) o7 M, n
int ht;
- r) z Y& r; k- v+ [/* initialize graphics and local variables */
4 j D7 Q! Z0 y: T5 n* Finitgraph(&gdriver, &gmode, "");
& u+ J* s3 V- v+ n8 G; f/* read result of initialization */
D, l! b) i- b' f+ U' C! n. Q/ eerrorcode = graphresult(); ; ^; S( V- u: g/ i2 I# u
if (errorcode != grOk) /* an error occurred */ & B- T: N5 P# m3 g9 u; T1 Q, a
{ ( J9 H- Z' a t5 u( y& ~
printf("Graphics error: %s\n", grapherrormsg(errorcode));
6 m5 C3 i) U6 [: ~5 V) Eprintf("Press any key to halt:"); }0 w2 B2 s; H" r3 ~5 h
getch();
' s& h( Y q/ G Sexit(1); /* terminate with an error code */ $ ^" \. r0 J/ B
}
$ Z/ {+ V5 U, H6 ]( u1 ]1 ^1 ^ Osetcolor(getmaxcolor()); 6 s( ~6 Z6 j4 p3 Z2 u5 K* Z4 z$ ^
ht = textheight("W"); ) ]0 z- y( b3 C6 f4 y
/* message in default full-screen viewport */
0 j5 r3 y9 @/ s X+ a! q3 `' J7 o, Fouttextxy(0, 0, "* <-- (0, 0) in default viewport");
5 Y" w# m$ p( K% S( T3 Z" {- ]3 H7 s/* create a smaller viewport */ 7 a% T( K" s# @4 d' N7 g( t- E0 U
setviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON); 7 R3 W" J) F$ c6 L% D0 J& z4 _
/* display some messages */
, f6 `- U& K# q6 X/ Q( `. Wouttextxy(0, 0, "* <-- (0, 0) in smaller viewport");
8 S; D" A+ b; b0 Eouttextxy(0, 2*ht, "Press any key to clear viewport:"); 5 _+ @+ x/ r1 M9 i$ \$ }. d$ z
/* wait for a key */
; z( H: d: A7 _$ ^getch(); 9 h: h2 X% k# u& G, S6 _& F
/* clear the viewport */ / V X- b. b) p) x! z" l ^' G F
clearviewport(); 0 ? ]0 z& g5 ?/ q
/* output another message */
6 P/ ]- m- X+ l* `& G qouttextxy(0, 0, "Press any key to quit:");
5 ?# K+ j" n. X6 o d: i/* clean up */ 2 m \) E# ]# `9 _; o8 g5 M
getch(); 7 ~/ L) o2 n9 n! `
closegraph(); ( P" x4 d9 n9 e- }. z; J+ W
return 0; + o6 u5 B" A8 o7 y$ n# n" L
}
9 ?$ m: v% U( V$ G5 V; M: `- h5 _ Q6 r3 ?, Q2 H1 t$ [, G2 a* z
9 W) _5 S, ~; ]1 b, P1 S- T
8 t4 k* c% h9 t
函数名: _close, close 7 a5 a g N2 u9 x; d% C9 P
功 能: 关闭文件句柄 6 f6 t. s* x6 |1 f9 R3 k
用 法: int close(int handle);
& l1 k' U* \; U/ T程序例:
, h7 I8 d* q) H2 D6 V2 J1 b#include + t6 T/ e" S6 S: r$ @/ o) ]
#include
% p2 N2 @$ ~- D, _. {! q2 W#include
2 W* u2 F0 U9 ~% \; E* C1 C" R#include
( g) [- z+ p! G" Omain() 0 l) i) ^$ h$ ^
{ 8 C- d3 l! y* D0 {4 {
int handle;
& i' H& I- [* m# s. Fchar buf[11] = "0123456789";
7 }0 g' u# [0 V5 G' b L W: p/* create a file containing 10 bytes */ . \+ |0 {# l. G- B }& u& p- [9 x0 _2 `
handle = open("NEW.FIL", O_CREAT);
" i {$ u( Z2 l6 n2 H: ~" Hif (handle > -1) 5 w; U7 k* }. g1 u4 W0 ^
{
0 F% C% {0 S1 A+ }write(handle, buf, strlen(buf)); , _. E( i8 }0 a3 l0 K O$ s
/* close the file */ 7 b+ q7 ^0 ^& l6 O
close(handle);
) G& d; e; h' ]- {} ( X" ~+ j. F2 }/ m3 V M. T" B
else
# C* B. e" H6 a2 c+ b! m% z& q" D{
3 Y" A8 B: Q w: d3 o: Kprintf("Error opening file\n");
& F0 ~1 Q" `* p2 M3 j+ L" p' G+ A}
$ D! X- i8 i; r2 B5 Xreturn 0; 2 x" O% P: w- J, \6 a2 ^* ~0 v
} ' Y$ ` f( w2 F7 }5 @( G
+ |+ d0 J, L/ H6 I1 u! v7 ]% T8 i9 r4 L' [
/ y+ Y2 T; b( {" n函数名: clock / @& P6 Q/ ~! h* F8 T3 I) c, |
功 能: 确定处理器时间 9 _) X5 [7 E: D- l* W
用 法: clock_t clock(void); 5 C4 z' {8 ~' |9 |0 R# Z. I. C
程序例:
8 T* M7 ?" y7 H7 `$ S; ^#include
f [4 V8 e ]) l* `1 Y#include
; c6 E; |1 J, i* c& U0 U#include
; o5 `# x, @" m& Qint main(void) " _4 D$ {3 |9 _$ x
{ * k! k5 P6 N$ X$ D3 s# w* ^
clock_t start, end;
6 b9 Q6 Q5 x9 l% h1 F0 mstart = clock(); ( @; l1 s6 _' F! N
delay(2000); ~) J. e% P2 J
end = clock(); ) c& x$ h3 t: p# x9 j* U- I) \
printf("The time was: %f\n", (end - start) / CLK_TCK);
% \: c: h, X/ i" e; }. \return 0; * X- {6 _9 m$ s: ?+ d1 g8 I
}
; c- u1 w6 b. \! L# b+ }. j
- d; z G( F v3 i# [4 t: x8 f
4 d) ~ z5 }/ ]2 N, Y2 c A ) ^( w E& N" n
函数名: closegraph
+ j% `% b1 _% R功 能: 关闭图形系统 9 y2 b5 m% x7 o( x5 J& O
用 法: void far closegraph(void); 4 P. T' @$ V# @" Z" ~& N
程序例:
: n4 V0 W$ h# w2 ^: Z#include
% F7 M# R- G, I) J( [4 p8 s: W' R#include ; I) P E6 ]2 o( _, d# ?
#include ' ~3 {' B" B# k" J! T
#include
8 R! A/ z% [! J; aint main(void) 4 @7 a& ~0 ~6 M8 q! p" J9 F
{ 6 f4 V/ J" ?6 I# |! q
/* request auto detection */ ' E3 W0 D. p2 m8 f0 {
int gdriver = DETECT, gmode, errorcode; & v" ?5 ^* z. e' E
int x, y; 2 G' B7 C! a; C' p3 l) Q3 l S
/* initialize graphics mode */
( s- m, P) ]# {9 Q& J3 ainitgraph(&gdriver, &gmode, "");
& g9 B+ g% ?" k' N6 x/* read result of initialization */ & f' z/ Z* X. B P4 A+ C2 k
errorcode = graphresult();
7 q& J' O8 b* t, {- Z- x3 h/ Bif (errorcode != grOk) /* an error # \0 T' g- N# u1 a" G0 L1 J
occurred */ ' W: e8 f! o. U5 N0 W
{
4 \) X# i( W8 b- w0 Aprintf("Graphics error: %s\n", grapherrormsg(errorcode)); - D; v- _7 g$ ~; A
printf("Press any key to halt:"); ' x- {6 A. Q0 e1 }: n$ ~; r3 L
getch();
3 \8 \2 u7 ^) z( Eexit(1); /* terminate with an error code */ ! n5 L. s1 {; H9 O( {+ ~# T
}
# M a& o- Q. l5 @x = getmaxx() / 2;
+ w9 K. n) ~' | Hy = getmaxy() / 2;
+ }) y! r$ o8 B' Y+ `" \, d/* output a message */ 8 q6 p1 a8 r4 p: i8 v
settextjustify(CENTER_TEXT, CENTER_TEXT); 8 C2 @: r: Y- O" M d7 w
outtextxy(x, y, "Press a key to close the graphics system:"); 8 Z; F+ M; j: E# Y5 a
/* wait for a key */ " @3 Z; ?& }& e0 F7 U5 @5 o
getch();
4 y! H; E7 l U+ J3 R1 a3 ?/* closes down the graphics system */ , [- K, e {8 w8 U( ~4 {2 v
closegraph();
4 F: w; t7 _7 U3 Q5 Aprintf("We're now back in text mode.\n");
: u; {: C( i% e* zprintf("Press any key to halt:");
$ o! @; i# h' U( s& ^8 ^getch();
2 D* `& x6 t* O2 y- Greturn 0;
0 g! O' {( P/ a} ' @- x4 m# N. x1 ^; t
* G" f+ I9 H* P. ^5 c) J' h$ r. i( ?! ` L; V) x
; i) |. |4 Z$ E4 w; k1 W函数名: clreol % X1 J# g% A% `& I
功 能: 在文本窗口中清除字符到行末 0 b: q$ C# {: P- K- w! s
用 法: void clreol(void); 4 ?* d; m( l, X% j. \1 [
程序例:
1 K' }& j2 _; ]* _: H. ? L#include " |: ]- r4 u6 B x+ ~
int main(void)
9 F8 a7 A; g. k4 b) c. ?{ 7 G% ?9 w7 p& Y& h8 `3 ^
clrscr();
0 a, {7 F: L# @cprintf("The function CLREOL clears all characters from the\r\n");
. y, _' c. @, A# d6 dcprintf("cursor position to the end of the line within the\r\n"); 6 q% w1 {! [1 p
cprintf("current text window, without moving the cursor.\r\n");
+ f& Q4 `6 h/ Z4 p+ @cprintf("Press any key to continue . . ."); , Y7 a: a1 o4 H
gotoxy(14, 4);
# a) g' V, z; ]getch();
4 T% h6 e' D, b( \8 _2 N8 Xclreol(); % N4 B! t: t9 p$ S0 A4 H/ W! Y
getch(); ; }6 i1 Q! h, S2 R- T
return 0;
" O) L. p! |) {/ a5 e) b0 X: V}
1 c& @/ O# c8 m8 ^( @+ [3 N
; f0 ^4 i' |: ^4 G
5 S2 C' C3 S+ ^ `5 Q2 w4 ?3 C8 C. J) {# |4 P# A- x
函数名: clrscr
2 s( T0 Y/ P2 d* G功 能: 清除文本模式窗口 + i" a7 I& z+ \" j
用 法: void clrscr(void);
3 @/ a+ c1 {/ |# A/ W- i' F j程序例: - G: Y3 u9 i f$ S0 x
#include , d! K3 N0 E7 j& T( P
int main(void)
0 d/ \' H) |% T* ?' R( s6 g6 ~{ . j6 {+ n$ C( ?! ~( Z4 |
int i;
7 T: D: r, l; E/ v) G0 \+ u% wclrscr();
; z3 H$ [# f9 f, Kfor (i = 0; i < 20; i++)
v+ m Q5 T" \% @, {& _ b/ gcprintf("%d\r\n", i); $ v/ Z+ X/ Q# H
cprintf("\r\nPress any key to clear screen"); / m9 \7 e4 q2 h! T4 Y# v
getch();
3 [2 m6 X" V; Aclrscr(); ; O0 R- F' b; y |0 ^' J
cprintf("The screen has been cleared!");
$ Y- I& l9 n9 X7 \$ J5 jgetch(); ' |7 r+ l, L+ l: W4 a+ V
return 0; " @* u; n0 X: r' z, p' Z7 p7 t
} ! J8 N* k3 G* j
4 o/ k+ I) y. f" e. m8 m ?
* o5 m3 O2 w/ C
/ f `' ]* {% G. n/ S函数名: coreleft
$ J) H8 J* d' b5 C- ^, n功 能: 返回未使用内存的大小 / I& `! \) K: W8 W8 k/ _
用 法: unsigned coreleft(void); 9 n+ l4 z+ X! v5 j. Z+ K3 Q
程序例: 3 d# g6 @2 N) h9 S1 t5 Y8 z/ C, `4 Z
#include % U4 \- U/ K) k/ H% [+ G
#include
0 ]$ y# |/ O5 a cint main(void)
. V$ j# j% W1 e7 m! y& m{
7 N7 u! P% a3 _printf("The difference between the highest allocated block and\n");
' H0 p1 B4 g+ `printf("the top of the heap is: %lu bytes\n", (unsigned long) coreleft());
# w' s4 l; U* d) l* c+ Greturn 0; " ?; |9 ?! k/ q# g9 ?- @
}
% @* m" O9 a; i3 v- i
5 O2 J5 x! V$ @" A/ b4 R函数名: cos : d K$ a1 H1 ^0 |
功 能: 余弦函数 - X t. H! F4 o
用 法: double cos(double x); 5 h% ]2 K1 R3 ? H$ o5 U
程序例:
/ Q& j/ I) j, p#include
" t( |9 v& Y7 D#include
X U# `8 e& ^" vint main(void) . r+ w& |2 Z$ M$ } W
{ 2 Y9 o4 r( Z* u! r, Y ]6 [. C$ f' Z
double result; 3 c4 w" r+ }* [
double x = 0.5; ! Y1 M& I, V6 G7 j3 k
result = cos(x);
2 x) S! z* a. o) C) k4 k- aprintf("The cosine of %lf is %lf\n", x, result); - Q3 q- g6 \+ A
return 0; . {' p: ?: O N
} 9 _. T3 r4 w; p
: l; D* Y4 x# l/ v0 ] y9 M6 M) w8 L' `" E% i
+ [2 U( G, H, e函数名: cosh
3 u8 h9 w. ^" }7 x. o功 能: 双曲余弦函数
! V% I7 ?3 j+ _, u" V: A用 法: dluble cosh(double x);
4 F/ M# Y3 o" H% q程序例: ; R3 l2 ~7 }( ^2 N- Q1 \) S
#include 5 M6 r' h: d. i5 N
#include ( \% ^2 d. w5 v5 j9 Q' g, d5 {" i6 g
int main(void) ) d' q, ]! {0 U5 N
{
, r6 u( Y/ G2 {0 Z' w) f* Vdouble result;
5 H& L3 x4 k( ~+ Jdouble x = 0.5; / S0 K4 o; n, t; D4 p
result = cosh(x);
+ X0 ?. J6 I0 R# K1 p# c) K9 }printf("The hyperboic cosine of %lf is %lf\n", x, result);
( o5 e4 x |$ x) xreturn 0;
) ~& V) R5 j6 |3 [) k} * A1 `+ Q# W4 `) f7 c+ ^4 \" u: E
( b. N& B# l9 l0 ?. g
" j; |/ I8 C b8 l. \8 s9 e
4 b$ e+ j9 e i8 V+ L' V函数名: country
- b- R+ f) M( l功 能: 返回与国家有关的信息 8 {* v9 G% K) |! P9 c4 q4 s
用 法: struct COUNTRY *country(int countrycode, struct country *country); ( s% I! \ q' f) Y J! v+ Q6 i2 P
程序例: 9 {3 [* A4 d2 |) o' x
#include
9 n6 u3 A* z! `5 B#include
2 T& J* a, o( g N#define USA 0 ; {4 V1 e' }* L$ A- s
int main(void)
# ~* q! u# ^8 ^* J3 V{ 2 ?1 j* f; a/ f, n: _( C W& n
struct COUNTRY country_info; |& x( @ r/ h+ ~
country(USA, &country_info);
* x- R. I4 J' ]: J ?0 Wprintf("The currency symbol for the USA is: %s\n", 9 [7 S# x% i8 f. B) \' F
country_info.co_curr); - `6 F# V2 M8 i
return 0;
9 l {' v6 q5 c/ n s} 9 S( I% Y1 }5 B9 x( V S# s, e# v
2 j2 i, Z! r5 O, p( _
/ n3 X F! y. M8 k: i$ M5 f
5 T' [* {3 L9 I函数名: cprintf 5 I2 n( ]1 b5 a) @/ r0 L9 s/ G. c2 U2 \
功 能: 送格式化输出至屏幕
$ l z4 U' v$ C ~- J用 法: int cprintf(const char *format[, argument, ...]); . \( T2 z2 O* O) M
程序例:
' f8 z3 w, q0 Y4 z% A+ p#include % I* F* r1 ~" E# q$ n* `
int main(void) # e9 e# w( ?' k+ B0 z
{
r2 z% y- M$ g, e" N' U: k/* clear the screen */
. y& R0 Q; w9 P! J' H6 w; Nclrscr(); . _0 V/ r2 ~4 y0 L2 Q2 |0 ?
/* create a text window */ 2 @" W0 u" ` p7 c4 A6 \
window(10, 10, 80, 25); X X0 D6 M- M K$ `- T, W
/* output some text in the window */
+ T9 Y+ j; b, u5 I! r3 Ucprintf("Hello world\r\n");
9 T7 e% s' n7 D0 b) Z/* wait for a key */
; w) A: N) ~: lgetch();
$ I9 Y j/ G% N0 E8 n R0 Yreturn 0; 5 J2 t6 m' i& B
}
! a a) w( Z% T+ n- J$ d! u, U, C: k9 X
& S7 N: H% Y8 l' W5 T" `
- V& w+ A; g& Z( F) v0 X4 Z函数名: cputs 5 V# X$ Q) A5 ^ p0 C
功 能: 写字符到屏幕
1 {* p" r+ U0 p5 m$ g% u D用 法: void cputs(const char *string);
% G6 a9 z6 y/ t1 [程序例: ' }+ I2 G2 S: n% L; j' M2 D
#include 7 v% l2 R: ~) s5 Q8 @" p
int main(void)
* F# }+ [: v. ^$ H& G3 `{ : v) j; S4 e, r, V
/* clear the screen */ # A' H2 u6 S% R K
clrscr();
( H/ Q ?. x6 F$ |; H" Y+ G/* create a text window */ # n8 a9 n8 H: J: J' G
window(10, 10, 80, 25);
; q" ^& p4 t* `' h6 L/* output some text in the window */
! _6 x2 k: n8 ?- o& c* |cputs("This is within the window\r\n"); 3 e l# K! p& B* }
/* wait for a key */ $ e; h: z4 |5 }4 ]( X
getch();
% e- c: Q7 }" s8 D" y+ k lreturn 0;
$ v- S$ f, z. G5 o, Z& Z} 7 [+ ^' \! [" [0 G
9 q* | }+ r0 y8 O0 O8 m
. l: T# i1 u0 o; @9 f; n
3 s1 ^5 K: j; P9 b: G5 Y函数名: _creat creat 2 C1 S2 Z7 {; d6 e: `8 |" c4 J
功 能: 创建一个新文件或重写一个已存在的文件
# w+ z! a; {9 C) n5 A用 法: int creat (const char *filename, int permiss); % @) V% z$ M' M" a1 y2 @% w
程序例: . Z) d* k1 {: U1 D' c
#include
. b( M2 \2 s a* t#include
2 L$ ~* @$ u3 ]) [#include 9 m! S7 {( C0 }+ Q# | s+ i
#include
& u3 _ ^3 V' D/ `int main(void) B) J9 |; i% T9 C. \3 I1 O1 x. X
{
9 ^1 T! D. H. ~: p6 f7 v6 |: P; p9 dint handle; 1 z* G% e, e( {" q6 l
char buf[11] = "0123456789";
( x3 P* }- M& r7 z) [/* change the default file mode from text to binary */ 6 e4 T2 S3 h) j6 W K+ ?
_fmode = O_BINARY; 2 R+ Y: E7 x+ n2 }
/* create a binary file for reading and writing */
" u# t6 K! x$ w: A5 b0 k' U# Vhandle = creat("DUMMY.FIL", S_IREAD | S_IWRITE);
* g' R7 ]% D& ]3 p/ v/* write 10 bytes to the file */ 8 X3 I# w( d$ L
write(handle, buf, strlen(buf));
" U5 o0 h2 g% r/* close the file */ , X3 h$ c; R1 C
close(handle);
- \, k, c7 R5 X! i7 dreturn 0; 5 q3 Z7 @6 Z, ^# H/ Y. x
}
4 e0 ~ L" f; e9 y" `* M1 t* ?7 F' i
6 P# A7 ]( b% P+ M* ^/ ?; S2 U函数名: creatnew
) W6 S! D7 Q$ j4 Z功 能: 创建一个新文件
2 ?/ \! t. b" e" S% F+ j3 g用 法: int creatnew(const char *filename, int attrib); 4 y: I( B8 V7 Q: m, e5 s- N% [
程序例:
4 e3 n" e. T( ]7 W* b% y4 }/ X#include Q% A- \$ H$ v1 z+ [6 T5 D
#include ) f8 \5 u" e5 Q: Q
#include * S: e! ^* w/ k5 c* S
#include
8 N( T. M& F9 L) V1 R7 |& `5 m/ Q! ?#include 5 Y F7 Y$ y( Q3 x, s# r. v. `
int main(void)
* u3 k0 [; @+ Q0 J5 g `# j{
k0 m2 b8 a+ d4 T- E+ rint handle; " q/ x$ e5 b2 P7 e" D1 W" ]6 e* c' _
char buf[11] = "0123456789";
& Y2 v$ h, Q/ i+ j, }% w/* attempt to create a file that doesn't already exist */
5 Q- v# c0 h! Dhandle = creatnew("DUMMY.FIL", 0);
) M0 ], ^+ C! Cif (handle == -1) " X' i: F* f! Y; W# R5 W; Q
printf("DUMMY.FIL already exists.\n"); " E$ w) g6 n4 Z+ H
else
9 P6 D }+ c" J$ e3 N{ / f) R3 N. Z+ g! R& z W' s
printf("DUMMY.FIL successfully created.\n"); % U% Z, c1 q, x$ ~! q
write(handle, buf, strlen(buf)); ' x( E( B! b$ G, ^* Q: T9 A9 a
close(handle); * H, s; j, J9 F& ~
} 6 m# y2 K/ b# H
return 0; 2 K; T( H2 q/ z5 W+ z. i6 _, R
} 4 ~+ h3 _/ {. l o! E7 o! h
I: J) D$ b0 n" P7 b; _ B/ ^- N$ \" V. R$ H
3 Z6 z' T* j+ k% U
函数名: creattemp & q# r: ~' s& c! D6 k" i, `$ ^ F
功 能: 创建一个新文件或重写一个已存在的文件 ! E2 y% g. u; j* ?3 n9 w$ l
用 法: int creattemp(const char *filename, int attrib); 5 J) i4 g$ x1 |! z- V
程序例: , r" c# X: |' Y
#include # U, y( E7 [. {) l& r; B6 U
#include
: U+ E6 k p$ b5 w#include
$ i- s& f' Q) L+ V+ S! iint main(void) 5 N- r( Z/ t3 m
{ ' G/ i) E) b1 z7 e# d4 {( W
int handle;
& ?8 L2 I; T- ~: z) P @& Echar pathname[128];
' R! q$ `+ b0 m; X3 G4 }strcpy(pathname, "\\");
1 q: ^, @1 l* h8 n3 {8 }1 U' i/* create a unique file in the root directory */ ! C4 S7 C7 M* ]2 b
handle = creattemp(pathname, 0); # c2 R( p" s1 a, }, m
printf("%s was the unique file created.\n", pathname); 9 R# |! _5 r; k/ h! s
close(handle); ; }: ^3 O, l D! I0 c6 l4 c! r
return 0; 1 \% ^# v6 P# F) d; l
} % ?6 T# `3 K% e" U: n
4 O! \, S5 a5 W2 d3 Z6 ]1 ~4 v, J% h# I! x& }0 ^- D0 Y9 R) E
4 U( Y7 C% t7 t+ D
函数名: cscanf & \- W: P, l9 ?8 F! ^2 t
功 能: 从控制台执行格式化输入
4 j* ^4 N5 C. ?; B! V" N9 ~用 法: int cscanf(char *format[,argument, ...]); ) Y3 D. N! N1 H+ }' o( Q
程序例: 7 \ i0 _/ K% I( {9 v
#include & L) s& b5 H9 X; }
int main(void) 9 h( A3 C; O o6 I
{ 9 y9 \4 a8 Z: l5 t. Q" P
char string[80];
( n4 i- n N7 O. E: Y3 N/* clear the screen */
! G& G+ d2 [" S0 cclrscr();
/ D5 q. `. G. A. P9 w# {/* Prompt the user for input */
! n; h/ E4 b8 J' U S4 u8 H. q* Ncprintf("Enter a string with no spaces:"); ( p& Q* w! ^8 R" u% i1 l
/* read the input */ 5 N4 s& f* A# Z7 v; w
cscanf("%s", string); % ?7 q: a0 {& U" I( c: w1 s; M
/* display what was read */
+ T" Z3 G9 Y% x8 o- ]cprintf("\r\nThe string entered is: %s", string);
y- F8 n" o2 |8 t$ nreturn 0;
$ T( V' y8 [6 G+ t& y} / p7 x& U' s8 m v! G1 ^* ?
6 V, C }) u* w$ W2 [
; ]! w% U8 J ^
, V: M) M4 }0 @3 a
函数名: ctime ( n5 w- A- z% n8 N9 f
功 能: 把日期和时间转换为字符串 2 b) p0 @6 U4 [ E
用 法: char *ctime(const time_t *time); ! l- f1 R }- y9 r
程序例:
" g; ?4 g! }1 j* [; i#include
; p" @2 @' Z5 z+ U, A. p# Z8 N#include
$ J9 g; b( ^4 Wint main(void)
: F' ] p1 S3 L) n, e& |/ U{
2 V5 E, s1 G$ T! Z" H, xtime_t t;
1 H4 T. z+ i# i* H" m& mtime(&t);
% M0 s3 X! ?5 Z3 q5 dprintf("Today's date and time: %s\n", ctime(&t));
* R {' Y+ @3 l" ^( P) c- v2 G# Mreturn 0; & |( i) j2 C) Y! ~8 o. J
}
z( e7 w! V* d. ]) ^4 k4 v1 [1 l: E/ i7 F+ k' I/ d- h
L3 m# m+ y6 Q8 Y% F0 M l
4 {, D. S; e# @4 ^7 B S函数名: ctrlbrk
. O. o8 F& v; O功 能: 设置Ctrl-Break处理程序
2 L. Y! W4 V' V9 P% M# R- {用 法: void ctrlbrk(*fptr)(void); 5 x- E% R$ S. |4 w3 r. Q7 I
程序例:
9 ?: m+ l) ^: `#include
- h5 Q$ ?: W" e* z$ e: y#include
8 @& H( A( T: U" s6 x- ^8 Y5 E#define ABORT 0 : D# P4 G9 F! L4 b
int c_break(void) ( G; k0 |2 }8 E- g8 ~) i5 d
{ 9 I! S( t$ }$ T. {. @( D
printf("Control-Break pressed. Program aborting ...\n"); 5 b E5 L; y$ W, w8 N/ A
return (ABORT);
0 N7 {( s, b4 y N, v. a} : k, l& s% {/ U4 U/ d
int main(void) $ Y% @# L; i) m. c. y- Z. u
{ ) Y+ Y8 e) M& J
ctrlbrk(c_break);
) ~! m0 ]: D3 C3 W# N% b2 L# Ufor(;;) : R8 S. j! |$ \$ f- x
{ 0 @! h* R9 j6 g* v$ W
printf("Looping... Press to quit:\n");
7 s/ U5 x2 H; Q0 V! U, v}
I, [8 G* s2 E$ Areturn 0; 0 G8 A5 ?4 y3 }8 a0 m# {5 v
} |