本文將介紹 C語言標準庫<stdlib.h> 中的 宏、函式、資料型別
在最后給出一個實體程式,盡可能運用到所有函式
宏
EXIT_SUCCESS 和 EXIT_FAILURE
exit函式成功退出和失敗的回傳值
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1
RAND_MAX rand 函式回傳的最大的數
#define RAND_MAX 0x7FFF
MB_CUR_MAX 一個多位元組字符的最大位元組數
原始碼處理的比較復雜
函式
將字串轉換為 double
double atof (const char *);
將字串轉換為 int
int atoi (const char *);
將字串轉換為 long int
long atol (const char *);
將以數字開頭的字串轉換為 double
str 是一個以數字開頭的字串,
回傳把 str 轉換后的 double , endptr 指向第一個不是數字的字符
double strtod(const char *str, char **endptr)
將以數字開頭的字串轉換為 long int
str 是一個以數字開頭的字串,
回傳把 str 以 base 為基數(進制)轉換后的 long int ,
endptr 指向第一個不是數字的字符
long int strtol(const char *str, char **endptr, int base)
str 是一個以數字開頭的字串,
回傳把 str 以 base 為基數 轉換后的 unsigned long int ,
endptr 指向第一個不是數字的字符
unsigned long int strtoul(const char *str, char **endptr, int base)
分配請求的記憶體大小(nitems * size)并回傳指向它的指標
void *calloc(size_t nitems, size_t size)
分配請求的記憶體(size)并回傳指向它的指標
void *malloc(size_t size)
調整之前分配的記憶體塊大小
- ptr 執行要調整的記憶體塊
- size 要調整到的位元組數
void *realloc(void *ptr, size_t size)
回收先前分配的記憶體
void free(void *ptr)
搜索環境變數并回傳字串
char *getenv(const char *name)
把命令(string)傳遞給主機由主機執行
int system(const char *string)
快速排序
- base是陣列的基地址
- nitems是陣列的元素個數
- size是陣列元素大小
- compar是比較函式,靠回傳值正負來比較,為0的情況未定義
void qsort(void *base, size_t nitems, size_t size,
int (*compar)(const void *, const void*))
二分查找
- key是要查找的元素
- base是陣列的基地址
- num是陣列的元素個數
- size陣列元素大小
- cmp是比較函式,靠回傳值正負來比較,陣列必須是排好序的
- 回傳找到的元素的指標
void *bsearch(const void *key, const void *base, size_t num, size_t size,
int (*cmp)(const void *, const void *));
回傳x的絕對值
int abs(int x)
long int labs(long int x)
做除法,分別得到除數和被除數
div_t div(int numer, int denom)
ldiv_t ldiv(long int numer, long int denom)
回傳一個亂數,范圍(0~RAND_MAX)
int rand(void)
對隨機函式設定種子
void srand(unsigned int seed)
計算一個多位元組字符的長度
- n 是多位元組字符的長度的最大值
int mblen(const char *str, size_t n)
多位元組字串轉換為陣列
將由引數str指向的多位元組字串轉換為由pwcs指向的陣列
- n 是多位元組字符的長度的最大值
size_t mbstowcs(schar_t *pwcs, const char *str, size_t n)
在程式正常退出時執行
- func 要執行的函式
int atexit(void (*func)(void))
程式例外退出
void abort(void)
程式正常退出 ,回傳給作業系統 status
void exit(int status)
資料型別
size_t是sizeof關鍵詞的值,無符號整形
wchar_t 寬字符型別
div_t與ldiv_t結構體 作為 div 的回傳值
typedef struct {
int quot; // 商
int rem; // 余數
} div_t;
//-------------------------------
typedef struct {
long int quot; // 商
long int rem; // 余數
} ldiv_t;
示例檔案
Github
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void test(){ printf("do this at exit\n"); }
int cmptest(const void * a, const void * b){
return (int)( *(char*)a - *(char*)b );
}
int main (){
///
/// ## 資料型別
///
/// ### size_t是sizeof關鍵詞的值,無符號整形
printf("size_t %d\n",sizeof(size_t));
/// ### wchar_t 寬字符型別
printf("wchar_t %d\n",sizeof(wchar_t));
/// ### div_t與ldiv_t結構體 作為 div 的回傳值
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
/// typedef struct {
/// int quot; // 商
/// int rem; // 余數
/// } div_t;
div_t div_t_timp;
printf("div_t %d div_t.quot %d div_t.rem %d\n",
sizeof(div_t),sizeof(div_t_timp.quot),sizeof(div_t_timp.rem));
/// //-------------------------------
/// typedef struct {
/// long int quot; // 商
/// long int rem; // 余數
/// } ldiv_t;
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ldiv_t ldiv_t_timp;
printf("ldiv_t %d ldiv_t.quot %d ldiv_t.rem %d\n",
sizeof(ldiv_t),sizeof(ldiv_t_timp.quot),sizeof(ldiv_t_timp.rem));
///
/// ## 宏
///
/// ### EXIT_SUCCESS 和 EXIT_FAILURE
/// exit函式成功退出和失敗的回傳值
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
/// #define EXIT_SUCCESS 0
/// #define EXIT_FAILURE 1
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
printf("EXIT_SUCCESS %d\nEXIT_FAILURE %d\n",EXIT_SUCCESS,EXIT_FAILURE);
/// ### RAND_MAX rand 函式回傳的最大的數
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
/// #define RAND_MAX 0x7FFF
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
printf("RAND_MAX %d\n",RAND_MAX);
/// ### MB_CUR_MAX 一個多位元組字符的最大位元組數
/// 頭檔案中的處理比較復雜
printf("MB_CUR_MAX %d\n",MB_CUR_MAX);
///
/// ## 函式
///
/// ### 將字串轉換為 double
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
/// double atof (const char *);
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
printf("atof(\"12.3\") %f\n",atof("12.3"));
/// ### 將字串轉換為 int
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
/// int atoi (const char *);
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
printf("atoi(\"12\") %d\n",atoi("12"));
/// ### 將字串轉換為 long int
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
/// long atol (const char *);
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
printf("atol(\"123456789\") %ld\n",atol("123456789"));
/// ### 將以數字開頭的字串轉換為 double
/// str 是一個以數字開頭的字串,
/// 回傳把 str 轉換后的 double , endptr 指向第一個不是數字的字符
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
/// double strtod(const char *str, char **endptr)
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
char *p;
printf("strtod(\"12.030 hello\",&p) %f",strtod("12.030hello",&p));
printf(" %s\n",p);
/// ### 將以數字開頭的字串轉換為 long int
/// str 是一個以數字開頭的字串,
/// 回傳把 str 以 base 為基數(進制)轉換后的 long int ,
/// endptr 指向第一個不是數字的字符
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
/// long int strtol(const char *str, char **endptr, int base)
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
printf("strtol(\"ffhello\",&p,16) %ld",strtol("ffhello",&p,16));
printf(" %s\n",p);
/// ### str 是一個以數字開頭的字串,
/// 回傳把 str 以 base 為基數 轉換后的 unsigned long int ,
/// endptr 指向第一個不是數字的字符
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
/// unsigned long int strtoul(const char *str, char **endptr, int base)
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
printf("strtoul(\"ffhello\",&p,16) %uld",strtoul("ffhello",&p,16));
printf(" %s\n",p);
/// ### 分配請求的記憶體大小(nitems * size)并回傳指向它的指標
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
/// void *calloc(size_t nitems, size_t size)
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
p = (char *)calloc(5, sizeof(char));
printf("calloc(5, sizeof(char)) %p\n",p);
/// ### 分配請求的記憶體(size)并回傳指向它的指標
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
/// void *malloc(size_t size)
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/* p = (char *)calloc(5 * sizeof(char)); */
/// ### 調整之前分配的記憶體塊大小
/// * ptr 執行要調整的記憶體塊
/// * size 要調整到的位元組數
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
/// void *realloc(void *ptr, size_t size)
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
p = realloc(p,10*sizeof(char));
printf("realloc(p,10*sizeof(char)) %p\n",p);
p = realloc(p,10000*sizeof(char));
printf("realloc(p,10000*sizeof(char)) %p\n",p);
/// ### 回收先前分配的記憶體
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
/// void free(void *ptr)
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
free(p);
/// ### 搜索環境變數并回傳字串
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
/// char *getenv(const char *name)
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
printf("getenv(\"PATH\") %.30s ...\n", getenv("PATH"));
/// ### 把命令(string)傳遞給主機由主機執行
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
/// int system(const char *string)
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
printf("system(\"echo ok\")\n");
system("echo ok");
/// ### 快速排序
/// * base是陣列的基地址
/// * nitems是陣列的元素個數
/// * size是陣列元素大小
/// * compar是比較函式,靠回傳值正負來比較,為0的情況未定義
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
/// void qsort(void *base, size_t nitems, size_t size,
/// int (*compar)(const void *, const void*))
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
p = (char *)calloc(11, sizeof(char));
strcpy(p,"5147096382");
qsort(p,10,sizeof(char),cmptest);
printf("qsort ");
printf(p);
putchar('\n');
/// ### 二分查找
/// * key是要查找的元素
/// * base是陣列的基地址
/// * num是陣列的元素個數
/// * size陣列元素大小
/// * cmp是比較函式,靠回傳值正負來比較,陣列必須是排好序的
/// * 回傳找到的元素的指標
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
/// void *bsearch(const void *key, const void *base, size_t num, size_t size,
/// int (*cmp)(const void *, const void *));
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
char a = '3'; /* 搜索a */
printf("bsearch ");
printf((char *)bsearch(&a,p,10,sizeof(char),cmptest));
free(p);
putchar('\n');
/// ### 回傳x的絕對值
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
/// int abs(int x)
/// long int labs(long int x)
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
printf("abs(-5) %d\n",abs(-5));
/// ### 做除法,分別得到除數和被除數
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
/// div_t div(int numer, int denom)
/// ldiv_t ldiv(long int numer, long int denom)
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
div_t div_out = div(7,2);
printf("div(7,2)={%d,%d}\n",div_out.quot,div_out.rem);
/// ### 回傳一個亂數,范圍(0~RAND_MAX)
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
/// int rand(void)
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
printf("rand() = %d\n",rand());
/// ### 對隨機函式設定種子
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
/// void srand(unsigned int seed)
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
srand(15);
/// ### 計算一個多位元組字符的長度
/// * n 是多位元組字符的長度的最大值
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
/// int mblen(const char *str, size_t n)
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
wchar_t mywchar = L'漢'; /***** todo: 換成'國'就不行了 */
printf("mblen('%x',MB_CUR_MAX) %u\n",L'漢',mblen((char *)&mywchar,MB_CUR_MAX));
/// ### 多位元組字串轉換為陣列
/// 將由引數str指向的多位元組字串轉換為由pwcs指向的陣列
/// * n 是多位元組字符的長度的最大值
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
/// size_t mbstowcs(schar_t *pwcs, const char *str, size_t n)
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/***** todo: 添加實體 */
/* todu 寬字符處理看不懂
int mbtowc(whcar_t *pwc, const char *str, size_t n)
Examines the multibyte character pointed to by the argument str.
size_t wcstombs(char *str, const wchar_t *pwcs, size_t n)
Converts the codes stored in the array pwcs to multibyte characters and stores them in the string str.
int wctomb(char *str, wchar_t wchar)
Examines the code which corresponds to a multibyte character given by the argument wchar.
*/
/// ### 在程式正常退出時執行
/// * func 要執行的函式
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
/// int atexit(void (*func)(void))
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
atexit(test);
/// ### 程式例外退出
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
/// void abort(void)
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/* abort(); */
/// ### 程式正常退出 ,回傳給作業系統 status
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
/// void exit(int status)
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
exit(0);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/224255.html
標籤:其他
上一篇:【和60】軟體即服務的三重境界
