代码点读机 / C / strcmp

strcmp C · 字符串函数

按字典序比较两个字符串,返回负数、0 或正数。

怎么用

int r = strcmp(串1, 串2);

小例子

#include <stdio.h>
#include <string.h>
int main(void) {
    char* a = "abc";
    char* b = "abd";
    printf("%d", strcmp(a, b));
    return 0;
}

运行结果

输出负数

提醒:相等返回 0;比较的是字符的 ASCII 码,不是字符串长度

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

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