首页  

C语言stdlib使用实例     所属分类 c 浏览量 626
7类不同的实用工具函数
字符串转换函数
伪随机序列生成函数
内存管理函数
与外部环境通信的函数
搜索和排序函数
整数算术运算函数
多字节字符和字符串函数



字符串转换函数 double atof(const char *nptr); int atoi(const char *nptr); long int atol(const char *nptr); double strtod(const char *nptr, char **endptr); long int strtol(const char *nptr, char **endptr, int base); unsigned long int strtoul(const char *nptr, char **endptr, int base); 前三个函数旧函数 跳过开头空格,碰到第一个非数字字符停止解析,转换成 数字 后3个新函数 base指定基数 值溢出时 将ERANGE放在 errno.h errno变量中 double strtod(const char *str, char **endptr) 把参数 str 所指向的字符串转换为一个浮点数(类型为 double 型) 如果 endptr 不为空,则指向转换中最后一个字符后的字符的指针 存储在 endptr 引用的位置 C语言字符串转浮点数
伪随机数序列生成函数 int rand(void); void srand(unsigned int seed); C语言 rand srand rand_r
内存管理函数 内存分配 释放 void *malloc(size_t size); void *calloc(size_t n, size_t size); void *realloc(void *ptr, size_t size); void free(void *ptr); malloc 分配内存但不初始化 calloc 分配内存并清零 realloc 调整已分配内存块的大小 free 释放内存 C语言动态内存分配与释放
与外部环境通信函数 正常或不正常终止,返回一个状态码 从用户的外部环境获取信息 执行操作系统命令 void exit(int n); void abort(void n); int atexit(void (*func)(void)); char *getenv(const char *name); int system(const char *command); exit atexit 及 abort
搜索和排序函数 void qsort(void *base, size_t num, size_t size, int (*compar)(const void*, const void*)); void *bsearch(const void *key, const void *base, size_t num, size_t size, int (*compar)(const void*, const void*)); qsort 对数组进行排序 base 数组,num为数组中元素数量,size为每个数组元素大小,compar为指向比较函数的指针 bsearch 二分查找有序数组 key 关键字 base 数组 num 数组中元素个数 size 每个数组元素的大小 compare 比较函数的指针 C语言排序和搜索
整数算术运算函数 int abs(int j); div_t div(int number, int denom); long int labs(long int j); ldivt ldiv(long int num, long int denom) abs 返回int绝对值 labs 返回long绝对值 div int除法 div_t 结构 quot ,rem 余数 ldiv long除法 int x = -99; printf("%d abs %d\n", x, abs(x)); long y = -1000L; printf("%ld (long) abs %ld\n", y, labs(y)); int x1 = 99; int x2 = 7; div_t dit = div(x1, x2); printf("%d / %d = %d remainder %d\n", x1, x2, dit.quot, dit.rem); long y1 = 999L; long y2 = 20L; ldiv_t ldit = ldiv(y1, y2); printf("%ld / %ld = %ld remainder %ld\n", y1, y2, ldit.quot, ldit.rem);
多字节和字符串处理函数 宽字符和多字节字符 int mblen(const char *s, size_t n); int mbtowc(wchar_t *pwc, const char *s, size_t n); int wctomb(char *s, wchar_t wchar); size_t mbstowcs(wchar_t *pwcs, const char *s, size_t n); size_t wcstombs(char *s, const wchar_t *s, size_t n); mblen 检测字节数组是否是一个有效的多字节字符序列 mbtowc 多字节字符转换为单字符 wctomb 宽字符转换为多字节字符 mbstowcs 多字节字符转换为宽字符 wcstombs 宽字符串转换为多字节字符串 wctomb = wide character to multibyte setlocale(LC_ALL, ""); wchar_t zhong = L'中'; char mb[MB_CUR_MAX]; int num = wctomb(mb, zhong); wprintf(L"mb的字节数为%d\n", num); if (mblen(mb, MB_CUR_MAX)){ wprintf(L"mb是一个有效的多字符序列!\n"); } wchar_t china[] = L"中国"; char *mb2 = malloc(MB_CUR_MAX * sizeof(china)/sizeof(wchar_t)); size_t num2 = wcstombs(mb2, china, MB_CUR_MAX * sizeof(china)/sizeof(wchar_t)); wprintf(L"mb2的字节数为%d\n", num2); wchar_t love[3] = L""; mbstowcs(love, mb2, 2); wprintf(L"我爱%Ls\n", love);
完整代码 https://gitee.com/dyyx/hellocode/tree/master/web/tech/c/demo/stdlib

上一篇     下一篇
C语言 rand srand rand_r

C语言动态内存分配与释放

exit atexit 及 abort

程序各个段text data bss stack heap

C++左值和右值

springboot jar包无法解压