memcmp.c (892B)
1 2 #include "../stdint.h" 3 #include "string.h" 4 5 int memcmp (const void *s1, const void *s2, size_t n) { 6 7 const unsigned char *str_1 = (const unsigned char *)s1; 8 const unsigned char *str_2 = (const unsigned char *)s2; 9 10 int b_same = 1; // initialization of return value 11 12 size_t i = 0; //lc -> loop counter 13 while (1) { 14 15 if (i == n) { // loop finished successfully, indicating every dereference byte of s1 and s2 are same 16 b_same = 0; // return zero 17 break; 18 } 19 20 if (*str_1 != *str_2) { // loop broke: s1 != s2 21 b_same = (int)*str_1 - (int)*str_2; // if s1 > s2 value returned (difference of bytes) is positive; otherwise negative 22 break; 23 } 24 25 str_1++; str_2++; i++; // increment pointers and loop counter 26 } 27 28 return b_same; // return difference of bytes (+/-) or successful comparison (0) 29 }