当前位置: 首页 > 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…...

多场景 OkHttpClient 管理器 - Android 网络通信解决方案

下面是一个完整的 Android 实现&#xff0c;展示如何创建和管理多个 OkHttpClient 实例&#xff0c;分别用于长连接、普通 HTTP 请求和文件下载场景。 <?xml version"1.0" encoding"utf-8"?> <LinearLayout xmlns:android"http://schemas…...

PPT|230页| 制造集团企业供应链端到端的数字化解决方案:从需求到结算的全链路业务闭环构建

制造业采购供应链管理是企业运营的核心环节&#xff0c;供应链协同管理在供应链上下游企业之间建立紧密的合作关系&#xff0c;通过信息共享、资源整合、业务协同等方式&#xff0c;实现供应链的全面管理和优化&#xff0c;提高供应链的效率和透明度&#xff0c;降低供应链的成…...

【大模型RAG】Docker 一键部署 Milvus 完整攻略

本文概要 Milvus 2.5 Stand-alone 版可通过 Docker 在几分钟内完成安装&#xff1b;只需暴露 19530&#xff08;gRPC&#xff09;与 9091&#xff08;HTTP/WebUI&#xff09;两个端口&#xff0c;即可让本地电脑通过 PyMilvus 或浏览器访问远程 Linux 服务器上的 Milvus。下面…...

React Native在HarmonyOS 5.0阅读类应用开发中的实践

一、技术选型背景 随着HarmonyOS 5.0对Web兼容层的增强&#xff0c;React Native作为跨平台框架可通过重新编译ArkTS组件实现85%以上的代码复用率。阅读类应用具有UI复杂度低、数据流清晰的特点。 二、核心实现方案 1. 环境配置 &#xff08;1&#xff09;使用React Native…...

Oracle11g安装包

Oracle 11g安装包 适用于windows系统&#xff0c;64位 下载路径 oracle 11g 安装包...

redis和redission的区别

Redis 和 Redisson 是两个密切相关但又本质不同的技术&#xff0c;它们扮演着完全不同的角色&#xff1a; Redis: 内存数据库/数据结构存储 本质&#xff1a; 它是一个开源的、高性能的、基于内存的 键值存储数据库。它也可以将数据持久化到磁盘。 核心功能&#xff1a; 提供丰…...

阿里云Ubuntu 22.04 64位搭建Flask流程(亲测)

cd /home 进入home盘 安装虚拟环境&#xff1a; 1、安装virtualenv pip install virtualenv 2.创建新的虚拟环境&#xff1a; virtualenv myenv 3、激活虚拟环境&#xff08;激活环境可以在当前环境下安装包&#xff09; source myenv/bin/activate 此时&#xff0c;终端…...

32单片机——基本定时器

STM32F103有众多的定时器&#xff0c;其中包括2个基本定时器&#xff08;TIM6和TIM7&#xff09;、4个通用定时器&#xff08;TIM2~TIM5&#xff09;、2个高级控制定时器&#xff08;TIM1和TIM8&#xff09;&#xff0c;这些定时器彼此完全独立&#xff0c;不共享任何资源 1、定…...

Python第七周作业

Python第七周作业 文章目录 Python第七周作业 1.使用open以只读模式打开文件data.txt&#xff0c;并逐行打印内容 2.使用pathlib模块获取当前脚本的绝对路径&#xff0c;并创建logs目录&#xff08;若不存在&#xff09; 3.递归遍历目录data&#xff0c;输出所有.csv文件的路径…...

【Linux应用】Linux系统日志上报服务,以及thttpd的配置、发送函数

【Linux应用】Linux系统日志上报服务&#xff0c;以及thttpd的配置、发送函数 文章目录 thttpd服务安装thttpd配置thttpd服务thttpd函数日志效果和文件附录&#xff1a;开发板快速上手&#xff1a;镜像烧录、串口shell、外设挂载、WiFi配置、SSH连接、文件交互&#xff08;RADX…...