读书人

[转贴]部分C语言库函数。为了避免重复

发布时间: 2012-02-26 20:19:45 作者: rapoo

[转贴]部分C语言库函数。为了避免重复造车轮,请尽量使用标准库提供的函数
-------------------------------------------------------------------------
分类函数,所在函数库为ctype.h
int isalpha(int ch) 若ch是字母( 'A '- 'Z ', 'a '- 'z ')返回非0值,否则返回0
int isalnum(int ch) 若ch是字母( 'A '- 'Z ', 'a '- 'z ')或数字( '0 '- '9 ')
返回非0值,否则返回0
int isascii(int ch) 若ch是字符(ASCII码中的0-127)返回非0值,否则返回0
int iscntrl(int ch) 若ch是作废字符(0x7F)或普通控制字符(0x00-0x1F)
返回非0值,否则返回0
int isdigit(int ch) 若ch是数字( '0 '- '9 ')返回非0值,否则返回0
int isgraph(int ch) 若ch是可打印字符(不含空格)(0x21-0x7E)返回非0值,否则返回0
int islower(int ch) 若ch是小写字母( 'a '- 'z ')返回非0值,否则返回0
int isprint(int ch) 若ch是可打印字符(含空格)(0x20-0x7E)返回非0值,否则返回0
int ispunct(int ch) 若ch是标点字符(0x00-0x1F)返回非0值,否则返回0
int isspace(int ch) 若ch是空格( ' '),水平制表符( '\t '),回车符( '\r '),
走纸换行( '\f '),垂直制表符( '\v '),换行符( '\n ')
返回非0值,否则返回0
int isupper(int ch) 若ch是大写字母( 'A '- 'Z ')返回非0值,否则返回0
int isxdigit(int ch) 若ch是16进制数( '0 '- '9 ', 'A '- 'F ', 'a '- 'f ')返回非0值,
否则返回0
int tolower(int ch) 若ch是大写字母( 'A '- 'Z ')返回相应的小写字母( 'a '- 'z ')
int toupper(int ch) 若ch是小写字母( 'a '- 'z ')返回相应的大写字母( 'A '- 'Z ')
----------------------------------------------------------------------
数学函数,所在函数库为math.h、stdlib.h、string.h、float.h
int abs(int i) 返回整型参数i的绝对值
double cabs(struct complex znum) 返回复数znum的绝对值
double fabs(double x) 返回双精度参数x的绝对值
long labs(long n) 返回长整型参数n的绝对值
double exp(double x) 返回指数函数ex的值
double frexp(double value,int *eptr) 返回value=x*2n中x的值,n存贮在eptr中
double ldexp(double value,int exp); 返回value*2exp的值


double log(double x) 返回logex的值
double log10(double x) 返回log10x的值
double pow(double x,double y) 返回xy的值
double pow10(int p) 返回10p的值
double sqrt(double x) 返回x的开方
double acos(double x) 返回x的反余弦cos-1(x)值,x为弧度
double asin(double x) 返回x的反正弦sin-1(x)值,x为弧度
double atan(double x) 返回x的反正切tan-1(x)值,x为弧度
double atan2(double y,double x) 返回y/x的反正切tan-1(x)值,y的x为弧度
double cos(double x) 返回x的余弦cos(x)值,x为弧度
double sin(double x) 返回x的正弦sin(x)值,x为弧度
double tan(double x) 返回x的正切tan(x)值,x为弧度
double cosh(double x) 返回x的双曲余弦cosh(x)值,x为弧度
double sinh(double x) 返回x的双曲正弦sinh(x)值,x为弧度
double tanh(double x) 返回x的双曲正切tanh(x)值,x为弧度
double hypot(double x,double y) 返回直角三角形斜边的长度(z),
x和y为直角边的长度,z2=x2+y2
double ceil(double x) 返回不小于x的最小整数
double floor(double x) 返回不大于x的最大整数
void srand(unsigned seed) 初始化随机数发生器
int rand() 产生一个随机数并返回这个数


