I tried to create my own strncmp function that was required from my school project, I run my code that use the same srcmp function but with providing the limit size for which ASCII Code I want to compare: The code:
#include <stdio.h>
int strncmp(char *s1, char *s2, unsigned int n)
{
int i;
int diff;
i = 0;
diff = 0;
while ((s1[i] != '\0' || s2[i] != '\0') && (unsigned)i < n)
{
diff = s1[i] - s2[i];
if (diff != 0)
{
break ;
}
i++;
}
return (diff);
}
int main(){
char s11[] = "abcs";
char s12[] = "zzfs";
unsigned int p = 3;
printf("%d", strncmp(s11, s12, p));
}
The output :
-22
It's supposed to compare the 3 characters not only the first one, Am I right ? I didn't get the idea in MAN, I'm a beginner just so you know. Thanks for your help!