memcmp


memcmp

文章插图
memcmpmemcmp是比较记忆体区域buf1和buf2的前count个位元组 。该函式是按位元组比较的 。
基本介绍外文名:memcmp
功能:比较buf1和buf2的前count个位元组
所需头档案:#include <string.h>
返回值:当buf1<buf2时,返回值<0
函式原型int memcmp(const void *buf1, const void *buf2, unsigned int count);功能比较记忆体区域buf1和buf2的前count个位元组 。所需头档案#include <string.h>或#include<memory.h>返回值当buf1<buf2时,返回值小于0当buf1==buf2时,返回值=0当buf1>buf2时,返回值大于0说明该函式是按位元组比较的 。例如:s1,s2为字元串时候memcmp(s1,s2,1)就是比较s1和s2的第一个位元组的ascII码值;memcmp(s1,s2,n)就是比较s1和s2的前n个位元组的ascII码值;如:char *s1="abc";char *s2="acd";int r=memcmp(s1,s2,3);就是比较s1和s2的前3个位元组,第一个位元组相等,第二个位元组比较中大小已经确定,不必继续比较第三位元组了 。所以r=-1.示例程式【memcmp】#include<string.h>#include<stdio.h>int main(){char *s1 = "Hello,Programmers!";char *s2 = "Hello,Programmers!";int r;r = memcmp(s1,s2,strlen(s1));if(!r)    printf("s1 and s2 are identical\n");/*s1等于s2*/elseif(r<0)    printf("s1 is less than s2\n");/*s1小于s2*/else    printf("s1 is greater than s2\n");/*s1大于s2*/return 0;}输出结果:s1 and s2 are identical请按任意键继续...参考文档参考C99文档:7.21.4.1 The memcmp functionSynopsis1#include <string.h>int memcmp(const void *s1, const void *s2, size_t n);DescriptionThe memcmp function compares the first n characters of the object pointed to by s1 tothe first n characters of the object pointed to by s2.ReturnsThe memcmp function 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 objectpointed to by s2.