C语言库函数大全及应用实例一

简介: [编程资料]C语言库函数大全及应用实例一 函数名: abort 功 能: 异常终止一个进程 用 法: void abort(void); 程序例: #i nclude #i nclude int main(void...
                                1.gif[编程资料]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);
程序例:
#i nclude
#i nclude

int main(void)
{
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 */

#i nclude
#i nclude
#i nclude
#i nclude

int main(void)
{
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);
程序例:
#i nclude
#i nclude

int file_exists(char *filename);

int main(void)
{
printf("Does NOTEXIST.FIL exist: %s\n",
file_exists("NOTEXISTS.FIL") ? "YES" : "NO");
return 0;
}

int file_exists(char *filename)
{
return (access(filename, 0) == 0);
}

函数名: acos
功 能: 反余弦函数
用 法: double acos(double x);
程序例:
#i nclude
#i nclude

int main(void)
{
double result;
double x = 0.5;

result = acos(x);
printf("The arc cosine of %lf is %lf\n", x, result);
return 0;
}

函数名: allocmem
功 能: 分配DOS存储段
用 法: int allocmem(unsigned size, unsigned *seg);
程序例:
#i nclude
#i nclude
#i nclude

int main(void)
{
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);

return 0;
}

函数名: arc
功 能: 画一弧线
用 法: void far arc(int x, int y, int stangle, int endangle, int radius);
程序例:
#i nclude
#i nclude
#i nclude
#i nclude

int main(void)
{
/* 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:");
getch();

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();
closegraph();
return 0;
}

函数名: asctime
功 能: 转换日期和时间为ASCII码
用 法: char *asctime(const struct tm *tblock);
程序例:
#i nclude
#i nclude
#i nclude

int main(void)
{
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);

return 0;
}

函数名: asin
功 能: 反正弦函数
用 法: double asin(double x);
程序例:
#i nclude
#i nclude

int main(void)
{
double result;
double x = 0.5;

result = asin(x);
printf("The arc sin of %lf is %lf\n", x, result);
return(0);
}

函数名: assert
功 能: 测试一个条件并可能使程序终止
用 法: void assert(int test);
程序例:
#i nclude
#i nclude
#i nclude

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
功 能: 反正切函数
用 法: double atan(double x);
程序例:
#i nclude
#i nclude

int main(void)
{
double result;
double x = 0.5;

result = atan(x);
printf("The arc tangent of %lf is %lf\n", x, result);
return(0);
}

函数名: atan2
功 能: 计算Y/X的反正切值
用 法: double atan2(double y, double x);
程序例:
#i nclude
#i nclude

int main(void)
{
double result;
double x = 90.0, y = 45.0;

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

函数名: atexit
功 能: 注册终止函数
用 法: int atexit(atexit_t func);
程序例:
#i nclude
#i nclude

void exit_fn1(void)
{
printf("Exit function #1 called\n");
}

void exit_fn2(void)
{
printf("Exit function #2 called\n");
}

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);
程序例:
#i nclude
#i nclude

int main(void)
{
float f;
char *str = "12345.67";

f = atof(str);
printf("string = %s float = %f\n", str, f);
return 0;
}

函数名: atoi
功 能: 把字符串转换成长整型数
用 法: int atoi(const char *nptr);
程序例:
#i nclude
#i nclude

int main(void)
{
int n;
char *str = "12345.67";

n = atoi(str);
printf("string = %s integer = %d\n", str, n);
return 0;
}

函数名: atol
功 能: 把字符串转换成长整型数
用 法: long atol(const char *nptr);
程序例:

#i nclude
#i nclude

int main(void)
{
long l;
char *str = "98765432";

l = atol(lstr);
printf("string = %s integer = %ld\n", str, l);
return(0);
}

函数名: bar
功 能: 画一个二维条形图
用 法: void far bar(int left, int top, int right, int bottom);
程序例:

#i nclude
#i nclude
#i nclude
#i nclude

int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy, i;

/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");

/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", 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
{
/* set the fill style */
setfillstyle(i, getmaxcolor());

/* draw the bar */
bar(midx-50, midy-50, midx+50,
midy+50);

getch();
}

/* clean up */
closegraph();
return 0;
}


函数名: bar3d
功 能: 画一个三维条形图
用 法: void far bar3d(int left, int top, int right, int bottom,
int depth, int topflag);
程序例:

#i nclude
#i nclude
#i nclude
#i nclude

int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy, i;

/* initialize graphics, local variables */
initgraph(&gdriver, &gmode, "");

/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(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
{
/* set the fill style */
setfillstyle(i, getmaxcolor());

/* draw the 3-d bar */
bar3d(midx-50, midy-50, midx+50, midy+50, 10, 1);

getch();
}

/* clean up */
closegraph();
return 0;
}

函数名: bdos
功 能: DOS系统调用
用 法: int bdos(int dosfun, unsigned dosdx, unsigned dosal);
程序例:

#i nclude
#i nclude

