C51库函数.docx
《C51库函数.docx》由会员分享,可在线阅读,更多相关《C51库函数.docx(12页珍藏版)》请在冰豆网上搜索。
C51库函数
C51库函数积累:
(1)_chkfloat_:
函数定义:
unsignedchar_chkfloat_(floatval);/*numbertocheck*/
函数功能:
_chkfloat_函数检查浮点数val的类型。
返回值:
_chkfloat_函数返回浮点数val的类型。
返回值
意义
0
标准浮点数
1
浮点0
2
+INF正溢出
3
-INF负溢出
4
NaN非数
/*
本实验测试本征库中的_chkfloat_函数:
函数定义:
unsignedchar_chkfloat_(floatval);
返回值:
ReturnValueMeaning
0Standardfloating-pointnumber.
1Floating-pointvalue0.
2+INF(positiveoverflow).
3-INF(negativeoverflow).
4NaN(NotaNumber)errorstatus.
用P1指示返回值
*/
delay(50000);
P2=~_chkfloat_;delay(50000);
P2=~_chkfloat_;delay(50000);
P2=~_chkfloat_;delay(50000);
P2=~_chkfloat_(*((float*)f));while
(1);
}
(2)_crol_:
函数定义:
unsignedchar_crol_(unsignedcharc, unsignedcharb); /*charactertorotateleft*/
/*bitpositionstorotate*/
函数功能:
_crol_函数将一个字节c循环左移b位。
返回值:
_crol函数返回将c循环左移b位后的值。
/*
本实验测试本征库中的_crol_函数
函数定义:
unsignedchar_crol_(unsignedcharc,unsignedcharb);
返回值:
_crol_函数返回将c循环左移b位后的值。
用P1指示返回值
*/
y=cos(x);
y=cosh(x); //y=
while
(1);
}
(28)exp:
函数定义:
floatexp(floatx);/*powertouseforexfunction*/
函数功能:
取x的自然指数值。
返回值:
x的自然指数值。
/*
本实验测试标准库中的exp函数
函数定义:
floatexp(floatx);
返回值:
x的自然指数值。
*/
函数测试:
#include<>
#include<>
voidmain()
{
floatx,y;
x=;
y=exp(x); //y=
while
(1);
}
(29)fabs:
函数定义:
floatfabs(floatval);/*numbertotakeabsolutevalueof*/
函数功能:
取浮点val的绝对值。
返回值:
浮点val的绝对值。
/*
本实验测试标准库中的fabs函数
函数定义:
floatfabs(floatval);
返回值:
浮点val的绝对值。
*/
函数测试:
#include<>
#include<>
voidmain()
{
floatx,y;
x=;
y=fabs(x); //y=
while
(1);
}
(30)floor:
函数定义:
floatfloor(floatval);/*numbertocalculatefloorfor*/
函数功能:
取比val小的最大的整数。
返回值:
比val小的最大的整数。
/*
本实验测试标准库中的floor函数
函数定义:
floatfloor(floatval);
返回值:
比val小的最大的整数。
*/
函数测试:
#include<>
#include<>
voidmain()
{
floatx,y;
x=;
y=floor(x); //y=
while
(1);
}
(31)fmod:
函数定义:
floatfmod(floatx,/*valuetocalculatemodulofor*/floaty);/*integerportionofmodulo*/
函数功能:
对x取y的模。
返回值:
x取y的模。
/*
本实验测试标准库中的fmod函数
函数定义:
floatfmod(floatx,floaty);
返回值:
x取y的模。
*/
函数测试:
#include<>
#include<>
voidmain()
{
floatx,y;
x=;
y=fmod(x,; //y=
while
(1);
}
(32)free:
函数定义:
voidfree(voidxdata*p);/*blocktofree*/
函数功能:
用来释放先前由calloc、malloc或realloc开辟的内存空间。
返回值:
无。
/*
本实验测试标准库中的free函数
函数定义:
voidfree(voidxdata*p);
返回值:
无
*/
函数测试:
#include<>
#include<>
voidmain()
{
voidxdata*buf;
buf=malloc(1000);//在片外ram中开辟一块有1000个字节的空间。
if(buf!
=NULL)
{
free(buf);//把由malloc开屏的空间释放掉。
}
while
(1);
}
(33)getchar:
函数定义:
chargetchar(void);
函数功能:
从串口读取一个字符。
返回值:
从串口读到字符。
/*
本实验测试标准库中的getchar函数
函数定义:
chargetchar(void);
返回值:
从串口读到字符。
*/
函数测试:
#include<>
#include<>
voidmain()
{
charc;
while((c=getchar())!
=0x0a)
{
P1=c; //如果收到的字符不是回车,则将收到字符输出到P1口
}
//注:
在程序开始要去串口进行初始,确定波特率。
while
(1);
}
(34)gets:
函数定义:
char*gets(char*string,/*stringtoread*/intlen);/*maxcharacterstoread*/
函数功能:
从串口读取一行字符串,len为能够读取的最大字节数。
返回值:
从串口读到的字符串的指针。
/*
本实验测试标准输入输出库中的gets函数
函数定义:
char*gets(char*string,intlen);
返回值:
从串口读到的字符串的指针。
*/
//函数测试:
#include<>
#include<>
voidmain()
{
charbuf[21];
do
{
gets(buf,sizeof(buf)-1);
printf("Inputstring\"%s\"",buf);
}while(buf[0]!
='\0');
//注:
在程序开始要去串口进行初始,确定波特率。
while
(1);
}
(35)init_mempool:
函数定义:
voidinit_mempool(voidxdata*p,/*startofmemorypool*/
unsignedintsize);/*lengthofmemorypool*/
函数功能:
用来初始化一个内容池,用calloc、free、malloc与realloc来进行管理。
返回值:
无。
/*
本实验测试标准库中的init_mempool函数
函数定义:
voidinit_mempool(voidxdata*p,unsignedintsize);
返回值:
无。
*/
//函数测试:
#include<>
#include<>
unsignedcharxdatamalloc_mempool[0x1000];
voidmain()
{
inti;
voidxdata*p;
init_mempool(&malloc_mempool,sizeof(malloc_mempool));
p=malloc(100);
for(i=0;i<100;i++)
{
((char*)p)[i]=i;
}
free(p);
while
(1);
}
(36)isalnum:
函数定义:
bitisalnum(charc);/*charactertotest*/
函数功能:
用来测试c是否是一个英文或数码字符('A'-'Z','a'-'z',or'0'-'9')。
返回值:
如果c是一个英文或数码字符则返回1,否则为0。
/*
本实验测试ctype库中的isalnum函数
函数定义:
bitisalnum(charc);
返回值:
如果c是一个英文或数码字符则返回1,否则为0。
*/
//函数测试:
#include<>
#include<>
voidmain()
{
unsignedcharc=';';
unsignedchard='a';
unsignedchare='A';
unsignedcharf='1';
unsignedcharresult1,result2,result3,result4;
result1=isalnum(c);//result1=0
result2=isalnum(d);//result2=1
result3=isalnum(e);//result3=1
result4=isalnum(f);//result4=1
while
(1);
}
(37)isalpha:
函数定义:
bitisalpha(charc);/*charactertotest*/
函数功能:
用来测试c是否是一个英文字符('A'-'Z','a'-'z')。
返回值:
如果c是一个英文字符则返回1,否则为0。
/*
本实验测试ctype库中的isalpha函数
函数定义:
bitisalpha(charc);
返回值:
如果c是一个英文字符则返回1,否则为0。
*/
//函数测试:
#include<>
#include<>
voidmain()
{
unsignedcharc=';';
unsignedchard='a';
unsignedchare='A';
unsignedcharf='1';
unsignedcharresult1,result2,result3,result4;
result1=isalpha(c);//result1=0
result2=isalpha(d);//result2=1
result3=isalpha(e);//result3=1
result4=isalpha(f);//result4=0
while
(1);
}