`strcmp` 的 return 值的顺序是什么?

What is the ordering for `strcmp`'s return value?

我的代码量很少:

int test = strcmp("Websecurity", "easily");
printf("%d\n", test);

结果为-1。为什么?显然 'W' 大于 'e'?

在 ASCII 中大写字母排在小写字母之前,因此 'W' < 'e'

strcmp 的 return 值取决于被比较字符串的编码。根据 Posix 标准:

发件人:http://pubs.opengroup.org/onlinepubs/009695399/functions/strcmp.html

The sign of a non-zero return value shall be determined by the sign of the difference between the values of the first pair of bytes (both interpreted as type unsigned char) that differ in the strings being compared.

在 ASCII(和 utf-8)中,大写字母由比小写字母低的字节值表示。特别地,'W'是0x57,e是0x65。因此,您肯定会得到一个负 return 值。