/* 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);
}

int main(void)
{
printf("The current drive is %c:\n", current_drive());
return 0;
}

函数名: bdosptr
功 能: DOS系统调用
用 法: int bdosptr(int dosfun, void *argument, unsigned dosal);
程序例:

#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude

#define BUFLEN 80

int main(void)
{
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);

return 0;
}

函数名: bioscom
功 能: 串行I/O通信
用 法: int bioscom(int cmd, char abyte, int port);
程序例:

#i nclude
#i nclude

#define COM1 0
#define DATA_READY 0x100
#define TRUE 1
#define FALSE 0

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

int main(void)
{
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);
}
}
return 0;
}


函数名: biosdisk
功 能: 软硬盘I/O
用 法: int biosdisk(int cmd, int drive, int head, int track, int sector
int nsects, void *buffer);
程序例:

#i nclude
#i nclude

int main(void)
{
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"));

return 0;
}

函数名: biosequip
功 能: 检查设备
用 法: int biosequip(void);
程序例:

#i nclude
#i nclude

int main(void)
{
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"));

return 0;
}

目录
相关文章
|
7天前
|
存储 Serverless C语言
【C语言基础考研向】11 gets函数与puts函数及str系列字符串操作函数
本文介绍了C语言中的`gets`和`puts`函数,`gets`用于从标准输入读取字符串直至换行符,并自动添加字符串结束标志`\0`。`puts`则用于向标准输出打印字符串并自动换行。此外,文章还详细讲解了`str`系列字符串操作函数,包括统计字符串长度的`strlen`、复制字符串的`strcpy`、比较字符串的`strcmp`以及拼接字符串的`strcat`。通过示例代码展示了这些函数的具体应用及注意事项。
|
10天前
|
存储 C语言
C语言程序设计核心详解 第十章:位运算和c语言文件操作详解_文件操作函数
本文详细介绍了C语言中的位运算和文件操作。位运算包括按位与、或、异或、取反、左移和右移等六种运算符及其复合赋值运算符,每种运算符的功能和应用场景都有具体说明。文件操作部分则涵盖了文件的概念、分类、文件类型指针、文件的打开与关闭、读写操作及当前读写位置的调整等内容,提供了丰富的示例帮助理解。通过对本文的学习,读者可以全面掌握C语言中的位运算和文件处理技术。
|
10天前
|
存储 C语言
C语言程序设计核心详解 第七章 函数和预编译命令
本章介绍C语言中的函数定义与使用,以及预编译命令。主要内容包括函数的定义格式、调用方式和示例分析。C程序结构分为`main()`单框架或多子函数框架。函数不能嵌套定义但可互相调用。变量具有类型、作用范围和存储类别三种属性,其中作用范围分为局部和全局。预编译命令包括文件包含和宏定义,宏定义分为无参和带参两种形式。此外,还介绍了变量的存储类别及其特点。通过实例详细解析了函数调用过程及宏定义的应用。
|
10天前
|
存储 C语言
数据结构基础详解(C语言): 树与二叉树的应用_哈夫曼树与哈夫曼曼编码_并查集_二叉排序树_平衡二叉树
本文详细介绍了树与二叉树的应用,涵盖哈夫曼树与哈夫曼编码、并查集以及二叉排序树等内容。首先讲解了哈夫曼树的构造方法及其在数据压缩中的应用;接着介绍了并查集的基本概念、存储结构及优化方法;随后探讨了二叉排序树的定义、查找、插入和删除操作;最后阐述了平衡二叉树的概念及其在保证树平衡状态下的插入和删除操作。通过本文,读者可以全面了解树与二叉树在实际问题中的应用技巧和优化策略。
|
15天前
|
Linux C语言
C语言 多进程编程(三)信号处理方式和自定义处理函数
本文详细介绍了Linux系统中进程间通信的关键机制——信号。首先解释了信号作为一种异步通知机制的特点及其主要来源,接着列举了常见的信号类型及其定义。文章进一步探讨了信号的处理流程和Linux中处理信号的方式,包括忽略信号、捕捉信号以及执行默认操作。此外,通过具体示例演示了如何创建子进程并通过信号进行控制。最后,讲解了如何通过`signal`函数自定义信号处理函数,并提供了完整的示例代码,展示了父子进程之间通过信号进行通信的过程。
|
15天前
|
C语言
C语言 字符串操作函数
本文档详细介绍了多个常用的字符串操作函数,包括 `strlen`、`strcpy`、`strncpy`、`strcat`、`strncat`、`strcmp`、`strncpy`、`sprintf`、`itoa`、`strchr`、`strspn`、`strcspn`、`strstr` 和 `strtok`。每个函数均提供了语法说明、参数解释、返回值描述及示例代码。此外,还给出了部分函数的自实现版本,帮助读者深入理解其工作原理。通过这些函数,可以轻松地进行字符串长度计算、复制、连接、比较等操作。
|
15天前
|
存储 安全 C语言
C语言 二级指针应用场景
本文介绍了二级指针在 C 语言中的应用,
|
16天前
|
SQL 关系型数据库 C语言
PostgreSQL SQL扩展 ---- C语言函数(三)
可以用C(或者与C兼容,比如C++)语言编写用户自定义函数(User-defined functions)。这些函数被编译到动态可加载目标文件(也称为共享库)中并被守护进程加载到服务中。“C语言函数”与“内部函数”的区别就在于动态加载这个特性,二者的实际编码约定本质上是相同的(因此,标准的内部函数库为用户自定义C语言函数提供了丰富的示例代码)
|
27天前
|
程序员 C语言
位操作在C语言中的解析与应用
位操作在C语言中的解析与应用
62 0
|
27天前
|
机器学习/深度学习 编译器 Serverless
C语言中函数
C语言中函数
19 0