c语言库函数总结----stdlib.h库

c语言库函数总结----stdlib.h库思维导图大纲 前言 stdlib h 头文件定义了四个变量类型 一些宏和各种通用工具函数 double atof const char str 函数原型 double atof const char str 函数功能 把参数 str 所指向的字符串转换为一个浮点数 类型为 double 型

大家好,我是讯享网,很高兴认识大家。

思维导图大纲

在这里插入图片描述
讯享网

前言

stdlib .h 头文件定义了四个变量类型、一些宏和各种通用工具函数。

  1. double atof(const char *str)
    函数原型 double atof(const char *str)
    函数功能: 把参数 str 所指向的字符串转换为一个浮点数(类型为 double 型)。
//时间:2019年11月18日 //作者:Kroner //编译环境:VS 2019 //库函数 stdlib.h //函数原型 double atof(const char *str) //函数功能: 把参数 str 所指向的字符串转换为一个浮点数(类型为 double 型)。 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { 
    float val; char str[20]; strcpy(str, ""); val = atof(str); printf("字符串值 = %s, 浮点值 = %f\n", str, val); strcpy(str, "runoob"); val = atof(str); printf("字符串值 = %s, 浮点值 = %f\n", str, val); return 0; } 

讯享网

测试结果:
在这里插入图片描述

  1. int atoi(const char *str)
    函数原型 int atoi(const char *str)
    函数功能: 把参数 str 所指向的字符串转换为一个整数(类型为 int 型)。
讯享网//时间:2019年11月18日 //作者:Kroner //编译环境:VS 2019 //库函数 stdlib.h //函数原型 int atoi(const char *str) //函数功能: 把参数 str 所指向的字符串转换为一个整数(类型为 int 型)。 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { 
    int val; char str[20]; strcpy(str, ""); val = atoi(str); printf("字符串值 = %s, 整型值 = %d\n", str, val); strcpy(str, "runoob.com"); val = atoi(str); printf("字符串值 = %s, 整型值 = %d\n", str, val); return 0; } 

测试结果:
在这里插入图片描述

  1. long int atol(const char *str)
    函数原型 long int atol(const char *str)
    函数功能: 把参数 str 所指向的字符串转换为一个长整数(类型为 long int 型)
//时间:2019年11月18日 //作者:Kroner //编译环境:VS 2019 //库函数 stdlib.h //函数原型 long int atol(const char *str) //函数功能: 把参数 str 所指向的字符串转换为一个长整数(类型为 long int 型) #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { 
    long val; char str[20]; strcpy(str, ""); val = atol(str); printf("字符串值 = %s, 长整型值 = %ld\n", str, val); strcpy(str, "runoob.com"); val = atol(str); printf("字符串值 = %s, 长整型值 = %ld\n", str, val); return 0; } 

测试结果:
在这里插入图片描述

  1. double strtod(const char *str, char endptr)
    函数原型 double strtod(const char *str, char *endptr)
    函数功能: 把参数 str 所指向的字符串转换为一个浮点数(类型为 double 型),
    str – 要转换为双精度浮点数的字符串。
    endptr – 对类型为 char
    的对象的引用,其值由函数设置为 str 中数值后的下一个字符。
    这个当第二个参数为空时和 double atof(const char *str)是相同,但是当而第二形参引用后,第二个指针会指向存储字符串位置的地址。
讯享网//时间:2019年11月18日 //作者:Kroner //编译环境:VS 2019 //库函数 stdlib.h //函数原型 double strtod(const char *str, char endptr) //函数功能: 把参数 str 所指向的字符串转换为一个浮点数(类型为 double 型)。 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> int main() { 
    char str[30] = "20.30300 This is test"; char* ptr; double ret; ret = strtod(str, &ptr); printf("数字(double)是 %lf\n", ret); printf("字符串部分是 |%s|", ptr); return 0; } 

测试结果:
在这里插入图片描述

  1. long int strtol(const char *str, char endptr, int base)
    函数原型 long int strtol(const char *str, char *endptr, int base)
    函数功能: 把参数 str 所指向的字符串转换为一个长整数(类型为 long int 型)。
    str – 要转换为长整数的字符串。
    endptr – 对类型为 char
    的对象的引用,其值由函数设置为 str 中数值后的下一个字符。
    base – 基数,必须介于 2 和 36(包含)之间,或者是特殊值 0。这个基数就表示这个多少数字就多少进制
    如果下列代码改为ret = strtol(str, &ptr, 8);就表示11112是八进制的数
