# Why strcmp equals zero

• 07-21-2004
barim
Why strcmp equals zero
I followed Hammer's tutorial on Comparing Strings, but one thing I don't understand.

Code:

``` if(strcmp(foo, bar) == 0)   puts("Strings equal"); else  puts("Strings not equal");```
Why compare these strings to zero. I know that in boolean 0 is false and 1 is true. I have to say that these tutorials are very good for beginners.

Thanks.
• 07-21-2004
alvarorahul
string comparison starts with the first character and goes on till a character differs or one of the strings gets completed
coonsider strcmp(s1,s2)
if strcmp(s1,s2) == 0 => s1==s2
if strcmp(s1,s2) <0 => s1<s2
ifstrcmp(s1,s2) > 0 => s1>s2
• 07-21-2004
alvarorahul
oh and the value strcmp returns is the diff. betwn. the value of the corres. characters of the strings. //pls. verify this
• 07-21-2004
jverkoey
Somehow I got the code for this (and many other c functions)

-edit-
Ok, they're all the source code for C functions....in C. Some of the functions are written in assembly, but I don't feel like finding the one's I've seen. It's just nice to see how they did the actual functions written out.

anyways
Code:

```        /***     *strcmp - compare two strings, returning less than, equal to, or greater than     *     *Purpose:     *          STRCMP compares two strings and returns an integer     *          to indicate whether the first is less than the second, the two are     *          equal, or whether the first is greater than the second.     *     *          Comparison is done byte by byte on an UNSIGNED basis, which is to     *          say that Null (0) is less than any other character (1-255).     *     *Entry:     *          const char * src - string for left-hand side of comparison     *          const char * dst - string for right-hand side of comparison     *     *Exit:     *          returns -1 if src <  dst     *          returns  0 if src == dst     *          returns +1 if src >  dst     *     *Exceptions:     *     *******************************************************************************/         int __cdecl strcmp (                     const char * src,                     const char * dst                     )     {                     int ret = 0 ;                     while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)                                     ++src, ++dst;                         if ( ret < 0 )                                     ret = -1 ;                     else if ( ret > 0 )                                     ret = 1 ;                         return( ret );     }```
• 07-21-2004
Thantos
To reiterate what alvarorahul said:

strcmp() compares the strings one character at a time. If the characters match it goes on to the next character. Once it finds a difference it subtracts the character from string2 from the character from string1.

So given:
Code:

```char s1[] = "My name is Bob"; char s2[] = "My name is Billy";```
It will compare "My name is B" from both strings and then read o from s1 and i from s2 and then returns the value of o-i. If the value returned is < 0 then s1 is less than s2, if the value returned is > 0 then s1 is greater then s2. So by default (only range left) 0 means they are the same.
• 07-21-2004
jverkoey
strcmp is really helpful in sorting algorithms.