Thread: strcmp return value

  1. #1
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    86

    strcmp return value

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

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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. #3
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    86
    Quote Originally Posted by CommonTater View Post
    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. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by cph View Post
    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. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tabstop View Post
    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. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by CommonTater View Post
    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. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by zacs7 View Post
    Or, when in doubt, don't make assumptions...
    Ever noticed how the little piggie that takes sloppy seconds never gets the best milk?

  9. #9
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    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.
    Last edited by Bayint Naung; 01-27-2011 at 08:56 PM.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    OK OK .... I give up.

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

    GEES!
    Last edited by CommonTater; 01-27-2011 at 09:02 PM.

  11. #11
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    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. #12
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    86
    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. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by cph View Post
    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".
    Last edited by tabstop; 01-28-2011 at 10:19 AM.

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,410
    Quote 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
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An efficient approach to TicTacToe AI?
    By xofvc4rqb in forum C++ Programming
    Replies: 11
    Last Post: 01-15-2011, 05:09 PM
  2. New string functions
    By Elysia in forum C Programming
    Replies: 11
    Last Post: 03-28-2009, 05:03 AM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM