strncmp.c (299B)
1#include <string.h> 2 3/* 4 * Compare strings (at most n bytes): 5 * s1>s2: >0 6 * s1==s2: 0 7 * s1<s2: <0 8 */ 9 10int strncmp(const char *s1, const char *s2, int n) { 11 while ((n > 0) && (*s1 == *s2++)) { 12 if (*s1++ == '\0') return 0; 13 n--; 14 } 15 return (n == 0 ? 0 : *s1 - *--s2); 16}