C 标准库 - <stdlib.h>

简介

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

库变量

下面是头文件 stdlib.h 中定义的变量类型:

序号变量 & 描述
1size_t
2wchar_t
3div_t
4ldiv_t

库宏

下面是头文件 stdlib.h 中定义的宏:

序号宏 & 描述
1NULL
2EXIT_FAILURE
3EXIT_SUCCESS
4RAND_MAX
5MB_CUR_MAX

库函数

下面是头文件 stdlib.h 中定义的函数:

1. double atof(const char *str)

把参数 str 所指向的字符串转换为一个浮点数(类型为 double 型)。

#include <stdlib.h>
#include <stdio.h>int main() {const char *str = "3.14";double value = atof(str);printf("The converted value is: %lf\n", value);return 0;
}

2. int atoi(const char *str)

把参数 str 所指向的字符串转换为一个整数(类型为 int 型)。

#include <stdlib.h>
#include <stdio.h>int main() {const char *str = "12345";int value = atoi(str);printf("The converted value is: %d\n", value);return 0;
}

3. long int atol(const char *str)

把参数 str 所指向的字符串转换为一个长整数(类型为 long int 型)。

#include <stdlib.h>
#include <stdio.h>int main() {const char *str = "987654321";long int value = atol(str);printf("The converted value is: %ld\n", value);return 0;
}

4. double strtod(const char *str, char **endptr)

把参数 str 所指向的字符串转换为一个浮点数(类型为 double 型)。

#include <stdlib.h>
#include <stdio.h>int main() {const char *str = "3.14159 This is a string";char *endptr;double value = strtod(str, &endptr);printf("The converted value is: %lf\n", value);printf("The remaining string is: %s\n", endptr);return 0;
}

5. long int strtol(const char *str, char **endptr, int base)

把参数 str 所指向的字符串转换为一个长整数(类型为 long int 型)。

#include <stdlib.h>
#include <stdio.h>int main() {const char *str = "12345 This is a string";char *endptr;long int value = strtol(str, &endptr, 10);printf("The converted value is: %ld\n", value);printf("The remaining string is: %s\n", endptr);return 0;
}

6. unsigned long int strtoul(const char *str, char **endptr, int base)

把参数 str 所指向的字符串转换为一个无符号长整数(类型为 unsigned long int 型)。

#include <stdlib.h>
#include <stdio.h>int main() {const char *str = "12345 This is a string";char *endptr;unsigned long int value = strtoul(str, &endptr, 10);printf("The converted value is: %lu\n", value);printf("The remaining string is: %s\n", endptr);return 0;
}

7. void *calloc(size_t nitems, size_t size)

分配所需的内存空间,并返回一个指向它的指针。

#include <stdlib.h>int main() {int *ptr;ptr = (int *)calloc(5, sizeof(int));free(ptr);return 0;
}

8. void free(void *ptr)

释放之前调用 callocmallocrealloc 所分配的内存空间。

#include <stdlib.h>int main() {int *ptr;ptr = (int *)malloc(5 * sizeof(int));free(ptr);return 0;
}

9. void *malloc(size_t size)

分配所需的内存空间,并返回一个指向它的指针。

#include <stdlib.h>int main() {int *ptr;ptr = (int *)malloc(5 * sizeof(int));free(ptr);return 0;
}

10. void *realloc(void *ptr, size_t size)

尝试重新调整之前调用 malloccalloc 所分配的 ptr 所指向的内存块的大小。

#include <stdlib.h>int main() {int *ptr;ptr = (int *)malloc(5 * sizeof(int));ptr = (int *)realloc(ptr, 10 * sizeof(int));free(ptr);return 0;
}

11. void abort(void)

使一个异常程序终止。

#include <stdlib.h>int main() {abort();return 0;
}

12. int atexit(void (*func)(void))

当程序正常终止时,调用指定的函数 func

#include <stdlib.h>
#include <stdio.h>void cleanup_function() {printf("Exiting program...\n");
}int main() {atexit(cleanup_function);return 0;
}

13. void exit(int status)

使程序正常终止。

#include <stdlib.h>int main() {exit(0);return 0;
}

14. char *getenv(const char *name)

搜索 name 所指向的环境字符串,并返回相关的值给字符串。

#include <stdlib.h>
#include <stdio.h>int main() {const char *value = getenv("HOME");printf("Home directory: %s\n", value);return 0;
}

15. int system(const char *string)

