文章目录
1.数学函数(#include <math.h> )
2.字符函数和字符串函数 (#include <ctype.h> 和 #include <string.h> )
3.动态存储分配函数(#include<stdlib.h> )
1.数学函数
包含文件:math.h
函数名 | 函数原型 | 功能 | 返回值 |
---|---|---|---|
abs | int abs (int x); | x 的绝对值 | 计算结果 |
acos | double acos (double x); | cos-1(x) (x 弧度表示) | 计算结果 |
asin | double asin (double x); | sin-1(x) | 计算结果 |
atan | double atan (double x); | tan-1(x) | 计算结果 |
atan2 | double atan2 (double x,double y); | sin-1/(x/y) | 计算结果 |
ceil | double ceil (double x) | 大于等于x的最小整数值 | 计算结果 |
cos | double cos (double x); | cos(x) | 计算结果 |
cosh | double cosh (double x); | 双曲余弦 cosh(x) | 计算结果 |
exp | double exp(double x); | ex | 计算结果 |
fabs | double fabs( double x); | x 的绝对值 | 计算结果 |
floor | double floor( double x); | 小于等于 x 的最大整数 | 计算结果 |
fmod | double fmod( double x,double y); | x/y 的余数(x%y) | 计算结果 |
frexp | double frexp(double val,int *eptr); | val 分解为val=x*2n | 返回 x (0.5<=x<1) |
labs | long labs(long x) | x的绝对值 | 计算结果 |
ldexp | double ldexp(double x,int y) | x*2y | 计算结果 |
log | double log( double x); | logex | 计算结果 |
log10 | double log10(double x); | log10x | 计算结果 |
modf | double modf (double val,double *iptr); | val小数部分 | 小数部分 |
pow | double pow (double x,double y); | x y | 计算结果 |
sin | double sin( double x); | sin x | 计算结果 |
sinh | double sinh( double x); | 双曲正弦函数sinh(x) | 计算结果 |
sqrt | double sqrt(double x); | 计算√x | 计算结果 |
tan | double tan( double x); | tan(x) | 计算结果 |
tanh | double tanh( double x); | 双曲正切函数tanh(x) | 计算结果 |
ps:
1. acos & asin: x应在-1到1之间。
2. cos & sin & tan: x的单位为弧度。
3. sqrt: x 应>=0.
4. floor:可解决需要省略 四舍五入 计算的情况
2.字符函数和字符串函数
包含文件: ctype.h
函数名 | 函数原型 | 功能 | 返回值 |
---|---|---|---|
isalnum | int isalnum(int c ); | 判断c 是否是字母 或数字 | 是返回1,否则返回0 |
isalpha | int isalpha(int c ); | 判断c 是否字母 | 是,返回1;不是,返回0 |
iscntrl | int iscntrl(int c ); | 判断c 是否控制字符(其ASCII码在0和0×1F之间) | 是,返回1;不是,返回0 |
isdigit | int isdigit(int c ); | 判断c 是否数字(0~9) | 是,返回1;不是,返回0 |
isgraph | int isgraph(int c ); | 判断c 是否可打印字符(其ASCII码在ox21到o7E之间),不包括空格 | 是,返回1;不是,返回0 |
islower | int islower(int c ); | 判断c 是否小写字母(a~z) | 是,返回1;不是,返回0 |
isprint | int isprint(int c ); | 判断c 是否可打印字符(包括空格),其 ASCII码在ox20到ox7E之间 | 是,返回1;不是,返回0 |
ispunct | int ispunct(int c ); | 判断c 是否标点字符(不包括空格),即除字母、数字和空格以外的所有可打印字符 | 是,返回1;不是,返回0 |
isspace | int isspace(int c ); | 判断c 是否空格、跳格符(制表符)或换行符 | 是,返回1;不是,返回0 |
isupper | int isupper(int c ); | 判断c 是否大写字母(A~Z) | 是,返回1;不是,返回0 |
isxdigit | int isxdigit(int c ); | 判断c 是否一个十六进制数字字符(即0 ~ 9,或A到F,或a ~ f) | 是,返回1;不是,返回0 |
tolower | int tolower(int c ); | 返回c 所代表的字符的小写字母 | 转换后的小写字母 |
toupper | int toupper(int c ); | 若c 为小写则转换成大写字母 ,否则返回原值 | 转换后的大写字母或原值 |
ps:
1.可打印字符:字母数字字符、标点符号字符和空格字符
包含文件: string.h
函数名 | 函数原型 | 功能 | 返回值 |
---|---|---|---|
memchr | void *memchr(const void *str, int c, sizeof n) | 在str 的前 n 个字节中搜索第一次出现无符号字符 c的位置。 | 指向匹配字节的指针 |
memcmp | int memcmp(const void *str1, const void *str2, sizeof n); | 存储区str1 和存储区str2 的前 n 个字节进行比较。 | str1<str2,返回负数,str1=str2,返回0,str1>str2,返回正数 |
memcpy | void *memcpy(void *str1, const void *str2, sizeof n) | 从存储区 str2复制 n个字符到存储区 str1 | str1 |
memmove | void *memmove(void *str1, const void *str2, sizeof n) | 从str2 复制n 个字符到str1 | str1 |
memset | void *memset(void *str, int c, sizeof n) | 复制无符号字符 c到str 所指向的字符串的前 n 个字符。 | str |
strcat | char *strcat(char *str1, char *str2); | 把字符串str2接到str1后面,str1最后面的’\0’忽略 | str1 |
strncat | char *strncat(char *str1,const char *str2,sizeof n); | 把字符串str2接到str1后面,直到n字符长度为止。 | str1 |
strchr | char *strchr(char *str, int ch); | 找出str指向的字符串中第一次出现字符ch的位置 | 返回指向该位置的指针,如找不到,则返回空指针 |
strcmp | int strcmp(char *str1, char *str2); | 比较两个字符串str1、str2 | str1<str2,返回负数;str1=str2,返回0;str1>str2,返回正数 |
strncmp | int strncmp(const char *str1, const char *str2, sizeof n) | 把 str1 和 str2 进行比较,最多比较前 n 个字节。 | str1<str2,返回负数;str1=str2,返回0;str1>str2,返回正数 |
strcoll | int strcoll(const char *str1, const char *str2) | 把 str1 和 str2 进行比较 | str1<str2,返回负数;;str1=str2,返回0;str1>str2,返回正数 |
strcpy | char *strcpy(char *str1, char *str2); | 把str2指向的字符串复制到str中去 | 返回str1 |
strncpy | char *strncpy(char *str1, const char *str2, sizeof n) | 把str2 所指向的字符串复制到 str1 | str1 |
strcspn | sizeof strcspn(const char *str1, const char *str2) | 搜索字符串 str1 开头连续有几个字符都不含字符串 str2 中的字符。 | 字符个数 |
strerror | char *strerror(int errno) | 搜索错误号 errno,并返回一个指向错误消息字符串的指针。 | errno |
strlen | unsigned int strlen( char *str); | 统计字符串str中字符的个数(不包括终止符 ‘\0’) | 字符个数 |
strrchr | char *strrchr(char *str, int ch); | 找出str指向的字符串中最后一次出现字符ch的位置 | 返回指向该位置的指针,如找不到,则返回空指针 |
strstr | char *strstr(char *str1, char *str2); | 找出str2字符串在str字符串中第一次出现的位置(不包括str2后的’\0’) | 返回该位置的指针,如找不到,返回空指针 |
ps:
1. 宏:NULL
3.动态储存函数
包含文件: stdlib.h
函数名 | 函数原型 | 功能 | 返回值 |
---|---|---|---|
calloc | void *calloc(unsigned n,unsign size); | 分配n 个数据项的内存连续空间,每个数据项的大小为size | 分配 |
free | void free(void *p); | 释放p 所指的内存区 | 无 |
malloc | void *malloc(unsigned size) | 分配 size字节的存储区 | 所分配的内存区起始地址,如内存不够,返回0 |
qsort | void qsort(void *base,size_t num,size_t size,int ( * comparator ) ( const void *, const void * ) ); | 快速排序 | 排序后的结果 |
rand | int rand ( void); | 产生 0 到 32767 间的随机整数 | 随机整数 |
realloc | void *realloc(void *p,unsigned size); | 将p 所指的已分配内存区大小改为size ,size可以比原来分配的空间大或小 | 返回指向该内存区的指针 |
srand | void srand(unsigned int seed); | 用于伪随机数生成算法播种(与rand搭配使用) | 无返回值 |
ps:
1. qsort (感觉这个函数很方便!!!) :
void qsort(void *base,size_t num,size_t size,int ( * comparator ) ( const void *, const void * ) );
参数的意义如下:
*base :数组名
num :数组长度
size :数组单个元素大小(size_t,相当于数据类型int)
compare :比较函数
这篇播客主要是收集一些写代码时用起来方便的函数,所以关于这个函数的解释我就直接放链接啦~
【C语言】中qsort使用详解
2. rand 函数与srand搭配使用:
百度问答: srand函数和rand函数到底如何使用