Re: Strange strcmp() action?
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Thu, 01 Jan 2009 15:37:57 -0800
Eric <answer.to.newsgroup@xxxxxxxxxx> writes:
On Thu, 1 Jan 2009 14:30:50 -0800 (PST), JC <jason.cipriani@xxxxxxxxx>
wrote:
No. You must have misread some documentation somewhere. The strcmp()
function compares the entire string.
The string &abc[1] is "wertyu", which is not "wer". The function strcmp
() returns 0 if the strings are equal, "wertyu" and "wer" are not
equal. It returns 1 because "wertyu" comes after "wer" alphabetically.
Hmmm... OK.
http://www.cplusplus.com/reference/clibrary/cstring/strcmp.html says:
"Compares the C string str1 to the C string str2.
This function starts comparing the first character of each string. If
they are equal to each other, it continues with the following pairs
until the characters differ or until a terminanting null-character is
reached."
To me, that seems to say that if a null terminator is found, as it
would be at the end of "wer" in str2, then if the strings are equal up
to that point, they are equal.
It doesn't say anything about comparing the terminating null character
in str2 with the corresponding character in str1 and finding that they
are not equal (because the corresponding character in str1 is "t").
I know it doesn't say this explicitly, and I can't find anything else,
including "man strcmp" for both Linux and FreeBSD, that is explicit
about it.
Here's what the C99 standard says.
7.21.4:
The sign of a nonzero value returned by the comparison functions
memcmp, strcmp, and strncmp is determined by the sign of the
difference between the values of the first pair of characters
(both interpreted as unsigned char) that differ in the objects
being compared.
7.21.4.2:
The strcmp function returns an integer greater than, equal to, or
less than zero, accordingly as the string pointed to by s1 is
greater than, equal to, or less than the string pointed to by s2.
A "string" is, by the definition in 7.1.1p1:
a contiguous sequence of characters terminated by and including
the first null character.
So the terminating null character is part of the string, and is
compared if a mismatch isn't found sooner. In comparing the strings
"abc" and "abcd", the characters compared are:
'a' vs. 'a' (equal, keep looking)
'b' vs. 'b' (equal, keep looking)
'c' vs. 'c' (equal, keep looking)
'\0' vs. 'd' (less, return a negative result)
In my opinion it would be nice if this were made a bit more explicit.
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx
<http://www.ghoti.net/~kst> Nokia "We must do something. This is
something. Therefore, we must do this." -- Antony Jay and Jonathan
Lynn, "Yes Minister"
.
- References:
- Strange strcmp() action?
- From: Eric
- Re: Strange strcmp() action?
- From: JC
- Re: Strange strcmp() action?
- From: Eric
- Strange strcmp() action?
- Prev by Date: elliminate 0s
- Next by Date: Re: OO and C: Still a Good Idea?
- Previous by thread: Re: Strange strcmp() action?
- Next by thread: Re: Strange strcmp() action?
- Index(es):
Relevant Pages
|
Loading