linux中以A开头的函数使用方式历程及详解

本文介绍了多个以A开头的Linux C函数,包括abort、abs、acos、asin、atan、asctime、atexit、atof、atoi和atol等。详细解释了每个函数的功能、用法及示例代码,适合于Linux环境下进行C语言编程的学习者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A开头的Linux C函数

abort

异常终止程序

abort函数在调用的时候,会触发SIGABRT信号

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

static void signalHandler(int sig);

// 信号处理函数
void signalHandler(int sig)
{
  if(sig == SIGABRT)    //对应ctrl+c
  {
    printf("abort 函数被调用,触发SIGABRT信号量 。\n");
  }
}
//以下是主函数
int main(int argc,char *argv[])
{
  signal(SIGABRT, signalHandler);   //注册SIGINT对应的处理函数

  abort();
 
  printf("程序走不到这里。\n");
  return 0;
}

abs

对整数求绝对值的函数

#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
/*
    ┌──────────────────────────────┬───────────────┬─────────┐
    │Interface                     │ Attribute     │ Value   │
    ├──────────────────────────────┼───────────────┼─────────┤
    │abs(), labs(), llabs(), imax‐ │ Thread safety │ MT-Safe │
    │abs()                         │               │         │
    └──────────────────────────────┴───────────────┴─────────┘
*/
int main(int argc, char const *argv[])
{
    /* code */
    
    printf("abs(-1) = [%d]\n", abs(-1));
    return 0;
}

acos

反余弦函数

#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <math.h>



/*
       ┌─────────────────────────┬───────────────┬─────────┐
       │Interface                │ Attribute     │ Value   │
       ├─────────────────────────┼───────────────┼─────────┤
       │acos(), acosf(), acosl() │ Thread safety │ MT-Safe │
       └─────────────────────────┴───────────────┴─────────┘
*/

int main(int argc, char const *argv[])
{
    
    double real = acos(-0.5);

    printf("%lf\n", real);
    return 0;
}

asctime time localtime

系统时间获取函数,注意这几个函数的时间受校时函数的影响

time() returns the time as the number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC)

loacaltime() 将time_t 结构的数据,转换为对应的日历时间,详见tm结构体

asctime() 将日历形式结构体时间数据转换为对应的字符串

#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <time.h>
#include <string.h>

#if 0
struct tm
{
  int tm_sec;			/* Seconds.	[0-60] (1 leap second) */
  int tm_min;			/* Minutes.	[0-59] */
  int tm_hour;			/* Hours.	[0-23] */
  int tm_mday;			/* Day.		[1-31] */
  int tm_mon;			/* Month.	[0-11] */
  int tm_year;			/* Year	- 1900.  */
  int tm_wday;			/* Day of week.	[0-6] */
  int tm_yday;			/* Days in year.[0-365]	*/
  int tm_isdst;			/* DST.		[-1/0/1]*/


  long int __tm_gmtoff;		
  const char *__tm_zone;	
};
#endif


int main(int argc, char const *argv[])
{
    //定义时间结构体指针,结构体值见注释
    struct tm *pTm = NULL;
    time_t nowTime;
    char *pSzAscTime = NULL;

    memset(&nowTime, 0 , sizeof(nowTime));

    nowTime = time(NULL);
    pTm = localtime(&nowTime);

    pSzAscTime = asctime(pTm);

    printf("Asc time = %s", pSzAscTime);

    return 0;
}

asin

反正弦函数

#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <math.h>
 

int main(int argc, char const *argv[])
{
    
    double real = asin(-0.5);

    printf("%lf\n", real);
    return 0;
}

assert

assert()函数是诊断函数,诊断一个表达式是否为真,只有为真才能继续执行

#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <assert.h>

/**
 * 诊断表达式的真值
 * */

int main(int argc, char const *argv[])
{
    
    assert(1);
    return 0;
}

atan

atan() 反正弦函数

#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <math.h>

int main(int argc, char const *argv[])
{
    
    double real = atan(-0.5);

    printf("%lf\n", real);
    return 0;
}

atexit

atexit() 在程序要退出时注册要调用的函数

该函数在做项目,一个进程中有多个线程,又申请内存需要释放的时候,非常有用,将需要释放的内存放到被注册的函数里面,无论在那个线程里面退出程序,都会出发释放资源的函数,这样就不用担心资源的释放问题了。

#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <math.h>

// C语言中用于注册  正常退出时调用的函数
// 当程序调用exit函数进行推出时就会先调用atexit函数注册的函数
#define	TRUE		1			//真 
#define	FALSE		0			//假
#define YES			1			//是
#define NO          0			//否 
#define	OK			0			//通过
#define	ERROR		-1			//错误

void callHnadler(void);

void callHnadler(void)
{
    printf("这个函数会在函数退出前调用一次\n");
}

int main(int argc, char const *argv[])
{
    int ret = ERROR;   
    ret = atexit(callHnadler);
    if(OK != ret)
    {
        printf("atexit register callHandler function failed\n");
    }

    return 0;
}

atof

将字符串转换为浮点数的函数

#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <math.h>

// 将字符串转换为浮点数的函数

int main(int argc, char const *argv[])
{
    
    printf("0.12345 = [%f]\n", atof("0.12345"));
    return 0;
}

atoi

将字符串转换为整数的函数

#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <math.h>

// 将字符串转化为整数的函数

int main(int argc, char const *argv[])
{
    
    printf("12345 = [%d]\n", atoi("12345"));
    return 0;
}

atol

将字符串转换为长整型的函数

#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <math.h>

// 将字符串转化为整数的函数

int main(int argc, char const *argv[])
{
    
    printf("12345 = [%d]\n", atol("12345"));
    return 0;
}

扫码关注,一起探究linux编程的乐趣
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Achilles.Wang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值