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

利用ngx_stream_return_module构建简易 TCP/UDP 响应网关

一、模块概述 ngx_stream_return_module 提供了一个极简的指令&#xff1a; return <value>;在收到客户端连接后&#xff0c;立即将 <value> 写回并关闭连接。<value> 支持内嵌文本和内置变量&#xff08;如 $time_iso8601、$remote_addr 等&#xff09;&a…...

k8s从入门到放弃之Ingress七层负载

k8s从入门到放弃之Ingress七层负载 在Kubernetes&#xff08;简称K8s&#xff09;中&#xff0c;Ingress是一个API对象&#xff0c;它允许你定义如何从集群外部访问集群内部的服务。Ingress可以提供负载均衡、SSL终结和基于名称的虚拟主机等功能。通过Ingress&#xff0c;你可…...

Spring AI 入门:Java 开发者的生成式 AI 实践之路

一、Spring AI 简介 在人工智能技术快速迭代的今天&#xff0c;Spring AI 作为 Spring 生态系统的新生力量&#xff0c;正在成为 Java 开发者拥抱生成式 AI 的最佳选择。该框架通过模块化设计实现了与主流 AI 服务&#xff08;如 OpenAI、Anthropic&#xff09;的无缝对接&…...

EtherNet/IP转DeviceNet协议网关详解

一&#xff0c;设备主要功能 疆鸿智能JH-DVN-EIP本产品是自主研发的一款EtherNet/IP从站功能的通讯网关。该产品主要功能是连接DeviceNet总线和EtherNet/IP网络&#xff0c;本网关连接到EtherNet/IP总线中做为从站使用&#xff0c;连接到DeviceNet总线中做为从站使用。 在自动…...

ArcGIS Pro制作水平横向图例+多级标注

今天介绍下载ArcGIS Pro中如何设置水平横向图例。 之前我们介绍了ArcGIS的横向图例制作&#xff1a;ArcGIS横向、多列图例、顺序重排、符号居中、批量更改图例符号等等&#xff08;ArcGIS出图图例8大技巧&#xff09;&#xff0c;那这次我们看看ArcGIS Pro如何更加快捷的操作。…...

MySQL用户和授权

开放MySQL白名单 可以通过iptables-save命令确认对应客户端ip是否可以访问MySQL服务&#xff1a; test: # iptables-save | grep 3306 -A mp_srv_whitelist -s 172.16.14.102/32 -p tcp -m tcp --dport 3306 -j ACCEPT -A mp_srv_whitelist -s 172.16.4.16/32 -p tcp -m tcp -…...

GitFlow 工作模式(详解)

今天再学项目的过程中遇到使用gitflow模式管理代码&#xff0c;因此进行学习并且发布关于gitflow的一些思考 Git与GitFlow模式 我们在写代码的时候通常会进行网上保存&#xff0c;无论是github还是gittee&#xff0c;都是一种基于git去保存代码的形式&#xff0c;这样保存代码…...

淘宝扭蛋机小程序系统开发:打造互动性强的购物平台

淘宝扭蛋机小程序系统的开发&#xff0c;旨在打造一个互动性强的购物平台&#xff0c;让用户在购物的同时&#xff0c;能够享受到更多的乐趣和惊喜。 淘宝扭蛋机小程序系统拥有丰富的互动功能。用户可以通过虚拟摇杆操作扭蛋机&#xff0c;实现旋转、抽拉等动作&#xff0c;增…...

Rust 开发环境搭建

环境搭建 1、开发工具RustRover 或者vs code 2、Cygwin64 安装 https://cygwin.com/install.html 在工具终端执行&#xff1a; rustup toolchain install stable-x86_64-pc-windows-gnu rustup default stable-x86_64-pc-windows-gnu ​ 2、Hello World fn main() { println…...

WPF八大法则:告别模态窗口卡顿

⚙️ 核心问题&#xff1a;阻塞式模态窗口的缺陷 原始代码中ShowDialog()会阻塞UI线程&#xff0c;导致后续逻辑无法执行&#xff1a; var result modalWindow.ShowDialog(); // 线程阻塞 ProcessResult(result); // 必须等待窗口关闭根本问题&#xff1a…...