memcmp

转自百度百科:http://baike.baidu.com/view/1026877.htm

memcmp

原型:int memcmp(const void *buf1, const void *buf2, unsigned int count);
用法:#include <string.h>或#include<memory.h>
功能:比较内存区域buf1和buf2的前count个字节。
说明:
当buf1<buf2时,返回值<0
当buf1=buf2时,返回值=0
当buf1>buf2时,返回值>0
举例:
#include <conio.h>
#include <string.h>
#include<stdio.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");
return 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.
参考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 characters of the object pointed to by s1 to
the first n characters of the object pointed to by s2.
Returns
The 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 object
pointed to by s2.
[1]
参考资料
  • 1.  ISO
    copyright office .ISO/IEC 1999 Programming languages — C .Switzerland :Switzerland ,1999 :327 .

扩展阅读:
  • 1

    c c库函数 c++

原文链接: https://www.cnblogs.com/pamxy/archive/2013/03/09/2991490.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    memcmp

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/80109

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 下午7:24
下一篇 2023年2月9日 下午7:25

相关推荐