# Thread: strcmp return value

1. ## strcmp return value

does strcmp always return 0, 1 (or -1) for equal, greater (or the opposite) respectively?

2. Yes...

If the two strings are equal it will return 0.
If the first string is alphabetically earlier (less than) the second, it will return -1
If the first string is alphabetically later (greater than) than the second it will return 1

3. Originally Posted by CommonTater
Yes...

If the two strings are equal it will return 0.
If the first string is alphabetically earlier (less than) the second, it will return -1
If the first string is alphabetically later (greater than) than the second it will return 1
but why does the strcmp manual only mention that it will be zero if the strings are equal, greater than zero, or less than zero?

4. Originally Posted by cph
but why does the strcmp manual only mention that it will be zero if the strings are equal, greater than zero, or less than zero?
Because CommonTater is mistaken. (I feel as though I remember getting numbers larger than 1, but I don't have an example on me.)

5. It may be implementation dependent...
My best suggestion is to test it with your library....

Code:
```char a[4] = "aaa";
char e[4] = "eee";
int x;
x = strcmp(a,a);
printf("%d\n",x);
x = strcmp(a,e);
printf("%d\n",x);
x = strcmp(e,a);
printf("%d\n",x);```
When in doubt... check it out!

6. Originally Posted by tabstop
Because CommonTater is mistaken. (I feel as though I remember getting numbers larger than 1, but I don't have an example on me.)
Ummm... come to think of it... I am wrong... the relationships are right but yes, it will return higher or lower values...

Thanks.

7. Originally Posted by CommonTater
It may be implementation dependent...
My best suggestion is to test it with your library....

Code:
```char a[4] = "aaa";
char e[4] = "eee";
int x;
x = strcmp(a,a);
printf("%d\n",x);
x = strcmp(a,e);
printf("%d\n",x);
x = strcmp(e,a);
printf("%d\n",x);```
When in doubt... check it out!
Or, when in doubt, don't make assumptions...

8. Originally Posted by zacs7
Or, when in doubt, don't make assumptions...
Ever noticed how the little piggie that takes sloppy seconds never gets the best milk?

9. Better read doc, coz one experiment does not mean all.
eg fflush(stdin) works with my compiler!

The strcmp() function compares the two strings s1 and s2. It returns
an integer less than, equal to, or greater than zero if s1 is found,
respectively, to be less than, to match, or be greater than s2.

10. OK OK .... I give up.

The nits are all thoroughly picked.
The enemy is vanquished.
Peace and sanity will return to your valley.

GEES!

11. I have found that the returned value is most likely the numeric difference between the chars that were not equal. There is really no reason why a speedy program would want to convert a positive/negative value to +1 and -1 respectively when it already meets the documented criteria.

12. does the result depend on the compiler?
this code gave the same result on my PC (compiled with msvc & gcc)
Code:
```#include <stdio.h>
#include <string.h>

int main (void)
{
const char *str[2] = { "eeny meeny miny mo",
"function calls are so damn slow" };

printf("result: %d\n", strcmp(str[1], str[0]));

return(0);
}```
output:
Code:
`result: 1`

13. Originally Posted by cph
does the result depend on the compiler?
this code gave the same result on my PC (compiled with msvc & gcc)
Code:
```#include <stdio.h>
#include <string.h>

int main (void)
{
const char *str[2] = { "eeny meeny miny mo",
"function calls are so damn slow" };

printf("result: %d\n", strcmp(str[1], str[0]));

return(0);
}```
output:
Code:
`result: 1`
It would depend on the version of the standard library used. If you're using minGW, I think you're using the same version of the standard library regardless of compiler.

EDIT: So for instance, I get -1, 0, or +1 on my windows machine regardless of whether I'm using minGW or whatever. On my Mac I get -13 for comparing "eieio" with "evil".

14. I said....
If the two strings are equal it will return 0.
If the first string is alphabetically earlier (less than) the second, it will return -1
If the first string is alphabetically later (greater than) than the second it will return 1
Tabstop says...
Because CommonTater is mistaken. (I feel as though I remember getting numbers larger than 1, but I don't have an example on me.)
and later...
So for instance, I get -1, 0, or +1 on my windows machine regardless of whether I'm using minGW or whatever
Ain't it a pain when you get so zealous about correcting people that it comes right back around to bite you....

15. Originally Posted by Draft C99 Standard
7.21.4.2 The strcmp function
1. Synopsis
#include <string.h>
int strcmp(const char *s1, const char *s2);

2. Description
The strcmp function compares the string pointed to by s1 to the string pointed to by
s2.

3. Returns
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.
http://www.open-std.org/jtc1/sc22/wg...69/n869.pdf.gz
Regardless of whatever experiments you do with your current compiler, or whatever your local manual pages say.
Portable code use <0, ==0 and >0

Popular pages Recent additions