当前位置: 首页 > news >正文

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

相关文章:

C 标准库 - <stdlib.h>

简介 <stdlib.h> 头文件定义了四个变量类型、一些宏和各种通用工具函数。 库变量 下面是头文件 stdlib.h 中定义的变量类型&#xff1a; 序号变量 & 描述1size_t2wchar_t3div_t4ldiv_t 库宏 下面是头文件 stdlib.h 中定义的宏&#xff1a; 序号宏 & 描述1…...

Python中回调函数的理解与应用

前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到网站零基础入门的AI学习网站~。 目录 前言 回调函数的概念 回调函数的基本用法 回调函数的实现方式 1 使用函数 2 使用类方法 3 使用类实…...

抖音数据挖掘软件|视频内容提取

针对用户获取抖音视频的需求&#xff0c;我们开发了一款功能强大的工具&#xff0c;旨在解决用户在获取抖音视频时需要逐个复制链接、下载的繁琐问题。我们希望用户能够通过简单的关键词搜索&#xff0c;实现自动批量抓取视频&#xff0c;并根据需要进行选择性批量下载。因此&a…...

PostgreSQL如何使用UUID

离线安装时&#xff0c;一般有四个包&#xff0c;都安装的话&#xff0c;只需要开启uuid的使用即可&#xff0c;如果工具包(即 postgresql11-contrib&#xff09;没有安装的话&#xff0c;需要单独安装一次&#xff0c;再进行开启。 开启UUID方法 下面介绍一下如何开启&#…...

网络原理 - HTTP/HTTPS(4)

HTTP响应详解 认识"状态码"(status code) 状态码表示访问一个页面的结果.(是访问成功,还是失败,还是其它的一些情况...).(响应结果如何) 学习状态码 -> 为了调试问题. 写服务器时,按照状态码的含义正确使用. 200 OK 这是最常见的状态码,表示访问成功. 抓包抓…...

Vue+SpringBoot打造在线课程教学系统

目录 一、摘要1.1 系统介绍1.2 项目录屏 二、研究内容2.1 课程类型管理模块2.2 课程管理模块2.3 课时管理模块2.4 课程交互模块2.5 系统基础模块 三、系统设计3.1 用例设计3.2 数据库设计 四、系统展示4.1 管理后台4.2 用户网页 五、样例代码5.1 新增课程类型5.2 网站登录5.3 课…...

数据存储-文件存储

一、CSV文件存储 csv是python的标准库 列表数据写入csv文件 import csvheader [班级, 姓名, 性别, 手机号, QQ] # 二维数组 rows [[学习一班, 大娃, 男, a130111111122, 987456123],[学习二班, 二娃, 女, a130111111123, 987456155],[学习三班, 三娃, 男, a130111111124, …...

【Activiti7】全新Activiti7工作流讲解

一、Activiti7概述 官网地址:https://www.activiti.org/ Activiti由Alfresco软件开发,目前最高版本Activiti 7。是BPMN的一个基于java的软件实现,不过 Activiti 不仅仅包括BPMN,还有DMN决策表和CMMN Case管理引擎,并且有自己的用户管理、微 服务API 等一系列功能,是一…...

C++ 学习(1)---- 左值 右值和右值引用

这里写目录标题 左值右值左值引用和右值引用右值引用和移动构造函数std::move 移动语义返回值优化移动操作要保证安全 万能引用std::forward 完美转发传入左值传入右值 左值 左值是指可以使用 & 符号获取到内存地址的表达式&#xff0c;一般出现在赋值语句的左边&#xff…...

Redis能保证数据不丢失吗?

引言 大家即使没用过Redis&#xff0c;也应该都听说过Redis的威名。 Redis是一种Nosql类型的数据存储&#xff0c;全称Remote Dictionary Server&#xff0c;也就是远程字典服务器&#xff0c;用过Dictionary的应该都知道它是一种键值对&#xff08;Key-Value&#xff09;的数…...

C++基础知识(六:继承)

