gpt4 book ai didi

c - 这是 C 中 strcmp() 的唯一返回值吗?

转载 作者:行者123 更新时间:2023-12-04 13:07:09 25 4
gpt4 key购买 nike

我正在学习 C,目前正在学习字符串处理。从我学习的地方,strcmp()被定义为——

This is a function which compares two strings to find out whether they are same or different. The two strings are compared character by character until there is a mismatch or end of one of the strings is reached, whichever occurs first. If the two strings are identical, strcmp( ) returns a value zero. If they’re not, it returns the numeric difference between the ASCII values of the first non-matching pairs of characters.



给出了一个示例程序,这就是我的问题-
main( )
{
char string1[ ] = "Jerry" ;
char string2[ ] = "Ferry" ;
int i, j, k ;
i = strcmp ( string1, "Jerry" ) ;
j = strcmp ( string1, string2 ) ;
k = strcmp ( string1, "Jerry boy" ) ;
printf ( "\n%d %d %d", i, j, k ) ;
}

我在我的 Windows(64 位)机器上的 Dev-C++ 上运行了这个程序,并得到了这个输出 - 0 1 -1
现在,本书给出的输出为 0 4 -32 ,根据这个推理——

In the first call to strcmp( ), the two strings are identical—“Jerry” and “Jerry”—and the value returned by strcmp( ) is zero. In the second call, the first character of “Jerry” doesn't match with the first character of “Ferry” and the result is 4, which is the numeric difference between ASCII value of ‘J’ and ASCII value of ‘F’. In the third call to strcmp( ) “Jerry” doesn’t match with “Jerry boy”, because the null character at the end of “Jerry” doesn’t match the blank in “Jerry boy”. The value returned is -32, which is the value of null character minus the ASCII value of space, i.e., ‘\0’ minus ‘ ’, which is equal to -32.



为了确认书中所说的,我在我的程序中添加了这段代码,只是为了验证 J 和 F 之间的 ASCII 差异:
printf("\n Ascii value of J is %d", 'J' );
printf("\n Ascii value of F is %d", 'F' );

然后我相应地在输出中得到了这个 -
 Ascii value of J is 74
Ascii value of F is 70

这是根据书上所说的,然而,正如你所看到的,我得到了不同的 j 和 k 值,也就是说,当字符串不匹配时。我确实在 SO 上查找了类似的问题,并得到了其中一些,但无法找到不同输出的明确答案(当它返回 1 and -1 时),因此我决定提出一个新问题。

这个问题 here好像有点类似,问题描述中包含了以下关于 strcmp()的信息——

The strcmp() and strncmp() functions return an integer less than, equal to, or greater than zero if s1 (or the first n bytes thereof) is found, respectively, to be less than, to match, or be greater than s2



在其中一个答案中,我遇到了 this link其中记录了 strcmp() 的功能.它还说——

The strcmp() function shall compare the string pointed to by s1 to the string pointed to by s2.

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.

RETURN VALUE

Upon completion, strcmp() shall return an integer greater than, equal to, or less than 0, if the string pointed to by s1 is greater than, equal to, or less than the string pointed to by s2, respectively.



因此,在阅读完所有这些之后,我倾向于认为无论使用的实现/平台如何, strcmp()函数应仅用于将返回值视为三个类别之一 ( 0, positive and negative ),而不是依赖于返回的确切值。

我的理解正确吗?

最佳答案

这是 strcmp() 的简单实现在来自 Apple 的 libc 的 C 中:

int
strcmp(const char *s1, const char *s2)
{
for ( ; *s1 == *s2; s1++, s2++)
if (*s1 == '\0')
return 0;
return ((*(unsigned char *)s1 < *(unsigned char *)s2) ? -1 : +1);
}

FreeBSD 的 libc 实现:
int
strcmp(const char *s1, const char *s2)
{
while (*s1 == *s2++)
if (*s1++ == '\0')
return (0);
return (*(const unsigned char *)s1 - *(const unsigned char *)(s2 - 1));
}

这是 GNU libc 的实现,它返回字符之间的差异:
int
strcmp (p1, p2)
const char *p1;
const char *p2;
{
const unsigned char *s1 = (const unsigned char *) p1;
const unsigned char *s2 = (const unsigned char *) p2;
unsigned char c1, c2;

do
{
c1 = (unsigned char) *s1++;
c2 = (unsigned char) *s2++;
if (c1 == '\0')
return c1 - c2;
}
while (c1 == c2);

return c1 - c2;
}

这就是为什么我读过的大多数比较都写在 < 0 , == 0> 0如果它不需要知道字符串中字符之间的确切区别。

关于c - 这是 C 中 strcmp() 的唯一返回值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25015155/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com