%>
比较两个缓冲区中的字符。
语法
int memcmp(
const void *buffer1,
const void *buffer2,
size_t count
);
int wmemcmp(
const wchar_t * buffer1,
const wchar_t * buffer2,
size_t count
);
参数
buffer1
第一个缓冲区。
buffer2
第二个缓冲区。
count
要比较的字符数。 (比较 memcmp
的字节,wmemcmp
的宽字符)。
返回值
返回值指示缓冲区之间的关系。
返回值 | buf1 和 buf2 的前 count 个字符之间的关系 |
---|---|
< 0 | buffer1 小于 buffer2 |
0 | buffer1 等于 buffer2 |
> 0 | buffer1 大于 buffer2 |
备注
比较 buffer1
和 buffer2
的前 count
个字符,并返回指示其关系的值。 非零返回值的符号是缓冲区中的第一个不同值对之差的符号。 值针对 memcmp
解释为 unsigned char
,针对 wmemcmp
解释为 wchar_t
。
要求
例程 | 必需的标头 |
---|---|
memcmp |
<memory.h> 或 <string.h> |
wmemcmp |
<wchar.h> |
有关兼容性的详细信息,请参阅 兼容性。
库
C 运行时库的所有版本。
示例
// crt_memcmp.c
/* This program uses memcmp to compare
* the strings named first and second. If the first
* 19 bytes of the strings are equal, the program
* considers the strings to be equal.
*/
#include <string.h>
#include <stdio.h>
int main( void )
{
char first[] = "12345678901234567890";
char second[] = "12345678901234567891";
int int_arr1[] = {1,2,3,4};
int int_arr2[] = {1,2,3,4};
int result;
printf( "Compare '%.19s' to '%.19s':\n", first, second );
result = memcmp( first, second, 19 );
if( result < 0 )
printf( "First is less than second.\n" );
else if( result == 0 )
printf( "First is equal to second.\n" );
else
printf( "First is greater than second.\n" );
printf( "Compare '%d,%d' to '%d,%d':\n", int_arr1[0], int_arr1[1], int_arr2[0], int_arr2[1]);
result = memcmp( int_arr1, int_arr2, sizeof(int) * 2 );
if( result < 0 )
printf( "int_arr1 is less than int_arr2.\n" );
else if( result == 0 )
printf( "int_arr1 is equal to int_arr2.\n" );
else
printf( "int_arr1 is greater than int_arr2.\n" );
}
Compare '1234567890123456789' to '1234567890123456789':
First is equal to second.
Compare '1,2' to '1,2':
int_arr1 is equal to int_arr2.