string 指定的命令传给要被命令处理器执行的主机环境。

#include <stdlib.h>int main() {system("ls -l");return 0;
}

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

执行二分查找。

#include <stdlib.h>
#include <stdio.h>int compare(const void *a, const void *b) {return (*(int *)a - *(int *)b);
}int main() {int values[] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91};int key = 23;int *result = (int *)bsearch(&key, values, 10, sizeof(int), compare);if (result != NULL)printf("Value %d found in the array.\n", *result);elseprintf("Value not found in the array.\n");return 0;
}

17. void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*))

数组排序。

#include <stdlib.h>
#include <stdio.h>int compare(const void *a, const void *b) {return (*(int *)a - *(int *)b);
}int main() {int values[] = {42, 10, 6, 88, 15};int n = sizeof(values) / sizeof(values[0]);qsort(values, n, sizeof(int), compare);for (int i = 0; i < n; ++i) {printf("%d ", values[i]);}printf("\n");return 0;
}

18. int abs(int x)

返回 x 的绝对值。

#include <stdlib.h>
#include <stdio.h>int main() {int x = -5;int abs_value = abs(x);printf("The absolute value of %d is: %d\n", x, abs_value);return 0;
}

19. div_t div(int numer, int denom)

分子除以分母。

#include <stdlib.h>
#include <stdio.h>int main() {div_t result = div(10, 3);printf("Quotient: %d, Remainder: %d\n", result.quot, result.rem);return 0;
}

20. long int labs(long int x)

返回 x 的绝对值。

#include <stdlib.h>
#include <stdio.h>int main() {long int x = -123456;long int abs_value = labs(x);printf("The absolute value of %ld is: %ld\n", x, abs_value);return 0;
}

21. ldiv_t ldiv(long int numer, long int denom)

分子除以分母。

#include <stdlib.h>
#include <stdio.h>int main() {ldiv_t result = ldiv(100, 25);printf("Quotient: %ld, Remainder: %ld\n", result.quot, result.rem);return 0;
}

22. int rand(void)

返回一个范围在 0 到 RAND_MAX 之间的伪随机数。

#include <stdlib.h>
#include <stdio.h>int main() {int random_value = rand();printf("Random value: %d\n", random_value);return 0;
}

23. void srand(unsigned int seed)

该函数播种由函数 rand 使用的随机数发生器。

#include <stdlib.h>
#include <stdio.h>
#include <time.h>int main() {srand(time(NULL));int random_value = rand();printf("Random value: %d\n", random_value);return 0;
}

24. int mblen(const char *str, size_t n)

返回参数 str 所指向的多字节字符的长度。

#include <stdlib.h>
#include <stdio.h>int main() {const char *str = "A";int length = mblen(str, MB_CUR_MAX);printf("Character length: %d\n", length);return 0;
}

25. size_t mbstowcs(schar_t *pwcs, const char *str, size_t n)

把参数 str 所指向的多字节字符的字符串转换为参数 pwcs 所指向的数组。

#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>int main() {const char *str = "AB";wchar_t pwcs[10];size_t result = mbstowcs(pwcs, str, 10);wprintf(L"Converted string: %ls\n", pwcs);printf("Number of wide characters: %zu\n", result);return 0;
}

26. int mbtowc(wchar_t *pwc, const char *str, size_t n)

检查参数 str 所指向的多字节字符。

#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>int main() {const char *str = "A";wchar_t pwc;int result = mbtowc(&pwc, str, MB_CUR_MAX);if (result > 0) {wprintf(L"Character: %lc\n", pwc);} else if (result == 0) {printf("Null character detected.\n");} else {printf("Invalid multibyte character.\n");}return 0;
}

27. size_t wcstombs(char *str, const wchar_t *pwcs, size_t n)

把数组 pwcs 中存储的编码转换为多字节字符,并把它们存储在字符串 str 中。

#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>int main() {const wchar_t pwcs[] = {L'A', L'B', L'\0'};char str[10];size_t result = wcstombs(str, pwcs, 10);printf("Converted string: %s\n", str);printf("Number of bytes: %zu\n", result);return 0;
}

28. int wctomb(char *str, wchar_t wchar)

检查对应于参数 wchar 所给出的多字节字符的编码。

#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>int main() {wchar_t wchar = L'A';char str[MB_CUR_MAX];int result = wctomb(str, wchar);if (result > 0) {printf("Multibyte character: %s\n", str);} else {printf("Invalid wide character.\n");}return 0;
}

