c语言m开头函数大全新手必看 1.docx
《c语言m开头函数大全新手必看 1.docx》由会员分享,可在线阅读,更多相关《c语言m开头函数大全新手必看 1.docx(16页珍藏版)》请在冰豆网上搜索。
![c语言m开头函数大全新手必看 1.docx](https://file1.bdocx.com/fileroot1/2022-11/27/17b2a2d1-2fba-4262-ae49-5305f3f40937/17b2a2d1-2fba-4262-ae49-5305f3f409371.gif)
c语言m开头函数大全新手必看1
C语言函数大全(m开头)
main()主函数
每一C程序都必须有一main()函数,可以根据自己的爱好把它放在程序的某个地方。
有些程序员把它放在最前面,而另一些程序员把它放在最后面,无论放在哪个地方,以下几点说明都是适合的。
1.main()参数
在TurboC2.0启动过程中,传递main()函数三个参数:
argc,argv和env。
*argc:
整数,为传给main()的命令行参数个数。
*argv:
字符串数组。
在DOS3.X版本中,argv[0]为程序运行的全路径名;对DOS3.0
以下的版本,argv[0]为空串("")。
argv[1]为在DOS命令行中执行程序名后的第一个字符串;
argv[2]为执行程序名后的第二个字符串;
...
argv[argc]为NULL。
*env:
安符串数组。
env[]的每一个元素都包含ENVVAR=value形式的字符
串。
其中ENVVAR为环境变量如PATH或87。
value为ENVVAR的对应值如C:
\DOS,C:
\TURBOC(对于PATH)或YES(对于87)。
TurboC2.0启动时总是把这三个参数传递给main()函数,可以在用户程序中
说明(或不说明)它们,如果说明了部分(或全部)参数,它们就成为main()子程序
的局部变量。
请注意:
一旦想说明这些参数,则必须按argc,argv,env的顺序,如以下
的例子:
main()
main(intargc)
main(intargc,char*argv[])
main(intargc,char*argv[],char*env[])
其中第二种情况是合法的,但不常见,因为在程序中很少有只用argc,而不
用argv[]的情况。
以下提供一样例程序EXAMPLE.EXE,演示如何在main()函数中使用三个参数:
/*programnameEXAMPLE.EXE*/
#include
#include
main(intargc,char*argv[],char*env[])
{
inti;
printf("Thesearethe%dcommand-lineargumentspassedto
main:
\n\n",argc);
for(i=0;i<=argc;i++)
printf("argv[%d]:
%s\n",i,argv[i]);
printf("\nTheenvironmentstring(s)onthissystemare:
\n\n");
for(i=0;env[i]!
=NULL;i++)
printf("env[%d]:
%s\n",i,env[i]);
}
如果在DOS提示符下,按以下方式运行EXAMPLE.EXE:
C:
\examplefirst_argument"argumentwithblanks"34"lastbut
one"stop!
注意:
可以用双引号括起内含空格的参数,如本例中的:
"argument
withblanks"和"Lastbutone")。
结果是这样的:
Thevalueofargcis7
Thesearethe7command-lineargumentspassedtomain:
argv[0]:
C:
\TURBO\EXAMPLE.EXE
argv[1]:
first_argument
argv[2]:
argumentwithblanks
argv[3]:
3
argv[4]:
4
argv[5]:
lastbutone
argv[6]:
stop!
argv[7]:
(NULL)
Theenvironmentstring(s)onthissystemare:
env[0]:
COMSPEC=C:
\COMMAND.COM
env[1]:
PROMPT=$P$G/*视具体设置而定*/
env[2]:
PATH=C:
\DOS;C:
\TC/*视具体设置而定*/
应该提醒的是:
传送main()函数的命令行参数的最大长度为128个字符(包
括参数间的空格),这是由DOS限制的。
函数名:
matherr
功能:
用户可修改的数学错误处理程序
用法:
intmatherr(structexception*e);
程序例:
/*Thisisauser-definedmatherrfunctionthatprevents
anyerrormessagesfrombeingprinted.*/
#include
intmatherr(structexception*a)
{
return1;
}
函数名:
memccpy
功能:
从源source中拷贝n个字节到目标destin中
用法:
void*memccpy(void*destin,void*source,unsignedcharch,
unsignedn);
程序例:
#include
#include
intmain(void)
{
char*src="Thisisthesourcestring";
chardest[50];
char*ptr;
ptr=memccpy(dest,src,'c',strlen(src));
if(ptr)
{
*ptr='\0';
printf("Thecharacterwasfound:
%s\n",dest);
}
else
printf("Thecharacterwasn'tfound\n");
return0;
}
函数名:
malloc
功能:
内存分配函数
用法:
void*malloc(unsignedsize);
程序例:
#include
#include
#include
#include
intmain(void)
{
char*str;
/*allocatememoryforstring*/
/*Thiswillgenerateanerrorwhencompiling*/
/*withC++,usethenewoperatorinstead.*/
if((str=malloc(10))==NULL)
{
printf("Notenoughmemorytoallocatebuffer\n");
exit
(1);/*terminateprogramifoutofmemory*/
}
/*copy"Hello"intostring*/
strcpy(str,"Hello");
/*displaystring*/
printf("Stringis%s\n",str);
/*freememory*/
free(str);
return0;
}
函数名:
memchr
功能:
在数组的前n个字节中搜索字符
用法:
void*memchr(void*s,charch,unsignedn);
程序例:
#include
#include
intmain(void)
{
charstr[17];
char*ptr;
strcpy(str,"Thisisastring");
ptr=memchr(str,'r',strlen(str));
if(ptr)
printf("Thecharacter'r'isatposition:
%d\n",ptr-str);
else
printf("Thecharacterwasnotfound\n");
return0;
}
函数名:
memcpy
功能:
从源source中拷贝n个字节到目标destin中
用法:
void*memcpy(void*destin,void*source,unsignedn);
程序例:
#include
#include
intmain(void)
{
charsrc[]="******************************";
chardest[]="abcdefghijlkmnopqrstuvwxyz0123456709";
char*ptr;
printf("destinationbeforememcpy:
%s\n",dest);
ptr=memcpy(dest,src,strlen(src));
if(ptr)
printf("destinationaftermemcpy:
%s\n",dest);
else
printf("memcpyfailed\n");
return0;
}
函数名:
memicmp
功能:
比较两个串s1和s2的前n个字节,忽略大小写
用法:
intmemicmp(void*s1,void*s2,unsignedn);
程序例:
#include
#include
intmain(void)
{
char*buf1="ABCDE123";
char*buf2="abcde456";
intstat;
stat=memicmp(buf1,buf2,5);
printf("Thestringstoposition5are");
if(stat)
printf("not");
printf("thesame\n");
return0;
}
函数名:
memmove
功能:
移动一块字节
用法:
void*memmove(void*destin,void*source,unsignedn);
程序例:
#include
#include
intmain(void)
{
char*dest="abcdefghijklmnopqrstuvwxyz0123456789";
char*src="******************************";
printf("destinationpriortomemmove:
%s\n",dest);
memmove(dest,src,26);
printf("destinationaftermemmove:
%s\n",dest);
return0;
}
函数名:
memset
功能:
设置s中的所有字节为ch,s数组的大小由n给定
用法:
void*memset(void*s,charch,unsignedn);
程序例:
#include
#include
#include
intmain(void)
{
charbuffer[]="Helloworld\n";
printf("Bufferbeforememset:
%s\n",buffer);
memset(buffer,'*',strlen(buffer)-1);
printf("Bufferaftermemset:
%s\n",buffer);
return0;
}
函数名:
mkdir
功能:
建立一个目录
用法:
intmkdir(char*pathname);
程序例:
#include
#include
#include
#include
intmain(void)
{
intstatus;
clrscr();
status=mkdir("asdfjklm");
(!
status)?
(printf("Directorycreated\n")):
(printf("Unabletocreatedirectory\n"));
getch();
system("dir");
getch();
status=rmdir("asdfjklm");
(!
status)?
(printf("Directorydeleted\n")):
(perror("Unabletodeletedirectory"));
return0;
}
函数名:
mktemp
功能:
建立唯一的文件名
用法:
char*mktemp(char*template);
程序例:
#include
#include
intmain(void)
{
/*fnamedefinesthetemplateforthe
temporaryfile.*/
char*fname="TXXXXXX",*ptr;
ptr=mktemp(fname);
printf("%s\n",ptr);
return0;
}
函数名:
MK_FP
功能:
设置一个远指针
用法:
voidfar*MK_FP(unsignedseg,unsignedoff);
程序例:
#include
#include
intmain(void)
{
intgd,gm,i;
unsignedintfar*screen;
detectgraph(&gd,&gm);
if(gd==HERCMONO)
screen=MK_FP(0xB000,0);
else
screen=MK_FP(0xB800,0);
for(i=0;i<26;i++)
screen[i]=0x0700+('a'+i);
return0;
}QQ291911320
函数名:
modf
功能:
把数分为指数和尾数
用法:
doublemodf(doublevalue,double*iptr);
程序例:
#include
#include
intmain(void)
{
doublefraction,integer;
doublenumber=100000.567;
fraction=modf(number,&integer);
printf("Thewholeandfractionalpartsof%lfare%lfand%lf\n",
number,integer,fraction);
return0;
}
函数名:
movedata
功能:
拷贝字节
用法:
voidmovedata(intsegsrc,intoffsrc,intsegdest,
intoffdest,unsignednumbytes);
程序例:
#include
#defineMONO_BASE0xB000
/*savesthecontentsofthemonochromescreeninbuffer*/
voidsave_mono_screen(charnear*buffer)
{
movedata(MONO_BASE,0,_DS,(unsigned)buffer,80*25*2);
}
intmain(void)
{
charbuf[80*25*2];
save_mono_screen(buf);
}
函数名:
moverel
功能:
将当前位置(CP)移动一相对距离
用法:
voidfarmoverel(intdx,intdy);
程序例:
#include
#include
#include
#include
intmain(void)
{
/*requestautodetection*/
intgdriver=DETECT,gmode,errorcode;
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*/
}
/*movetheC.P.tolocation(20,30)*/
moveto(20,30);
/*plotapixelattheC.P.*/
putpixel(getx(),gety(),getmaxcolor());
/*createandoutputamessageat(20,30)*/
sprintf(msg,"(%d,%d)",getx(),gety());
outtextxy(20,30,msg);
/*movetoapointarelativedistance*/
/*awayfromthecurrentvalueofC.P.*/
moverel(100,100);
/*plotapixelattheC.P.*/
putpixel(getx(),gety(),getmaxcolor());
/*createandoutputamessageatC.P.*/
sprintf(msg,"(%d,%d)",getx(),gety());
outtext(msg);
/*cleanup*/
getch();
closegraph();
return0;
}
函数名:
movetext
功能:
将屏幕文本从一个矩形区域拷贝到另一个矩形区域
用法:
intmovetext(intleft,inttop,intright,intbottom,
intnewleft,intnewtop);
程序例:
#include
#include
intmain(void)
{
char*str="Thisisateststring";
clrscr();
cputs(str);
getch();
movetext(1,1,strlen(str),2,10,10);
getch();
return0;
}
函数名:
moveto
功能:
将CP移到(x,y)
用法:
voidfarmoveto(intx,inty);
程序例:
#include
#include
#include
#include
intmain(void)
{
/*requestautodetection*/
intgdriver=DETECT,gmode,errorcode;
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*/
}
/*movetheC.P.tolocation(20,30)*/
moveto(20,30);
/*plotapixelattheC.P.*/
putpixel(getx(),gety(),getmaxcolor());
/*createandoutputamessageat(20,30)*/
sprintf(msg,"(%d,%d)",getx(),gety());
outtextxy(20,30,msg);
/*moveto(100,100)*/
moveto(100,100);
/*plotapixelattheC.P.*/
putpixel(getx(),gety(),getmaxcolor());
/*createandoutputamessageatC.P.*/
sprintf(msg,"(%d,%d)",getx(),gety());
outtext(msg);
/*cleanup*/
getch();
closegraph();
return0;
}
函数名:
movemem
功能:
移动一块字节
用法:
voidmovemem(void*source,void*destin,unsignedlen);
程序例:
#include
#include
#include
#include
intmain(void)
{
char*source="BorlandInternational";
char*destination;
intlength;
length=strlen(source);
destination=malloc(length+1);
movmem(source,destination,length);
printf("%s\n",destination);
return0;
}
函数名:
normvideo
功能:
选择正常亮度字符
用法:
voidnormvideo(void);
程序例:
#include
intmain(void)
{
normvideo();
cprintf("NORMALIntensityText\r\n");
return0;
}
函数名:
nosound
功能:
关闭PC扬声器
用法:
voidnosound(void);
程序例:
/*Emitsa7-Hztonefor10seconds.
Truestory:
7Hzistheresonantfrequencyofachicken'sskullcavity.
Thiswasdeterminedempiric