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編程的樂趣