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 1680: [Usaco2005 Mar]Yogurt factory【贪心】
贪心,一边读入一边更新mn,用mn更新答案,mn每次加s #include<iostream> #include<cstdio> using namespace std; in ...
- Java SE 开篇
一. Java SE 开篇 1. Java 基本数据类型及其对应的包装类 基本数据类型 对应的包装类 * byte Byte * boolean Boolean * char Character ...
- Springboot整合elasticsearch以及接口开发
Springboot整合elasticsearch以及接口开发 搭建elasticsearch集群 搭建过程略(我这里用的是elasticsearch5.5.2版本) 写入测试数据 新建索引book( ...
- docker 中部署一个springBoot项目
docker 中部署一个springBoot项目 (1)介绍 springBoot项目 1.项目结构 2.pom.xml <?xml version="1.0" encodi ...
- 软件常用版本英文snapshot和ga
版本号,顾名思义,系统.架包.软件的标识号.版本号的数字信息通俗易懂, 格式:主版本号+次版本+(修正版本号build-可选)+(编译版本号-可选)+英文常见号(重点). 常见号:英文各种架包名,Ma ...
- java 调用动态库打包sdk
java连接c++动态库并生成jar包提供给别人调用 1.需要将java通过jni生成头文件,并导入到c++项目并对c++进行jni方法继承 在项目的src目录执行,否则会提示 错误:找不到符号 ja ...
- ueditor 编辑
1.net config.json imageUrlPrefix
- 【已解决】python中文字符乱码(GB2312,GBK,GB18030相关的问题)
http://againinput4.blog.163.com/blog/static/1727994912011111011432810/ [已解决]python中文字符乱码(GB2312,GB ...
- ACM_给你100块钱
给你100块钱 Time Limit: 2000/1000ms (Java/Others) Problem Description: 小光见到昨晚旭能神没拿到一血,又损失了一百块,很同情他.但是为了不 ...
- Objective-c单例模式的正确写法--用dispatch 线程安全
单例模式在iOS开发中可能算是最常用的模式之一了,但是由于oc本身的语言特性,想要写一个正确的单例模式相对来说比较麻烦,这里我就抛砖引玉来聊一聊iOS中单例模式的设计思路.关于单例模式更多的介绍请参考 ...