C 标准库 - <stdlib.h>

简介: 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;
}
#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);
    else
        printf("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 所指向的多字节字符。

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

相关文章
|
数据安全/隐私保护 Python 弹性计算
宝塔面板忘记账户名名和密码的解决方法
宝塔面板忘记密码怎么办?宝塔账户名和密码都忘了如何找回?LAMPLNMP分享宝塔面板忘记账户名名和密码的解决方法: 账户名和密码都忘了 宝塔面板默认的账户名为:admin那么,如果账号和密码都忘了怎么办?执行以下命令即可重置密码,命令返回值就是你的用户名:执行命令: cd /www/server/panel && python tools.
8334 0
|
存储 编解码 JSON
PowerToys微软最强工具箱软件集!好用的Windows增强工具箱,降低内存和存储占用
Microsoft PowerToys是微软官方推出的免费开源工具集,专为Win10/11设计,集成20+实用功能,如高级粘贴、颜色拾取、窗口布局管理(FancyZones)、文本提取、屏幕缩放标注等,全面提升办公效率,支持开发者、设计师等高效操作,模块化自由配置,堪称Windows效率神器。
1357 7
|
机器学习/深度学习 人工智能 运维
别只盯着 CPU 爆了!一篇文章带你看懂:从指标到根因的 AIOps 自动化故障定位流水线
别只盯着 CPU 爆了!一篇文章带你看懂:从指标到根因的 AIOps 自动化故障定位流水线
1037 15
|
11月前
|
监控 网络协议 安全
如何在 CentOS 主机上配置集中式 Syslog 服务器
分析 Linux/Unix 系统及其他网络设备生成的系统日志(Syslog),是IT管理员的核心工作内容之一。为提升日志分析的效率,管理员通常会采用日志集中采集的方式。
397 1
|
人工智能 算法 开发者
一个提示词模板,搞定抖音短视频脚本创作
专为技术人打造的抖音脚本提示词模板,结构化拆解短视频创作套路,结合DeepSeek、通义千问等AI工具,快速生成可执行脚本框架,助力技术分享、产品演示高效落地,30秒讲清重点,开头抓人、节奏紧凑、完播率提升。
8403 13
|
存储 关系型数据库 MySQL
修复.net Framework4.x连接MYSQL时遇到utf8mb3字符集不支持错误方案。
通过上述步骤大多数情况下能够解决由于UTF-encoding相关错误所带来影响,在实施过程当中要注意备份重要信息以防止意外发生造成无法挽回损失,并且逐一排查确认具体原因以采取针对性措施解除障碍。
896 12
|
存储 人工智能 算法
通过Milvus内置Sparse-BM25算法进行全文检索并将混合检索应用于RAG系统
阿里云向量检索服务Milvus 2.5版本在全文检索、关键词匹配以及混合检索(Hybrid Search)方面实现了显著的增强,在多模态检索、RAG等多场景中检索结果能够兼顾召回率与精确性。本文将详细介绍如何利用 Milvus 2.5 版本实现这些功能,并阐述其在RAG 应用的 Retrieve 阶段的最佳实践。
3065 1
通过Milvus内置Sparse-BM25算法进行全文检索并将混合检索应用于RAG系统
|
安全 搜索推荐 数据挖掘
淘宝API接口介绍:助你高效开展电商业务
淘宝API接口是连接淘宝平台与外部应用的桥梁,提供商品管理、订单处理、用户分析等多功能服务。它以丰富的数据资源、实时性和高安全性著称,支持电商平台搭建、商品推荐优化及市场分析等场景。未来,淘宝API将融合AI、大数据等技术,拓展生态合作,并强化安全合规性,为电商发展注入新动力。
|
Ubuntu
在Ubuntu 20.04 x64系统中增加中文支持
将 Ubuntu 系统语言设置为中文: 1. 更新系统语言包: `sudo apt update` 和 `sudo apt install language-pack-zh-hans`。 2. 修改区域设置:编辑 `/etc/default/locale` 文件,设置 `LANG=zh_CN.UTF-8` 和 `LANGUAGE=zh_CN:zh`。 3. 生成中文 locale: `sudo locale-gen zh_CN.UTF-8` 和 `sudo update-locale`。 4. 重启系统: `sudo reboot`。 5. 验证设置: `locale` 命令检查是否生效。
4251 1
|
关系型数据库 MySQL Docker
docker pull mysql:8.0.26提示Error response from daemon: Get “https://registry-1.docker.io/v2/“: EOF错误
docker pull mysql:8.0.26提示Error response from daemon: Get “https://registry-1.docker.io/v2/“: EOF错误
5214 9