问题描述
当我做给C字符串调用比较函数是这样的:
When I make a call to the C string compare function like this:
的strcmp(时间,时间)
它返回0,这意味着该字符串不相等。
It returns 0, which implies that the strings are not equal.
谁能告诉我,为什么C实现似乎做到这一点?我想,如果等于它会返回一个非零值。我很好奇,我看到这种行为的原因。
Can anyone tell me why C implementations seem to do this? I would think it would return a non-zero value if equal. I am curious to the reasons I am seeing this behavior.
推荐答案
STRCMP返回一个词汇差异(或者我应该称之为短路串行字节比较?:-))两个字符串你给作为参数。
0意味着两个字符串相等
strcmp returns a lexical difference (or should i call it "short-circuit serial byte comparator" ? :-) ) of the two strings you have given as parameters. 0 means that both strings are equal
一个正值意味着S1将是一本字典S2之后。
A positive value means that s1 would be after s2 in a dictionary.
一个负值意味着S1将是一本字典S2之前。
A negative value means that s1 would be before s2 in a dictionary.
比较时间和金钱,这是明显的不同,即使一个人会说,当你因此非零值,时间就是金钱! : - )
Hence your non-zero value when comparing "time" and "money" which are obviously different, even though one would say that time is money ! :-)
这篇关于为什么STRCMP()返回0时,其输入是平等的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!