代码点读机 / C / memcmp

memcmp C · 字符串函数

按字节比较两块内存的内容,返回负数、0 或正数。

怎么用

int r = memcmp(地址1, 地址2, 字节数);

小例子

#include <stdio.h>
#include <string.h>
int main(void) {
    int a[3] = {1, 2, 3};
    int b[3] = {1, 2, 4};
    printf("%d", memcmp(a, b, sizeof(a)));
    return 0;
}

运行结果

输出负数

提醒:相等返回 0;按无符号字节逐字节比较

官方文档:https://en.cppreference.com/w/c/string/byte/memcmp

在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← C全部词条