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]
目录
相关文章
|
3月前
|
存储 C语言
`scanf`是C语言中用于按格式读取标准输入的函数
`scanf`是C语言中用于按格式读取标准输入的函数,通过格式字符串解析输入并存入指定变量。需注意输入格式严格匹配,并建议检查返回值以确保读取成功,提升程序健壮性。
1029 0
|
11月前
|
存储 算法 C语言
【C语言程序设计——函数】素数判定(头歌实践教学平台习题)【合集】
本内容介绍了编写一个判断素数的子函数的任务,涵盖循环控制与跳转语句、算术运算符(%)、以及素数的概念。任务要求在主函数中输入整数并输出是否为素数的信息。相关知识包括 `for` 和 `while` 循环、`break` 和 `continue` 语句、取余运算符 `%` 的使用及素数定义、分布规律和应用场景。编程要求根据提示补充代码,测试说明提供了输入输出示例,最后给出通关代码和测试结果。 任务核心:编写判断素数的子函数并在主函数中调用,涉及循环结构和条件判断。
692 23
|
5月前
|
安全 C语言
C语言中的字符、字符串及内存操作函数详细讲解
通过这些函数的正确使用,可以有效管理字符串和内存操作,它们是C语言编程中不可或缺的工具。
334 15
|
10月前
|
人工智能 Java 程序员
一文彻底搞清楚C语言的函数
本文介绍C语言函数:函数是程序模块化的工具,由函数头和函数体组成,涵盖定义、调用、参数传递及声明等内容。值传递确保实参不受影响,函数声明增强代码可读性。君志所向,一往无前!
432 1
一文彻底搞清楚C语言的函数
|
11月前
|
算法 C语言
【C语言程序设计——函数】利用函数求解最大公约数和最小公倍数(头歌实践教学平台习题)【合集】
本文档介绍了如何编写两个子函数,分别求任意两个整数的最大公约数和最小公倍数。内容涵盖循环控制与跳转语句的使用、最大公约数的求法(包括辗转相除法和更相减损术),以及基于最大公约数求最小公倍数的方法。通过示例代码和测试说明,帮助读者理解和实现相关算法。最终提供了完整的通关代码及测试结果,确保编程任务的成功完成。
614 15
【C语言程序设计——函数】利用函数求解最大公约数和最小公倍数(头歌实践教学平台习题)【合集】
|
11月前
|
C语言
【C语言程序设计——函数】亲密数判定(头歌实践教学平台习题)【合集】
本文介绍了通过编程实现打印3000以内的全部亲密数的任务。主要内容包括: 1. **任务描述**:实现函数打印3000以内的全部亲密数。 2. **相关知识**: - 循环控制和跳转语句(for、while循环,break、continue语句)的使用。 - 亲密数的概念及历史背景。 - 判断亲密数的方法:计算数A的因子和存于B,再计算B的因子和存于sum,最后比较sum与A是否相等。 3. **编程要求**:根据提示在指定区域内补充代码。 4. **测试说明**:平台对代码进行测试,预期输出如220和284是一组亲密数。 5. **通关代码**:提供了完整的C语言代码实现
263 24
|
11月前
|
存储 C语言
【C语言程序设计——函数】递归求斐波那契数列的前n项(头歌实践教学平台习题)【合集】
本关任务是编写递归函数求斐波那契数列的前n项。主要内容包括: 1. **递归的概念**:递归是一种函数直接或间接调用自身的编程技巧,通过“俄罗斯套娃”的方式解决问题。 2. **边界条件的确定**:边界条件是递归停止的条件,确保递归不会无限进行。例如,计算阶乘时,当n为0或1时返回1。 3. **循环控制与跳转语句**:介绍`for`、`while`循环及`break`、`continue`语句的使用方法。 编程要求是在右侧编辑器Begin--End之间补充代码,测试输入分别为3和5,预期输出为斐波那契数列的前几项。通关代码已给出,需确保正确实现递归逻辑并处理好边界条件,以避免栈溢出或结果
650 16
|
11月前
|
存储 编译器 C语言
【C语言程序设计——函数】分数数列求和2(头歌实践教学平台习题)【合集】
函数首部:按照 C 语言语法,函数的定义首部表明这是一个自定义函数,函数名为fun,它接收一个整型参数n,用于指定要求阶乘的那个数,并且函数的返回值类型为float(在实际中如果阶乘结果数值较大,用float可能会有精度损失,也可以考虑使用double等更合适的数据类型,这里以float为例)。例如:// 函数体代码将放在这里函数体内部变量定义:在函数体中,首先需要定义一些变量来辅助完成阶乘的计算。比如需要定义一个变量(通常为float或double类型,这里假设用float。
539 3
|
11月前
|
存储 算法 安全
【C语言程序设计——函数】分数数列求和1(头歌实践教学平台习题)【合集】
if 语句是最基础的形式,当条件为真时执行其内部的语句块;switch 语句则适用于针对一个表达式的多个固定值进行判断,根据表达式的值与各个 case 后的常量值匹配情况,执行相应 case 分支下的语句,直到遇到 break 语句跳出 switch 结构,若没有匹配值则执行 default 分支(可选)。例如,在判断一个数是否大于 10 的场景中,条件表达式为 “num> 10”,这里的 “num” 是程序中的变量,通过比较其值与 10 的大小关系来确定条件的真假。常量的值必须是唯一的,且在同一个。
418 2
|
11月前
|
存储 编译器 C语言
【C语言程序设计——函数】回文数判定(头歌实践教学平台习题)【合集】
算术运算于 C 语言仿若精密 “齿轮组”,驱动着数值处理流程。编写函数求区间[100,500]中所有的回文数,要求每行打印10个数。根据提示在右侧编辑器Begin--End之间的区域内补充必要的代码。如果操作数是浮点数,在 C 语言中是不允许直接进行。的结果是 -1,因为 -7 除以 3 商为 -2,余数为 -1;注意:每一个数据输出格式为 printf("%4d", i);的结果是 1,因为 7 除以 -3 商为 -2,余数为 1。取余运算要求两个操作数必须是整数类型,包括。开始你的任务吧,祝你成功!
211 1