![](http://d.aap5.com/20230209/t014e4550a02130cadf.webp)
memcmp是比较内存区域buf1和buf2的前count个字节。该函数来自是按字节比较的。
- 外文名 memcmp
- 功能 比较buf1和buf2的前count个字节
- 所需头文件 #include <string.h>
- 返回值 当buf1<buf2时,返回值<0
函数原型
int memcmp(const void *buf1, const void *buf2, uns来自igned int coun360百科t);
功能
比较内存区教菜且眼域buf1和buf2的前cou试被力零协喜这亲nt个字节。
所需头文件
#include <string.h>或#in领或八河取货除假顾息clude<me接仍到记振张mory.h>
返回值
当buf1<buf2时,返回值小于0
当buf1==buf2时,返回值=0
当buf1>buf2时,返回值大于0
说明
该函数是按字节比较的。
例如:
s1,s2为字符串时候memcmp(s1,s2,1)就是比较亚南迫席九二s1和s2的第一个字节的ascII码值;
memcmp(s1,s来自2,n)就是比较s胞推氧志官脱安跳问1和s2的前n个字节的ascII码值;
如:cha防步叫留境r *s1="abc";
char *s2="acd";
int r=memcmp(s1,s2,3);
具就是比较s1和s2的何强就笑笔倍前3个字节,第一个字节相等,第二个字节比较中大360百科小已经确定,不必继续比较破少第三字节了。所以r=-1.
参考文档
参考C99文档:
7.21.4.1 The memcmp function
Synopsis
1
铁全谓沙一兰没它#include <string.h>
int memcmp(const void *s1, const void *s2, size_t n);
Description
The memcmp function compares the first n cha利层体验升世指机racters of the object poi音军半nted to by s1 to
the first n characters of the object p翻纪独已用就确跑序ointed to by 三精那运争黄材足封s2.
Returns
The memcmp func金tion returns an integer greater than, equal to, or less than zero,
accordingly as the object pointed to by s1 is greater than, equal to, or less than the 或object
pointed to by s2.