//时间:2019年11月18日 //作者:Kroner //编译环境:VS 2019 //库函数 stdlib.h //函数原型 long int strtol(const char *str, char endptr, int base) //函数功能: 把参数 str 所指向的字符串转换为一个长整数(类型为 long int 型)。 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> int main() { 
    char str[30] = "11112 This is test"; char* ptr; long ret; ret = strtol(str, &ptr, 10); printf("数字(无符号长整数)是 %ld\n", ret); printf("字符串部分是 |%s|", ptr); return 0; } 

测试结果:
在这里插入图片描述

  1. unsigned long int strtoul(const char *str, char endptr, int base)
    函数原型 unsigned long int strtoul(const char *str, char endptr, int base)
    函数功能: 把参数 str 所指向的字符串转换为一个无符号长整数(类型为 unsigned long int 型)。
讯享网//时间:2019年11月19日 //作者:Kroner //编译环境:VS 2019 //库函数 stdlib.h //函数原型 unsigned long int strtoul(const char *str, char endptr, int base) //函数功能: 把参数 str 所指向的字符串转换为一个无符号长整数(类型为 unsigned long int 型)。 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> int main() { 
    char str[30] = " This is test"; char* ptr; long ret; ret = strtoul(str, &ptr, 10); printf("数字(无符号长整数)是 %lu\n", ret); printf("字符串部分是 |%s|", ptr); return 0; } 

测试结果:
在这里插入图片描述

  1. void *calloc(size_t nitems, size_t size)
    函数原型 void *calloc(size_t nitems, size_t size)
    函数功能: 分配所需的内存空间,并返回一个指向它的指针。
    malloc 和 calloc 之间的不同点是,malloc 不会设置内存为零,
//时间: 2019年11月19日 //作者: Kroner //编译环境: VS 2019 //库函数 stdlib.h //函数原型 void *calloc(size_t nitems, size_t size) //函数功能: 释放之前调用 calloc、malloc 或 realloc 所分配的内存空间。 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> int main() { 
    int i, n; int* a; printf("要输入的元素个数:"); scanf("%d", &n); a = (int*)calloc(n, sizeof(int)); printf("输入 %d 个数字:\n", n); for (i = 0; i < n; i++) { 
    scanf("%d", &a[i]); } printf("输入的数字为:"); for (i = 0; i < n; i++) { 
    printf("%d ", a[i]); } free(a); // 释放内存 return 0; } 

测试结果:
在这里插入图片描述

  1. void free(void *ptr)
    函数原型 void free(void *ptr)
    函数功能: 分配所需的内存空间,该函数不返回任何值。
讯享网//时间:2019年11月19日 //作者:Kroner //编译环境:VS 2019 //库函数 stdlib.h //函数原型 void free(void *ptr) //函数功能: 分配所需的内存空间,并返回一个指向它的指针。 // malloc 和 calloc 之间的不同点是,malloc 不会设置内存为零,而 calloc 会设置分配的内存为零。 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { 
    char* str; /* 最初的内存分配 */ str = (char*)malloc(15); strcpy(str, "runoob"); printf("String = %s, Address = %p\n", str, str); /* 重新分配内存 */ str = (char*)realloc(str, 25); strcat(str, ".com"); printf("String = %s, Address = %p\n", str, str); /* 释放已分配的内存 */ free(str); return 0; } 

测试结果:
在这里插入图片描述

  1. void *malloc(size_t size)
    函数原型 void *malloc(size_t size)
    函数功能: 分配所需的内存空间,并返回一个指向它的指针。
    该函数返回一个指针 ,指向已分配大小的内存。如果请求失败,则返回 NULL。
//时间:2019年11月15日 //作者:Kroner //编译环境:VS 2019 //库函数 stdlib.h //函数原型 void *malloc(size_t size) //函数功能: 分配所需的内存空间,并返回一个指向它的指针。 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> #include <stdlib.h> int main() { 
    char* str; /* 最初的内存分配 */ str = (char*)malloc(15); strcpy(str, "Kroner"); printf("String = %s, Address = %u\n", str, str); /* 重新分配内存 */ str = (char*)realloc(str, 25); strcat(str, ".com"); printf("String = %s, Address = %u\n", str, str); free(str); //释放内存 return 0; } 

测试结果
在这里插入图片描述

  1. void *realloc(void *ptr, size_t size)
    函数原型 void *realloc(void *ptr, size_t size)
    函数功能: 尝试重新调整之前调用 malloc 或 calloc 所分配的 ptr 所指向的内存块的大小。
    该函数返回一个指针 ,指向重新分配大小的内存。如果请求失败,则返回 NULL。
