《c语言的用法.docx》由会员分享,可在线阅读,更多相关《c语言的用法.docx(20页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、c语言的用法c语言的用法语言程序设计是一门基础的程序设计语言,学好这门课对以后程序开发至关重要。由于C语言灵敏、强大,初学者要全面地把握它非常吃力,因而在学习C语言的经过中,但一定要熟练把握C语言的流程控制语句、数组、函数、指针等基础知识的应用,为学习面向对象程序设计打下坚实的基础。下面我就为大家来介绍下c语言的用法。c语言的用法:abort功能:异常终止一个进程用法:voidabort(void);程序例:#includestdio.h#includestdlib.hintmain(void)printf(Callingabort()nabort();return0;/*Thisisneve
2、rreached*/c语言的用法:abs功能:求整数的绝对值用法:intabs(inti);程序例:#includestdio.h#includemath.hintmain(void)intnumber=-1234;printf(number:%dabsolutevalue:%dn,number,abs(number);return0;c语言的用法:absread,abswirte功能:绝对磁盘扇区读、写数据用法:intabsread(intdrive,intnsects,intsectno,void*buffer);intabswrite(intdrive,intnsects,intsect
3、no,void*buffer);程序例:/*absreadexample*/#includestdio.h#includeconio.h#includeprocess.h#includedos.hintmain(void)inti,strt,ch_out,sector;charbuf512;printf(InsertadisketteintodriveAandpressanykeyngetch();sector=0;if(absread(0,1,sector,buf)!=0)perror(Diskproblemexit(1);printf(ReadOKnstrt=3;for(i=0;ii+)c
4、h_out=bufstrt+i;putchar(ch_out);printf(nreturn(0);c语言的用法:access功能:确定文件的访问权限用法:intaccess(constchar*filename,intamode);程序例:#includestdio.h#includeio.hintfile_exists(char*filename);intmain(void)printf(DoesNOTEXIST.FILexist:%sn,file_exists(NOTEXISTS.FIL)?YES:NOreturn0;intfile_exists(char*filename)return
5、(access(filename,0)=0);c语言的用法:acos功能:反余弦c语言的用法用法:doubleacos(doublex);程序例:#includestdio.h#includemath.hintmain(void)doubleresult;doublex=0.5;result=acos(x);printf(Thearccosineof%lfis%lfn,x,result);return0;c语言的用法:allocmem功能:分配DOS存储段用法:intallocmem(unsignedsize,unsigned*seg);程序例:#includedos.h#includeall
6、oc.h#includestdio.hintmain(void)unsignedintsize,segp;intstat;size=64;/*(64x16)=1024bytes*/stat=allocmem(size,segp);if(stat=-1)printf(Allocatedmemoryatsegment:%xn,segp);elseprintf(Failed:maximumnumberofparagraphsavailableis%un,stat);return0;c语言的用法:arc功能:画一弧线用法:voidfararc(intx,inty,intstangle,intendan
7、gle,intradius);程序例:#includegraphics.h#includestdlib.h#includestdio.h#includeconio.hintmain(void)/*requestautodetection*/intgdriver=DETECT,gmode,errorcode;intmidx,midy;intstangle=45,endangle=135;intradius=100;/*initializegraphicsandlocalvariables*/initgraph(gdriver,gmode,/*readresultofinitialization*
8、/errorcode=graphresult();/*anerroroccurred*/if(errorcode!=grOk)printf(Graphicserror:%sn,grapherrormsg(errorcode);printf(Pressanykeytohalt:getch();exit(1);/*terminatewithanerrorcode*/midx=getmaxx()/2;midy=getmaxy()/2;setcolor(getmaxcolor();/*drawarc*/arc(midx,midy,stangle,endangle,radius);/*cleanup*/
9、getch();closegraph();return0;c语言的用法:asctime功能:转换日期和时间为ASCII码用法:char*asctime(conststructtm*tblock);程序例:#includestdio.h#includestring.h#includetime.hintmain(void)structtmt;charstr80;/*sampleloadingoftmstructure*/t.tm_sec=1;/*Seconds*/t.tm_min=30;/*Minutes*/t.tm_hour=9;/*Hour*/t.tm_mday=22;/*DayoftheMo
10、nth*/t.tm_mon=11;/*Month*/t.tm_year=56;/*Year-doesnotincludecentury*/t.tm_wday=4;/*Dayoftheweek*/t.tm_yday=0;/*Doesnotshowinasctime*/t.tm_isdst=0;/*IsDaylightSavTime;doesnotshowinasctime*/*convertsstructuretonullterminatedstring*/strcpy(str,asctime(t);printf(%sn,str);return0;c语言的用法:asin功能:反正弦c语言的用法用
11、法:doubleasin(doublex);程序例:#includestdio.h#includemath.hintmain(void)doubleresult;doublex=0.5;result=asin(x);printf(Thearcsinof%lfis%lfn,x,result);return(0);c语言的用法:assert功能:测试一个条件并可能使程序终止用法:voidassert(inttest);程序例:#includeassert.h#includestdio.h#includestdlib.hstructITEMintkey;intvalue;/*additemtolis
12、t,makesurelistisnotnull*/voidadditem(structITEM*itemptr)assert(itemptr!=NULL);/*additemtolist*/intmain(void)additem(NULL);return0;c语言的用法:atan功能:反正切c语言的用法用法:doubleatan(doublex);程序例:#includestdio.h#includemath.hintmain(void)doubleresult;doublex=0.5;result=atan(x);printf(Thearctangentof%lfis%lfn,x,resu
13、lt);return(0);c语言的用法:atan2功能:计算Y/X的反正切值用法:doubleatan2(doubley,doublex);程序例:#includestdio.h#includemath.hintmain(void)doubleresult;doublex=90.0,y=45.0;result=atan2(y,x);printf(Thearctangentratioof%lfis%lfn,(y/x),result);return0;c语言的用法:atexit功能:注册终止c语言的用法用法:intatexit(atexit_tfunc);程序例:#includestdio.h#
14、includestdlib.hvoidexit_fn1(void)printf(Exitfunction#1callednvoidexit_fn2(void)printf(Exitfunction#2callednintmain(void)/*postexitfunction#1*/atexit(exit_fn1);/*postexitfunction#2*/atexit(exit_fn2);return0;c语言的用法:atof功能:把字符串转换成浮点数用法:doubleatof(constchar*nptr);程序例:#includestdlib.h#includestdio.hintma
15、in(void)floatf;char*str=12345.67f=atof(str);printf(string=%sfloat=%fn,str,f);return0;c语言的用法:atoi功能:把字符串转换成长整型数用法:intatoi(constchar*nptr);程序例:#includestdlib.h#includestdio.hintmain(void)intn;char*str=12345.67n=atoi(str);printf(string=%sinteger=%dn,str,n);return0;c语言的用法:atol功能:把字符串转换成长整型数用法:longatol(c
16、onstchar*nptr);程序例:#includestdlib.h#includestdio.hintmain(void)longl;char*str=98765432l=atol(lstr);printf(string=%sinteger=%ldn,str,l);return(0);c语言的用法:bar功能:画一个二维条形图用法:voidfarbar(intleft,inttop,intright,intbottom);程序例:#includegraphics.h#includestdlib.h#includestdio.h#includeconio.hintmain(void)/*re
17、questautodetection*/intgdriver=DETECT,gmode,errorcode;intmidx,midy,i;/*initializegraphicsandlocalvariables*/initgraph(gdriver,gmode,/*readresultofinitialization*/errorcode=graphresult();if(errorcode!=grOk)/*anerroroccurred*/printf(Graphicserror:%sn,grapherrormsg(errorcode);printf(Pressanykeytohalt:g
18、etch();exit(1);/*terminatewithanerrorcode*/midx=getmaxx()/2;midy=getmaxy()/2;/*loopthroughthefillpatterns*/for(i=SOLID_FILL;iUSER_FILL;i+)/*setthefillstyle*/setfillstyle(i,getmaxcolor();/*drawthebar*/bar(midx-50,midy-50,midx+50,midy+50);getch();/*cleanup*/closegraph();return0;c语言的用法:bar3d功能:画一个三维条形图
19、用法:voidfarbar3d(intleft,inttop,intright,intbottom,intdepth,inttopflag);程序例:#includegraphics.h#includestdlib.h#includestdio.h#includeconio.hintmain(void)/*requestautodetection*/intgdriver=DETECT,gmode,errorcode;intmidx,midy,i;/*initializegraphics,localvariables*/initgraph(gdriver,gmode,/*readresultof
20、initialization*/errorcode=graphresult();if(errorcode!=grOk)/*anerroroccurred*/printf(Graphicserror:%sn,grapherrormsg(errorcode);printf(Pressanykeytohalt:getch();exit(1);/*terminatewitherrorcode*/midx=getmaxx()/2;midy=getmaxy()/2;/*loopthroughthefillpatterns*/for(i=EMPTY_FILL;iUSER_FILL;i+)/*setthefi
21、llstyle*/setfillstyle(i,getmaxcolor();/*drawthe3-dbar*/bar3d(midx-50,midy-50,midx+50,midy+50,10,1);getch();/*cleanup*/closegraph();return0;c语言的用法:bdos功能:DOS系统调用用法:intbdos(intdosfun,unsigneddosdx,unsigneddosal);程序例:#includestdio.h#includedos.h/*GetcurrentdriveasA,B,.*/charcurrent_drive(void)charcurdr
22、ive;/*Getcurrentdiskas0,1,.*/curdrive=bdos(0x19,0,0);return(A+curdrive);intmain(void)printf(Thecurrentdriveis%c:n,current_drive();return0;c语言的用法:bdosptr功能:DOS系统调用用法:intbdosptr(intdosfun,void*argument,unsigneddosal);程序例:#includestring.h#includestdio.h#includedir.h#includedos.h#includeerrno.h#includes
23、tdlib.h#defineBUFLEN80intmain(void)charbufferBUFLEN;inttest;printf(Enterfullpathnameofadirectoryngets(buffer);test=bdosptr(0x3B,buffer,0);if(test)printf(DOSerrormessage:%dn,errno);/*Seeerrno.hforerrorlistings*/exit(1);getcwd(buffer,BUFLEN);printf(Thecurrentdirectoryis:%sn,buffer);return0;c语言的用法:bios
24、com功能:串行I/O通信用法:intbioscom(intcmd,charabyte,intport);程序例:#includebios.h#includeconio.h#defineCOM10#defineDATA_READY0x100#defineTRUE1#defineFALSE0#defineSETTINGS(0x80|0x02|0x00|0x00)intmain(void)intin,out,status,DONE=FALSE;bioscom(0,SETTINGS,COM1);cprintf(.BIOSCOMESCtoexit.nwhile(!DONE)status=bioscom
25、(3,0,COM1);if(statusDATA_READY)if(out=bioscom(2,0,COM1)0x7F)!=0)putch(out);if(kbhit()if(in=getch()=x1B)DONE=TRUE;bioscom(1,in,COM1);return0;c语言的用法:biosdisk功能:软硬盘I/O用法:intbiosdisk(intcmd,intdrive,inthead,inttrack,intsectorintnsects,void*buffer);程序例:#includebios.h#includestdio.hintmain(void)intresult;
26、charbuffer512;printf(Testingtoseeifdrivea:isreadynresult=biosdisk(4,0,0,0,0,1,buffer);result=0x02;(result)?(printf(DriveA:Readyn):(printf(DriveA:NotReadyn);return0;c语言的用法:biosequip功能:检查设备用法:intbiosequip(void);程序例:#includebios.h#includestdio.hintmain(void)intresult;charbuffer512;printf(Testingtoseeif
27、drivea:isreadynresult=biosdisk(4,0,0,0,0,1,buffer);result=0x02;(result)?(printf(DriveA:Readyn):(printf(DriveA:NotReadyn);return0;c语言的用法:bioskey功能:直接使用BIOS服务的键盘接口用法:intbioskey(intcmd);程序例:#includestdio.h#includebios.h#includectype.h#defineRIGHT0x01#defineLEFT0x02#defineCTRL0x04#defineALT0x08intmain(v
28、oid)intkey,modifiers;/*function1returns0untilakeyispressed*/while(bioskey(1)=0);/*function0returnsthekeythatiswaiting*/key=bioskey(0);/*usefunction2todetermineifshiftkeyswereused*/modifiers=bioskey(2);if(modifiers)printf(if(modifiersRIGHT)printf(RIGHTif(modifiersLEFT)printf(LEFTif(modifiersCTRL)prin
29、tf(CTRLif(modifiersALT)printf(ALTprintf(/*printoutthecharacterread*/if(isalnum(key0xFF)printf(%cn,key);elseprintf(%#02xn,key);return0;c语言的用法:biosmemory功能:返回存储块大小用法:intbiosmemory(void);程序例:#includestdio.h#includebios.hintmain(void)intmemory_size;memory_size=biosmemory();/*returnsvalueupto640K*/printf
30、(RAMsize=%dKn,memory_size);return0;c语言的用法:biosprint功能:直接使用BIOS服务的打印机I/O用法:intbiosprint(intcmd,intbyte,intport);程序例:#includestdio.h#includeconio.h#includebios.hintmain(void)#defineSTATUS2/*printerstatuscommand*/#definePORTNUM0/*portnumberforLPT1*/intstatus,abyte=0;printf(Pleaseturnoffyourprinter.Pres
31、sanykeytocontinuengetch();status=biosprint(STATUS,abyte,PORTNUM);if(status0x01)printf(Devicetimeout.nif(status0x08)printf(I/Oerror.nif(status0x10)printf(Selected.nif(status0x20)printf(Outofpaper.nif(status0x40)printf(Acknowledge.nif(status0x80)printf(Notbusy.nreturn0;c语言的用法:biostime功能:读取或设置BIOS时间用法:
32、longbiostime(intcmd,longnewtime);程序例:#includestdio.h#includebios.h#includetime.h#includeconio.hintmain(void)longbios_time;clrscr();cprintf(Thenumberofclocktickssincemidnightis:rncprintf(Thenumberofsecondssincemidnightis:rncprintf(Thenumberofminutessincemidnightis:rncprintf(Thenumberofhourssincemidni
33、ghtis:rncprintf(rnPressanykeytoquit:while(!kbhit()bios_time=biostime(0,0L);gotoxy(50,1);cprintf(%lu,bios_time);gotoxy(50,2);cprintf(%.4f,bios_time/CLK_TCK);gotoxy(50,3);cprintf(%.4f,bios_time/CLK_TCK/60);gotoxy(50,4);cprintf(%.4f,bios_time/CLK_TCK/3600);return0;c语言的用法:brk功能:改变数据段空间分配用法:intbrk(void*e
34、ndds);程序例:#includestdio.h#includealloc.hintmain(void)char*ptr;printf(Changingallocationwithbrk()nptr=malloc(1);printf(Beforebrk()call:%lubytesfreen,coreleft();brk(ptr+1000);printf(Afterbrk()call:%lubytesfreen,coreleft();return0;c语言的用法:bsearch功能:二分法搜索用法:void*bsearch(constvoid*key,constvoid*base,size_
35、t*nelem,size_twidth,int(*fcmp)(constvoid*,const*);程序例:#includestdlib.h#includestdio.h#defineNELEMS(arr)(sizeof(arr)/sizeof(arr0)intnumarray=123,145,512,627,800,933;intnumeric(constint*p1,constint*p2)return(*p1-*p2);intlookup(intkey)int*itemptr;/*Thecastof(int(*)(constvoid*,constvoid*)isneededtoavoid
36、atypemismatcherroratcompiletime*/itemptr=bsearch(key,numarray,NELEMS(numarray),sizeof(int),(int(*)(constvoid*,constvoid*)numeric);return(itemptr!=NULL);intmain(void)if(lookup(512)printf(512isinthetable.nelseprintf(512isntinthetable.nreturn0;c语言的用法:cabs功能:计算复数的绝对值用法:doublecabs(structcomplexz);程序例:#in
37、cludestdio.h#includemath.hintmain(void)structcomplexz;doubleval;z.x=2.0;z.y=1.0;val=cabs(z);printf(Theabsolutevalueof%.2lfi%.2lfjis%.2lf,z.x,z.y,val);return0;c语言的用法:calloc功能:分配主存储器用法:void*calloc(size_tnelem,size_telsize);程序例:#includestdio.h#includealloc.hintmain(void)char*str=NULL;/*allocatememoryfo
38、rstring*/str=calloc(10,sizeof(char);/*copyHellointostring*/strcpy(str,Hello/*displaystring*/printf(Stringis%sn,str);/*freememory*/free(str);return0;c语言的用法:ceil功能:向上舍入用法:doubleceil(doublex);程序例:#includemath.h#includestdio.hintmain(void)doublenumber=123.54;doubledown,up;down=floor(number);up=ceil(numb
39、er);printf(originalnumber%5.2lfn,number);printf(numberroundeddown%5.2lfn,down);printf(numberroundedup%5.2lfn,up);return0;c语言的用法:cgets功能:从控制台读字符串用法:char*cgets(char*str);程序例:#includestdio.h#includeconio.hintmain(void)charbuffer83;char*p;/*Theresspacefor80charactersplustheNULLterminator*/buffer0=81;pri
40、ntf(Inputsomechars:p=cgets(buffer);printf(ncgetsread%dcharacters:%sn,buffer1,p);printf(Thereturnedpointeris%p,buffer0isat%pn,p,buffer);/*Leaveroomfor5charactersplustheNULLterminator*/buffer0=6;printf(Inputsomechars:p=cgets(buffer);printf(ncgetsread%dcharacters:%sn,buffer1,p);printf(Thereturnedpointe
41、ris%p,buffer0isat%pn,p,buffer);return0;c语言的用法:chdir功能:改变工作目录用法:intchdir(constchar*path);程序例:#includestdio.h#includestdlib.h#includedir.hcharold_dirMAXDIR;charnew_dirMAXDIR;intmain(void)if(getcurdir(0,old_dir)perror(getcurdir()exit(1);printf(Currentdirectoryis:%sn,old_dir);if(chdir()perror(chdir()exi
42、t(1);if(getcurdir(0,new_dir)perror(getcurdir()exit(1);printf(Currentdirectoryisnow:%sn,new_dir);printf(nChangingbacktoorignaldirectory:%sn,old_dir);if(chdir(old_dir)perror(chdir()exit(1);return0;c语言的用法:_chmod,chmod功能:改变文件的访问方式用法:intchmod(constchar*filename,intpermiss);程序例:#includesysstat.h#includest
43、dio.h#includeio.hvoidmake_read_only(char*filename);intmain(void)make_read_only(NOTEXIST.FILmake_read_only(MYFILE.FILreturn0;voidmake_read_only(char*filename)intstat;stat=chmod(filename,S_IREAD);if(stat)printf(Couldntmake%sread-onlyn,filename);elseprintf(Made%sread-onlyn,filename);c语言的用法:chsize功能:改变文
44、件大小用法:intchsize(inthandle,longsize);程序例:#includestring.h#includefcntl.h#includeio.hintmain(void)inthandle;charbuf11=0123456789/*createtextfilecontaining10bytes*/handle=open(DUMMY.FIL,O_CREAT);write(handle,buf,strlen(buf);/*truncatethefileto5bytesinsize*/chsize(handle,5);/*closethefile*/close(handle)
45、;return0;c语言的用法:circle功能:在给定半径以(x,y)为圆心画圆用法:voidfarcircle(intx,inty,intradius);程序例:#includegraphics.h#includestdlib.h#includestdio.h#includeconio.hintmain(void)/*requestautodetection*/intgdriver=DETECT,gmode,errorcode;intmidx,midy;intradius=100;/*initializegraphicsandlocalvariables*/initgraph(gdrive
46、r,gmode,/*readresultofinitialization*/errorcode=graphresult();if(errorcode!=grOk)/*anerroroccurred*/printf(Graphicserror:%sn,grapherrormsg(errorcode);printf(Pressanykeytohalt:getch();exit(1);/*terminatewithanerrorcode*/midx=getmaxx()/2;midy=getmaxy()/2;setcolor(getmaxcolor();/*drawthecircle*/circle(
47、midx,midy,radius);/*cleanup*/getch();closegraph();return0;c语言的用法:cleardevice功能:去除图形屏幕用法:voidfarcleardevice(void);程序例:#includegraphics.h#includestdlib.h#includestdio.h#includeconio.hintmain(void)/*requestautodetection*/intgdriver=DETECT,gmode,errorcode;intmidx,midy;/*initializegraphicsandlocalvariabl
48、es*/initgraph(gdriver,gmode,/*readresultofinitialization*/errorcode=graphresult();if(errorcode!=grOk)/*anerroroccurred*/printf(Graphicserror:%sn,grapherrormsg(errorcode);printf(Pressanykeytohalt:getch();exit(1);/*terminatewithanerrorcode*/midx=getmaxx()/2;midy=getmaxy()/2;setcolor(getmaxcolor();/*forcenteringscreenmessages*/settextjustify(CENTER_TEXT,CENTER_TEXT);/*outputamessagetothescreen*/outtextxy(midx,midy,pressanykeytoclearthescreen:/*waitforakey*/getch();/*clearthescreen*/cleardevice();/*outputanothermessage*/outtextxy(midx,midy,pressanykeytoquit:/*cleanup*/getch();closeg