#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    2

    Strcmp() returns wrong value


    here's the code for example:
    Code:
    int main()
    {
    	char str1[15], str2[15];
    
    	printf("Enter first string:");
    	scanf("%s", str1);
    	printf("Enter second string:");
    	scanf("%s", str2);
    
    	printf("%d", strcmp(str1,str2));
    }
    here's the output:

    Code:
    Enter first string: hello
    Enter second string: world
    -15
    what's wrong with it?
    why wouldn't it return 1 as it should?
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    Why would you expect it to return 1?

    The spec for strcmp reads
    <0 if a is before b
    >0 if a is after b
    == 0 if they're the same

    You have -15, indicating "hello" is before "world" (which it is).
    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
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    2
    :hf:

    for some reason (maybe i read it somwhere) i thought that if it reurns 1 then first string is before second string, 0 if they're the same, and -1 if second string is before the first string...

    thank you!
  6. #4
  7. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,119
    Rep Power
    9398
    Some languages have a comparison function like strcmp but specifically return -1, 0, or 1. The C function returns the difference between the two strings (as you may have realized) and thus merely guarantees a negative, zero, or positive number.
    As for returning 1/positive if the first string < second string, I'm not sure where you got that from. No offense but you might just be remembering it backwards.
  8. #5
  9. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,113
    Rep Power
    1803
    Originally Posted by so.very.tired
    for some reason (maybe i read it somwhere) i thought that if it reurns 1 then first string is before second string, 0 if they're the same, and -1 if second string is before the first string...
    Reading it somewhere is good; but perhaps you should have checked before posting a question. There are many sources for documentation of teh standard library; here's one. If the documentation only guarantees <0, 0, or >0, even if you observe -1, 0 and 1 in any particular implementation, you cannot assume that that will always be the case.

    In this case -15 is the result of performing 'w' - 'h' which gives a clue as to how strcmp() is working in this case. It is simply performing a numerical comparison of the character codes, and returns the difference between the first two non-matching characters.

IMN logo majestic logo threadwatch logo seochat tools logo