1、字符处理函数
本类别函数用于对单个字符进行处理,包括字符的类别测试和字符的大小写转换
头文件ctype.h
intisalpha(int ch) 若ch是字母('A'-'Z','a'-'z')返回非0值,否则返回0
intisalnum(int ch) 若ch是字母('A'-'Z','a'-'z')或数字('0'-'9'),返回非0值,否则返回0
intisascii(int ch) 若ch是字符(ASCII码中的0-127)返回非0值,否则返回0
intiscntrl(int ch) 若ch是作废字符(0x7F)或普通控制字符(0x00-0x1F),返回非0值,否则返回0
intisdigit(int ch) 若ch是数字('0'-'9')返回非0值,否则返回0
intisgraph(int ch) 若ch是可打印字符(不含空格)(0x21-0x7E)返回非0值,否则返回0
intislower(int ch) 若ch是小写字母('a'-'z')返回非0值,否则返回0
intisprint(int ch) 若ch是可打印字符(含空格)(0x20-0x7E)返回非0值,否则返回0
intispunct(int ch) 若ch是标点字符(0x00-0x1F)返回非0值,否则返回0
intisspace(int ch) 若ch是空格(' '),水平制表符('\t'),回车符('\r'), 走纸换行('\f'),垂直制表符('\v'),换行符('\n'),返回非0值,否则返回0
intisupper(int ch) 若ch是大写字母('A'-'Z')返回非0值,否则返回0
intisxdigit(int ch) 若ch是16进制数('0'-'9','A'-'F','a'-'f')返回非0值, 否则返回0
inttolower(int ch) 若ch是大写字母('A'-'Z')返回相应的小写字母('a'-'z')
inttoupper(int ch) 若ch是小写字母('a'-'z')返回相应的大写字母('A'-'Z')
2、数学函数
本分类给出了各种数学计算函数
头文件math.h
intabs(int i) 返回整型参数i的绝对值
doublecabs(struct complex znum) 返回复数znum的绝对值
doublefabs(double x) 返回双精度参数x的绝对值
longlabs(long n) 返回长整型参数n的绝对值
doubleexp(double x) 返回指数函数ex的值
doublefrexp(double value,int *eptr) 返回value=x*2n中x的值,n存贮在eptr中
doubleldexp(double value,int exp); 返回value*2exp的值
doublelog(double x) 返回logex的值
doublelog10(double x) 返回log10x的值
doublepow(double x,double y) 返回xy的值
doublepow10(int p) 返回10p的值
doublesqrt(double x) 返回x的开方
doubleacos(double x) 返回x的反余弦cos-1(x)值,x为弧度
doubleasin(double x) 返回x的反正弦sin-1(x)值,x为弧度
doubleatan(double x) 返回x的反正切tan-1(x)值,x为弧度
doubleatan2(double y,double x) 返回y/x的反正切tan-1(x)值,y的x为弧度
doublecos(double x) 返回x的余弦cos(x)值,x为弧度
doublesin(double x) 返回x的正弦sin(x)值,x为弧度
doubletan(double x) 返回x的正切tan(x)值,x为弧度
doublecosh(double x) 返回x的双曲余弦cosh(x)值,x为弧度
doublesinh(double x) 返回x的双曲正弦sinh(x)值,x为弧度
doubletanh(double x) 返回x的双曲正切tanh(x)值,x为弧度
doublehypot(double x,double y) 返回直角三角形斜边的长度(z), x和y为直角边的长度,z2=x2+y2
doubleceil(double x) 返回不小于x的最小整数
doublefloor(double x) 返回不大于x的最大整数
voidsrand(unsigned seed) 初始化随机数发生器
intrand() 产生一个随机数并返回这个数
doublemodf(double value,double *iptr) 将双精度数value分解成尾数和阶
doublefmod(double x,double y) 返回x/y的余数
3、字符串处理
本分类的函数用于对字符串进行合并、比较等操作
头文件string.h
charstpcpy(char *dest,const char *src) 将字符串src复制到dest
charstrcat(char *dest,const char *src) 将字符串src添加到dest末尾
charstrchr(const char *s,int c) 眷索并返回字符c在字符串s中第一次出现的位置
intstrcmp(const char *s1,const char *s2) 比较字符串s1与s2的大小,并返回s1-s2
charstrcpy(char *dest,const char *src) 将字符串src复制到dest
size_tstrcspn(const char *s1,const char *s2) 扫描s1,返回在s1中有,在s2中也有的字符个数
charstrdup(const char *s) 将字符串s复制到最近建立的单元
intstricmp(const char *s1,const char *s2) 比较字符串s1和s2,并返回s1-s2
size_tstrlen(const char *s) 返回字符串s的长度
charstrlwr(char *s)
将字符串s中的大写字母全部转换成小写字母,并返回转换后的字符串
charstrncat(char *dest,const char *src,size_t maxlen)
将字符串src中最多maxlen个字符复制到字符串dest中
intstrncmp(const char *s1,const char *s2,size_t maxlen)
比较字符串s1与s2中的前maxlen个字符
charstrncpy(char *dest,const char *src,size_t maxlen)
复制src中的前maxlen个字符到dest中
intstrnicmp(const char *s1,const char *s2,size_t maxlen)
比较字符串s1与s2中的前maxlen个字符
charstrnset(char *s,int ch,size_t n)
将字符串s的前n个字符置于ch中
charstrpbrk(const char *s1,const char *s2)
扫描字符串s1,并返回在s1和s2中均有的字符个数
charstrrchr(const char *s,int c)
扫描最后出现一个给定字符c的一个字符串s
charstrrev(char *s)
将字符串s中的字符全部颠倒顺序重新排列,并返回排列后的字符串
charstrset(char *s,int ch)
将一个字符串s中的所有字符置于一个给定的字符ch
size_tstrspn(const char *s1,const char *s2)
扫描字符串s1,并返回在s1和s2中均有的字符个数
charstrstr(const char *s1,const char *s2)
扫描字符串s2,并返回第一次出现s1的位置
charstrtok(char *s1,const char *s2)
检索字符串s1,该字符串s1是由字符串s2中定义的定界符所分隔
charstrupr(char *s)
将字符串s中的小写字母全部转换成大写字母,并返回转换后的字符串
4、输入输出函数
该分类用于处理包括文件、控制台等各种输入输出设备,各种函数以“流”的方式实现
头文件stdio.h
C语言输入输出函数有很多,标准I/O函数中包含了如下几个常用的函数:
scanf,printf,getc,putc,getchar,putchar,gets,puts,fgets,fputs,fgetc,fputc,fscanf,fprintf等.
intscanf(const char *format, arg_list)
scanf主要从标准输入流中获取参数值,format为指定的参数格式及参数类型,如scanf("%s,%d",str,icount);
它要求在标准输入流中输入类似"son of bitch,1000"这样的字符串,同时程序会将"son ofbitch"给str,1000给icount.
scanf函数的返回值为int值,即成功赋值的个数,在上例中如果函数调用成功,则会返回2,所以我们在写程序时,可以通过
语句if(scanf("%s,%d",str,icount)!= 2){...}来判断用户输入是否正确.
intprintf(const char *format, arg_list)
printf主要是将格式化字符串输出到标准输出流中,在stdio.h头文件中定义了标准的输入和输出,分别是stdin,stdout.
arg_list可以是变量名,也可以是表达式,但最终都会以值的形式填充进format中.
intgetc(FILE *fp)
getc主要是从文件中读出一个字符.常用的判断文件是否读取结束的语句为:(ch= getc(fp)) != EOF.EOF为文件结束标志,
定义在stdio.h中,就像EXIT_SUCCESS,EXIT_FAILURE定义在stdlib.h中一样,文件也可以被理解为一种流,所以当fp为stdin
时,getc(stdin)就等同于getchar()了.
intputc(int ch,FILE *fp)
putc主要是把字符ch写到文件fp中去.如果fp为stdout,则putc就等同于putchar()了.
intgetchar(void)
getchar主要是从标准输入流读取一个字符.默认的标准输入流即stdio.h中定义的stdin.但是从输入流中读取字符时又
涉及到缓冲的问题,所以并不是在屏幕中敲上一个字符程序就会运行,一般是通过在屏幕上敲上回车键,然后将回车前的字符
串放在缓冲区中,getchar就是在缓冲区中一个一个的读字符.当然也可以在while循环中指定终止字符,如下面的语句:
while((c = getchar()) != '#')这是以#来结束的.
intputchar(int ch)
putchar(ch)主要是把字符ch写到标准流stdout中去.
char* gets(char *str)
gets主要是从标准输入流读取字符串并回显,读到换行符时退出,并会将换行符省去.
intputs(char *str)
puts主要是把字符串str写到标准流stdout中去,并会在输出到最后时添加一个换行符.
char*fgets(char *str, int num, FILE *fp)
str是存放读入的字符数组指针,num是最大允许的读入字符数,fp是文件指针.fgets的功能是读一行字符,该行的字符数
不大于num-1.因为fgets函数会在末尾加上一个空字符以构成一个字符串.另外fgets在读取到换行符后不会将其省略.
intfputs(char *str, file *fp)
fputs将str写入fp.fputs与puts的不同之处是fputs在打印时并不添加换行符.
intfgetc(FILE *fp)
fgetc从fp的当前位置读取一个字符.
intfputc(int ch, file *fp)
fputc是将ch写入fp当前指定位置.
intfscanf(FILE *fp, char *format,...)
fscanf按照指定格式从文件中出读出数据,并赋值到参数列表中.
intfprintf(FILE *fp, char *format,...)
fprintf将格式化数据写入流式文件中.
5、控制台输入输出函数
该类函数主要包含了一些文本模式的屏幕控制函数,象绘画窗口、显示彩色文本,不是 Ansi标准函数,比如getch()函数等等。
头文件conio.h
包含的函数
cgets(char*);
cprintf(constchar *, ...);
cputs(constchar *);
cscanf(constchar *, ...);
inp(unsignedshort);
inpw(unsignedshort);
getch(void);
getche(void);
kbhit(void);
outp(unsignedshort, int);
outpw(unsignedshort, unsigned short);
putch(int);
ungetch(int);
6、常用工具类函数
本分类给出了一些函数无法按以上分类,但又是编程所必须要的。
头文件stdlib.h
函数
atof,atoi, atol, strtod, strtof, strtols, strtol, strtoll, strtoul, strtoull, rand,srand, callc, free, maloc, realloc, abort, atexit, exit, getenv, system,bsearch, qsort, abs, div, labs, ldiv, llabs, tlldiv, mblen, mbtowc, wctomb,mbstowcs, wcstombs
包含函数:
1函数名称: calloc
函数原型:void * calloc(unsigned n,unsign size);
函数功能:分配n个数据项的内存连续空间,每个数据项的大小为size
函数返回:分配内存单元的起始地址,如果不成功,返回0
2函数名称: free
函数原型:void free(void* p);
函数功能:释放p所指的内存区
函数返回:
参数说明:p-被释放的指针
3函数名称: malloc
函数原型:void * malloc(unsigned size);
函数功能:分配size字节的存储区
函数返回:所分配的内存区地址,如果内存不够,返回0
4函数名称: realloc
函数原型:void * realloc(void * p,unsigned size);
函数功能:将p所指出的已分配内存区的大小改为size,size可以比原来分配的空间大或小
函数返回:返回指向该内存区的指针.NULL-分配失败
5函数名称: rand
函数原型:int rand(void);
函数功能:产生0到32767间的随机整数(0到0x7fff之间)
函数返回:随机整数
6函数名称: abort
函数原型:void abort(void)
函数功能:异常终止一个进程.
7函数名称: exit
函数原型:void exit(int state)
函数功能:程序中止执行,返回调用过程
函数返回:
参数说明:state:0-正常中止,非0-非正常中止
8函数名称: getenv
函数原型:char* getenv(const char *name)
函数功能:返回一个指向环境变量的指针
函数返回:环境变量的定义
参数说明:name-环境字符串
9函数名称: putenv
函数原型:int putenv(const char *name)
函数功能:将字符串name增加到DOS环境变量中
函数返回:0:操作成功,-1:操作失败
参数说明:name-环境字符串
10函数名称: labs
函数原型:long labs(long num)
函数功能:求长整型参数的绝对值
函数返回:绝对值
11函数名称: atof
函数原型:double atof(char *str)
函数功能:将字符串转换成一个双精度数值
函数返回:转换后的数值
参数说明:str-待转换浮点型数的字符串
12函数名称: atoi
函数原型:int atoi(char *str)
函数功能:将字符串转换成一个整数值
函数返回:转换后的数值
参数说明:str-待转换为整型数的字符串
13函数名称: atol
函数原型:long atol(char *str)
函数功能:将字符串转换成一个长整数
函数返回:转换后的数值
参数说明:str-待转换为长整型的字符串
14函数名称: ecvt
函数原型:char *ecvt(double value,int ndigit,int *dec,int *sign)
函数功能:将浮点数转换为字符串
函数返回:转换后的字符串指针
参数说明:value-待转换底浮点数,ndigit-转换后的字符串长度
15函数名称: fcvt
函数原型:char *fcvt(double value,int ndigit,int *dec,int *sign)
函数功能:将浮点数变成一个字符串
函数返回:转换后字符串指针
参数说明: value-待转换底浮点数,ndigit-转换后底字