1、C 语言常用函数 第 1 页 共 250 页函数名: abort 功 能: 异常终止一个进程用 法: void abort(void); 程序例: #include #include int main(void) printf(“Calling abort()n“); abort(); return 0; /* This is never reached */ 函数名: abs 功 能: 求整数的绝对值用 法: int abs(int i); 程序例: #include #include int main(void) int number = -1234; printf(“number: %d
2、 absolute value: %dn“, number, abs(number); return 0; 函数名: absread, abswirte 功 能: 绝对磁盘扇区读、写数据用 法: int absread(int drive, int nsects, int sectno, void *buffer); int abswrite(int drive, int nsects, in tsectno, void *buffer); 程序例: /* absread example */ #include #include #include #include int main(void)
3、 int i, strt, ch_out, sector; char buf512; printf(“Insert a diskette into drive A and press any keyn“); getch(); sector = 0; if (absread(0, 1, sector, exit(1); C 语言常用函数 第 2 页 共 250 页 printf(“Read OKn“); strt = 3; for (i=0; i #include int file_exists(char *filename); int main(void) printf(“Does NOTEX
4、IST.FIL exist: %sn“, file_exists(“NOTEXISTS.FIL“) “YES“ : “NO“); return 0; int file_exists(char *filename) return (access(filename, 0) = 0); 函数名: acos 功 能: 反余弦函数用 法: double acos(double x); 程序例: #include #include int main(void) double result; double x = 0.5; result = acos(x); printf(“The arc cosine o
5、f %lf is %lfn“, x, result); return 0; C 语言常用函数 第 3 页 共 250 页函数名: allocmem 功 能: 分配 DOS 存储段用 法: int allocmem(unsigned size, unsigned *seg); 程序例: #include #include #include int main(void) unsigned int size, segp; int stat; size = 64; /* (64 x 16) = 1024 bytes */ stat = allocmem(size, if (stat = -1) pri
6、ntf(“Allocated memory at segment: %xn“, segp); else printf(“Failed: maximum number of paragraphs available is %un“, stat); return 0; 函数名: arc 功 能: 画一弧线用 法: void far arc(int x, int y, int stangle, int endangle, int radius); 程序例: #include #include #include #include int main(void) /* request auto detec
7、tion */ int gdriver = DETECT, gmode, errorcode; int midx, midy; int stangle = 45, endangle = 135; int radius = 100; /* initialize graphics and local variables */ initgraph( /* read result of initialization */ errorcode = graphresult(); /* an error occurred */ if (errorcode != grOk) printf(“Graphics
8、error: %sn“, grapherrormsg(errorcode); printf(“Press any key to halt:“); getch(); C 语言常用函数 第 4 页 共 250 页exit(1); /* terminate with an error code */ midx = getmaxx() / 2; midy = getmaxy() / 2; setcolor(getmaxcolor(); /* draw arc */ arc(midx, midy, stangle, endangle, radius); /* clean up */ getch(); c
9、losegraph(); return 0; 函数名: asctime 功 能: 转换日期和时间为 ASCII 码用 法: char *asctime(const struct tm *tblock); 程序例: #include #include #include int main(void) struct tm t; char str80; /* sample loading of tm structure */ t.tm_sec = 1; /* Seconds */ t.tm_min = 30; /* Minutes */ t.tm_hour = 9; /* Hour */ t.tm_m
10、day = 22; /* Day of the Month */ t.tm_mon = 11; /* Month */ t.tm_year = 56; /* Year - does not include century */ t.tm_wday = 4; /* Day of the week */ t.tm_yday = 0; /* Does not show in asctime */ t.tm_isdst = 0; /* Is Daylight SavTime; does not show in asctime */ /* converts structure to null termi
11、nated string */ strcpy(str, asctime( printf(“%sn“, str); return 0; 函数名: asin 功 能: 反正弦函数用 法: double asin(double x); 程序例: #include #include C 语言常用函数 第 5 页 共 250 页int main(void) double result; double x = 0.5; result = asin(x); printf(“The arc sin of %lf is %lfn“, x, result); return(0); 函数名: assert 功 能:
12、 测试一个条件并可能使程序终止用 法: void assert(int test); 程序例: #include #include #include struct ITEM int key; int value; ; /* add item to list, make sure list is not null */ void additem(struct ITEM *itemptr) assert(itemptr != NULL); /* add item to list */ int main(void) additem(NULL); return 0; 函数名: atan 功 能: 反正
13、切函数用 法: double atan(double x); 程序例: #include #include int main(void) double result; double x = 0.5; result = atan(x); printf(“The arc tangent of %lf is %lfn“, x, result); return(0); C 语言常用函数 第 6 页 共 250 页函数名: atan2 功 能: 计算 Y/X 的反正切值用 法: double atan2(double y, double x); 程序例: #include #include int ma
14、in(void) double result; double x = 90.0, y = 45.0; result = atan2(y, x); printf(“The arc tangent ratio of %lf is %lfn“, (y / x), result); return 0; 函数名: atexit 功 能: 注册终止函数用 法: int atexit(atexit_t func); 程序例: #include #include void exit_fn1(void) printf(“Exit function #1 calledn“); void exit_fn2(void
15、) printf(“Exit function #2 calledn“); int main(void) /* post exit function #1 */ atexit(exit_fn1); /* post exit function #2 */ atexit(exit_fn2); return 0; 函数名: atof 功 能: 把字符串转换成浮点数用 法: double atof(const char *nptr); 程序例: #include #include int main(void) float f; C 语言常用函数 第 7 页 共 250 页char *str = “12
16、345.67“; f = atof(str); printf(“string = %s float = %fn“, str, f); return 0; 函数名: atoi 功 能: 把字符串转换成长整型数用 法: int atoi(const char *nptr); 程序例: #include #include int main(void) int n; char *str = “12345.67“; n = atoi(str); printf(“string = %s integer = %dn“, str, n); return 0; 函数名: atol 功 能: 把字符串转换成长整型
17、数用 法: long atol(const char *nptr); 程序例: #include #include int main(void) long l; char *str = “98765432“; l = atol(lstr); printf(“string = %s integer = %ldn“, str, l); return(0); 函数名: bar 功 能: 画一个二维条形图用 法: void far bar(int left, int top, int right, int bottom); 程序例: #include #include #include #includ
18、e int main(void) /* request auto detection */ C 语言常用函数 第 8 页 共 250 页int gdriver = DETECT, gmode, errorcode; int midx, midy, i; /* initialize graphics and local variables */ initgraph( /* read result of initialization */ errorcode = graphresult(); if (errorcode != grOk) /* an error occurred */ printf
19、(“Graphics error: %sn“, grapherrormsg(errorcode); printf(“Press any key to halt:“); getch(); exit(1); /* terminate with an error code */ midx = getmaxx() / 2; midy = getmaxy() / 2; /* loop through the fill patterns */ for (i=SOLID_FILL; i #include #include #include int main(void) C 语言常用函数 第 9 页 共 25
20、0 页 /* request auto detection */ int gdriver = DETECT, gmode, errorcode; int midx, midy, i; /* initialize graphics, local variables */ initgraph( /* read result of initialization */ errorcode = graphresult(); if (errorcode != grOk) /* an error occurred */ printf(“Graphics error: %sn“, grapherrormsg(
21、errorcode); printf(“Press any key to halt:“); getch(); exit(1); /* terminate with error code */ midx = getmaxx() / 2; midy = getmaxy() / 2; /* loop through the fill patterns */ for (i=EMPTY_FILL; i #include /* Get current drive as A, B, . */ char current_drive(void) char curdrive; /* Get current dis
22、k as 0, 1, . */ curdrive = bdos(0x19, 0, 0); return(A + curdrive); int main(void) C 语言常用函数 第 10 页 共 250 页 printf(“The current drive is %c:n“, current_drive(); return 0; 函数名: bdosptr 功 能: DOS 系统调用用 法: int bdosptr(int dosfun, void *argument, unsigned dosal); 程序例: #include #include #include #include #i
23、nclude #include #define BUFLEN 80 int main(void) char bufferBUFLEN; int test; printf(“Enter full pathname of a directoryn“); gets(buffer); test = bdosptr(0x3B,buffer,0); if(test) printf(“DOS error message: %dn“, errno); /* See errno.h for error listings */ exit (1); getcwd(buffer, BUFLEN); printf(“The current directory is: %sn“, buffer); return 0; 函数名: bioscom 功 能: 串行 I/O 通信用 法: int bioscom(int cmd, char abyte, int port); 程序例: #include #include #define COM1 0 #define DATA_READY 0x100 #define TRUE 1 #define FALSE 0 #define SETTINGS ( 0x80 | 0x02 | 0x00 | 0x00) int main(void)