C语言函数大全S.docx
《C语言函数大全S.docx》由会员分享,可在线阅读,更多相关《C语言函数大全S.docx(71页珍藏版)》请在冰豆网上搜索。
C语言函数大全S
函数名:
sbrk
功 能:
改变数据段空间位置
用 法:
char*sbrk(intincr);
程序例:
#include
intmain(void)
{
printf("Changingallocationwithsbrk()\n");
printf("Beforesbrk()call:
%lubytesfree\n",
(unsignedlong)coreleft());
sbrk(1000);
printf("Aftersbrk()call:
return0;
}
scanf
执行格式化输入
intscanf(char*format[,argument,...]);
charlabel[20];
charname[20];
intentries=0;
intloop,age;
doublesalary;
structEntry_struct
char name[20];
int age;
floatsalary;
}entry[20];
/*Inputalabelasastringofcharactersrestrictingto20characters*/
printf("\n\nPleaseenteralabelforthechart:
");
scanf("%20s",label);
fflush(stdin); /*flushtheinputstreamincaseofbadinput*/
/*Inputnumberofentriesasaninteger*/
printf("Howmanyentrieswilltherebe?
(lessthan20)");
scanf("%d",&entries);
/*inputanamerestrictinginputtoonlylettersupperorlowercase*/
for(loop=0;loop { printf("Entry%d\n",loop); printf(" Name :"); scanf("%[A-Za-z]",entry[loop].name); fflush(stdin); /*flushtheinputstreamincaseofbadinput*//*inputanageasaninteger*/ printf(" Age :"); scanf("%d",&entry[loop].age); fflush(stdin); /*flushtheinputstreamincaseofbadinput*//*inputasalaryasafloat*/ printf(" Salary:"); scanf("%f",&entry[loop].salary); fflush(stdin);/*flushtheinputstreamincaseofbadinput*/ }/*Inputaname,ageandsalaryasastring,integer,anddouble*/ printf("\nPleaseenteryourname,ageandsalary\n"); scanf("%20s%d%lf",name,&age,&salary); /*Printoutthedatathatwasinput*/ printf("\n\nTable%s\n",label); printf("Compiledby%s age%d $%15.2lf\n",name,age,salary); printf("-----------------------------------------------------\n"); for(loop=0;loop printf("%4d|%-20s|%5d|%15.2lf\n", loop+1, entry[loop].name, entry[loop].age, entry[loop].salary); printf("-----------------------------------------------------\n"); return0;} 函数名:searchpath功 能:搜索DOS路径用 法:char*searchpath(char*filename);程序例:#include#includeintmain(void){ char*p; /*LooksforTLINKandreturnsapointer tothepath */ p=searchpath("TLINK.EXE"); printf("SearchforTLINK.EXE:%s\n",p); /*Looksfornon-existentfile */ p=searchpath("NOTEXIST.FIL"); printf("SearchforNOTEXIST.FIL:%s\n",p); return0;} 函数名:sector功 能:画并填充椭圆扇区用 法:voidfarsector(intx,inty,intstangle,intendangle);程序例:#include#include#include#includeintmain(void){ /*requestautodetection*/ intgdriver=DETECT,gmode,errorcode; intmidx,midy,i; intstangle=45,endangle=135; intxrad=100,yrad=50; /*initializegraphicsandlocalvariables*/ 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; /*loopthroughthefillpatterns*/ for(i=EMPTY_FILL;i { /*setthefillstyle*/ setfillstyle(i,getmaxcolor()); /*drawthesectorslice*/ sector(midx,midy,stangle,endangle,xrad,yrad); getch(); } /*cleanup*/ closegraph(); return0;} 函数名:segread功 能:读段寄存器值用 法:voidsegread(structSREGS*segtbl);程序例:#include#includeintmain(void){ structSREGSsegs; segread(&segs); printf("Currentsegmentregistersettings\n\n"); printf("CS:%X DS:%X\n",segs.cs,segs.ds); printf("ES:%X SS:%X\n",segs.es,segs.ss); return0;} 函数名:setactivepage功 能:设置图形输出活动页用 法:voidfarsetactivepage(intpagenum);程序例:#include#include#include#includeintmain(void){ /*selectadriverandmodethatsupports*/ /*multiplepages. */ intgdriver=EGA,gmode=EGAHI,errorcode; intx,y,ht; /*initializegraphicsandlocalvariables*/ initgraph(&gdriver,&gmode,""); /*readresultofinitialization*/ errorcode=graphresult(); if(errorcode!=grOk) /*anerroroccurred*/ { printf("Graphicserror:%s\n",grapherrormsg(errorcode)); printf("Pressanykeytohalt:"); getch(); exit(1);/*terminatewithanerrorcode*/ } x=getmaxx()/2; y=getmaxy()/2; ht=textheight("W"); /* selecttheoffscreenpagefordrawing*/ setactivepage(1); /*drawalineonpage#1*/ line(0,0,getmaxx(),getmaxy()); /*outputamessageonpage#1*/ settextjustify(CENTER_TEXT,CENTER_TEXT); outtextxy(x,y,"Thisispage#1:"); outtextxy(x,y+ht,"Pressanykeytohalt:"); /*selectdrawingtopage#0*/ setactivepage(0); /*outputamessage onpage#0*/ outtextxy(x,y,"Thisispage#0."); outtextxy(x,y+ht,"Pressanykeytoviewpage#1:"); getch(); /*selectpage#1asthevisiblepage*/ setvisualpage(1); /*cleanup*/ getch(); closegraph(); return0;} 函数名:setallpallette功 能:按指定方式改变所有的调色板颜色用 法:voidfarsetallpallette(structpalette,far*pallette);程序例:#include#include#include#includeintmain(void){ /*requestautodetection*/ intgdriver=DETECT,gmode,errorcode; structpalettetypepal; intcolor,maxcolor,ht; inty=10; charmsg[80]; /*initializegraphicsandlocalvariables*/ initgraph(&gdriver,&gmode,""); /*readresultofinitialization*/ errorcode=graphresult(); if(errorcode!=grOk) /*anerroroccurred*/ { printf("Graphicserror:%s\n",grapherrormsg(errorcode)); printf("Pressanykeytohalt:"); getch(); exit(1);/*terminatewithanerrorcode*/ } maxcolor=getmaxcolor(); ht=2*textheight("W"); /*grabacopyofthepalette*/ getpalette(&pal); /*displaythedefaultpalettecolors*/ for(color=1;color<=maxcolor;color++) { setcolor(color); sprintf(msg,"Color:%d",color); outtextxy(1,y,msg); y+=ht; } /*waitforakey*/ getch(); /*blackoutthecolorsonebyone*/ for(color=1;color<=maxcolor;color++) { setpalette(color,BLACK); getch(); } /*restorethepalettecolors*/ setallpalette(&pal); /*cleanup*/ getch(); closegraph(); return0;} 函数名:setaspectratio功 能:设置图形纵横比用 法:voidfarsetaspectratio(intxasp,intyasp);程序例:#include#include#include#includeintmain(void){ /*requestautodetection*/ intgdriver=DETECT,gmode,errorcode; intxasp,yasp,midx,midy; /*initializegraphicsandlocalvariables*/ 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()); /*getcurrentaspectratiosettings*/ getaspectratio(&xasp,&yasp); /*drawnormalcircle*/ circle(midx,midy,100); getch(); /*claerthescreen*/ cleardevice(); /*adjusttheaspectforawidecircle*/ setaspectratio(xasp/2,yasp); circle(midx,midy,100); getch(); /*adjusttheaspectforanarrowcircle*/ cleardevice(); setaspectratio(xasp,yasp/2); circle(midx,midy,100); /*cleanup*/ getch(); closegraph(); return0;} 函数名:setbkcolor功 能:用调色板设置当前背景颜色用 法:voidfarsetbkcolor(intcolor);程序例:#include#include#include#includeintmain(void){ /*selectadriverandmodethatsupports*/ /*multiplebackgroundcolors. */ intgdriver=EGA,gmode=EGAHI,errorcode; intbkcol,maxcolor,x,y; charmsg[80]; /*initializegraphicsandlocalvariables*/ initgraph(&gdriver,&gmode,""); /*readresultofinitialization*/ errorcode=graphresult(); if(errorcode!=grOk) /*anerroroccurred*/ { printf("Graphicserror:%s\n",grapherrormsg(errorcode)); printf("Pressanykeytohalt:"); getch(); exit(1);/*terminatewithanerrorcode*/ } /*maximumcolorindexsupported*/ maxcolor=getmaxcolor(); /*forcenteringtextmessages*/ settextjustify(CENTER_TEXT,CENTER_TEXT); x=getmaxx()/2; y=getmaxy()/2; /*loopthrought
printf("Entry%d\n",loop);
printf(" Name :
scanf("%[A-Za-z]",entry[loop].name);
/*inputanageasaninteger*/
printf(" Age :
scanf("%d",&entry[loop].age);
/*inputasalaryasafloat*/
printf(" Salary:
scanf("%f",&entry[loop].salary);
fflush(stdin);/*flushtheinputstreamincaseofbadinput*/
/*Inputaname,ageandsalaryasastring,integer,anddouble*/
printf("\nPleaseenteryourname,ageandsalary\n");
scanf("%20s%d%lf",name,&age,&salary);
/*Printoutthedatathatwasinput*/
printf("\n\nTable%s\n",label);
printf("Compiledby%s age%d $%15.2lf\n",name,age,salary);
printf("-----------------------------------------------------\n");
for(loop=0;loop printf("%4d|%-20s|%5d|%15.2lf\n", loop+1, entry[loop].name, entry[loop].age, entry[loop].salary); printf("-----------------------------------------------------\n"); return0;} 函数名:searchpath功 能:搜索DOS路径用 法:char*searchpath(char*filename);程序例:#include#include
printf("%4d|%-20s|%5d|%15.2lf\n",
loop+1,
entry[loop].name,
entry[loop].age,
entry[loop].salary);
searchpath
搜索DOS路径
char*searchpath(char*filename);
char*p;
/*LooksforTLINKandreturnsapointer
tothepath */
p=searchpath("TLINK.EXE");
printf("SearchforTLINK.EXE:
%s\n",p);
/*Looksfornon-existentfile */
p=searchpath("NOTEXIST.FIL");
printf("SearchforNOTEXIST.FIL:
sector
画并填充椭圆扇区
voidfarsector(intx,inty,intstangle,intendangle);
/*requestautodetection*/
intgdriver=DETECT,gmode,errorcode;
intmidx,midy,i;
intstangle=45,endangle=135;
intxrad=100,yrad=50;
/*initializegraphicsandlocalvariables*/
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;
/*loopthroughthefillpatterns*/
for(i=EMPTY_FILL;i { /*setthefillstyle*/ setfillstyle(i,getmaxcolor()); /*drawthesectorslice*/ sector(midx,midy,stangle,endangle,xrad,yrad); getch(); } /*cleanup*/ closegraph(); return0;} 函数名:segread功 能:读段寄存器值用 法:voidsegread(structSREGS*segtbl);程序例:#include#includeintmain(void){ structSREGSsegs; segread(&segs); printf("Currentsegmentregistersettings\n\n"); printf("CS:%X DS:%X\n",segs.cs,segs.ds); printf("ES:%X SS:%X\n",segs.es,segs.ss); return0;} 函数名:setactivepage功 能:设置图形输出活动页用 法:voidfarsetactivepage(intpagenum);程序例:#include#include#include#includeintmain(void){ /*selectadriverandmodethatsupports*/ /*multiplepages. */ intgdriver=EGA,gmode=EGAHI,errorcode; intx,y,ht; /*initializegraphicsandlocalvariables*/ initgraph(&gdriver,&gmode,""); /*readresultofinitialization*/ errorcode=graphresult(); if(errorcode!=grOk) /*anerroroccurred*/ { printf("Graphicserror:%s\n",grapherrormsg(errorcode)); printf("Pressanykeytohalt:"); getch(); exit(1);/*terminatewithanerrorcode*/ } x=getmaxx()/2; y=getmaxy()/2; ht=textheight("W"); /* selecttheoffscreenpagefordrawing*/ setactivepage(1); /*drawalineonpage#1*/ line(0,0,getmaxx(),getmaxy()); /*outputamessageonpage#1*/ settextjustify(CENTER_TEXT,CENTER_TEXT); outtextxy(x,y,"Thisispage#1:"); outtextxy(x,y+ht,"Pressanykeytohalt:"); /*selectdrawingtopage#0*/ setactivepage(0); /*outputamessage onpage#0*/ outtextxy(x,y,"Thisispage#0."); outtextxy(x,y+ht,"Pressanykeytoviewpage#1:"); getch(); /*selectpage#1asthevisiblepage*/ setvisualpage(1); /*cleanup*/ getch(); closegraph(); return0;} 函数名:setallpallette功 能:按指定方式改变所有的调色板颜色用 法:voidfarsetallpallette(structpalette,far*pallette);程序例:#include#include#include#includeintmain(void){ /*requestautodetection*/ intgdriver=DETECT,gmode,errorcode; structpalettetypepal; intcolor,maxcolor,ht; inty=10; charmsg[80]; /*initializegraphicsandlocalvariables*/ initgraph(&gdriver,&gmode,""); /*readresultofinitialization*/ errorcode=graphresult(); if(errorcode!=grOk) /*anerroroccurred*/ { printf("Graphicserror:%s\n",grapherrormsg(errorcode)); printf("Pressanykeytohalt:"); getch(); exit(1);/*terminatewithanerrorcode*/ } maxcolor=getmaxcolor(); ht=2*textheight("W"); /*grabacopyofthepalette*/ getpalette(&pal); /*displaythedefaultpalettecolors*/ for(color=1;color<=maxcolor;color++) { setcolor(color); sprintf(msg,"Color:%d",color); outtextxy(1,y,msg); y+=ht; } /*waitforakey*/ getch(); /*blackoutthecolorsonebyone*/ for(color=1;color<=maxcolor;color++) { setpalette(color,BLACK); getch(); } /*restorethepalettecolors*/ setallpalette(&pal); /*cleanup*/ getch(); closegraph(); return0;} 函数名:setaspectratio功 能:设置图形纵横比用 法:voidfarsetaspectratio(intxasp,intyasp);程序例:#include#include#include#includeintmain(void){ /*requestautodetection*/ intgdriver=DETECT,gmode,errorcode; intxasp,yasp,midx,midy; /*initializegraphicsandlocalvariables*/ 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()); /*getcurrentaspectratiosettings*/ getaspectratio(&xasp,&yasp); /*drawnormalcircle*/ circle(midx,midy,100); getch(); /*claerthescreen*/ cleardevice(); /*adjusttheaspectforawidecircle*/ setaspectratio(xasp/2,yasp); circle(midx,midy,100); getch(); /*adjusttheaspectforanarrowcircle*/ cleardevice(); setaspectratio(xasp,yasp/2); circle(midx,midy,100); /*cleanup*/ getch(); closegraph(); return0;} 函数名:setbkcolor功 能:用调色板设置当前背景颜色用 法:voidfarsetbkcolor(intcolor);程序例:#include#include#include#includeintmain(void){ /*selectadriverandmodethatsupports*/ /*multiplebackgroundcolors. */ intgdriver=EGA,gmode=EGAHI,errorcode; intbkcol,maxcolor,x,y; charmsg[80]; /*initializegraphicsandlocalvariables*/ initgraph(&gdriver,&gmode,""); /*readresultofinitialization*/ errorcode=graphresult(); if(errorcode!=grOk) /*anerroroccurred*/ { printf("Graphicserror:%s\n",grapherrormsg(errorcode)); printf("Pressanykeytohalt:"); getch(); exit(1);/*terminatewithanerrorcode*/ } /*maximumcolorindexsupported*/ maxcolor=getmaxcolor(); /*forcenteringtextmessages*/ settextjustify(CENTER_TEXT,CENTER_TEXT); x=getmaxx()/2; y=getmaxy()/2; /*loopthrought
/*setthefillstyle*/
setfillstyle(i,getmaxcolor());
/*drawthesectorslice*/
sector(midx,midy,stangle,endangle,xrad,yrad);
/*cleanup*/
closegraph();
segread
读段寄存器值
voidsegread(structSREGS*segtbl);
structSREGSsegs;
segread(&segs);
printf("Currentsegmentregistersettings\n\n");
printf("CS:
%X DS:
%X\n",segs.cs,segs.ds);
printf("ES:
%X SS:
%X\n",segs.es,segs.ss);
setactivepage
设置图形输出活动页
voidfarsetactivepage(intpagenum);
/*selectadriverandmodethatsupports*/
/*multiplepages. */
intgdriver=EGA,gmode=EGAHI,errorcode;
intx,y,ht;
x=getmaxx()/2;
y=getmaxy()/2;
ht=textheight("W");
/* selecttheoffscreenpagefordrawing*/
(1);
/*drawalineonpage#1*/
line(0,0,getmaxx(),getmaxy());
/*outputamessageonpage#1*/
settextjustify(CENTER_TEXT,CENTER_TEXT);
outtextxy(x,y,"Thisispage#1:
outtextxy(x,y+ht,"Pressanykeytohalt:
/*selectdrawingtopage#0*/
setactivepage(0);
/*outputamessage onpage#0*/
outtextxy(x,y,"Thisispage#0.");
outtextxy(x,y+ht,"Pressanykeytoviewpage#1:
/*selectpage#1asthevisiblepage*/
setvisualpage
setallpallette
按指定方式改变所有的调色板颜色
voidfarsetallpallette(structpalette,far*pallette);
structpalettetypepal;
intcolor,maxcolor,ht;
inty=10;
charmsg[80];
maxcolor=getmaxcolor();
ht=2*textheight("W");
/*grabacopyofthepalette*/
getpalette(&pal);
/*displaythedefaultpalettecolors*/
for(color=1;color<=maxcolor;color++)
setcolor(color);
sprintf(msg,"Color:
%d",color);
outtextxy(1,y,msg);
y+=ht;
/*waitforakey*/
/*blackoutthecolorsonebyone*/
setpalette(color,BLACK);
/*restorethepalettecolors*/
setallpalette(&pal);
setaspectratio
设置图形纵横比
voidfarsetaspectratio(intxasp,intyasp);
intxasp,yasp,midx,midy;
setcolor(getmaxcolor());
/*getcurrentaspectratiosettings*/
getaspectratio(&xasp,&yasp);
/*drawnormalcircle*/
circle(midx,midy,100);
/*claerthescreen*/
cleardevice();
/*adjusttheaspectforawidecircle*/
setaspectratio(xasp/2,yasp);
/*adjusttheaspectforanarrowcircle*/
setaspectratio(xasp,yasp/2);
setbkcolor
用调色板设置当前背景颜色
voidfarsetbkcolor(intcolor);
/*multiplebackgroundcolors. */
intbkcol,maxcolor,x,y;
/*maximumcolorindexsupported*/
/*forcenteringtextmessages*/
/*loopthrought
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1