首先我们应该知道C的三大特性就是封装、继承和多态。 此篇文章将详细的讲解继承的作用和使用方法。 继承 一个类&#xff0c;继承另一个已有的类&#xff0c;创建的过程 父类(基类)派生出子类(派生类)的过程 继承提高了代码的复用性 【1】继承的格式 class 类名:父类名 {}; 【…...

RM电控讲义【HAL库篇】(二)

8080并口模式是一种常见的计算机接口模式&#xff0c;主要用于LCD&#xff08;液晶显示屏&#xff09;模块。 在8080并口模式中&#xff0c;通信端口包括多种信号线&#xff0c;用于实现数据的读写和控制功能。主要的信号线包括&#xff1a; CS&#xff08;片选信号&#xff…...

Mac安装Appium

一、环境依赖 一、JDK环境二、Android-SDK环境&#xff08;android自动化&#xff09;三、Homebrew环境四、Nodejs 安装cnpm 五、安装appium六、安装appium-doctor来确认安装环境是否完成七、安装相关依赖 二、重头大戏&#xff0c; 配置wda&#xff08;WebDriverAgent&#x…...

数据库管理-第153期 Oracle Vector DB AI-05(20240221)

数据库管理153期 2024-02-21 数据库管理-第153期 Oracle Vector DB & AI-05&#xff08;20240221&#xff09;1 Oracle Vector的其他特性示例1&#xff1a;示例2 2 简单使用Oracle Vector环境创建包含Vector数据类型的表插入向量数据 总结 数据库管理-第153期 Oracle Vecto…...

通过傅里叶变换进行音频变声变调

文章目录 常见音频变声算法使用Wav库读写音频文件使用pitchShift算法进行音频变调主文件完整代码工程下载地址常见音频变声算法 在游戏或者一些特殊场景下为了提高娱乐性或者保护声音的特征,我们会对音频进行变声变调处理。常用的算法包括: 1.基于傅里叶变换的频域算法,该类…...

Opencv(C++)学习 ARM上引用opencv报相关头文件找不到

简单问题记录&#xff0c;C 与C互相引用时应该多注意类似问题。 问题描述&#xff1a;在项目中&#xff0c;建立了一个interface.h提供了一个C语言兼容的接口void work()&#xff0c;并在对应的interface.cpp中使用OpenCV完成相关处理实现。在PC端测试时&#xff0c;main.cpp成…...

中国服装行业ERP的现状与未来发展

随着全球数字化浪潮的兴起&#xff0c;中国服装行业也在不断探索数字化转型的路径&#xff0c;其中ERP&#xff08;企业资源计划&#xff09;系统作为管理和优化企业资源的重要工具&#xff0c;在服装行业中发挥着日益重要的作用。本文将探讨中国服装行业ERP的现状、作用&#…...

Unix与Linux区别

目录 历史和所有权 内核 发行版 开源性质 用户群体 命令行界面 历史和所有权 Unix&#xff1a; Unix是一个操作系统家族的名称&#xff0c;最早由贝尔实验室&#xff08;Bell Labs&#xff09;的肖像电机公司&#xff08;AT&T&#xff09;开发。最早的Unix版本是在19…...

惠尔顿 网络安全审计系统 任意文件读取漏洞复现

0x01 产品简介 惠尔顿网络安全审计产品致力于满足军工四证、军工保密室建设、国家涉密网络建设的审计要求&#xff0c;规范网络行为&#xff0c;满足国家的规范&#xff1b;支持1-3线路的internet接入、1-3对网桥&#xff1b;含强大的上网行为管理、审计、监控模块&#xff1b…...

Chrome插件(二)—Hello World!

本小节将指导你从头到尾创建一个基本的Chrome插件&#xff0c;你可以认为是chrome插件开发的“hello world”&#xff01; 以下详细描述了各个步骤&#xff1a; 第一步&#xff1a;设置开发环境 确保你拥有以下工具&#xff1a; 文本编辑器&#xff1a;如Visual Studio Cod…...

中南大学无人机智能体的全面评估!BEDI:用于评估无人机上具身智能体的综合性基准测试

作者&#xff1a;Mingning Guo, Mengwei Wu, Jiarun He, Shaoxian Li, Haifeng Li, Chao Tao单位&#xff1a;中南大学地球科学与信息物理学院论文标题&#xff1a;BEDI: A Comprehensive Benchmark for Evaluating Embodied Agents on UAVs论文链接&#xff1a;https://arxiv.…...

