库函数并不是C语言的一部分。它是由人们根据需要编制并提供用户使用的。每一种C编译系统都提供了一批库函数,不同的编译系统所提供的库函数的数目和函数名以及函数功能是不完全相同的。ANSIC标准提出了一批建议提供的标准库函数。它包括了目前多数C编译系统所提供的库函数,但也有一些是某些C编译系统未曾实现的。考虑到通用性,本书列出ANSIC标准建议提供的、常用的部分库函数。对多数C编译系统,可以使用这些函数的绝大部分。由于C库函数的种类和数目很多(例如,还有屏幕和图形函数、时间日期函数、与系统有关的函数等,每一类函数又包括各种功能的函数),限于篇幅,不能全部介绍,只从需要的角度列出最基本的。读者在编制C程序时可能要用到更多的函数,请查阅所用系统的手册。
1.数学函数
使用数学函数时,应该在该源文件中使用以下命令行:
# include或# include"math.h
函数名 |
函数原型 |
功 能 |
返回值 |
说明 |
abs |
int abs (int x) |
求整数x的绝对值 |
计算结果 |
|
acoS |
double acos (double x); |
计算 cos(x)的值。 |
计算结果 |
x应在-1到1范围内。 |
asin |
double asin (double x); |
计算 sin(x)的值。 |
计算结果 |
x应在-1到1范围内。 |
atan |
double atan (double x); |
计算tan(x)的值。 |
计算结果 |
|
atan2 |
double atan2 (double x, double y); |
计算 tan-1(x/y)的值。 |
计算结果 |
|
cos |
double cos(double x); |
计算cos(r)的值 |
计算结果 |
x的单位为弧度 |
cosh |
double cosh (double x); |
计算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); |
求整除r/y的余数 |
返回余数的双精度数 |
|
frexp |
double frexp (double val,int*eptr); |
把双精度数val分解为数字部分(尾数)x和以2为底的指数 n,即 val=x*2",n存放在eptr指向的变量中。 |
返回数字部分r 0.5≤x<1 |
|
log |
double log(double x); |
求 logex,即 In x。 |
计算结果 |
|
log10 |
double log10(double x); |
求log10 x。 |
计算结果 |
|
modf |
double modf (double val,double *iptr); |
把双精度数 val分解为整数部分和小数部分,把整数部分存到iptr指分向的单元。 |
val 的小数部分 |
|
pow |
double pow (double x,double y); |
计算xy的值。 |
计算结果 |
|
rand |
int rand (void); |
产生-90到32767间的随机整数 |
随机整数 |
|
sin |
double sin (double x); |
计算 sinx 的值 |
计算结果 |
x单位为弧度 |
sinh |
double sinh (double x); |
计算x的双曲正弦函数 sinh(x)的值。 |
计算结果 |
|
sqrt |
double tan (double x); |
计算√x |
计算结果 |
x应≥0. |
tan |
double sqrt (double x); |
计算tan(x)的值。 |
计算结果 |
x单位为弧度 |
tanh |
double tanh (double x); |
计算x的双曲正切函数tanh(x)的值. |
2.字符函数和字符串函数
ANSIC标准要求在使用字符串函数时要包含头文件“string.h”,在使用字符函数时
要包含头文件“ctype.h”。有的C编译不遵循ANSIC标准的规定,而用其他名称的头文件,请使用时查有关手册。
函数名 |
函数原型 |
功 能 |
返回值 |
包含文件 |
isalnum |
int isalnum Gint ch); |
检查 ch是否是字母(alpha)或数字(numerie)。 |
是字母或数字返回1;否则返回0. |
ctype.h |
isalpha |
int isalpha (int ch); |
检查ch是否字母。 |
是,返回1;不是则返回0. |
ctype.h |
iscntrl |
int iscntrl (int ch); |
检查ch是否控制字符(其ASCII 码在0和 0x1F之间)。 |
是,返回1; 不是,返回0. |
ctype.h |
isdigit |
int isdigit (int ch); |
检查ch是否数字(0~9)。 |
是,返回1; 不是,返回0. |
ctype.h |
isgraph |
int isgraph (int ch); |
检查ch是否可打印字符(其ASCII码在ox21到ox7E 之间),不包括空格。 |
是,返回1; 不是,返回0. |
ctype.h |
islower |
int islower (intch); |
检查ch是否小写字母(a~z)。 |
是,返回1; 不是,返回0. |
ctype.h |
isprint |
int isprint (intch); |
检查ch是否可打印字符(包括空格),其ASCII码在ox20到ox7E之间。 |
是,返回1; 不是,返回0. |
ctype.h |
ispunct |
int ispunct (int ch); |
检查ch是否标点字符(不包括空格),即除字母、数字和空格以外的所有可打印字符。 |
是,返回1; 不是,返回0. |
ctype.h |
isspace |
int isspace (int ch); |
检查ch是否空格、跳格符(制表符)或换行符。 |
是,返回1; 不是,返回0. |
ctype.h |
isupper |
int isupper(int ch); |
检查ch是否大写字母(A~Z). |
是,返回1; 不是,返回0. |
ctype.h |
isxdigit |
int isxdigit (int ch); |
检查ch是否一个十六进制数字字符(即0~9,或A到F,或a~f). |
是,返回1; 不是,返回0. |
ctype.h |
strcat |
char * strcat ( char *strl, char*str2) |
把字符串 str2 接到str1后面,strl最后面的'\0’被取消。 |
strl |
string.h |
strchr |
char * strchr (char *str, int ch); |
找出str指向的字符串中第一次出现字符 ch 的位置。 |
返回指向该位置的指针,如找不到,则返回空指针. |
string.h |
strcmp |
int strcmp (char* strl,char *str2); |
比较两个字符串strl、str2。 |
str1<str2,返回负数 str1=str2,返回0 str1>str2,返回正数 |
string.h |
strcpy |
char * strcpy (char *strl,char *str2); |
把str2指向的字符串拷贝到str1中去。 |
返回str1。 |
string.h |
strlen |
unsigned int strlen(char*str); |
统计字符串str 中字符的个数(不包括终止符‘\0')。 |
返回字符个数。 |
string.h |
strstr |
char* strstr (char* str1,char *str2); |
返回该位置的指针,如找不到,返回空指针。 |
返回该位置的指针,如找不到,返回空指针。 |
string.h |
tolower |
int tolower (int ch); |
ch字符转换为小写字母。 |
返回ch所代表的字符的小写字母。 |
ctype.h |
toupper |
int toupper (int ch); |
将ch字符转换成大写字母。 |
与ch相应的大写字母。 |
ctype.h |