linux中以A开头的函数使用方式历程及详解
生活随笔
收集整理的這篇文章主要介紹了
linux中以A开头的函数使用方式历程及详解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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; }; #endifint 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編程的樂趣
總結
以上是生活随笔為你收集整理的linux中以A开头的函数使用方式历程及详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2016科学数据大会临时通知
- 下一篇: 《大数据》2020年第4期目次摘要