1 / 36
文档名称:

C语言库函数C库函数.ppt

格式:ppt   大小:297KB   页数:36页
下载后只包含 1 个 PPT 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

分享

预览

C语言库函数C库函数.ppt

上传人:ouyangxiahe 2019/5/22 文件大小:297 KB

下载得到文件列表

C语言库函数C库函数.ppt

文档介绍

文档介绍:C语言常用库函数版本:讲师:联系方式:课程编号:()函数名:malloc 功能:内存分配函数 用法:void*malloc(unsignedsize); 程序例: #include<>/*For_MAX_PATHdefinition*/#include<> #include<> voidmain(void) { char*string;/*Allocatespaceforapathname*/ string=malloc(_MAX_PATH); if(string==NULL) printf("Insufficientmemoryavailable\n"); else{ printf("Memoryspaceallocatedforpathname\n"); free(string); printf("Memoryfreed\n"); } }动态存储分配()函数名:free 功能:释放已分配的块 用法:voidfree(void*ptr); 程序例: #include<> #include<> #include<> intmain(void) { char*str; /*allocatememoryforstring*/ str=malloc(10); /*copy"Hello"tostring*/ strcpy(str,"Hello"); /*displaystring*/ printf("Stringis%s\n",str); /*freememory*/ free(str); return0; }动态存储分配()函数名:realloc 功能:改变已分配内存的大小,ptr为已分配有内存区域的指针,newsize为新的长度,返回分配好的内存指针; 用法:void*realloc(void*ptr,unsignednewsize) 程序例: #include<> #include<> #include<> voidmain(void) { long*buffer; size_tsize; if((buffer=(long*)malloc(1000*sizeof(long)))==NULL) exit(1); size=_msize(buffer); printf("Sizeofblockaftermallocof1000longs:%u\n",size); /*Reallocateandshownewsize:*/ if((buffer=realloc(buffer,size+(1000*sizeof(long))))==NULL) exit(1); size=_msize(buffer); printf("Sizeofblockafterreallocof1000morelongs:%u\n",size); free(buffer); exit(0); }动态存储分配()函数名:calloc 功能:分配nelem个长度为elsize的内存空间并返回所分配内存的指针; 用法:void*calloc(size_tnum,size_tsize); 程序例: #include<> #include<> voidmain(void) { long*buffer; buffer=(long*)calloc(40,sizeof(long)); if(buffer!=NULL) printf("Allocated40longintegers\n"); else printf("Can'tallocatememory\n"); free(buffer); }类型转换函数()函数名:atof 功能:把字符串转换成双精度数,并返回这个数,错误返回0; 用法:doubleatof(constchar*nptr); 程序例: #include<> #include<> intmain(void) { floatf; char*str=""; f=atof(str); printf("string=%sfloat=%f\n",str,f); return0; }类型转换函数()函数名:atoi 功能:把字符串转换成整型数,并返回这个数,错误返回0; 用法:intat