server-api-memcmp

原型

The memcmp() function compares the first n bytes (each interpreted as unsigned char) of the memory areas s1 and s2.

用来比较s1和s2所指的内存区间前n个字符,进行比较操作时,假定两个不等的字节均为无符号字符(unsigned char)

函数原型
1
2
3
4
5
6
7
8
9
10
11
12
13
// s1
// 要比较内存区域1
// s2
// 要比较内存区域2
// n
// 要比较的字节数
//
// return:
// The memcmp() function returns an integer less than, equal to, or greater than zero if the first n bytes of s1 is found, respectively, to be less than, to match, or be greater than the first n bytes of s2.
// 若参数s1和s2所指的内存内容都完全相同则返回0值。s1若大于s2则返回大于0的值。s1若小于s2则返回小于0的值。
//
//
int memcmp(const void *s1, const void *s2, size_t n);

使用参考

简单的比较操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <string.h>
#include <strings.h>
#include <stdio.h>

int main()
{
char *a = "aBcDeF";
char *b = "AbCdEf";
char *c = "aacdef";
char *d = "aBcDeF";

printf("memcmp(a,b):%d\n",memcmp((void*)a, (void*) b, 6));
printf("memcmp(a,c):%d\n",memcmp((void*)a, (void*) c, 6));
printf("memcmp(a,d):%d\n",memcmp((void*)a, (void*) d, 6));

printf("bcmp(a,b):%d\n",bcmp((void*)a, (void*) b, 6));
printf("bcmp(a,c):%d\n",bcmp((void*)a, (void*) c, 6));
printf("bcmp(a,d):%d\n",bcmp((void*)a, (void*) d, 6));

return 0;
}