uni-app学习笔记二十二---使用vite.config.js全局导入常用依赖

在前面的练习中&#xff0c;每个页面需要使用ref&#xff0c;onShow等生命周期钩子函数时都需要像下面这样导入 import {onMounted, ref} from "vue" 如果不想每个页面都导入&#xff0c;需要使用node.js命令npm安装unplugin-auto-import npm install unplugin-au…...

AtCoder 第409​场初级竞赛 A~E题解

A Conflict 【题目链接】 原题链接&#xff1a;A - Conflict 【考点】 枚举 【题目大意】 找到是否有两人都想要的物品。 【解析】 遍历两端字符串&#xff0c;只有在同时为 o 时输出 Yes 并结束程序&#xff0c;否则输出 No。 【难度】 GESP三级 【代码参考】 #i…...

C++ 求圆面积的程序(Program to find area of a circle)

给定半径r&#xff0c;求圆的面积。圆的面积应精确到小数点后5位。 例子&#xff1a; 输入&#xff1a;r 5 输出&#xff1a;78.53982 解释&#xff1a;由于面积 PI * r * r 3.14159265358979323846 * 5 * 5 78.53982&#xff0c;因为我们只保留小数点后 5 位数字。 输…...

20个超级好用的 CSS 动画库

分享 20 个最佳 CSS 动画库。 它们中的大多数将生成纯 CSS 代码&#xff0c;而不需要任何外部库。 1.Animate.css 一个开箱即用型的跨浏览器动画库&#xff0c;可供你在项目中使用。 2.Magic Animations CSS3 一组简单的动画&#xff0c;可以包含在你的网页或应用项目中。 3.An…...

在Mathematica中实现Newton-Raphson迭代的收敛时间算法(一般三次多项式)

考察一般的三次多项式&#xff0c;以r为参数&#xff1a; p[z_, r_] : z^3 (r - 1) z - r; roots[r_] : z /. Solve[p[z, r] 0, z]&#xff1b; 此多项式的根为&#xff1a; 尽管看起来这个多项式是特殊的&#xff0c;其实一般的三次多项式都是可以通过线性变换化为这个形式…...

R 语言科研绘图第 55 期 --- 网络图-聚类

在发表科研论文的过程中&#xff0c;科研绘图是必不可少的&#xff0c;一张好看的图形会是文章很大的加分项。 为了便于使用&#xff0c;本系列文章介绍的所有绘图都已收录到了 sciRplot 项目中&#xff0c;获取方式&#xff1a; R 语言科研绘图模板 --- sciRplothttps://mp.…...

毫米波雷达基础理论(3D+4D)

3D、4D毫米波雷达基础知识及厂商选型 PreView : https://mp.weixin.qq.com/s/bQkju4r6med7I3TBGJI_bQ 1. FMCW毫米波雷达基础知识 主要参考博文&#xff1a; 一文入门汽车毫米波雷达基本原理 &#xff1a;https://mp.weixin.qq.com/s/_EN7A5lKcz2Eh8dLnjE19w 毫米波雷达基础…...

破解路内监管盲区:免布线低位视频桩重塑停车管理新标准

城市路内停车管理常因行道树遮挡、高位设备盲区等问题&#xff0c;导致车牌识别率低、逃费率高&#xff0c;传统模式在复杂路段束手无策。免布线低位视频桩凭借超低视角部署与智能算法&#xff0c;正成为破局关键。该设备安装于车位侧方0.5-0.7米高度&#xff0c;直接规避树枝遮…...

二维FDTD算法仿真

二维FDTD算法仿真&#xff0c;并带完全匹配层&#xff0c;输入波形为高斯波、平面波 FDTD_二维/FDTD.zip , 6075 FDTD_二维/FDTD_31.m , 1029 FDTD_二维/FDTD_32.m , 2806 FDTD_二维/FDTD_33.m , 3782 FDTD_二维/FDTD_34.m , 4182 FDTD_二维/FDTD_35.m , 4793...