C 标准库 - <stdlib.h>
简介
<stdlib.h>
头文件定义了四个变量类型、一些宏和各种通用工具函数。
库变量
下面是头文件 stdlib.h
中定义的变量类型:
序号 | 变量 & 描述 |
---|---|
1 | size_t |
2 | wchar_t |
3 | div_t |
4 | ldiv_t |
库宏
下面是头文件 stdlib.h
中定义的宏:
序号 | 宏 & 描述 |
---|---|
1 | NULL |
2 | EXIT_FAILURE |
3 | EXIT_SUCCESS |
4 | RAND_MAX |
5 | MB_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)
释放之前调用 calloc
、malloc
或 realloc
所分配的内存空间。
#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)
尝试重新调整之前调用 malloc
或 calloc
所分配的 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 中定义的变量类型: 序号变量 & 描述1size_t2wchar_t3div_t4ldiv_t 库宏 下面是头文件 stdlib.h 中定义的宏: 序号宏 & 描述1…...

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

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

PostgreSQL如何使用UUID
离线安装时,一般有四个包,都安装的话,只需要开启uuid的使用即可,如果工具包(即 postgresql11-contrib)没有安装的话,需要单独安装一次,再进行开启。 开启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 完美转发传入左值传入右值 左值 左值是指可以使用 & 符号获取到内存地址的表达式,一般出现在赋值语句的左边ÿ…...

Redis能保证数据不丢失吗?
引言 大家即使没用过Redis,也应该都听说过Redis的威名。 Redis是一种Nosql类型的数据存储,全称Remote Dictionary Server,也就是远程字典服务器,用过Dictionary的应该都知道它是一种键值对(Key-Value)的数…...

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

RM电控讲义【HAL库篇】(二)
8080并口模式是一种常见的计算机接口模式,主要用于LCD(液晶显示屏)模块。 在8080并口模式中,通信端口包括多种信号线,用于实现数据的读写和控制功能。主要的信号线包括: CS(片选信号ÿ…...

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

数据库管理-第153期 Oracle Vector DB AI-05(20240221)
数据库管理153期 2024-02-21 数据库管理-第153期 Oracle Vector DB & AI-05(20240221)1 Oracle Vector的其他特性示例1:示例2 2 简单使用Oracle Vector环境创建包含Vector数据类型的表插入向量数据 总结 数据库管理-第153期 Oracle Vecto…...
通过傅里叶变换进行音频变声变调
文章目录 常见音频变声算法使用Wav库读写音频文件使用pitchShift算法进行音频变调主文件完整代码工程下载地址常见音频变声算法 在游戏或者一些特殊场景下为了提高娱乐性或者保护声音的特征,我们会对音频进行变声变调处理。常用的算法包括: 1.基于傅里叶变换的频域算法,该类…...
Opencv(C++)学习 ARM上引用opencv报相关头文件找不到
简单问题记录,C 与C互相引用时应该多注意类似问题。 问题描述:在项目中,建立了一个interface.h提供了一个C语言兼容的接口void work(),并在对应的interface.cpp中使用OpenCV完成相关处理实现。在PC端测试时,main.cpp成…...
中国服装行业ERP的现状与未来发展
随着全球数字化浪潮的兴起,中国服装行业也在不断探索数字化转型的路径,其中ERP(企业资源计划)系统作为管理和优化企业资源的重要工具,在服装行业中发挥着日益重要的作用。本文将探讨中国服装行业ERP的现状、作用&#…...
Unix与Linux区别
目录 历史和所有权 内核 发行版 开源性质 用户群体 命令行界面 历史和所有权 Unix: Unix是一个操作系统家族的名称,最早由贝尔实验室(Bell Labs)的肖像电机公司(AT&T)开发。最早的Unix版本是在19…...

惠尔顿 网络安全审计系统 任意文件读取漏洞复现
0x01 产品简介 惠尔顿网络安全审计产品致力于满足军工四证、军工保密室建设、国家涉密网络建设的审计要求,规范网络行为,满足国家的规范;支持1-3线路的internet接入、1-3对网桥;含强大的上网行为管理、审计、监控模块;…...

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

AI-调查研究-01-正念冥想有用吗?对健康的影响及科学指南
点一下关注吧!!!非常感谢!!持续更新!!! 🚀 AI篇持续更新中!(长期更新) 目前2025年06月05日更新到: AI炼丹日志-28 - Aud…...

Linux 文件类型,目录与路径,文件与目录管理
文件类型 后面的字符表示文件类型标志 普通文件:-(纯文本文件,二进制文件,数据格式文件) 如文本文件、图片、程序文件等。 目录文件:d(directory) 用来存放其他文件或子目录。 设备…...

srs linux
下载编译运行 git clone https:///ossrs/srs.git ./configure --h265on make 编译完成后即可启动SRS # 启动 ./objs/srs -c conf/srs.conf # 查看日志 tail -n 30 -f ./objs/srs.log 开放端口 默认RTMP接收推流端口是1935,SRS管理页面端口是8080,可…...
Rust 异步编程
Rust 异步编程 引言 Rust 是一种系统编程语言,以其高性能、安全性以及零成本抽象而著称。在多核处理器成为主流的今天,异步编程成为了一种提高应用性能、优化资源利用的有效手段。本文将深入探讨 Rust 异步编程的核心概念、常用库以及最佳实践。 异步编程基础 什么是异步…...

C++ 求圆面积的程序(Program to find area of a circle)
给定半径r,求圆的面积。圆的面积应精确到小数点后5位。 例子: 输入:r 5 输出:78.53982 解释:由于面积 PI * r * r 3.14159265358979323846 * 5 * 5 78.53982,因为我们只保留小数点后 5 位数字。 输…...

【LeetCode】算法详解#6 ---除自身以外数组的乘积
1.题目介绍 给定一个整数数组 nums,返回 数组 answer ,其中 answer[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积 。 题目数据 保证 数组 nums之中任意元素的全部前缀元素和后缀的乘积都在 32 位 整数范围内。 请 不要使用除法,且在 O…...

ubuntu系统文件误删(/lib/x86_64-linux-gnu/libc.so.6)修复方案 [成功解决]
报错信息:libc.so.6: cannot open shared object file: No such file or directory: #ls, ln, sudo...命令都不能用 error while loading shared libraries: libc.so.6: cannot open shared object file: No such file or directory重启后报错信息&…...
k8s从入门到放弃之HPA控制器
k8s从入门到放弃之HPA控制器 Kubernetes中的Horizontal Pod Autoscaler (HPA)控制器是一种用于自动扩展部署、副本集或复制控制器中Pod数量的机制。它可以根据观察到的CPU利用率(或其他自定义指标)来调整这些对象的规模,从而帮助应用程序在负…...
comfyui 工作流中 图生视频 如何增加视频的长度到5秒
comfyUI 工作流怎么可以生成更长的视频。除了硬件显存要求之外还有别的方法吗? 在ComfyUI中实现图生视频并延长到5秒,需要结合多个扩展和技巧。以下是完整解决方案: 核心工作流配置(24fps下5秒120帧) #mermaid-svg-yP…...

【Linux】Linux安装并配置RabbitMQ
目录 1. 安装 Erlang 2. 安装 RabbitMQ 2.1.添加 RabbitMQ 仓库 2.2.安装 RabbitMQ 3.配置 3.1.启动和管理服务 4. 访问管理界面 5.安装问题 6.修改密码 7.修改端口 7.1.找到文件 7.2.修改文件 1. 安装 Erlang 由于 RabbitMQ 是用 Erlang 编写的,需要先安…...