I am trying to create my own memcmp file but whenever I compare it with original memcmp function the equal strings return zero but in case of unequal strings return values are different.
char *p = (char *)s1;
char *q = (char *)s2;
int charCompareStatus = 0;
if (s1 == s2) {
return charCompareStatus;
}
while (n > 0) {
if (*p != *q) {
charCompareStatus = (*p > *q) ? (*p - *q) : (*p - *q);
break;
}
n--;
p++;
q++;
}
return charCompareStatus;
The output when compared to real memcmp function is
./a.out "ajinkya" "akinkya"
MEMCMP: -256
SST_MEMCMP: -1
The exact return values of
strcmpandmemcmpare not specified. They can return any negative value if the first argument is logographically less and any positive number if it's greater. So, a return value of-1,-10,-42all mean the same thing.