C语言函数大全--a开头的函数

简介: 【6月更文挑战第1天】本篇介绍 C语言中 a开头的函数【C语言函数大全】

image.png

总览

函数声明 函数功能
void abort(void); 异常终止一个进程
int abs(int i); 求整数的绝对值
int absread(int drive, int nsects, int sectno, void *buffer); 从drive指定的驱动器磁盘上,sectno指定的逻辑扇区号开始读取nsects个(最多64K个)扇区的内容,储存于buffer所指的缓冲区中。
int abswrite(int drive, int nsects, int sectno, void *buffer); 将指定内容写入磁盘上的指定扇区
int access(const char *filename, int amode); 确定文件的访问权限
double acos(double x); 反余弦函数
int allocmem(unsigned size, unsigned *seg); 分配DOS存储段
void arc(int x, int y, int stangle, int endangle, int radius); 画一弧线
char *asctime(const struct tm *tblock); 转换日期和时间为ASCII码
double asin(double x); 反正弦函数
void assert(int test); 测试一个条件并可能使程序终止
double atan(double x); 反正切函数
double atan2(double y, double x); 计算Y/X的反正切值
int atexit(atexit_t func); 注册终止函数
double atof(const char *nptr); 把字符串转换成浮点数
int atoi(const char *nptr); 把字符串转换成整型数
long atol(const char *nptr); 把字符串转换成长整型数

1. abort

1.1 函数说明

函数声明 函数功能
void abort(void); 异常终止一个进程

注意: abort() 函数用于终止当前程序的执行。当程序调用 abort() 函数时,它会立即退出,并生成一个错误信号,通知操作系统程序非正常终止。如果程序已经打开了一些文件或句柄,但尚未关闭它们,则这些资源可能无法被正确释放。

1.2 演示示例

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
   
    printf("Calling abort()\n");
    abort();
    printf("already abort()"); // 这里永远也到不了
    return 0; 
}

1.3 运行结果

image.png

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 运行结果

image.png

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

上述的代码实现了从 A 驱动器读取一个扇区的数据,并将其中一些字符输出到屏幕上。

  • 首先提示用户插入一个软盘到 A 驱动器中。
  • 然后读取 A 驱动器上第 0 个扇区的数据到缓冲区 buf 中。
  • 接着检查读取是否成功。如果不成功,输出错误信息并退出程序。
  • 最后将 buf 缓冲区中偏移量为 3 到偏移量为 82 的字符依次输出到屏幕上。

注意: 程序中使用了一些 DOS 特定的函数,比如 absread()getch(),可能不适用于其他操作系统或编译器环境。

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");
}

上述代码是一个使用 DOS 命令格式化软盘的程序。它会提示用户输入软盘,然后清空软盘的FAT和根目录区域,并在完成后打印 “QuickFormat Completed!” 的信息。程序使用了 <dos.h><stdio.h> 头文件,其中包含了一些 DOS 和标准输入输出函数。

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 运行结果

image.png

image.png

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 运行结果

image.png

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;
}

在上述的示例代码,

  • 首先调用了 allocmem() 函数来分配内存,其中传递了两个参数:size 表示请求的内存大小(以段为单位),这里设置为 64 段;&segp 表示返回的内存段地址将存储在此变量中。
  • 如果成功分配内存,allocmem()函数将返回 -1,并打印出已分配内存的段地址;
  • 否则,它将返回最大可用段数,并打印出失败的消息。

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>

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 */
    char ch[] = "";
    initgraph(&gdriver, &gmode, ch);

    /* 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 运行结果

image.png

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 运行结果

image.png

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 运行结果

image.png

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 运行结果

image.png

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 运行结果

image.png

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 运行结果

image.png

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 运行结果

image.png

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 运行结果

image.png

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 运行结果

image.png

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 运行结果

image.png

参考

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