5

The first printf gives output as -1, whereas the second printf gives output as -115.

#include<stdio.h>
#include<string.h>
int mystrcmp(char*s, char*t){
    for(;*s==*t;s++,t++){
        if(*s=='\0'){
            return 0;
        }
    }
    return (*s-*t);
}
int main()
{
    char *y,*x="this";
    y="thiss";
    printf("%d\n\n",strcmp(x,y));
    printf("%d",mystrcmp(x,y));
    return 0;
}

I understand, that in my implementation, the code is calculating 0(ASCII of Null) - 's'(ASCII value 115). Can anyone please help me as to how I may exactly duplicate the working of strcmp function that is in string.h

5
  • 2
    Google "source strcmp" for implementations Jun 14, 2019 at 12:41
  • 2
    Each library writer is free to implement strcmp() as she wishes (in any language), as long as it follows the Standard description. There is no single "strcmp() function that is in <string.h>". I believe the code for GNU libc is open source; it may have strcmp() implemented in C.
    – pmg
    Jun 14, 2019 at 12:42
  • 1
    @MOehm: If I had a dollar for every time I misread that...
    – Bathsheba
    Jun 14, 2019 at 12:42
  • @sukhbir1996: Yes, nobody is perfect. Upvoted your question for good feelings!
    – Bathsheba
    Jun 14, 2019 at 12:43
  • 3
    Hardcore library implementations are more likely to work with chunks of aligned data, comparing for example 4 bytes at a time, then doing tricks to look for 0 value bytes. That makes the code far less branch-intense. But also it will not really be possible to write such a lib in standard C, because of pointer aliasing violations etc.
    – Lundin
    Jun 14, 2019 at 13:12

3 Answers 3

10

The exact values returned from strcmp in the unequal cases aren't defined explicitly. In your particular case, any negative value is considered valid. From the man page:

The strcmp() and strncmp() functions return an integer less than, equal to, or greater than zero if s1 (or the first n bytes thereof) is found, respectively, to be less than, to match, or be greater than s2.

So the only guarantee is that if the first argument is "less than" the second then the result is negative, and if the first is "greater than" the second then the result is positive. Different implementations may return different values for the same strings.

As an example, if I compile and run your code on my machine with optimization set at -O0, I get back -115 from strcmp. If I change the optimization to -O1, it returns -1 instead. So not only can the result change from one machine to the next, but it can even vary on the same machine with different compiler settings.

2

The implementation of the "real" strcmp on your platform is most likely close to this code:

int strcmp(const char *s, const char *t) {
    for(; *s == *t; s++, t++) {
        if (*s == '\0') {           // are we at the end ?
            return 0;               // yes
        }
    }
    return (*s-*t) > 0 ? 1 : -1;   // return either +1 or -1
}

BTW: it should be int strcmp(const char *s, const char *t) instead of int strcmp(char *s, char *t)

1
  • 1
    @sukhbir1996 It explains your specific environment, but not the general case.
    – dbush
    Jun 14, 2019 at 15:46
0

The manuel page said that 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. Your can try this code :

    int ft_strcmp(const char *s1, const char *s2)
{
    while ((unsigned char)*s1 || (unsigned char)*s2)
    {
        if ((unsigned char)*s1 != (unsigned char)*s2)
            return ((unsigned char)*s1 - (unsigned char)*s2);
        s1++;
        s2++;
    }
    return (0);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.