double poly(double x,int n,double c[])从参数产生一个多项式
double modf(double value,double *iptr)将双精度数value分解成尾数和阶
double fmod(double x,double y) 返回x/y的余数
double frexp(double value,int *eptr) 将双精度数value分成尾数和阶
double atof(char *nptr) 将字符串nptr转换成浮点数并返回这个浮点数
double atoi(char *nptr) 将字符串nptr转换成整数并返回这个整数
double atol(char *nptr) 将字符串nptr转换成长整数并返回这个整数
char *ecvt(double value,int ndigit,int *decpt,int *sign)
将浮点数value转换成字符串并返回该字符串
char *fcvt(double value,int ndigit,int *decpt,int *sign)
将浮点数value转换成字符串并返回该字符串
char *gcvt(double value,int ndigit,char *buf)
将数value转换成字符串并存于buf中,并返回buf的指针
char *ultoa(unsigned long value,char *string,int radix)
将无符号整型数value转换成字符串并返回该字符串,radix为转换时所用基数
char *ltoa(long value,char *string,int radix)
将长整型数value转换成字符串并返回该字符串,radix为转换时所用基数
char *itoa(int value,char *string,int radix)
将整数value转换成字符串存入string,radix为转换时所用基数
double atof(char *nptr) 将字符串nptr转换成双精度数,并返回这个数,错误返回0
int atoi(char *nptr) 将字符串nptr转换成整型数, 并返回这个数,错误返回0
long atol(char *nptr) 将字符串nptr转换成长整型数,并返回这个数,错误返回0
double strtod(char *str,char **endptr)将字符串str转换成双精度数,并返回这个数,
long strtol(char *str,char **endptr,int base)将字符串str转换成长整型数,
并返回这个数,
int matherr(struct exception *e)
用户修改数学错误返回信息函数(没有必要使用)
double _matherr(_mexcep why,char *fun,double *arg1p,
double *arg2p,double retval)
用户修改数学错误返回信息函数(没有必要使用)
unsigned int _clear87() 清除浮点状态字并返回原来的浮点状态
void _fpreset() 重新初使化浮点数学程序包


unsigned int _status87() 返回浮点状态字
--------------------------------------------------------------------------

[解决办法]
我发现这里有些函数的描述有些问题,所以特在此提醒大家!
比如:
lidongri(海浪 源码之前了无秘密) 说:
...
void *memmove(void *destin,void *source,unsigned n)
void *memcpy(void *destin,void *source,unsigned n)
...
memcpy从source复制一个n字节的块到destin.如果源块和目标块重迭,则选择复制方向,
以例正确地复制覆盖的字节.
memmove与memcpy相同.

可是我手上的手册
The Open Group Base Specifications Issue 6
IEEE Std 1003.1, 2004 Edition
Copyright © 2001-2004 The IEEE and The Open Group, All Rights reserved.

是这么写的:


NAME
memcpy - copy bytes in memory
SYNOPSIS
#include <string.h>

void *memcpy(void *restrict s1, const void *restrict s2, size_t n);


DESCRIPTION
[CX] The functionality described on this reference page is aligned with the ISO C standard. Any conflict between the requirements described here and the ISO C standard is unintentional. This volume of IEEE Std 1003.1-2001 defers to the ISO C standard.
The memcpy() function shall copy n bytes from the object pointed to by s2 into the object pointed to by s1. If copying takes place between objects that overlap, the behavior is undefined.

RETURN VALUE
The memcpy() function shall return s1; no return value is reserved to indicate an error.

ERRORS
No errors are defined.


--------------------------------------------

The following sections are informative.
EXAMPLES
None.

APPLICATION USAGE
The memcpy() function does not check for the overflow of the receiving memory area.

RATIONALE
None.

FUTURE DIRECTIONS
None.

SEE ALSO
The Base Definitions volume of IEEE Std 1003.1-2001, <string.h>

CHANGE HISTORY
First released in Issue 1. Derived from Issue 1 of the SVID.

Issue 6
The memcpy() prototype is updated for alignment with the ISO/IEC 9899:1999 standard.


NAME
memmove - copy bytes in memory with overlapping areas
SYNOPSIS
#include <string.h>

void *memmove(void *s1, const void *s2, size_t n);


DESCRIPTION
[CX] The functionality described on this reference page is aligned with the ISO C standard. Any conflict between the requirements described here and the ISO C standard is unintentional. This volume of IEEE Std 1003.1-2001 defers to the ISO C standard.
The memmove() function shall copy n bytes from the object pointed to by s2 into the object pointed to by s1. Copying takes place as if the n bytes from the object pointed to by s2 are first copied into a temporary array of n bytes that does not overlap the objects pointed to by s1 and s2, and then the n bytes from the temporary array are copied into the object pointed to by s1.

RETURN VALUE
The memmove() function shall return s1; no return value is reserved to indicate an error.

ERRORS
No errors are defined.


--------------------------------------------

The following sections are informative.
EXAMPLES
None.

APPLICATION USAGE
None.

RATIONALE
None.

FUTURE DIRECTIONS
None.

SEE ALSO
The Base Definitions volume of IEEE Std 1003.1-2001, <string.h>

CHANGE HISTORY
First released in Issue 4. Derived from the ANSI C standard.


所以建议在写程序多多查手册,如果您开发的软件比较重要涉及重大的财产人身的问题,请参考可靠的手册!

读书人网 >C语言

热点推荐