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