以上是 stdlib.h 中定义的所有函数的详细介绍和示例。该头文件提供了一系列有用的工具函数,能够帮助程序员进行内存分配、随机数生成、字符串转换等操作。熟练掌握这些函数将对编程工作大有裨益。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/701962.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

conntrack-tools 内核依赖,

1 内核依赖 You require a Linux kernel version > 2.6.18. Connection Tracking System. CONFIG_NF_CONNTRACKm CONFIG_NF_CONNTRACK_IPV4m CONFIG_NF_CONNTRACK_IPV6m (if your setup supports IPv6) nfnetlink: the generic messaging interface for Netfilter. CONF…

【深度学习:标记数据】为医生标记数据缓解疼痛

【深度学习&#xff1a;标记数据】为医生标记数据缓解疼痛 问题实验结果结论 我开始在物理学方面进行学术研究&#xff0c;但在第一年就退学了&#xff08;抱歉&#xff0c;休学了&#xff09;我的博士学位&#xff0c;并在定量金融领域做了很长一段时间。因此&#xff0c;在我…

宝塔面板安装了mysql5.7和phpMyadmin,但是访问phpMyadmin时提示502 Bad Gateway

操作流程截图如下&#xff1a; 原因是没有选择php版本 选择php版本 下一页找到phpMyAdmin&#xff0c;选择设置 目前只有纯净态&#xff0c;说明没有php环境&#xff0c;前去安装php环境 点击安装&#xff0c;选择版本&#xff0c;这里选择的是7.4版本&#xff0c;编译安…

创建者模式(Builder Pattern):构造复杂对象的通用解决方案

文章目录 **一、技术背景与应用场景****为何使用创建者模式&#xff1f;****典型应用场景包括但不限于&#xff1a;** **二、创建者模式定义与结构****三、使用步骤举例**四、优缺点分析总结 一、技术背景与应用场景 创建者模式是一种对象创建型设计模式&#xff0c;它通过将复…

Apache Doris 发展历程、技术特性及云原生时代的未来规划

文章目录 每日一句正能量前言作者介绍Apache Doris 特性极简架构高效自运维高并发场景支持MPP 执行引擎明细与聚合模型的统一便捷数据接入Apache Doris 极速 1.0 时代极速列式内存布局向量化的计算框架Cache 亲和度虚函数调用SIMD 指令集 稳定多源基于云原生向量数据库Milvus 的…

基于ZYNQ的PCIE高速数据采集卡的设计(三)硬件设计

采集卡硬件设计 3.1 引言 采集卡的硬件设计是实现采集功能的基础&#xff0c;良好的硬件设计可以使采集功能更容 易实现&#xff0c;方便软件开发。本章基于第二章的硬件设计方案来详细介绍采集卡硬件设计。 包括载卡和子卡的芯片的选型、配置和具体电路的设计。载卡和子卡…

蓝桥杯《修剪灌木》

题目描述 爱丽丝要完成一项修剪灌木的工作。有 N 棵灌木整齐的从左到右排成一排。爱丽丝在每天傍晚会修剪一棵灌木&#xff0c;让灌木的高度变为 0 厘米。爱丽丝修剪灌木的顺序是从最左侧的灌木开始&#xff0c;每天向右修剪一棵灌木。当修剪了最右侧的灌木后&#xff0c;她会…

【程序员必备技能】Git入门

目录 &#x1f308;前言&#x1f308; &#x1f4c1; Git的概念 &#x1f4c2; 版本控制 &#x1f4c2; 集中式 和 分布式 ​ &#x1f4c1; 创建和配置本地仓库 &#x1f4c1; 理解工作区&#xff0c;暂存区&#xff0c;版本库 &#x1f4c1; Git的基本操作 &#x1f4c2;…

JMeter实现接口自动化测试

一、JMETER的环境搭建 参考&#xff1a;https://www.cnblogs.com/qmfsun/p/4902534.html 二、JMETER的汉化 临时汉化方法&#xff1a;打开jmeter&#xff0c;options-->choose language-->选择语言 可以根据自己的需要选择简体中文或者繁体中文&#xff0c;如图&#xf…

字符串(算法竞赛)--字典树Trie与最大异或对