讯享网//时间:2019年11月15日 //作者:Kroner //编译环境:VS 2019 //库函数 stdlib.h //函数原型 void *realloc(void *ptr, size_t size) //函数功能: 尝试重新调整之前调用 malloc 或 calloc 所分配的 ptr 所指向的内存块的大小。 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { 
    char* str; /* 最初的内存分配 */ str = (char*)malloc(15); strcpy(str, "Kroner"); printf("String = %s, Address = %p\n", str, str); /* 重新分配内存 */ str = (char*)realloc(str, 25); strcat(str, ".com"); printf("String = %s, Address = %p\n", str, str); free(str); return 0; } 

测试结果:
在这里插入图片描述

  1. void abort(void)
    函数原型 void abort(void)
    函数功能: 使一个异常程序终止。
//时间:2019年11月15日 //作者:Kroner //编译环境:VS 2019 //库函数 stdlib.h //函数原型 void abort(void) //函数功能: 使一个异常程序终止。 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> int main() { 
    FILE* fp; printf("准备打开 nofile.txt\n"); fp = fopen("nofile.txt", "r"); if (fp == NULL) { 
    printf("准备终止程序\n"); abort(); } printf("准备关闭 nofile.txt\n"); fclose(fp); return(0); } 

测试结果:
由于我们没有创建nofile.txt,所以是无法使用fopen()打开文件的,所以就会有弹出这样的异常界面
在这里插入图片描述

  1. int atexit(void (*func)(void))
    函数原型 int atexit(void (*func)(void))
    函数功能: 当程序正常终止时,调用指定的函数 func。
讯享网//时间:2019年11月15日 //作者:Kroner //编译环境: VS 2019 //库函数 stdlib.h //函数原型 int atexit(void (*func)(void)) //函数功能: 当程序正常终止时,调用指定的函数 func。 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> void functionA() { 
    printf("这是函数A\n"); } int main() { 
    /* 注册终止函数 */ atexit(functionA); printf("启动主程序...\n"); printf("退出主程序...\n"); return 0; } 

测试结果:
在这里插入图片描述

  1. void exit(int status)
    函数原型 void exit(int status)
    函数功能: 使程序正常终止。
//时间:2019年11月15日 //作者:Kroner //编译环境:VS 2019 //库函数 stdlib.h //函数原型 void exit(int status) //函数功能: 使程序正常终止。 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> int main() { 
    printf("程序的开头....\n"); printf("退出程序....\n"); exit(0); printf("程序的结尾....\n"); return 0; } 

测试结果:
在这里插入图片描述

  1. char *getenv(const char *name)
    函数原型 char *getenv(const char *name)
    函数功能: 搜索 name 所指向的环境字符串,并返回相关的值给字符串。
讯享网//时间:2019年11月15日 //作者:Kroner //编译环境:VS 2019 //库函数 stdlib.h //函数原型 char *getenv(const char *name) //函数功能: 搜索 name 所指向的环境字符串,并返回相关的值给字符串。 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> int main() { 
    printf("PATH : %s\n", getenv("PATH")); printf("HOME : %s\n", getenv("HOME")); printf("ROOT : %s\n", getenv("ROOT")); return 0; } 

测试结果:
在这里插入图片描述

  1. int system(const char *string)
    函数原型 int system(const char *string)
    /函数功能: 由 string 指定的命令传给要被命令处理器执行的主机环境。
//时间:2019年11月15日 //作者:Kroner //编译环境: VS 2019 //库函数 stdlib.h //函数原型 int system(const char *string) //函数功能: 由 string 指定的命令传给要被命令处理器执行的主机环境。 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> #include<stdlib.h> int main() { 
    char command[50]; strcpy(command, "dir"); system(command); return 0; } 

测试结果:在windows 系统中显示的结果,相当于win+r运行cmd后再输入栏中输入dir的结果
在这里插入图片描述

  1. void *bsearch(const void *key, const void *base, size_t nitems,
    size_t size, int (*compar)(const void *, const void *))

函数原型 void *bsearch(const void *key, const void *base, size_t nitems, size_t size, int (*compar)(const void , const void ))
key – 指向要查找的元素的指针,类型转换为 void

base – 指向进行查找的数组的第一个对象的指针,类型转换为 void

nitems – base 所指向的数组中元素的个数。
size – 数组中每个元素的大小,以字节为单位。
compar – 用来比较两个元素的函数。
函数功能: 执行二分查找。

