C语言标准库函数总结
一.动态内存分配
1.malloc
原型:extern void *malloc(unsigned int num_bytes);
用法:#include <alloc.h>
功能:分配长度为num_bytes字节的内存块
说明:如果分配成功则返回指向被分配内存的指针,否则返回空指针NULL。
当内存不再使用时,应使用free()函数将内存块释放。
举例:
// malloc.c
#include <syslib.h>
#include <alloc.h>
main()
{
char *p;
clrscr(); // clear screen
p=(char *)malloc(100);
if(p)
printf("Memory Allocated at: %x",p);
else
printf("Not Enough Memory!\n");
free(p);
getchar();
return 0;
}
2.calloc
原型:extern void *calloc(int num_elems, int elem_size);
用法:#include <alloc.h>
功能:为具有num_elems个长度为elem_size元素的数组分配内存
说明:如果分配成功则返回指向被分配内存的指针,否则返回空指针NULL。
当内存不再使用时,应使用free()函数将内存块释放。
举例:
// calloc.c
#include <syslib.h>
#include <alloc.h>
main()
{
char *p;
clrscr(); // clear screen
p=(char *)calloc(100,sizeof(char));
if(p)
printf("Memory Allocated at: %x",p);
else
printf("Not Enough Memory!\n");
free(p);
getchar();
return 0;
}
3.realloc
原型:extern void *realloc(void *mem_address, unsigned int newsize);
用法:#include <alloc.h>
功能:改变mem_address所指内存区域的大小为newsize长度。
说明:如果重新分配成功则返回指向被分配内存的指针,否则返回空指针NULL。
当内存不再使用时,应使用free()函数将内存块释放。
举例:
// realloc.c
#include <syslib.h>
#include <alloc.h>
main()
{
char *p;
clrscr(); // clear screen
p=(char *)malloc(100);
if(p)
printf("Memory Allocated at: %x",p);
else
printf("Not Enough Memory!\n");
getchar();
p=(char *)realloc(p,256);
if(p)
printf("Memory Reallocated at: %x",p);
else
printf("Not Enough Memory!\n");
free(p);
getchar();
return 0;
}
4.free
原型:extern void free(void *p);
用法:#include <alloc.h>
功能:释放指针p所指向的的内存空间。
说明:p所指向的内存空间必须是用calloc,malloc,realloc所分配的内存。
如果p为NULL或指向不存在的内存块则不做任何操作。
举例:
// free.c
#include <syslib.h>
#include <alloc.h>
main()
{
char *p;
clrscr(); // clear screen
textmode(0x00);
p=(char *)malloc(100);
if(p)
printf("Memory Allocated at: %x",p);
else
printf("Not Enough Memory!\n");
getchar();
free(p); // release memory to reuse it
p=(char *)calloc(100,1);
if(p)
printf("Memory Reallocated at: %x",p);
else
printf("Not Enough Memory!\n");
free(p); // release memory at program end
getchar();
return 0;
}
二.字符串函数
1.memccpy
原型:extern void *memccpy(void *dest, void *src, unsigned char ch, unsigned int count);
用法:#include <string.h>
功能:由src所指内存区域复制不多于count个字节到dest所指内存区域,如果遇到字符ch则停止复制。
说明:返回指向字符ch后的第一个字符的指针,如果src前n个字节中不存在ch则返回NULL。ch被复制。
举例:
// memccpy.c
#include <syslib.h>
#include <string.h>
main()
{
char *s="Golden Global View";
char d[20],*p;
clrscr();
p=memccpy(d,s,'x',strlen(s));
if(p)
{
*p='\0'; // MUST Do This
printf("Char found: %s.\n",d);
}
else
printf("Char not found.\n");
getchar();
return 0;
}
2.memchr
原型:extern void *memchr(void *buf, char ch, unsigned count);
用法:#include <string.h>
功能:从buf所指内存区域的前count个字节查找字符ch。
说明:当第一次遇到字符ch时停止查找。如果成功,返回指向字符ch的指针;否则返回NULL。
举例:
// memchr.c
#include <syslib.h>
#include <string.h>
main()
{
char *s="Hello, Programmers!";
char *p;
clrscr();
p=memchr(s,'P',strlen(s));
if(p)
printf("%s",p);
else
printf("Not Found!");
getchar();
return 0;
}
3.memcmp
原型:extern int memcmp(void *buf1, void *buf2, unsigned int count);
用法:#include <string.h>
功能:比较内存区域buf1和buf2的前count个字节。
说明:
当buf1<buf2时,返回值<0
当buf1=buf2时,返回值=0
当buf1>buf2时,返回值>0
举例:
// memcmp.c
#include <syslib.h>
#include <string.h>
main()
{
char *s1="Hello, Programmers!";
char *s2="Hello, programmers!";
int r;
clrscr();
r=memcmp(s1,s2,strlen(s1));
if(!r)
printf("s1 and s2 are identical");
else
if(r<0)
printf("s1 less than s2");
else
printf("s1 greater than s2");
getchar();
return 0;
}
4.memcpy
原型:extern void *memcpy(void *dest, void *src, unsigned int count);
用法:#include <string.h>
功能:由src所指内存区域复制count个字节到dest所指内存区域。
说明:src和dest所指内存区域不能重叠,函数返回指向dest的指针。
举例:
// memcpy.c
#include <syslib.h>
#include <string.h>
main()
{
char *s="Golden Global View";
char d[20];
clrscr();
memcpy(d,s,strlen(s));
d[strlen(s)]=0;
printf("%s",d);
getchar();
return 0;
}
5.memicmp
原型:extern int memicmp(void *buf1, void *buf2, unsigned int count);
用法:#include <string.h>
功能:比较内存区域buf1和buf2的前count个字节但不区分字母的大小写。
说明:memicmp同memcmp的唯一区别是memicmp不区分大小写字母。
当buf1<buf2时,返回值<0
当buf1=buf2时,返回值=0
当buf1>buf2时,返回值>0
举例:
// memicmp.c
#include <syslib.h>
#include <string.h>
main()
{
char *s1="Hello, Programmers!";
char *s2="Hello, programmers!";
int r;
clrscr();
r=memicmp(s1,s2,strlen(s1));
if(!r)
printf("s1 and s2 are identical");
else
if(r<0)
printf("s1 less than s2");
else
printf("s1 greater than s2");
getchar();
return 0;
}
C语言标准库函数总结的更多相关文章
- C语言标准库函数(网络上copy的)
C语言标准库函数 标准io函数Standard C I/Oclearerr() clears errorsfclose() close a filefeof() true if at the end- ...
- C语言标准库函数strcpy与strcmp的简单实现
//C语言标准库函数strcpy的一种简单实现. //返回值:目标串的地址. //对于出现异常的情况ANSI-C99标准并未定义,故由实现者决定返回值,通常为NULL. //参数:des为目标字符串, ...
- C语言标准库函数memcpy和memmove的区别以及内存重叠问题处理
①memcpy()和memmove()都是C语言中的标准库函数,定义在头文件string.h中,作用是拷贝一定长度的内存的内容,原型分别如下: void *memcpy(void *dst, cons ...
- C语言标准库函数qsort具体解释
1 函数简单介绍 功 能: 使用高速排序例程进行排序 头文件:stdlib.h 用 法: void qsort(void *base,int nelem,int width,int (*fcmp)(c ...
- 文件I/O之C标准库函数和系统库函数差别
1.首先C标准库函数是工作在系统库函数之上的.C标准库函数在读写文件时候都有一个文件流指针.FILE*fp=NULL;// fp=fopen(F_PATH,"r"); fp文件流指 ...
- strcpy c标准库函数
C语言标准库函数strcpy,把从src地址开始且含有NULL结束符的字符串复制到以dest开始的地址空间. 已知strcpy函数的原型是: char *strcpy(char *dst, const ...
- C语言库函数,头文件
参看:https://zhidao.baidu.com/question/328173842.html 该文件包含了的C语言标准库函数的定义 stdlib.h里面定义了五种类型.一些宏和通用工具函数. ...
- C++ 中库函数bsearch的简单研究(含示例)
/**//*bsearch函数声明如下: void *bsearch(const void *key, const void *base, size_t *nelem, ...
- 关于fefo函数
feof是C语言标准库函数函数,其原型在stdio.h中,其功能是检测流上的文件结束符. 函数原型: int feof(FILE *stream); 返回值:如果文件结束,则返回非0值,否则返回0 在 ...
随机推荐
- bzoj 3566: [SHOI2014]概率充电器【树形概率dp】
设g[u]为这个点被儿子和自己充上电的概率,f[u]为被儿子.父亲和自己充上电的概率 然后根据贝叶斯公式(好像是叫这个),1.P(A+B)=P(A)+P(B)-P(A)*P(B),2.P(A)=(P( ...
- bzoj 3239: Discrete Logging && 2480: Spoj3105 Mod【BSGS】
都是BSGS的板子题 此时 \( 0 \leq x \leq p-1 \) 设 \( m=\left \lceil \sqrt{p} \right \rceil ,x=i*m-j \)这里-的作用是避 ...
- 洛谷P3261 [JLOI2015]城池攻占(左偏树)
传送门 每一个城市代表的点开一个小根堆,把每一个骑士合并到它开始攻占的城池所代表的点上 然后开始dfs,每一次把子树里那些还活着的骑士合并上来 然后再考虑当前点的堆,一直pop直到骑士全死光或者剩下的 ...
- css设置页面全屏背景
.background { background: url(xxx.png); background-size: 100% 100%; height: 100%; position: fixed; w ...
- vi 和vim中的查找和替换
查找 命令模式输入 : /the-string-you-want-to-lookup 替换 命令模式输入 : s /from/to/
- cxf CXF搭建webService服务器
http://observer.blog.51cto.com/4267416/1231205 手动发布: public class ServerMain { public static void ma ...
- [Usaco2005 Jan]Muddy Fields泥泞的牧场
Description 雨连续不断的击打了放牛的牧场,一个R行C列的格子(1<=R<=50,1<=C<=50).虽然这对草来说是件好事,但这却使得一些没有草遮盖的土地变得很泥泞 ...
- hdu 1044 Collect More Jewels
题意: 一个n*m的迷宫,在t时刻后就会坍塌,问:在逃出来的前提下,能带出来多少价值的宝藏. 其中: ’*‘:代表墙壁: '.':代表道路: '@':代表起始位置: '<':代表出口: 'A'~ ...
- 16-1 WEB存储基本操作
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- ATM机(非函数版)
#include<stdio.h>#include<stdlib.h>int main(void){char zhangHao[]="123";int mi ...