1、B站视频链接&#xff1a;F06 字典树(Trie)_哔哩哔哩_bilibili 题目链接&#xff1a;【模板】字典树 - 洛谷 #include <bits/stdc.h> using namespace std; const int N100010; int n; char s[N]; int ch[N][26];//ch[0][2]1表示0号节点通过c边走到了节点1 int cnt[…

【电子通识】认识FMEA(失效模式和影响分析)

FMEA是Failure Mode and Effect Analysis的英文缩写&#xff0c;中文名称为失效模式和影响分析。主要应用于航空航天、食品、汽车和核电等行业。 FMEA讨论的是事先策划以及执行措施&#xff0c;预防问题的发生或控制问题的发展&#xff0c;降低设计和过程的风险。由于问题还没…

基于java Springboot实现教务管理系统

基于java Springboot实现教务管理系统《视频版-建议收藏》 博主介绍&#xff1a;5年java开发经验&#xff0c;专注Java开发、定制、远程、文档编写指导等,csdn特邀作者、专注于Java技术领域 作者主页 央顺技术团队 Java毕设项目精品实战案例《1000套》 欢迎点赞 收藏 ⭐留言 文…

亲孙子和外孙女真的不一样吗

对老人来说&#xff0c;带孩子的性价比&#xff0c;孙子≥孙女≥外孙女&#xff1e;外孙。 没错&#xff0c;外孙是最差的选择。以上几个&#xff0c;出“白眼狼”概率最大的&#xff0c;是外孙。 因为男性天然家族意识强而血缘意识弱&#xff0c;女性则血缘意识强家族意识弱…

回归预测 | Matlab实现CPO-HKELM冠豪猪算法优化混合核极限学习机多变量回归预测

回归预测 | Matlab实现CPO-HKELM冠豪猪算法优化混合核极限学习机多变量回归预测 目录 回归预测 | Matlab实现CPO-HKELM冠豪猪算法优化混合核极限学习机多变量回归预测效果一览基本介绍程序设计参考资料 效果一览 基本介绍 1.Matlab实现CPO-HKELM冠豪猪算法优化混合核极限学习机…

C语言------操作符的巧妙使用

1.计算一个数字二进制补码里面1的个数 &#xff08;1&#xff09;方法一 根据这个10进制的整数&#xff0c;对这个数进行%10&#xff0c;/10不断地进行下去&#xff0c; %10得到最后一位&#xff0c;/10得到舍去最后一位之后剩余的数&#xff1b; 同理得到&#xff1a;二进…

Kubernetes(K8s)的一些重要概念以及术语简短解释

前言 温故而知新&#xff0c;不学就容易忘&#xff0c;本文将迅速介绍k8s的一些关键概念&#xff0c;供随时回顾 正文 Pod&#xff1a;Pod是Kubernetes中最小的部署单位&#xff0c;通常一个Pod内运行一个容器应用。Pod封装了容器&#xff08;可能是多个&#xff09;&#xf…

09 呼吸灯

呼吸灯简介 呼吸灯实际展示的效果就是一个 LED 灯的亮度由亮到暗&#xff0c;再由暗到亮的变化过程&#xff0c;并且该过程是循环往复的&#xff0c;像呼吸一样那么有节奏。 呼吸灯通常是采用 PWM(Pulse Width Modulation&#xff0c;即脉冲宽度调制) 的方式实现&#xff0c;在…

计算机视觉初探--LeNet原理与实践

LeNet&#xff1a;深度学习图像识别的里程碑 LeNet是卷积神经网络&#xff08;Convolutional Neural Network, CNN&#xff09;领域的先驱模型&#xff0c;由Yann LeCun等人在1998年提出&#xff0c;被广泛应用于手写数字识别和其他计算机视觉任务。本文将介绍LeNet模型的数学…

随想录刷题笔记 —二叉树篇11 538二叉搜索树转换为累加树 77组合

538二叉搜索树转换为累加树 递归&#xff1a;使用pronode标记仅小于该节点的节点&#xff0c;使用右中左的顺序 根据pronode值修改节点值 class Solution {TreeNode pronode null;public TreeNode convertBST(TreeNode root) {if (rootnull){return root;}if (root.right!nu…

深入了解计算机系统——1.计算机系统初步

文章目录 计算机系统信息是什么我们来看看hello在哪 程序被其他程序翻译成不同的格式处理器读取并解释内存中的指令硬件组成 计算机系统 信息是什么 信息是位上下文 下面写一段程序 ...(省略&#xff09; printf("hello world"); ... hello.c上面是一段hello程序…