天天看點

C語言庫函數大全及應用執行個體一

函數名: abort

功 能: 異常終止一個程序

用 法: void abort(void);

程式例:

#i nclude

#i nclude <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

int main(void)

{

printf("Calling abort()\n");

abort();

return 0; /* This is never reached */

}

函數名: abs

功 能: 求整數的絕對值

用 法: int abs(int i);

int number = -1234;

printf("number: %d absolute value: %d\n", 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 */

int i, strt, ch_out, sector;

char buf[512];

printf("Insert a diskette into drive A and press any key\n");

getch();

sector = 0;

if (absread(0, 1, sector, &buf) != 0)

perror("Disk problem");

exit(1);

printf("Read OK\n");

strt = 3;

for (i=0; i<80; i++)

ch_out = buf[strt+i];

putchar(ch_out);

printf("\n");

return(0);

函數名: access

功 能: 确定檔案的通路權限

用 法: int access(const char *filename, int amode);

int file_exists(char *filename);

printf("Does NOTEXIST.FIL exist: %s\n",

file_exists("NOTEXISTS.FIL") ? "YES" : "NO");

int file_exists(char *filename)

return (access(filename, 0) == 0);

函數名: acos

功 能: 反餘弦函數

用 法: double acos(double x);

double result;

double x = 0.5;

result = acos(x);

printf("The arc cosine of %lf is %lf\n", x, result);

函數名: allocmem

功 能: 配置設定DOS存儲段

用 法: int allocmem(unsigned size, unsigned *seg);

unsigned int size, segp;

int stat;

size = 64; /* (64 x 16) = 1024 bytes */

stat = allocmem(size, &segp);

if (stat == -1)

printf("Allocated memory at segment: %x\n", segp);

else

printf("Failed: maximum number of paragraphs available is %u\n",

stat);

函數名: arc

功 能: 畫一弧線

用 法: void far arc(int x, int y, int stangle, int endangle, int radius);

/* request auto detection */

int gdriver = DETECT, gmode, errorcode;

int midx, midy;

int stangle = 45, endangle = 135;

int radius = 100;

/* initialize graphics and local variables */

initgraph(&gdriver, &gmode, "");

/* read result of initialization */

errorcode = graphresult(); /* an error occurred */

if (errorcode != grOk)

printf("Graphics error: %s\n", grapherrormsg(errorcode));

printf("Press any key to halt:");

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 */

closegraph();

函數名: asctime

功 能: 轉換日期和時間為ASCII碼

用 法: char *asctime(const struct tm *tblock);

struct tm t;

char str[80];

/* sample loading of tm structure */

t.tm_sec = 1; /* Seconds */

t.tm_min = 30; /* Minutes */

t.tm_hour = 9; /* Hour */

t.tm_mday = 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 terminated

string */

strcpy(str, asctime(&t));

printf("%s\n", str);

函數名: asin

功 能: 反正弦函數

用 法: double asin(double x);

result = asin(x);

printf("The arc sin of %lf is %lf\n", x, result);

函數名: assert

功 能: 測試一個條件并可能使程式終止

用 法: void assert(int test);

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 */

additem(NULL);

函數名: atan

功 能: 反正切函數

用 法: double atan(double x);

result = atan(x);

printf("The arc tangent of %lf is %lf\n", x, result);

函數名: atan2

功 能: 計算Y/X的反正切值

用 法: double atan2(double y, double x);

double x = 90.0, y = 45.0;

result = atan2(y, x);

printf("The arc tangent ratio of %lf is %lf\n", (y / x), result);

函數名: atexit

功 能: 注冊終止函數

用 法: int atexit(atexit_t func);

void exit_fn1(void)

printf("Exit function #1 called\n");

void exit_fn2(void)

printf("Exit function #2 called\n");

/* post exit function #1 */

atexit(exit_fn1);

/* post exit function #2 */

atexit(exit_fn2);

函數名: atof

功 能: 把字元串轉換成浮點數

用 法: double atof(const char *nptr);

float f;

char *str = "12345.67";

f = atof(str);

printf("string = %s float = %f\n", str, f);

函數名: atoi

功 能: 把字元串轉換成長整型數

用 法: int atoi(const char *nptr);

int n;

n = atoi(str);

printf("string = %s integer = %d\n", str, n);

函數名: atol

用 法: long atol(const char *nptr);

long l;

char *str = "98765432";

l = atol(lstr);

printf("string = %s integer = %ld\n", str, l);

函數名: bar

功 能: 畫一個二維條形圖

用 法: void far bar(int left, int top, int right, int bottom);

int midx, midy, i;

errorcode = graphresult();

if (errorcode != grOk) /* an error occurred */

/* loop through the fill patterns */

for (i=SOLID_FILL; i

/* set the fill style */

setfillstyle(i, getmaxcolor());

/* draw the bar */

bar(midx-50, midy-50, midx+50,

midy+50);

函數名: bar3d

功 能: 畫一個三維條形圖

用 法: void far bar3d(int left, int top, int right, int bottom,

int depth, int topflag);

/* initialize graphics, local variables */

exit(1); /* terminate with error code */

for (i=EMPTY_FILL; i

/* draw the 3-d bar */

bar3d(midx-50, midy-50, midx+50, midy+50, 10, 1);

函數名: bdos

功 能: DOS系統調用

用 法: int bdos(int dosfun, unsigned dosdx, unsigned dosal);

/* Get current drive as 'A', 'B', ... */

char current_drive(void)

char curdrive;

/* Get current disk as 0, 1, ... */

curdrive = bdos(0x19, 0, 0);

return('A' + curdrive);

printf("The current drive is %c:\n", current_drive());

函數名: bdosptr

用 法: int bdosptr(int dosfun, void *argument, unsigned dosal);

#define BUFLEN 80

char buffer[BUFLEN];

int test;

printf("Enter full pathname of a directory\n");

gets(buffer);

test = bdosptr(0x3B,buffer,0);

if(test)

printf("DOS error message: %d\n", errno);

/* See errno.h for error listings */

exit (1);

getcwd(buffer, BUFLEN);

printf("The current directory is: %s\n", buffer);

函數名: bioscom

功 能: 串行I/O通信

用 法: int bioscom(int cmd, char abyte, int port);

#define COM1 0

#define DATA_READY 0x100

#define TRUE 1

#define FALSE 0

#define SETTINGS ( 0x80 | 0x02 | 0x00 | 0x00)

int in, out, status, DONE = FALSE;

bioscom(0, SETTINGS, COM1);

cprintf("... BIOSCOM [ESC] to exit ...\n");

while (!DONE)

status = bioscom(3, 0, COM1);

if (status & DATA_READY)

if ((out = bioscom(2, 0, COM1) & 0x7F) != 0)

putch(out);

if (kbhit())

if ((in = getch()) == '\x1B')

DONE = TRUE;

bioscom(1, in, COM1);

函數名: biosdisk

功 能: 軟硬碟I/O

用 法: int biosdisk(int cmd, int drive, int head, int track, int sector

int nsects, void *buffer);

int result;

char buffer[512];

printf("Testing to see if drive a: is ready\n");

result = biosdisk(4,0,0,0,0,1,buffer);

result &= 0x02;

(result) ? (printf("Drive A: Ready\n")) :

(printf("Drive A: Not Ready\n"));

函數名: biosequip

功 能: 檢查裝置

用 法: int biosequip(void);

繼續閱讀