C 库函数

数学函数

使用函数时,应该在源文件中使用以 下命令行:

1
2
3
# include <math.h>
or
# include "math.h"
函数名
函数原型 功能 返回值 说明
@@@@ @@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@ @@@ @@@@@@
sin double sin(double x) 计算 $sin(x)$ 的值 计算结果 x 的单位为弧度
asin double asin(double x) 计算 $sin^{-1}(x)$ 的值 计算结果 x 应在 -1 到 1 范围内
sinh double sinh(double x) 计算 x 的双曲正弦函数 $sinh(x)$ 的值 计算结果 -
cos double cos(double x) 计算 $cos(x)$ 的值 计算结果 x 的单位为弧度
acos double acos(double x) 计算 $cos^{-1}(x)$ 的值 计算结果 x 应在 -1 到 1 范围内
cosh double cosh(double x) 计算 x 的双曲余弦函数 $cosh(x)$ 的值 计算结果 -
tan double tan(double x) 计算 $tan(x)$ 的值 计算结果 x 的单位为弧度
atan double atan(double x) 计算 $tan^{-1}(x)$ 的值 计算结果 -
atan2 double atan2(double x, double y) 计算 $tan^{-1}(\frac xy)$ 的值 计算结果 -
tanh double tanh(double x) 计算 x 的双曲正切函数 $tanh(x)$ 的值 计算结果 -
abs int abs(int x) 求整数 x 的绝对值 计算结果 -
fabs int fabs(double x) 求 x 的绝对值 计算结果 -
exp double exp(double x) 求 $e^x$ 的值 计算结果 -
pow double pow(double x, double y) 求 $x^y$ 的值 计算结果 -
frexp double frexp(double val, int * eptr) 把双精度数val分解为数字部分(尾数) x 和以 2 为底的指数 n ,即 $val = x * 2^n$ , n 存放在 eptr 指向的变量中 返回数字部分x范围: $0.5 <= x <= 1$ -
modf double modf(double val, int * iptr) 把双精度数 val 分解为整数部分和小数部分 ,把整数部分存放在 iptr 指向的变量中 val 的小数部分 -
floor double floor(double x) 求出不大于 x 的最大整数 该整数的双精度实数 -
fmod double fmod(double x, double y) 求整除 $\frac xy$ 的余数 返回余数的双精度数 -
log double log(double x) 求 $log_e x$ , 即 $ln x$ 计算结果 -
log10 double log10(double x) 求 $log_{10} x$ , 即 $lg x$ 计算结果 -
rand int rand(void) 产生 -90 到 -32767 间的随机整数 随机整数 -
sqrt double sqrt(double x) 计算 $\sqrt[2]x$ 计算结果 x应大于等于0

字符函数和字符串函数

使用字符串函数时,应该在源文件中使用以下命令行:

1
2
3
# include <string.h>
or
# include "string.h"

使用字符函数时,应该在源文件中使用以下命令行:

