C语言函数大全
本篇介绍C语言函数中 a 开头的函数
1. abort
1.1 函数说明
函数声明 |
函数功能 |
void abort(void); |
异常终止一个进程 |
1.2 演示示例
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf("Calling abort()\n");
abort();
printf("already abort()"); // 这里永远也到不了
return 0;
}
1.3 运行结果
2. abs
2.1 函数说明
函数声明 |
函数功能 |
int abs(int i); |
求整数的绝对值 |
2.2 演示示例
#include <stdio.h>
#include <math.h>
int main(void)
{
int number = -666;
printf("number: %d absolute value: %d\n", number, abs(number));
return 0;
}
2.3 运行结果
3. absread
3.1 函数说明
函数声明 |
函数功能 |
int absread(int drive, int nsects, int sectno, void *buffer); |
从drive指定的驱动器磁盘上,sectno指定的逻辑扇区号开始读取nsects个(最多64K个)扇区的内容,储存于buffer所指的缓冲区中。 |
3.2 演示示例
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <dos.h>
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);
}
4. abswrite
4.1 函数说明
函数声明 |
函数功能 |
int abswrite(int drive, int nsects, int sectno, void *buffer); |
将指定内容写入磁盘上的指定扇区 |
4.2 演示示例
#include <dos.h>
#include <stdio.h>
unsigned char buff[512];
int main()
{
int i;
char c;
printf("\nQuick Format 1.44MB\n");
printf("Program by ChenQingyang.\n");
printf("ALL DATA IN THE FLOPPY DISK WILL BE LOST!!\n");
printf("\nInsert a diskette for drive A:\n");
printf("and press ENTER when ready. . .");
c=getchar();
printf("\n\nCleaning FAT area. . .");
buff[0]=0xf0;
buff[1]=buff[2]=0xff;
for (i=3;i<512;i++)
buff[i]=0;
abswrite(0,1,1,buff);
abswrite(0,1,10,buff);
for (i=0;i<512;i++)
buff[i]=0;
for (i=2;i<10;i++)
abswrite (0,1,i,buff);
for (i=11;i<19;i++)
abswrite (0,1,i,buff);
printf("\nCleaning ROOT area. . .");
for (i=19;i<33;i++)
abswrite (0,1,i,buff);
printf("\n\nQuickFormat Completed!\n");
}
5. access
5.1 函数说明
函数声明 |
函数功能 |
int access(const char *filename, int amode); |
确定文件的访问权限 |
5.2 演示示例
#include <stdio.h>
#include <io.h>
int file_exists(char *filename);
int main(void)
{
printf("Does students1.txt exist: %s\n",
file_exists("students1.txt") ? "YES" : "NO");
return 0;
}
int file_exists(char *filename)
{
return (access(filename, 0) == 0);
}
5.3 运行结果
6. acos
6.1 函数说明
函数声明 |
函数功能 |
double acos(double x); |
反余弦函数 |
6.2 演示示例
#include <stdio.h>
#include <math.h>
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;
}
6.3 运行结果
7. allocmem
7.1 函数说明
函数声明 |
函数功能 |
int allocmem(unsigned size, unsigned *seg); |
分配DOS存储段 |
7.2 演示示例
#include <dos.h>
#include <alloc.h>
#include <stdio.h>
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;
}
7.3 运行结果
暂无
8. arc
8.1 函数说明
函数声明 |
函数功能 |
void arc(int x, int y, int stangle, int endangle, int radius); |
画一弧线 |
8.2 演示示例
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
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;
}
8.3 运行结果
暂无
9. asctime
9.1 函数说明
函数声明 |
函数功能 |
char *asctime(const struct tm *tblock); |
转换日期和时间为ASCII码 |
9.2 演示示例
#include <stdio.h>
#include <string.h>
#include <time.h>
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;
}
9.3 运行结果
10. asin
10.1 函数说明
函数声明 |
函数功能 |
double asin(double x); |
反正弦函数 |
10.2 演示示例
#include <stdio.h>
#include <math.h>
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);
}
10.3 运行结果
11. assert
11.1 函数说明
函数声明 |
函数功能 |
void assert(int test); |
测试一个条件并可能使程序终止 |
11.2 演示示例
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
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;
}
11.3 运行结果
12. atan
12.1 函数说明
函数声明 |
函数功能 |
double atan(double x); |
反正切函数 |
12.2 演示示例
#include <stdio.h>
#include <math.h>
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);
}
12.3 运行结果
13. atan2
13.1 函数说明
函数声明 |
函数功能 |
double atan2(double y, double x); |
计算Y/X的反正切值 |
13.2 演示示例
#include <stdio.h>
#include <math.h>
int main(void)
{
double result;
double x = 60.0, y = 30.0;
result = atan2(y, x);
printf("The arc tangent ratio of %lf is %lf\n", (y / x), result);
return 0;
}
13.3 运行结果
14. atexit
14.1 函数说明
函数声明 |
函数功能 |
int atexit(atexit_t func); |
注册终止函数 |
14.2 演示示例
#include <stdio.h>
#include <stdlib.h>
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;
}
14.3 运行结果
15. atof
15.1 函数说明
函数声明 |
函数功能 |
double atof(const char *nptr); |
把字符串转换成浮点数 |
15.2 演示示例
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
float f;
char *str = "1234.5678";
f = atof(str);
printf("string = %s float = %f\n", str, f);
return 0;
}
15.3 运行结果
16. atoi
16.1 函数说明
函数声明 |
函数功能 |
int atoi(const char *nptr); |
把字符串转换成整型数 |
16.2 演示示例
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int n;
char *str = "1234.5678";
n = atoi(str);
printf("string = %s integer = %d\n", str, n);
return 0;
}
16.3 运行结果
17. atol
17.1 函数说明
函数声明 |
函数功能 |
long atol(const char *nptr); |
把字符串转换成长整型数 |
17.2 演示示例
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
long l;
char *lstr = "87654321";
l = atol(lstr);
printf("string = %s integer = %ld\n", lstr, l);
return(0);
}
17.3 运行结果
参考
- [API Reference Document]