讯享网//时间:2019年11月15日 //作者:Kroner //编译环境: VS 2019 //库函数 stdlib.h //函数原型 void *bsearch(const void *key, const void *base, size_t nitems, size_t size, int (*compar)(const void *, const void *)) // key -- 指向要查找的元素的指针,类型转换为 void* 。 // base -- 指向进行查找的数组的第一个对象的指针,类型转换为 void* 。 // nitems -- base 所指向的数组中元素的个数。 // size -- 数组中每个元素的大小,以字节为单位。 // compar -- 用来比较两个元素的函数。 //函数功能: 执行二分查找。 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> int cmpfunc(const void* a, const void* b) { 
    return (*(int*)a - *(int*)b); } int values[] = { 
    5, 20, 29, 32, 63 }; int main() { 
    int* item; int key = 20; /* 使用 bsearch() 在数组中查找值 32 */ item = (int*)bsearch(&key, values, 5, sizeof(int), cmpfunc); if (item != NULL) { 
    printf("Found item = %d\n", *item); } else { 
    printf("Item = %d could not be found\n", *item); } return(0); } 

测试结果:这个相当于C语言封装的一个二分算法。后缀讲解算法题目的时候在讲解。
在这里插入图片描述

  1. void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void , const void))
    函数原型 void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void , const void))
    base – 指向要排序的数组的第一个元素的指针。
    nitems – 由 base 指向的数组中元素的个数。
    size – 数组中每个元素的大小,以字节为单位。
    compar – 用来比较两个元素的函数。
    函数功能: 数组排序
//时间:2019年11月15日 //作者:Kroner //编译环境: VS 2019 //库函数 stdlib.h //函数原型 void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*)) // base -- 指向要排序的数组的第一个元素的指针。 // nitems -- 由 base 指向的数组中元素的个数。 //size -- 数组中每个元素的大小,以字节为单位。 //compar -- 用来比较两个元素的函数。 //函数功能: 数组排序 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> int values[] = { 
    88, 56, 100, 2, 25 }; int cmpfunc(const void* a, const void* b) { 
    return (*(int*)a - *(int*)b); } int main() { 
    int n; printf("排序之前的列表:\n"); for (n = 0; n < 5; n++) { 
    printf("%d ", values[n]); } qsort(values, sizeof(values)/sizeof(int), sizeof(int), cmpfunc); printf("\n排序之后的列表:\n"); for (n = 0; n < 5; n++) { 
    printf("%d ", values[n]); } return 0; } 

测试结果:若是要从大到小排列只需要cmpfunc的返回值前加个一个符号就可以。
在这里插入图片描述

  1. int abs(int x)
    函数原型 int abs(int x)
    函数功能: 返回 x 的绝对值。
讯享网//时间:2019年11月15日 //作者:Kroner //编译环境: VS 2019 //库函数 stdlib.h //函数原型 int abs(int x) //函数功能: 返回 x 的绝对值。 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> int main() { 
    int a, b; a = abs(5); printf("a 的值 = %d\n", a); b = abs(-10); printf("b 的值 = %d\n", b); return 0; } 

测试结果:
在这里插入图片描述

  1. div_t div(int numer, int denom)
    函数原型 div_t div(int numer, int denom)
    函数功能: 分子除以分母。
//时间:2019年11月15日 //作者:Kroner //编译环境: VS 2019 //库函数 stdlib.h //函数原型: div_t div(int numer, int denom) //函数功能: 分子除以分母。 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> int main() { 
    div_t output; output = div(27, 4); printf("(27/ 4) 的商 = %d\n", output.quot); printf("(27/4) 的余数 = %d\n", output.rem); output = div(27, 3); printf("(27/ 3) 的商 = %d\n", output.quot); printf("(27/3) 的余数 = %d\n", output.rem); return 0; } 

测试结果:
在这里插入图片描述

  1. int rand(void)
  2. void srand(unsigned int seed)
    函数原型: int rand(void)
    函数原型: void srand(unsigned int seed)
    函数功能: 返回一个范围在 0 到 RAND_MAX 之间的伪随机数。
    函数功能: 该函数播种由函数 rand 使用的随机数发生器。
讯享网//时间:2019年11月15日 //作者:Kroner //编译环境: VS 2019 //库函数 stdlib.h //函数原型: int rand(void) //函数原型 void srand(unsigned int seed) //函数功能: 返回一个范围在 0 到 RAND_MAX 之间的伪随机数。 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { 
    int i, n; time_t t; n = 5; /* 初始化随机数发生器 以CPU的时间作为随机种子所以是伪随机数*/ srand((unsigned)time(&t)); /* 输出 0 到 49 之间的 5 个随机数 */ for (i = 0; i < n; i++) { 
    printf("%d\n", rand() % 50); } return 0; } 

测试结果:
在这里插入图片描述

小讯
上一篇 2025-02-06 08:11
下一篇 2025-04-05 22:21

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/36887.html