C语言函数大全2.docx
《C语言函数大全2.docx》由会员分享,可在线阅读,更多相关《C语言函数大全2.docx(96页珍藏版)》请在冰豆网上搜索。
C语言函数大全2
返回>>C语言函数大全
函数名:
ecvt
功 能:
把一个浮点数转换为字符串
用 法:
charecvt(doublevalue,intndigit,int*decpt,int*sign);
程序例:
#include
#include
#include
intmain(void)
{
char*string;
doublevalue;
intdec,sign;
intndig=10;
clrscr();
value=9.876;
string=ecvt(value,ndig,&dec,&sign);
printf("string=%s dec=%d\
sign=%d\n",string,dec,sign);
value=-123.45;
ndig=15;
string=ecvt(value,ndig,&dec,&sign);
printf("string=%sdec=%dsign=%d\n",
string,dec,sign);
value=0.6789e5;/*scientific
notation*/
ndig=5;
string=ecvt(value,ndig,&dec,&sign);
printf("string=%s dec=%d\
sign=%d\n",string,dec,sign);
return0;
}
函数名:
ellipse
功 能:
画一椭圆
用 法:
voidfarellipse(intx,inty,intstangle,intendangle,
intxradius,intyradius);
程序例:
#include
#include
#include
#include
intmain(void)
{
/*requestautodetection*/
intgdriver=DETECT,gmode,errorcode;
intmidx,midy;
intstangle=0,endangle=360;
intxradius=100,yradius=50;
/*initializegraphics,localvariables*/
initgraph(&gdriver,&gmode,"");
/*readresultofinitialization*/
errorcode=graphresult();
if(errorcode!
=grOk)
/*anerroroccurred*/
{
printf("Graphicserror:
%s\n",
grapherrormsg(errorcode));
printf("Pressanykeytohalt:
");
getch();
exit
(1);
/*terminatewithanerrorcode*/
}
midx=getmaxx()/2;
midy=getmaxy()/2;
setcolor(getmaxcolor());
/*drawellipse*/
ellipse(midx,midy,stangle,endangle,
xradius,yradius);
/*cleanup*/
getch();
closegraph();
return0;
}
函数名:
enable
功 能:
开放硬件中断
用 法:
voidenable(void);
程序例:
/***NOTE:
Thisisaninterruptserviceroutine.YoucanNOTcompilethisprogram
withTestStackOverflowturnedonandgetanexecutablefilewhichwill
operatecorrectly.
*/
#include
#include
#include
/*Theclocktickinterrupt*/
#defineINTR0X1C
voidinterrupt(*oldhandler)(void);
intcount=0;
voidinterrupthandler(void)
{
/*
disableinterruptsduringthehandlingoftheinterrupt
*/
disable();
/*increasetheglobalcounter*/
count++;
/*
reenableinterruptsattheendofthehandler
*/
enable();
/*calltheoldroutine*/
oldhandler();
}
intmain(void)
{
/*savetheoldinterruptvector*/
oldhandler=getvect(INTR);
/*installthenewinterrupthandler*/
setvect(INTR,handler);
/*loopuntilthecounterexceeds20*/
while(count<20)
printf("countis%d\n",count);
/*resettheoldinterrupthandler*/
setvect(INTR,oldhandler);
return0;
}
函数名:
eof
功 能:
检测文件结束
用 法:
inteof(int*handle);
程序例:
#include
#include
#include
#include
#include
intmain(void)
{
inthandle;
charmsg[]="Thisisatest";
charch;
/*createafile*/
handle=open("DUMMY.FIL",
O_CREAT|O_RDWR,
S_IREAD|S_IWRITE);
/*writesomedatatothefile*/
write(handle,msg,strlen(msg));
/*seektothebeginningofthefile*/
lseek(handle,0L,SEEK_SET);
/*
readscharsfromthefileuntilhitEOF
*/
do
{
read(handle,&ch,1);
printf("%c",ch);
}while(!
eof(handle));
close(handle);
return0;
}
函数名:
exec...
功 能:
装入并运行其它程序的函数
用 法:
intexecl(char*pathname,char*arg0,arg1,...,argn,NULL);
intexecle(char*pathname,char*arg0,arg1,...,argn,NULL,
char*envp[]);
intexeclp(char*pathname,char*arg0,arg1,..,NULL);
intexecple(char*pathname,char*arg0,arg1,...,NULL,
char*envp[]);
intexecv(char*pathname,char*argv[]);
intexecve(char*pathname,char*argv[],char*envp[]);
intexecvp(char*pathname,char*argv[]);
intexecvpe(char*pathname,char*argv[],char*envp[]);
程序例:
/*execvexample*/
#include
#include
#include
voidmain(intargc,char*argv[])
{
inti;
printf("Commandlinearguments:
\n");
for(i=0;i printf("[%2d]:
%s\n",i,argv[i]);
printf("Abouttoexecchildwitharg1arg2...\n");
execv("CHILD.EXE",argv);
perror("execerror");
exit
(1);
}
函数名:
exit
功 能:
终止程序
用 法:
voidexit(intstatus);
程序例:
#include
#include
#include
intmain(void)
{
intstatus;
printf("Entereither1or2\n");
status=getch();
/*SetsDOSerrorlevel */
exit(status-'0');
/*Note:
thislineisneverreached*/
return0;
}
函数名:
exp
功 能:
指数函数
用 法:
doubleexp(doublex);
程序例:
#include
#include
intmain(void)
{
doubleresult;
doublex=4.0;
result=exp(x);
printf("'e'raisedtothepower\
of%lf(e^%lf)=%lf\n",
x,x,result);
return0;
}
返回>>C语言函数大全
函数名:
fabs
功 能:
返回浮点数的绝对值
用 法:
doublefabs(doublex);
程序例:
#include
#include
intmain(void)
{
float number=-1234.0;
printf("number:
%f absolutevalue:
%f\n",
number,fabs(number));
return0;
}
函数名:
farcalloc
功 能:
从远堆栈中申请空间
用 法:
voidfar*farcalloc(unsignedlongunits,unsignedlingunitsz);
程序例:
#include
#include
#include
#include
intmain(void)
{
charfar*fptr;
char*str="Hello";
/*allocatememoryforthefarpointer*/
fptr=farcalloc(10,sizeof(char));
/*copy"Hello"intoallocatedmemory*/
/*
Note:
movedataisusedbecauseyou
mightbeinasmalldatamodel,in
whichcaseanormalstringcopyroutine
cannotbeusedsinceitassumesthe
pointersizeisnear.
*/
movedata(FP_SEG(str),FP_OFF(str),
FP_SEG(fptr),FP_OFF(fptr),
strlen(str));
/*displaystring(notetheFmodifier)*/
printf("Farstringis:
%Fs\n",fptr);
/*freethememory*/
farfree(fptr);
return0;
}
函数名:
farcoreleft
功 能:
返回远堆中未作用存储区大小
用 法:
longfarcoreleft(void);
程序例:
#include
#include
intmain(void)
{
printf("Thedifferencebetweenthe\
highestallocatedblockinthe\
far\n");
printf("heapandthetopofthefarheap\
is:
%lubytes\n",farcoreleft());
return0;
}
函数名:
farfree
功 能:
从远堆中释放一块
用 法:
voidfarfree(void);
程序例:
#include
#include
#include
#include
intmain(void)
{
charfar*fptr;
char*str="Hello";
/*allocatememoryforthefarpointer*/
fptr=farcalloc(10,sizeof(char));
/*copy"Hello"intoallocatedmemory*/
/*
Note:
movedataisusedbecauseyoumightbeinasmalldatamodel,
inwhichcaseanormalstringcopyroutinecan'tbeusedsinceit
assumesthepointersizeisnear.
*/
movedata(FP_SEG(str),FP_OFF(str),
FP_SEG(fptr),FP_OFF(fptr),
strlen(str));
/*displaystring(notetheFmodifier)*/
printf("Farstringis:
%Fs\n",fptr);
/*freethememory*/
farfree(fptr);
return0;
}
函数名:
farmalloc
功 能:
从远堆中分配存储块
用 法:
voidfar*farmalloc(unsignedlongsize);
程序例:
#include
#include
#include
#include
intmain(void)
{
charfar*fptr;
char*str="Hello";
/*allocatememoryforthefarpointer*/
fptr=farmalloc(10);
/*copy"Hello"intoallocatedmemory*/
/*
Note:
movedataisusedbecausewemight
beinasmalldatamodel,inwhichcase
anormalstringcopyroutinecannotbe
usedsinceitassumesthepointersize
isnear.
*/
movedata(FP_SEG(str),FP_OFF(str),
FP_SEG(fptr),FP_OFF(fptr),
strlen(str));
/*displaystring(notetheFmodifier)*/
printf("Farstringis:
%Fs\n",fptr);
/*freethememory*/
farfree(fptr);
return0;
}
函数名:
farrealloc
功 能:
调整远堆中的分配块
用 法:
voidfar*farrealloc(voidfar*block,unsignedlongnewsize);
程序例:
#include
#include
intmain(void)
{
charfar*fptr;
fptr=farmalloc(10);
printf("Firstaddress:
%Fp\n",fptr);
fptr=farrealloc(fptr,20);
printf("Newaddress :
%Fp\n",fptr);
farfree(fptr);
return0;
}
函数名:
fclose
功 能:
关闭一个流
用 法:
intfclose(FILE*stream);
程序例:
#include
#include
intmain(void)
{
FILE*fp;
charbuf[11]="0123456789";
/*createafilecontaining10bytes*/
fp=fopen("DUMMY.FIL","w");
fwrite(&buf,strlen(buf),1,fp);
/*closethefile*/
fclose(fp);
return0;
}
函数名:
fcloseall
功 能:
关闭打开流
用 法:
intfcloseall(void);
程序例:
#include
intmain(void)
{
intstreams_closed;
/*opentwostreams*/
fopen("DUMMY.ONE","w");
fopen("DUMMY.TWO","w");
/*closetheopenstreams*/
streams_closed=fcloseall();
if(streams_closed==EOF)
/*issueanerrormessage*/
perror("Error");
else
/*printresultoffcloseall()function*/
printf("%dstreamswereclosed.\n",streams_closed);
return0;
}
函数名:
fcvt
功 能:
把一个浮点数转换为字符串
用 法:
char*fcvt(doublevalue,intndigit,int*decpt,int*sign);
程序例:
#include
#include
#include
intmain(void)
{
char*string;
doublevalue;
intdec,sign;
intndig=10;
clrscr();
value=9.876;
string=ecvt(value,ndig,&dec,&sign);
printf("string=%s dec=%d\
sign=%d\n",string,dec,sign);
value=-123.45;
ndig=15;
string=ecvt(value,ndig,&dec,&sign);
printf("string=%sdec=%dsign=%d\n",