C函数大全M文档格式.docx
《C函数大全M文档格式.docx》由会员分享,可在线阅读,更多相关《C函数大全M文档格式.docx(17页珍藏版)》请在冰豆网上搜索。
的局部变量。
请注意:
一旦想说明这些参数,则必须按argc,argv,env的顺序,如以下
的例子:
main()
main(intargc)
main(intargc,char*argv[])
main(intargc,char*argv[],char*env[])
其中第二种情况是合法的,但不常见,因为在程序中很少有只用argc,而不
用argv[]的情况。
以下提供一样例程序EXAMPLE.EXE,演示如何在main()函数中使用三个参数:
/*programnameEXAMPLE.EXE*/
#include
{
inti;
printf("
Thesearethe%dcommand-lineargumentspassedto
main:
\n\n"
argc);
for(i=0;
i<
=argc;
i++)
argv[%d]:
%s\n"
i,argv[i]);
\nTheenvironmentstring(s)onthissystemare:
);
env[i]!
=NULL;
env[%d]:
i,env[i]);
}
如果在DOS提示符下,按以下方式运行EXAMPLE.EXE:
C:
\examplefirst_argument"
argumentwithblanks"
34"
lastbut
one"
stop!
注意:
可以用双引号括起内含空格的参数,如本例中的:
"
argument
withblanks"
和"
Lastbutone"
)。
结果是这样的:
Thevalueofargcis7
Thesearethe7command-lineargumentspassedtomain:
argv[0]:
\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;
\TC/*视具体设置而定*/
应该提醒的是:
传送main()函数的命令行参数的最大长度为128个字符(包
括参数间的空格),这是由DOS限制的。
函数名:
matherr
功能:
用户可修改的数学错误处理程序
用法:
intmatherr(structexception*e);
程序例:
/*Thisisauser-definedmatherrfunctionthatprevents
anyerrormessagesfrombeingprinted.*/
intmatherr(structexception*a)
return1;
memccpy
从源source中拷贝n个字节到目标destin中
void*memccpy(void*destin,void*source,unsignedcharch,
unsignedn);
intmain(void)
char*src="
Thisisthesourcestring"
;
chardest[50];
char*ptr;
ptr=memccpy(dest,src,'
c'
strlen(src));
if(ptr)
*ptr='
\0'
Thecharacterwasfound:
%s\n"
dest);
else
Thecharacterwasn'
tfound\n"
return0;
malloc
内存分配函数
void*malloc(unsignedsize);
char*str;
/*allocatememoryforstring*/
/*Thiswillgenerateanerrorwhencompiling*/
/*withC++,usethenewoperatorinstead.*/
if((str=malloc(10))==NULL)
Notenoughmemorytoallocatebuffer\n"
exit
(1);
/*terminateprogramifoutofmemory*/
/*copy"
Hello"
intostring*/
strcpy(str,"
/*displaystring*/
Stringis%s\n"
str);
/*freememory*/
free(str);
memchr
在数组的前n个字节中搜索字符
void*memchr(void*s,charch,unsignedn);
charstr[17];
Thisisastring"
ptr=memchr(str,'
r'
strlen(str));
Thecharacter'
isatposition:
%d\n"
ptr-str);
Thecharacterwasnotfound\n"
memcpy
void*memcpy(void*destin,void*source,unsignedn);
charsrc[]="
******************************"
chardest[]="
abcdefghijlkmnopqrstuvwxyz0123456709"
destinationbeforememcpy:
ptr=memcpy(dest,src,strlen(src));
destinationaftermemcpy:
memcpyfailed\n"
memicmp
比较两个串s1和s2的前n个字节,忽略大小写
intmemicmp(void*s1,void*s2,unsignedn);
char*buf1="
ABCDE123"
char*buf2="
abcde456"
intstat;
stat=memicmp(buf1,buf2,5);
Thestringstoposition5are"
if(stat)
not"
thesame\n"
memmove
移动一块字节
void*memmove(void*destin,void*source,unsignedn);
char*dest="
abcdefghijklmnopqrstuvwxyz0123456789"
destinationpriortomemmove:
memmove(dest,src,26);
destinationaftermemmove:
memset
设置s中的所有字节为ch,s数组的大小由n给定
void*memset(void*s,charch,unsignedn);
charbuffer[]="
Helloworld\n"
Bufferbeforememset:
buffer);
memset(buffer,'
*'
strlen(buffer)-1);
Bufferaftermemset:
mkdir
建立一个目录
intmkdir(char*pathname);
intstatus;
clrscr();
status=mkdir("
asdfjklm"
(!
status)?
(printf("
Directorycreated\n"
)):
(printf("
Unabletocreatedirectory\n"
));
getch();
system("
dir"
status=rmdir("
Directorydeleted\n"
(perror("
Unabletodeletedirectory"
mktemp
建立唯一的文件名
char*mktemp(char*template);
/*fnamedefinesthetemplateforthe
temporaryfile.*/
char*fname="
TXXXXXX"
*ptr;
ptr=mktemp(fname);
ptr);
MK_FP
设置一个远指针
voidfar*MK_FP(unsignedseg,unsignedoff);
intgd,gm,i;
unsignedintfar*screen;
detectgraph(&
gd,&
gm);
if(gd==HERCMONO)
screen=MK_FP(0xB000,0);
screen=MK_FP(0xB800,0);
for(i=0;
26;
screen[i]=0x0700+('
a'
+i);
modf
把数分为指数和尾数
doublemodf(doublevalue,double*iptr);
doublefraction,integer;
doublenumber=100000.567;
fraction=modf(number,&
integer);
Thewholeandfractionalpartsof%lfare%lfand%lf\n"
number,integer,fraction);
movedata
拷贝字节
voidmovedata(intsegsrc,intoffsrc,intsegdest,
intoffdest,unsignednumbytes);
#defineMONO_BASE0xB000
/*savesthecontentsofthemonochromescreeninbuffer*/
voidsave_mono_screen(charnear*buffer)
movedata(MONO_BASE,0,_DS,(unsigned)buffer,80*25*2);
charbuf[80*25*2];
save_mono_screen(buf);
moverel
将当前位置(CP)移动一相对距离
voidfarmoverel(intdx,intdy);
/*requestautodetection*/
intgdriver=DETECT,gmode,errorcode;
charmsg[80];
/*initializegraphicsandlocalvariables*/
initgraph(&
gdriver,&
gmode,"
/*readresultofinitialization*/
errorcode=graphresult();
if(errorcode!
=grOk)/*anerroroccurred*/
Graphicserror:
grapherrormsg(errorcode));
Pressanykeytohalt:
/*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);
/*createandoutputamessageatC.P.*/
outtext(msg);
/*cleanup*/
closegraph();
movetext
将屏幕文本从一个矩形区域拷贝到另一个矩形区域
intmovetext(intleft,inttop,intright,intbottom,
intnewleft,intnewtop);
char*str="
Thisisateststring"
cputs(str);
movetext(1,1,strlen(str),2,10,10);
moveto
将CP移到(x,y)
voidfarmoveto(intx,inty);
/*moveto(100,100)*/
moveto(100,100);
movemem
voidmovemem(void*source,void*destin,unsignedlen);
char*source="
BorlandInternational"
char*destination;
intlength;
length=strlen(source);
destination=