转自百度百科: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
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.
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.
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
c c库函数 c++
-
1