C 语言 stdlib.h:常用函数分类、示例与使用要点
时间:2026-08-22 | 作者:实验室老王 | 阅读:0stdlib.h,这可是C语言里相当重要的一个标准库头文件哦!它所提供的函数,那功能可多了去了,涵盖了内存管理、随机数生成、字符串转换以及程序控制等等多个方面。在处理各种各样的数据结构和算法时,这个库简直就是个得力助手。所以呢,对于那些想要深入钻研C语言,并且能够熟练运用它的开发者们来说,熟悉stdlib.h里的函数,那绝对是至关重要的。
内存分配与释放
malloc() 函数
malloc() 函数用于动态分配内存。它接受一个参数,表示需要分配的字节数,并返回指向该内存区域的指针。如果分配失败,则返回空指针(NULL)。例如:
// www.ja vascriptcn.com code example #include#include int main() { int *p = (int *)malloc(sizeof(int) * 5); if (p == NULL) { printf("内存分配失败n"); return 1; } // 使用 p 指向的内存 for (int i = 0; i < 5; i++) { p[i] = i; } free(p); // 释放内存 return 0; }
calloc() 函数
calloc() 函数用于分配并初始化一块内存。它接受两个参数,第一个参数是元素的数量,第二个参数是每个元素的大小(以字节为单位)。calloc() 返回的内存会被初始化为零。例如:
// www.ja vascriptcn.com code example #include#include int main() { int *p = (int *)calloc(5, sizeof(int)); if (p == NULL) { printf("内存分配失败n"); return 1; } // 输出初始化后的值 for (int i = 0; i < 5; i++) { printf("%d ", p[i]); } free(p); // 释放内存 return 0; }
realloc() 函数
realloc() 函数用于改变之前通过 malloc() 或 calloc() 分配的内存块的大小。它接受两个参数,第一个参数是要调整大小的内存块的指针,第二个参数是新的大小(以字节为单位)。例如:
// www.ja vascriptcn.com code example #include#include int main() { int *p = (int *)malloc(sizeof(int) * 5); if (p == NULL) { printf("内存分配失败n"); return 1; } // 调整内存大小 p = (int *)realloc(p, sizeof(int) * 10); if (p == NULL) { printf("内存调整失败n"); return 1; } // 使用 p 指向的新内存 for (int i = 0; i < 10; i++) { p[i] = i; } free(p); // 释放内存 return 0; }
free() 函数
free() 函数用于释放之前通过 malloc()、calloc() 或 realloc() 分配的内存块。释放后的内存不能再被访问,否则会导致未定义行为。例如:
// www.ja vascriptcn.com code example #include#include int main() { int *p = (int *)malloc(sizeof(int) * 5); if (p == NULL) { printf("内存分配失败n"); return 1; } // 使用 p 指向的内存 for (int i = 0; i < 5; i++) { p[i] = i; } free(p); // 释放内存 return 0; }
随机数生成
rand() 函数
rand() 函数用于生成伪随机数。它不接受任何参数,返回一个介于 0 和 RAND_MAX 之间的整数。为了确保每次运行程序时产生的随机数序列不同,可以在调用 rand() 之前使用 srand() 函数进行初始化。例如:
// www.ja vascriptcn.com code example #include#include #include int main() { srand(time(NULL)); // 初始化随机数生成器 for (int i = 0; i < 5; i++) { printf("%d ", rand()); } return 0; }
srand() 函数
srand() 函数用于设置随机数生成器的种子。通常情况下,使用当前时间作为种子可以产生不同的随机数序列。例如:
// www.ja vascriptcn.com code example #include#include #include int main() { srand(time(NULL)); // 使用当前时间作为种子 for (int i = 0; i < 5; i++) { printf("%d ", rand()); } return 0; }
字符串转换
atoi() 函数
atoi() 函数用于将字符串转换成整数。它接受一个字符串参数,返回相应的整数值。如果字符串无法转换成整数,则返回 0。例如:
// www.ja vascriptcn.com code example #include#include int main() { const char *str = "123"; int num = atoi(str); printf("转换后的整数:%dn", num); return 0; }
atof() 函数
atof() 函数用于将字符串转换成浮点数。它接受一个字符串参数,返回相应的浮点数值。例如:
// www.ja vascriptcn.com code example #include#include int main() { const char *str = "123.456"; double num = atof(str); printf("转换后的浮点数:%fn", num); return 0; }
atol() 函数
atol() 函数用于将字符串转换成长整型。它接受一个字符串参数,返回相应的长整型值。例如:
// www.ja vascriptcn.com code example #include#include int main() { const char *str = "1234567890"; long num = atol(str); printf("转换后的长整型:%ldn", num); return 0; }
程序控制
exit() 函数
exit() 函数用于终止程序的执行。它接受一个整数参数,作为程序退出的状态码。通常情况下,0 表示正常退出,非零值表示异常退出。例如:
#include#include int main() { printf("程序即将退出n"); exit(0); // 正常退出 return 0; }
abort() 函数
abort() 函数用于强制终止程序的执行。它不接受任何参数,并且不会清理资源或执行任何清理工作。通常用于处理不可恢复的错误情况。例如:
#include#include int main() { printf("程序即将异常退出n"); abort(); // 异常退出 return 0; }
getenv() 函数
getenv() 函数用于获取环境变量的值。它接受一个字符串参数,表示环境变量的名称,并返回一个指向该环境变量值的指针。如果环境变量不存在,则返回 NULL。例如:
// www.ja vascriptcn.com code example #include#include int main() { const char *value = getenv("PATH"); if (value != NULL) { printf("PATH 环境变量的值:%sn", value); } else { printf("PATH 环境变量不存在n"); } return 0; }
system() 函数
system() 函数用于执行系统命令。它接受一个字符串参数,表示要执行的命令,并返回一个整数值。返回值取决于具体的实现和命令的执行结果。例如:
// www.ja vascriptcn.com code example #include#include int main() { int ret = system("echo Hello, World!"); if (ret == -1) { printf("执行命令失败n"); } else { printf("命令执行成功n"); } return 0; }
以上是 stdlib.h 头文件中一些常用函数的详细介绍。熟练掌握这些函数能够帮助你在 C 语言编程中更加高效地进行内存管理、随机数生成、字符串转换以及程序控制。
免责声明:文中图文均来自网络,如有侵权请联系删除,心愿游戏发布此文仅为传递信息,不代表心愿游戏认同其观点或证实其描述。
相关文章
更多-
- 【C语言基础】:深入理解指针(三)
- 时间:2026-08-27
-
- C语言基础:字符与字符串函数详解及模拟实现
- 时间:2026-08-27
-
- C语言进阶:深入解析字符串、结构体与typedef用法
- 时间:2026-08-27
-
- LibCoroutine:细粒度C语言协程库开发解析
- 时间:2026-08-27
-
- C语言分支与循环入门:if、switch 和三种循环该怎么用
- 时间:2026-08-25
-
- C 语言字符指针入门:搞清地址、字符串常量与数组的区别
- 时间:2026-08-24
-
- C语言指针入门:从地址、数组到函数调用一次讲清
- 时间:2026-08-24
-
- C 语言自定义数据类型梳理:枚举、结构体、共用体与 typedef
- 时间:2026-08-24
精选合集
更多大家都在玩
大家都在看
更多-
- 以下哪种食材被称为“地下苹果” 蚂蚁庄园今日答案9.18
- 时间:2026-09-17
-
- 蚂蚁庄园今天答题答案2026年9月18日
- 时间:2026-09-17
-
- 蚂蚁庄园答题今日答案2026年9月18日
- 时间:2026-09-17
-
- 蚂蚁庄园小课堂2026年9月18日最新题目答案
- 时间:2026-09-17
-
- 小鸡答题今天的答案是什么2026年9月18日
- 时间:2026-09-17
-
- 蚂蚁庄园每日答题答案2026年9月18日
- 时间:2026-09-17
-
- 蔬菜洗完掉色,说明是被染色了,是真的吗 蚂蚁庄园今日答案9月18日
- 时间:2026-09-17
-
- 满襟蜡绘花纹巧染就花纹当绣裳说的是哪种传统技艺 蚂蚁新村今日答案2026.9.17
- 时间:2026-09-17