1
2
3
# include <ctype.h>
or
# include "ctype.h"
函数名 函数原型 功能 返回值 包含文件
@@@@ @@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@ @@@@@@@@@ @@@@@
isalnum int isalnum(int ch) 检查ch是否是字母(alpha)或者数字(numeric) 是字母或者数字返回 1;否则返回 0 ctype.h
isalpha int isalpha(int ch) 检查ch是否是字母(alpha) 是字母返回1;否则返回0 ctype.h
isdigit int isdigit(int ch) 检查ch是否控制字符(其ASCLL码在 0 到 0x1F 之间) 是,返回 1;不是返回 0 ctype.h
isgraph int isgraph(int ch) 检查ch是否可打印字符(其ASCLL码在 0x21 到 0x7E 之间)不包含空格 是,返回 1;不是返回 0 ctype.h
islower int islower(int ch) 检查ch是否小写字母(a~z) 是,返回1;不是返回0 ctype.h
isprint int isprint(int ch) 检查ch是否可打印字符(其ASCLL码在 0x21 到 0x7E 之间)包含空格 是,返回 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是否是一个十六进制数字字符(即 09,或者 AF ,或 a~f ) 是,返回 1;不是返回 0 ctype.h
strcat char * strcat(char * str1, char * str2) 把字符串str2 接到字符串str1 后面,str1 最后面的 ‘\0’ 被取消 str1 string.h
strchr char * strchr(char * str, int ch) 找出字符串str指向的字符串中第一次出现字符ch的位置 返回指向该位置的指针,如找不到,则返回空指针 string.h
strcmp int strcmp(char * str1, char * str2) 比较 str1 和 str2 两个字符串 str1 < str2, 返回负数;str1 = str2, 返回0;str1 > str2, 返回整数 string.h
strcpy char * strcpy(char * str1, char * str2) 把str2指向的字符串复制到str1去 返回str1 string.h
strlen unsigned int strlen(char * str) 统计字符串str中字符的个数(不包括终止符 ‘\0’) 返回字符个数 string.h
strstr char * strstr(char * str1, char * str2) 找出字符串str2在字符串str1中第一次出现的位置(不包括 str2 的串结束符) 返回该位置的指针,如找不到,则返回空指针 string.h
tolower int tolower(int ch) 将 ch 字符转换成小写字母 返回 ch 所代表的的字符的小写字母 ctype.h
toupper int toupper(int ch) 将 ch 字符转换成大写字母 返回 ch 所代表的的字符的大写字母 ctype.h

输入输出函数

使用输入输出函数时,应该在源文件中使用以下命令行:

1
2
3
# include <stdio.h>
or
# include "stdio.h"
函数名 函数原型 功能 返回值 说明
@@@@ @@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@ @@@@@@@@@ @@@@@
clearerr void clearerr(FILE *fp) 使fp所致文件的错误,标志和文件结束标志置 0 -
close int close(int fp) 关闭文件 关闭成功返回 0;不成功返回 -1 非 ANSI 标准
creat int creat(char * filename, int mode) 以 mode 所指定的方式建立文件 成功则返回整数;否则返回 0 非 ANSI 标准
eof int eof(int fd) 检查文件是否结束 遇文件结束,则返回 1;否则返回 0 非 ANSI 标准
fclose int fclose(FILE * fp) 关闭 fp 所指的文件,释放文件缓冲区 成功返回 0;不成功返回非 0 -
feof int feof(FILE * fp) 检查文件是否结束 遇文件结束符返回非 0;否则返回 0 -
fgetc int fgetc(FILE * fp) 从 fp 所指定的文件中取得下一个字符 返回所得到的字符,若读入出错,返回 EOF -
fgets char *fgets(char * buf, int n, FILE * fp) 从fp指向的文件读取一个长度为(n-1)的字符串,存入起始地址为 buf 的空间 返回地址 buf,若遇到文件结束或出错,返回 NULL -
fopen FILE *fopen(char * filename, char * mode) 以 mode 指定的文件方式打开名为 filename 的文件 成功返回一个文件指针(文件信息区的起始地址);否则返回 0 -
fprintf int fprintf(FILE * fp, char * format, args, …) 把 args 的值以 format 指定的格式输出到 fp 指定的文件中 实际输出的字符数 -
fputc int fputc(char ch, FILE * fp) 将字符 ch 输出到 fp 指向的文件中 成功则返回该字符;否则返回非0 -
fputs char fputs(* str, FILE * fp) 将 str 指向的字符串输出到 fp 所指定的文件 成功返回0;若出错返回非 0 -
fread int fread(char * pt, unsigned size,unsigned n, FILE * fp) 从 fp 所指定的文件中读取长度为 size 的 n 个数据项,存放到 pt 所指定的内存区 返回所读的数据项个数,如遇文件结束或出错返回 0 -
fscanf int fscanf(FILE * fp, char format, args,…) 从 fp 指定的文件中按 format 给定的格式将输入数据送到 args 所指向的内存单元(args 是指针) 已输入的数据个数 -
fseek int fseek(FILE * fp, long offset, int base) 将 fp 所指向的文件的位置指针移到 base 所给出的位置为基准、以 offset 为位移量的位置 返回当前位置;否则返回 -1 -
ftell long ftell (FILE * fp) 返回fp所指向的文件中的读写位置 返回fp所指向的文件中的读写位置 -