文章目录
- C语言标准函数库
- 数学库
- 通用工具库
- double atof(const char* str)
- int atoi(const char *str)
- long int atol(const char *str)
- double strtod(const *str, char **endptr)
- long int strtol(const char *str, char **endptr, int base)
- unsigned long int strtoul(const char *str, char **endptr, int base)
- void *calloc(size_t nitems, size_t size)
- void *malloc(size_t size)
- void *realloc(void *ptr, size_t size)
- void free(void *ptr)
- void abort(void)
- int atexit(void(*func)(void))
- void exit(int status)
- char *getenv(const char *name)
- int system(const char *string)
- void *bsearch(const void *key, const void *base, size_t nitems, size_t size, int(*compar)(const void, const void))
- void qsort(void *base, size_t nitems, size_t size, int(*compar)(const void *, const void *))
- int abs(int x)
- double fabs(double x)
- div_t div(int numer, int denom)
- int rand(void)
- void srand(unsigned int seed)
C语言标准函数库
数学库
三角函数
函数名 | 功能 |
---|---|
sin(x) | 返回x的正弦值 |
cos(x) | 返回x的余弦值 |
tan(x) | 返回x的正切值 |
asin(x) | 返回x的反正弦值 |
acos(x) | 返回x的反余弦值 |
atan(x) | 返回x的反正切值 |
atan2(y,x) | 返回y/x的反正切值 |
双曲函数
函数名 | 功能 |
---|---|
sinh(x) | 返回x的双曲正弦值 |
cosh(x) | 返回x的双曲余弦值 |
tanh(x) | 返回x的双曲正切值 |
指数和对数函数
函数名 | 功能 |
---|---|
exp(x) | 返回e的x次方,即2.718^x |
log(x) | 返回x的自然对数 |
log10(x) | 返回x的以10为底的对数 |
exp2(x) | 返回2的x次方 |
log2(x) | 返回x的以2为底的对数 |
幂函数
函数名 | 功能 |
---|---|
pow(x,y) | 返回x的y次方 |
sqrt(x) | 返回x的平方根 |
cbrt(x) | 返回x的立方根 |
hypot(x,y) | 返回x和y的平方和的平方根 |
误差和伽马函数
函数名 | 功能 |
---|---|
erf(x) | 返回x的误差函数值 |
erfc(x) | 返回x的补误差函数值 |
tgamma(x) | 返回x的伽马函数值 |
lgamma(x) | 返回x的伽马函数的自然对数 |
四舍五入与余数函数
函数名 | 功能 |
---|---|
ceil(x) | 返回大于或等于x的最小整数 |
floor(x) | 返回小于或等于x的最大整数 |
round(x) | 返回离x最近的整数 |
fmod(x,y) | 返回x/y的余数 |
绝对值最小值最大值
函数名 | 功能 |
---|---|
fabs(x) | 返回x的绝对值 |
abs(x) | 返回x的绝对值 |
fmax(x,y) | 返回x和y中较大的数 |
fmin(x,y) | 返回x和y中较小的数 |
通用工具库
stdlib.h头文件定义了四种变量类型、一些宏和各种通用工具函数
double atof(const char* str)
函数原型:double atof(const char* str)
功能:将字符串str转换成double型数据,并返回转换后的结果
int atoi(const char *str)
函数原型:int atoi(const char *str)
函数功能:把参数str所指向的字符串转换成int型数据,并返回转换后的结果
long int atol(const char *str)
函数原型:long int atol(const char *str)
函数功能:把参数str所指向的字符串转换成long int型数据,并返回转换后的结果
double strtod(const *str, char **endptr)
函数原型:double strtod(const *str, char *endptr)
函数功能:把参数str所指向的字符串转换为一个浮点数(double型),并返回转换后的结果
str是字符串,当第二参数为空时和double atof(const char str)功能相同,当第二参数不为空时,第二个形参引用后,第二个指针会指向存储字符串位置的地址
long int strtol(const char *str, char **endptr, int base)
函数原型:long int strtol(const char *str, char **endptr, int base)
函数功能:把参数str所指向的字符串转换成long int型数据,并返回转换后的结果
base是进制,当base为0时,函数会根据字符串的第一个字符来判断进制
unsigned long int strtoul(const char *str, char **endptr, int base)
函数原型:unsigned long int strtoul(const char *str, char **endptr, int base)
函数功能:把参数 str 所指向的字符串转换为一个无符号长整数(类型为 unsigned long int 型)
void *calloc(size_t nitems, size_t size)
函数原型:void *calloc(size_t nitems, size_t size)
函数功能:申请一块连续的内存空间,并返回指向该空间的指针
malloc和calloc的不同是malloc不会设置内存为零
void *malloc(size_t size)
函数原型:void *malloc(size_t size)
函数功能:申请一块连续的内存空间,并返回指向该空间的指针
该函数返回一个指针,指向已经分配大小的内存,如果申请失败,则返回NULL
void *realloc(void *ptr, size_t size)
函数原型:void *realloc(void *ptr, size_t size)
函数功能:尝试重新调整之前调用malloc或calloc所分配的ptr所指向的内存块的大小。该函数返回一个指针,指向重新分配大小的内存,如果请求失败,则返回NULL
void free(void *ptr)
函数原型:void free(void *ptr)
函数功能:释放内存空间,将申请的内存空间返还给系统
void abort(void)
函数原型:void abort(void)
函数功能:使一个异常程序终止
int atexit(void(*func)(void))
函数原型:int atexit(void(*func)(void))
函数功能:当程序正常终止时,调用指定的函数func
void exit(int status)
函数原型:void exit(int status)
函数功能:使程序正常终止
参数status为0时,表示正常退出
参数status非零时,表示异常退出
char *getenv(const char *name)
函数原型:char *getenv(const char *name)
函数功能:获取环境变量name的值,并返回相关的值
int system(const char *string)
函数原型:int system(const char *string)
函数功能:调用shell程序,并执行string参数指定的命令
void *bsearch(const void *key, const void *base, size_t nitems, size_t size, int(*compar)(const void, const void))
函数原型:void *bsearch(const void *key, const void *base, size_t nitems, size_t size, int(*compar)(const void, const void))
key – 指向要查找的元素的指针,类型转换为void
base – 指向进行查找的数组的第一个对象的指针,类型转换为 void
nitems – base 所指向的数组中元素的个数
size – 数组中每个元素的大小,以字节为单位
compar – 用来比较两个元素的函数。
函数功能: 执行二分查找
void qsort(void *base, size_t nitems, size_t size, int(*compar)(const void *, const void *))
函数原型:void qsort(void *base, size_t nitems, size_t size, int(*compar)(const void *, const void *))
base – 指向要排序的数组的第一个元素的指针。
nitems – 由 base 指向的数组中元素的个数。
size – 数组中每个元素的大小,以字节为单位。
compar – 用来比较两个元素的函数。
函数功能: 数组排序
int abs(int x)
函数原型:int abs(int x)
函数功能:返回x的绝对值
double fabs(double x)
函数原型:double fabs(double x)
函数功能:返回x的绝对值
div_t div(int numer, int denom)
函数原型:div_t div(int numer, int denom)
函数功能:返回一个结构体,该结构体包含两个整型成员num和denom,其中quot为numer除以denom的商,rem为numer除以denom的余数
int rand(void)
函数原型:int rand(void)
函数功能:返回一个随机整数,范围为0~RAND_MAX
void srand(unsigned int seed)
函数原型:void srand(unsigned int seed)
函数功能:初始化随机数发生器
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
int i, n;
time_t t;
n = 5;
/* 初始化随机数发生器 以CPU的时间作为随机种子所以是伪随机数*/
srand((unsigned)time(&t));
for(i = 0; i < n; i++)
{
printf("%d\n", rand() % 10);
}
return 0;
}