-2

I was just trying to execute this program in code blocks and i got an output of 0,1,-1 , in some other compiler i got result 0,4,-1 but according to the working of strcmp() , i should get 0,4,-32 , i am not able to understand why i am getting diferent outputs on different compilers.

#include<stdio.h>
#include<string.h>
int main()
{
char string1[]="Jerry";
char string2[]="Ferry";
int i,j,k;
i=strcmp(string1,"Jerry");
j=strcmp(string1,string2);
k=strcmp(string1,"Jerry boy");
printf("%d %d %d \n",i,j,k);
return 0;
}
LocalHost
  • 910
  • 3
  • 8
  • 24
  • https://stackoverflow.com/questions/34824838/what-does-strcmp-exactly-return-in-c – KamilCuk Jul 02 '18 at 09:59
  • 1
    The standard only guarantees a negative return value (indicating first string lexicographically less than the second), zero (equal), or positive (indicating lexicographically greater than). There is no requirement about what the particular negative and positive values will be. – Peter Jul 02 '18 at 10:02
  • The only requirement for [`strcmp()`](https://en.cppreference.com/w/c/string/byte/strcmp) is to return an integer that is: **negative** if the first string is lexicographically smaller than the second string, **zero** if the strings are equal and **positive** if the first string is lexicographically larger than the second string. Only the sign is important. The implementations are allowed to return any value (as long as the sign is correct); this allows them to choose the best way to compute it by (potentially) taking advantage of features provided by the target hardware. – axiac Jul 02 '18 at 10:05

1 Answers1

1

strcmp() returns value greater than or less than or equal to 0. That means it will return 0 for sure if two strings are equal otherwise it can return any integer value greater or less than 0.

Ctx
  • 18,090
  • 24
  • 36
  • 51
haccks
  • 104,019
  • 25
  • 176
  • 264
  • 2
    I think "any value" should be made to stand out even more. It is after all the meat and potatoes of this answer :) – StoryTeller - Unslander Monica Jul 02 '18 at 10:01
  • 1
    Quickly writing an answer and then closing the question with duplicate, interesting strategy for locking out other answers ;)) – Ctx Jul 02 '18 at 10:01
  • @Ctx; No, that was not the strategy. I did close the question because I found the link in the comment after I gave the answer. – haccks Jul 02 '18 at 10:03
  • doesn't strcmp() returns the difference of ASCII values of the first non matching characters ??? – LocalHost Jul 02 '18 at 10:06
  • 1
    @user9121710; No, [it doesn't](http://pubs.opengroup.org/onlinepubs/009695399/functions/strcmp.html). – haccks Jul 02 '18 at 10:07
  • but i used some online compilers ,there it is returning the ASCII value difference – LocalHost Jul 02 '18 at 10:08
  • @user9121710 it *may* return that, but it's not guranteed to. The only guarantee is laid out in the answer above. – O.O.Balance Jul 02 '18 at 10:09
  • 1
    ok it means according to standard it should only return positive , negative or zero – LocalHost Jul 02 '18 at 10:10
  • @user9121710 I wish it was that well defined! Some implementations might do that, but the standard doesn't require it. Some implementations apply multi-byte comparison tricks, which they are allowed to as they are internal. I'd often like an implementation that returned the length of the two strings including the matching character, but I can't have it, sadly! – Gem Taylor Jul 02 '18 at 10:11
  • I indeed would not see much benefit from having the difference of the ascii value of the first non-matching characters over -1, 0 and 1 (except from being optimized wrt speed). – Ctx Jul 02 '18 at 10:11